2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2015 Intel Corporation.
4 * Copyright 2012 Hasan Alayli <halayli@gmail.com>
23 #include <rte_string_fns.h>
27 #include "lthread_api.h"
29 #include "lthread_timer.h"
30 #include "lthread_tls.h"
31 #include "lthread_objcache.h"
32 #include "lthread_diag.h"
36 * This function gets called after an lthread function has returned.
38 void _lthread_exit_handler(struct lthread *lt)
41 lt->state |= BIT(ST_LT_EXITED);
43 if (!(lt->state & BIT(ST_LT_DETACH))) {
44 /* thread is this not explicitly detached
45 * it must be joinable, so we call lthread_exit().
50 /* if we get here the thread is detached so we can reschedule it,
51 * allowing the scheduler to free it
58 * Free resources allocated to an lthread
60 void _lthread_free(struct lthread *lt)
63 DIAG_EVENT(lt, LT_DIAG_LTHREAD_FREE, lt, 0);
65 /* invoke any user TLS destructor functions */
66 _lthread_tls_destroy(lt);
68 /* free memory allocated for TLS defined using RTE_PER_LTHREAD macros */
69 if (sizeof(void *) < (uint64_t)RTE_PER_LTHREAD_SECTION_SIZE)
70 _lthread_objcache_free(lt->tls->root_sched->per_lthread_cache,
71 lt->per_lthread_data);
73 /* free pthread style TLS memory */
74 _lthread_objcache_free(lt->tls->root_sched->tls_cache, lt->tls);
77 _lthread_objcache_free(lt->stack_container->root_sched->stack_cache,
80 /* now free the thread */
81 _lthread_objcache_free(lt->root_sched->lthread_cache, lt);
86 * Allocate a stack and maintain a cache of stacks
88 struct lthread_stack *_stack_alloc(void)
90 struct lthread_stack *s;
92 s = _lthread_objcache_alloc((THIS_SCHED)->stack_cache);
93 RTE_ASSERT(s != NULL);
95 s->root_sched = THIS_SCHED;
96 s->stack_size = LTHREAD_MAX_STACK_SIZE;
101 * Execute a ctx by invoking the start function
102 * On return call an exit handler if the user has provided one
104 static void _lthread_exec(void *arg)
106 struct lthread *lt = (struct lthread *)arg;
108 /* invoke the contexts function */
110 /* do exit handling */
111 if (lt->exit_handler != NULL)
112 lt->exit_handler(lt);
116 * Initialize an lthread
117 * Set its function, args, and exit handler
120 _lthread_init(struct lthread *lt,
121 lthread_func_t fun, void *arg, lthread_exit_func exit_handler)
124 /* set ctx func and args */
127 lt->exit_handler = exit_handler;
129 /* set initial state */
130 lt->birth = _sched_now();
131 lt->state = BIT(ST_LT_INIT);
132 lt->join = LT_JOIN_INITIAL;
136 * set the lthread stack
138 void _lthread_set_stack(struct lthread *lt, void *stack, size_t stack_size)
142 lt->stack_size = stack_size;
144 arch_set_stack(lt, _lthread_exec);
148 * Create an lthread on the current scheduler
149 * If there is no current scheduler on this pthread then first create one
152 lthread_create(struct lthread **new_lt, int lcore_id,
153 lthread_func_t fun, void *arg)
155 if ((new_lt == NULL) || (fun == NULL))
156 return POSIX_ERRNO(EINVAL);
159 lcore_id = rte_lcore_id();
160 else if (lcore_id > LTHREAD_MAX_LCORES)
161 return POSIX_ERRNO(EINVAL);
163 struct lthread *lt = NULL;
165 if (THIS_SCHED == NULL) {
166 THIS_SCHED = _lthread_sched_create(0);
167 if (THIS_SCHED == NULL) {
168 perror("Failed to create scheduler");
169 return POSIX_ERRNO(EAGAIN);
173 /* allocate a thread structure */
174 lt = _lthread_objcache_alloc((THIS_SCHED)->lthread_cache);
176 return POSIX_ERRNO(EAGAIN);
178 bzero(lt, sizeof(struct lthread));
179 lt->root_sched = THIS_SCHED;
181 /* set the function args and exit handlder */
182 _lthread_init(lt, fun, arg, _lthread_exit_handler);
184 /* put it in the ready queue */
188 lcore_id = rte_lcore_id();
190 DIAG_CREATE_EVENT(lt, LT_DIAG_LTHREAD_CREATE);
193 _ready_queue_insert(_lthread_sched_get(lcore_id), lt);
198 * Schedules lthread to sleep for `nsecs`
199 * setting the lthread state to LT_ST_SLEEPING.
200 * lthread state is cleared upon resumption or expiry.
202 static inline void _lthread_sched_sleep(struct lthread *lt, uint64_t nsecs)
204 uint64_t state = lt->state;
205 uint64_t clks = _ns_to_clks(nsecs);
208 _timer_start(lt, clks);
209 lt->state = state | BIT(ST_LT_SLEEPING);
211 DIAG_EVENT(lt, LT_DIAG_LTHREAD_SLEEP, clks, 0);
218 * Cancels any running timer.
219 * This can be called multiple times on the same lthread regardless if it was
222 int _lthread_desched_sleep(struct lthread *lt)
224 uint64_t state = lt->state;
226 if (state & BIT(ST_LT_SLEEPING)) {
228 state &= (CLEARBIT(ST_LT_SLEEPING) & CLEARBIT(ST_LT_EXPIRED));
229 lt->state = state | BIT(ST_LT_READY);
236 * set user data pointer in an lthread
238 void lthread_set_data(void *data)
240 if (sizeof(void *) == RTE_PER_LTHREAD_SECTION_SIZE)
241 THIS_LTHREAD->per_lthread_data = data;
245 * Retrieve user data pointer from an lthread
247 void *lthread_get_data(void)
249 return THIS_LTHREAD->per_lthread_data;
253 * Return the current lthread handle
255 struct lthread *lthread_current(void)
257 struct lthread_sched *sched = THIS_SCHED;
260 return sched->current_lthread;
267 * Tasklet to cancel a thread
272 struct lthread *lt = (struct lthread *) arg;
274 lt->state |= BIT(ST_LT_CANCELLED);
281 * Mark the specified as canceled
283 int lthread_cancel(struct lthread *cancel_lt)
287 if ((cancel_lt == NULL) || (cancel_lt == THIS_LTHREAD))
288 return POSIX_ERRNO(EINVAL);
290 DIAG_EVENT(cancel_lt, LT_DIAG_LTHREAD_CANCEL, cancel_lt, 0);
292 if (cancel_lt->sched != THIS_SCHED) {
294 /* spawn task-let to cancel the thread */
296 cancel_lt->sched->lcore_id,
301 cancel_lt->state |= BIT(ST_LT_CANCELLED);
306 * Suspend the current lthread for specified time
308 void lthread_sleep(uint64_t nsecs)
310 struct lthread *lt = THIS_LTHREAD;
312 _lthread_sched_sleep(lt, nsecs);
317 * Suspend the current lthread for specified time
319 void lthread_sleep_clks(uint64_t clks)
321 struct lthread *lt = THIS_LTHREAD;
322 uint64_t state = lt->state;
325 _timer_start(lt, clks);
326 lt->state = state | BIT(ST_LT_SLEEPING);
328 DIAG_EVENT(lt, LT_DIAG_LTHREAD_SLEEP, clks, 0);
333 * Requeue the current thread to the back of the ready queue
335 void lthread_yield(void)
337 struct lthread *lt = THIS_LTHREAD;
339 DIAG_EVENT(lt, LT_DIAG_LTHREAD_YIELD, 0, 0);
341 _ready_queue_insert(THIS_SCHED, lt);
342 ctx_switch(&(THIS_SCHED)->ctx, <->ctx);
346 * Exit the current lthread
347 * If a thread is joining pass the user pointer to it
349 void lthread_exit(void *ptr)
351 struct lthread *lt = THIS_LTHREAD;
353 /* if thread is detached (this is not valid) just exit */
354 if (lt->state & BIT(ST_LT_DETACH))
357 /* There is a race between lthread_join() and lthread_exit()
358 * - if exit before join then we suspend and resume on join
359 * - if join before exit then we resume the joining thread
361 uint64_t join_initial = LT_JOIN_INITIAL;
362 if ((lt->join == LT_JOIN_INITIAL)
363 && __atomic_compare_exchange_n(<->join, &join_initial,
364 LT_JOIN_EXITING, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
366 DIAG_EVENT(lt, LT_DIAG_LTHREAD_EXIT, 1, 0);
368 /* set the exit value */
369 if ((ptr != NULL) && (lt->lt_join->lt_exit_ptr != NULL))
370 *(lt->lt_join->lt_exit_ptr) = ptr;
372 /* let the joining thread know we have set the exit value */
373 lt->join = LT_JOIN_EXIT_VAL_SET;
376 DIAG_EVENT(lt, LT_DIAG_LTHREAD_EXIT, 0, 0);
377 /* set the exit value */
378 if ((ptr != NULL) && (lt->lt_join->lt_exit_ptr != NULL))
379 *(lt->lt_join->lt_exit_ptr) = ptr;
380 /* let the joining thread know we have set the exit value */
381 lt->join = LT_JOIN_EXIT_VAL_SET;
382 _ready_queue_insert(lt->lt_join->sched,
383 (struct lthread *)lt->lt_join);
387 /* wait until the joinging thread has collected the exit value */
388 while (lt->join != LT_JOIN_EXIT_VAL_READ)
391 /* reset join state */
392 lt->join = LT_JOIN_INITIAL;
394 /* detach it so its resources can be released */
395 lt->state |= (BIT(ST_LT_DETACH) | BIT(ST_LT_EXITED));
400 * Suspend until the joined thread returns
402 int lthread_join(struct lthread *lt, void **ptr)
405 return POSIX_ERRNO(EINVAL);
407 struct lthread *current = THIS_LTHREAD;
408 uint64_t lt_state = lt->state;
410 /* invalid to join a detached thread, or a thread that is joined */
411 if ((lt_state & BIT(ST_LT_DETACH)) || (lt->join == LT_JOIN_THREAD_SET))
412 return POSIX_ERRNO(EINVAL);
413 /* pointer to the joining thread and a poingter to return a value */
414 lt->lt_join = current;
415 current->lt_exit_ptr = ptr;
416 /* There is a race between lthread_join() and lthread_exit()
417 * - if join before exit we suspend and will resume when exit is called
418 * - if exit before join we resume the exiting thread
420 uint64_t join_initial = LT_JOIN_INITIAL;
421 if ((lt->join == LT_JOIN_INITIAL)
422 && __atomic_compare_exchange_n(<->join, &join_initial,
423 LT_JOIN_THREAD_SET, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
425 DIAG_EVENT(current, LT_DIAG_LTHREAD_JOIN, lt, 1);
428 DIAG_EVENT(current, LT_DIAG_LTHREAD_JOIN, lt, 0);
429 _ready_queue_insert(lt->sched, lt);
432 /* wait for exiting thread to set return value */
433 while (lt->join != LT_JOIN_EXIT_VAL_SET)
436 /* collect the return value */
438 *ptr = *current->lt_exit_ptr;
440 /* let the exiting thread proceed to exit */
441 lt->join = LT_JOIN_EXIT_VAL_READ;
447 * Detach current lthread
448 * A detached thread cannot be joined
450 void lthread_detach(void)
452 struct lthread *lt = THIS_LTHREAD;
454 DIAG_EVENT(lt, LT_DIAG_LTHREAD_DETACH, 0, 0);
456 uint64_t state = lt->state;
458 lt->state = state | BIT(ST_LT_DETACH);
462 * Set function name of an lthread
463 * this is a debug aid
465 void lthread_set_funcname(const char *f)
467 struct lthread *lt = THIS_LTHREAD;
469 strlcpy(lt->funcname, f, sizeof(lt->funcname));