1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
|
/*
* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2015 Intel Corporation.
* Copyright 2012 Hasan Alayli <halayli@gmail.com>
*/
/**
* @file lthread_api.h
*
* @warning
* @b EXPERIMENTAL: this API may change without prior notice
*
* This file contains the public API for the L-thread subsystem
*
* The L_thread subsystem provides a simple cooperative scheduler to
* enable arbitrary functions to run as cooperative threads within a
* single P-thread.
*
* The subsystem provides a P-thread like API that is intended to assist in
* reuse of legacy code written for POSIX p_threads.
*
* The L-thread subsystem relies on cooperative multitasking, as such
* an L-thread must possess frequent rescheduling points. Often these
* rescheduling points are provided transparently when the application
* invokes an L-thread API.
*
* In some applications it is possible that the program may enter a loop the
* exit condition for which depends on the action of another thread or a
* response from hardware. In such a case it is necessary to yield the thread
* periodically in the loop body, to allow other threads an opportunity to
* run. This can be done by inserting a call to lthread_yield() or
* lthread_sleep(n) in the body of the loop.
*
* If the application makes expensive / blocking system calls or does other
* work that would take an inordinate amount of time to complete, this will
* stall the cooperative scheduler resulting in very poor performance.
*
* In such cases an L-thread can be migrated temporarily to another scheduler
* running in a different P-thread on another core. When the expensive or
* blocking operation is completed it can be migrated back to the original
* scheduler. In this way other threads can continue to run on the original
* scheduler and will be completely unaffected by the blocking behaviour.
* To migrate an L-thread to another scheduler the API lthread_set_affinity()
* is provided.
*
* If L-threads that share data are running on the same core it is possible
* to design programs where mutual exclusion mechanisms to protect shared data
* can be avoided. This is due to the fact that the cooperative threads cannot
* preempt each other.
*
* There are two cases where mutual exclusion mechanisms are necessary.
*
* a) Where the L-threads sharing data are running on different cores.
* b) Where code must yield while updating data shared with another thread.
*
* The L-thread subsystem provides a set of mutex APIs to help with such
* scenarios, however excessive reliance on on these will impact performance
* and is best avoided if possible.
*
* L-threads can synchronise using a fast condition variable implementation
* that supports signal and broadcast. An L-thread running on any core can
* wait on a condition.
*
* L-threads can have L-thread local storage with an API modelled on either the
* P-thread get/set specific API or using PER_LTHREAD macros modelled on the
* RTE_PER_LCORE macros. Alternatively a simple user data pointer may be set
* and retrieved from a thread.
*/
#ifndef LTHREAD_H
#define LTHREAD_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <rte_cycles.h>
struct lthread;
struct lthread_cond;
struct lthread_mutex;
struct lthread_condattr;
struct lthread_mutexattr;
typedef void *(*lthread_func_t) (void *);
/*
* Define the size of stack for an lthread
* Then this is the size that will be allocated on lthread creation
* This is a fixed size and will not grow.
*/
#define LTHREAD_MAX_STACK_SIZE (1024*64)
/**
* Define the maximum number of TLS keys that can be created
*
*/
#define LTHREAD_MAX_KEYS 1024
/**
* Define the maximum number of attempts to destroy an lthread's
* TLS data on thread exit
*/
#define LTHREAD_DESTRUCTOR_ITERATIONS 4
/**
* Define the maximum number of lcores that will support lthreads
*/
#define LTHREAD_MAX_LCORES RTE_MAX_LCORE
/**
* How many lthread objects to pre-allocate as the system grows
* applies to lthreads + stacks, TLS, mutexs, cond vars.
*
* @see _lthread_alloc()
* @see _cond_alloc()
* @see _mutex_alloc()
*
*/
#define LTHREAD_PREALLOC 100
/**
* Set the number of schedulers in the system.
*
* This function may optionally be called before starting schedulers.
*
* If the number of schedulers is not set, or set to 0 then each scheduler
* will begin scheduling lthreads immediately it is started.
* If the number of schedulers is set to greater than 0, then each scheduler
* will wait until all schedulers have started before beginning to schedule
* lthreads.
*
* If an application wishes to have threads migrate between cores using
* lthread_set_affinity(), or join threads running on other cores using
* lthread_join(), then it is prudent to set the number of schedulers to ensure
* that all schedulers are initialised beforehand.
*
* @param num
* the number of schedulers in the system
* @return
* the number of schedulers in the system
*/
int lthread_num_schedulers_set(int num);
/**
* Return the number of schedulers currently running
* @return
* the number of schedulers in the system
*/
int lthread_active_schedulers(void);
/**
* Shutdown the specified scheduler
*
* This function tells the specified scheduler to
* exit if/when there is no more work to do.
*
* Note that although the scheduler will stop
* resources are not freed.
*
* @param lcore
* The lcore of the scheduler to shutdown
*
* @return
* none
*/
void lthread_scheduler_shutdown(unsigned lcore);
/**
* Shutdown all schedulers
*
* This function tells all schedulers including the current scheduler to
* exit if/when there is no more work to do.
*
* Note that although the schedulers will stop
* resources are not freed.
*
* @return
* none
*/
void lthread_scheduler_shutdown_all(void);
/**
* Run the lthread scheduler
*
* Runs the lthread scheduler.
* This function returns only if/when all lthreads have exited.
* This function must be the main loop of an EAL thread.
*
* @return
* none
*/
void lthread_run(void);
/**
* Create an lthread
*
* Creates an lthread and places it in the ready queue on a particular
* lcore.
*
* If no scheduler exists yet on the curret lcore then one is created.
*
* @param new_lt
* Pointer to an lthread pointer that will be initialized
* @param lcore
* the lcore the thread should be started on or the current clore
* -1 the current lcore
* 0 - LTHREAD_MAX_LCORES any other lcore
* @param lthread_func
* Pointer to the function the for the thread to run
* @param arg
* Pointer to args that will be passed to the thread
*
* @return
* 0 success
* EAGAIN no resources available
* EINVAL NULL thread or function pointer, or lcore_id out of range
*/
int
lthread_create(struct lthread **new_lt,
int lcore, lthread_func_t func, void *arg);
/**
* Cancel an lthread
*
* Cancels an lthread and causes it to be terminated
* If the lthread is detached it will be freed immediately
* otherwise its resources will not be released until it is joined.
*
* @param new_lt
* Pointer to an lthread that will be cancelled
*
* @return
* 0 success
* EINVAL thread was NULL
*/
int lthread_cancel(struct lthread *lt);
/**
* Join an lthread
*
* Joins the current thread with the specified lthread, and waits for that
* thread to exit.
* Passes an optional pointer to collect returned data.
*
* @param lt
* Pointer to the lthread to be joined
* @param ptr
* Pointer to pointer to collect returned data
*
0 * @return
* 0 success
* EINVAL lthread could not be joined.
*/
int lthread_join(struct lthread *lt, void **ptr);
/**
* Detach an lthread
*
* Detaches the current thread
* On exit a detached lthread will be freed immediately and will not wait
* to be joined. The default state for a thread is not detached.
*
* @return
* none
*/
void lthread_detach(void);
/**
* Exit an lthread
*
* Terminate the current thread, optionally return data.
* The data may be collected by lthread_join()
*
* After calling this function the lthread will be suspended until it is
* joined. After it is joined then its resources will be freed.
*
* @param ptr
* Pointer to pointer to data to be returned
*
* @return
* none
*/
void lthread_exit(void *val);
/**
* Cause the current lthread to sleep for n nanoseconds
*
* The current thread will be suspended until the specified time has elapsed
* or has been exceeded.
*
* Execution will switch to the next lthread that is ready to run
*
* @param nsecs
* Number of nanoseconds to sleep
*
* @return
* none
*/
void lthread_sleep(uint64_t nsecs);
/**
* Cause the current lthread to sleep for n cpu clock ticks
*
* The current thread will be suspended until the specified time has elapsed
* or has been exceeded.
*
* Execution will switch to the next lthread that is ready to run
*
* @param clks
* Number of clock ticks to sleep
*
* @return
* none
*/
void lthread_sleep_clks(uint64_t clks);
/**
* Yield the current lthread
*
* The current thread will yield and execution will switch to the
* next lthread that is ready to run
*
* @return
* none
*/
void lthread_yield(void);
/**
* Migrate the current thread to another scheduler
*
* This function migrates the current thread to another scheduler.
* Execution will switch to the next lthread that is ready to run on the
* current scheduler. The current thread will be resumed on the new scheduler.
*
* @param lcore
* The lcore to migrate to
*
* @return
* 0 success we are now running on the specified core
* EINVAL the destination lcore was not valid
*/
int lthread_set_affinity(unsigned lcore);
/**
* Return the current lthread
*
* Returns the current lthread
*
* @return
* pointer to the current lthread
*/
struct lthread
*lthread_current(void);
/**
* Associate user data with an lthread
*
* This function sets a user data pointer in the current lthread
* The pointer can be retrieved with lthread_get_data()
* It is the users responsibility to allocate and free any data referenced
* by the user pointer.
*
* @param data
* pointer to user data
*
* @return
* none
*/
void lthread_set_data(void *data);
/**
* Get user data for the current lthread
*
* This function returns a user data pointer for the current lthread
* The pointer must first be set with lthread_set_data()
* It is the users responsibility to allocate and free any data referenced
* by the user pointer.
*
* @return
* pointer to user data
*/
void
*lthread_get_data(void);
struct lthread_key;
typedef void (*tls_destructor_func) (void *);
/**
* Create a key for lthread TLS
*
* This function is modelled on pthread_key_create
* It creates a thread-specific data key visible to all lthreads on the
* current scheduler.
*
* Key values may be used to locate thread-specific data.
* The same key value may be used by different threads, the values bound
* to the key by lthread_setspecific() are maintained on a per-thread
* basis and persist for the life of the calling thread.
*
* An optional destructor function may be associated with each key value.
* At thread exit, if a key value has a non-NULL destructor pointer, and the
* thread has a non-NULL value associated with the key, the function pointed
* to is called with the current associated value as its sole argument.
*
* @param key
* Pointer to the key to be created
* @param destructor
* Pointer to destructor function
*
* @return
* 0 success
* EINVAL the key ptr was NULL
* EAGAIN no resources available
*/
int lthread_key_create(unsigned int *key, tls_destructor_func destructor);
/**
* Delete key for lthread TLS
*
* This function is modelled on pthread_key_delete().
* It deletes a thread-specific data key previously returned by
* lthread_key_create().
* The thread-specific data values associated with the key need not be NULL
* at the time that lthread_key_delete is called.
* It is the responsibility of the application to free any application
* storage or perform any cleanup actions for data structures related to the
* deleted key. This cleanup can be done either before or after
* lthread_key_delete is called.
*
* @param key
* The key to be deleted
*
* @return
* 0 Success
* EINVAL the key was invalid
*/
int lthread_key_delete(unsigned int key);
/**
* Get lthread TLS
*
* This function is modelled on pthread_get_specific().
* It returns the value currently bound to the specified key on behalf of the
* calling thread. Calling lthread_getspecific() with a key value not
* obtained from lthread_key_create() or after key has been deleted with
* lthread_key_delete() will result in undefined behaviour.
* lthread_getspecific() may be called from a thread-specific data destructor
* function.
*
* @param key
* The key for which data is requested
*
* @return
* Pointer to the thread specific data associated with that key
* or NULL if no data has been set.
*/
void
*lthread_getspecific(unsigned int key);
/**
* Set lthread TLS
*
* This function is modelled on pthread_set_sepcific()
* It associates a thread-specific value with a key obtained via a previous
* call to lthread_key_create().
* Different threads may bind different values to the same key. These values
* are typically pointers to dynamically allocated memory that have been
* reserved by the calling thread. Calling lthread_setspecific with a key
* value not obtained from lthread_key_create or after the key has been
* deleted with lthread_key_delete will result in undefined behaviour.
*
* @param key
* The key for which data is to be set
* @param key
* Pointer to the user data
*
* @return
* 0 success
* EINVAL the key was invalid
*/
int lthread_setspecific(unsigned int key, const void *value);
/**
* The macros below provide an alternative mechanism to access lthread local
* storage.
*
* The macros can be used to declare define and access per lthread local
* storage in a similar way to the RTE_PER_LCORE macros which control storage
* local to an lcore.
*
* Memory for per lthread variables declared in this way is allocated when the
* lthread is created and a pointer to this memory is stored in the lthread.
* The per lthread variables are accessed via the pointer + the offset of the
* particular variable.
*
* The total size of per lthread storage, and the variable offsets are found by
* defining the variables in a unique global memory section, the start and end
* of which is known. This global memory section is used only in the
* computation of the addresses of the lthread variables, and is never actually
* used to store any data.
*
* Due to the fact that variables declared this way may be scattered across
* many files, the start and end of the section and variable offsets are only
* known after linking, thus the computation of section size and variable
* addresses is performed at run time.
*
* These macros are primarily provided to aid porting of code that makes use
* of the existing RTE_PER_LCORE macros. In principle it would be more efficient
* to gather all lthread local variables into a single structure and
* set/retrieve a pointer to that struct using the alternative
* lthread_data_set/get APIs.
*
* These macros are mutually exclusive with the lthread_data_set/get APIs.
* If you define storage using these macros then the lthread_data_set/get APIs
* will not perform as expected, the lthread_data_set API does nothing, and the
* lthread_data_get API returns the start of global section.
*
*/
/* start and end of per lthread section */
extern char __start_per_lt;
extern char __stop_per_lt;
#define RTE_DEFINE_PER_LTHREAD(type, name) \
__typeof__(type)__attribute((section("per_lt"))) per_lt_##name
/**
* Macro to declare an extern per lthread variable "var" of type "type"
*/
#define RTE_DECLARE_PER_LTHREAD(type, name) \
extern __typeof__(type)__attribute((section("per_lt"))) per_lt_##name
/**
* Read/write the per-lcore variable value
*/
#define RTE_PER_LTHREAD(name) ((typeof(per_lt_##name) *)\
((char *)lthread_get_data() +\
((char *) &per_lt_##name - &__start_per_lt)))
/**
* Initialize a mutex
*
* This function provides a mutual exclusion device, the need for which
* can normally be avoided in a cooperative multitasking environment.
* It is provided to aid porting of legacy code originally written for
* preemptive multitasking environments such as pthreads.
*
* A mutex may be unlocked (not owned by any thread), or locked (owned by
* one thread).
*
* A mutex can never be owned by more than one thread simultaneously.
* A thread attempting to lock a mutex that is already locked by another
* thread is suspended until the owning thread unlocks the mutex.
*
* lthread_mutex_init() initializes the mutex object pointed to by mutex
* Optional mutex attributes specified in mutexattr, are reserved for future
* use and are currently ignored.
*
* If a thread calls lthread_mutex_lock() on the mutex, then if the mutex
* is currently unlocked, it becomes locked and owned by the calling
* thread, and lthread_mutex_lock returns immediately. If the mutex is
* already locked by another thread, lthread_mutex_lock suspends the calling
* thread until the mutex is unlocked.
*
* lthread_mutex_trylock behaves identically to rte_thread_mutex_lock, except
* that it does not block the calling thread if the mutex is already locked
* by another thread.
*
* lthread_mutex_unlock() unlocks the specified mutex. The mutex is assumed
* to be locked and owned by the calling thread.
*
* lthread_mutex_destroy() destroys a mutex object, freeing its resources.
* The mutex must be unlocked with nothing blocked on it before calling
* lthread_mutex_destroy.
*
* @param name
* Optional pointer to string describing the mutex
* @param mutex
* Pointer to pointer to the mutex to be initialized
* @param attribute
* Pointer to attribute - unused reserved
*
* @return
* 0 success
* EINVAL mutex was not a valid pointer
* EAGAIN insufficient resources
*/
int
lthread_mutex_init(char *name, struct lthread_mutex **mutex,
const struct lthread_mutexattr *attr);
/**
* Destroy a mutex
*
* This function destroys the specified mutex freeing its resources.
* The mutex must be unlocked before calling lthread_mutex_destroy.
*
* @see lthread_mutex_init()
*
* @param mutex
* Pointer to pointer to the mutex to be initialized
*
* @return
* 0 success
* EINVAL mutex was not an initialized mutex
* EBUSY mutex was still in use
*/
int lthread_mutex_destroy(struct lthread_mutex *mutex);
/**
* Lock a mutex
*
* This function attempts to lock a mutex.
* If a thread calls lthread_mutex_lock() on the mutex, then if the mutex
* is currently unlocked, it becomes locked and owned by the calling
* thread, and lthread_mutex_lock returns immediately. If the mutex is
* already locked by another thread, lthread_mutex_lock suspends the calling
* thread until the mutex is unlocked.
*
* @see lthread_mutex_init()
*
* @param mutex
* Pointer to pointer to the mutex to be initialized
*
* @return
* 0 success
* EINVAL mutex was not an initialized mutex
* EDEADLOCK the mutex was already owned by the calling thread
*/
int lthread_mutex_lock(struct lthread_mutex *mutex);
/**
* Try to lock a mutex
*
* This function attempts to lock a mutex.
* lthread_mutex_trylock behaves identically to rte_thread_mutex_lock, except
* that it does not block the calling thread if the mutex is already locked
* by another thread.
*
*
* @see lthread_mutex_init()
*
* @param mutex
* Pointer to pointer to the mutex to be initialized
*
* @return
* 0 success
* EINVAL mutex was not an initialized mutex
* EBUSY the mutex was already locked by another thread
*/
int lthread_mutex_trylock(struct lthread_mutex *mutex);
/**
* Unlock a mutex
*
* This function attempts to unlock the specified mutex. The mutex is assumed
* to be locked and owned by the calling thread.
*
* The oldest of any threads blocked on the mutex is made ready and may
* compete with any other running thread to gain the mutex, it fails it will
* be blocked again.
*
* @param mutex
* Pointer to pointer to the mutex to be initialized
*
* @return
* 0 mutex was unlocked
* EINVAL mutex was not an initialized mutex
* EPERM the mutex was not owned by the calling thread
*/
int lthread_mutex_unlock(struct lthread_mutex *mutex);
/**
* Initialize a condition variable
*
* This function initializes a condition variable.
*
* Condition variables can be used to communicate changes in the state of data
* shared between threads.
*
* @see lthread_cond_wait()
*
* @param name
* Pointer to optional string describing the condition variable
* @param c
* Pointer to pointer to the condition variable to be initialized
* @param attr
* Pointer to optional attribute reserved for future use, currently ignored
*
* @return
* 0 success
* EINVAL cond was not a valid pointer
* EAGAIN insufficient resources
*/
int
lthread_cond_init(char *name, struct lthread_cond **c,
const struct lthread_condattr *attr);
/**
* Destroy a condition variable
*
* This function destroys a condition variable that was created with
* lthread_cond_init() and releases its resources.
*
* @param cond
* Pointer to pointer to the condition variable to be destroyed
*
* @return
* 0 Success
* EBUSY condition variable was still in use
* EINVAL was not an initialised condition variable
*/
int lthread_cond_destroy(struct lthread_cond *cond);
/**
* Wait on a condition variable
*
* The function blocks the current thread waiting on the condition variable
* specified by cond. The waiting thread unblocks only after another thread
* calls lthread_cond_signal, or lthread_cond_broadcast, specifying the
* same condition variable.
*
* @param cond
* Pointer to pointer to the condition variable to be waited on
*
* @param reserved
* reserved for future use
*
* @return
* 0 The condition was signalled ( Success )
* EINVAL was not a an initialised condition variable
*/
int lthread_cond_wait(struct lthread_cond *c, uint64_t reserved);
/**
* Signal a condition variable
*
* The function unblocks one thread waiting for the condition variable cond.
* If no threads are waiting on cond, the rte_lthead_cond_signal() function
* has no effect.
*
* @param cond
* Pointer to pointer to the condition variable to be signalled
*
* @return
* 0 The condition was signalled ( Success )
* EINVAL was not a an initialised condition variable
*/
int lthread_cond_signal(struct lthread_cond *c);
/**
* Broadcast a condition variable
*
* The function unblocks all threads waiting for the condition variable cond.
* If no threads are waiting on cond, the rte_lthead_cond_broadcast()
* function has no effect.
*
* @param cond
* Pointer to pointer to the condition variable to be signalled
*
* @return
* 0 The condition was signalled ( Success )
* EINVAL was not a an initialised condition variable
*/
int lthread_cond_broadcast(struct lthread_cond *c);
#ifdef __cplusplus
}
#endif
#endif /* LTHREAD_H */
|