4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Some portions of this software may have been derived from the
36 * https://github.com/halayli/lthread which carrys the following license.
38 * Copyright (C) 2012, Hasan Alayli <halayli@gmail.com>
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <lthread_api.h>
71 #include <sys/types.h>
76 #include <rte_memory.h>
77 #include <rte_cycles.h>
78 #include <rte_per_lcore.h>
79 #include <rte_timer.h>
80 #include <rte_atomic_64.h>
81 #include <rte_spinlock.h>
84 #include <lthread_api.h>
86 #include "lthread_diag.h"
87 #include "lthread_tls.h"
102 #define BIT(x) (1 << (x))
103 #define CLEARBIT(x) ~(1 << (x))
105 #define POSIX_ERRNO(x) (x)
107 #define MAX_LTHREAD_NAME_SIZE 64
109 #define RTE_LOGTYPE_LTHREAD RTE_LOGTYPE_USER1
112 /* define some shorthand for current scheduler and current thread */
113 #define THIS_SCHED RTE_PER_LCORE(this_sched)
114 #define THIS_LTHREAD RTE_PER_LCORE(this_sched)->current_lthread
117 * Definition of an scheduler struct
119 struct lthread_sched
{
120 struct ctx ctx
; /* cpu context */
121 uint64_t birth
; /* time created */
122 struct lthread
*current_lthread
; /* running thread */
123 unsigned lcore_id
; /* this sched lcore */
124 int run_flag
; /* sched shutdown */
125 uint64_t nb_blocked_threads
; /* blocked threads */
126 struct lthread_queue
*ready
; /* local ready queue */
127 struct lthread_queue
*pready
; /* peer ready queue */
128 struct lthread_objcache
*lthread_cache
; /* free lthreads */
129 struct lthread_objcache
*stack_cache
; /* free stacks */
130 struct lthread_objcache
*per_lthread_cache
; /* free per lthread */
131 struct lthread_objcache
*tls_cache
; /* free TLS */
132 struct lthread_objcache
*cond_cache
; /* free cond vars */
133 struct lthread_objcache
*mutex_cache
; /* free mutexes */
134 struct qnode_pool
*qnode_pool
; /* pool of queue nodes */
135 struct key_pool
*key_pool
; /* pool of free TLS keys */
137 uint64_t diag_ref
; /* diag ref */
138 } __rte_cache_aligned
;
140 RTE_DECLARE_PER_LCORE(struct lthread_sched
*, this_sched
);
144 * State for an lthread
147 ST_LT_INIT
, /* initial state */
148 ST_LT_READY
, /* lthread is ready to run */
149 ST_LT_SLEEPING
, /* lthread is sleeping */
150 ST_LT_EXPIRED
, /* lthread timeout has expired */
151 ST_LT_EXITED
, /* lthread has exited and needs cleanup */
152 ST_LT_DETACH
, /* lthread frees on exit*/
153 ST_LT_CANCELLED
, /* lthread has been cancelled */
157 * lthread sub states for exit/join
160 LT_JOIN_INITIAL
, /* initial state */
161 LT_JOIN_EXITING
, /* thread is exiting */
162 LT_JOIN_THREAD_SET
, /* joining thread has been set */
163 LT_JOIN_EXIT_VAL_SET
, /* exiting thread has set ret val */
164 LT_JOIN_EXIT_VAL_READ
, /* joining thread has collected ret val */
167 /* defnition of an lthread stack object */
168 struct lthread_stack
{
169 uint8_t stack
[LTHREAD_MAX_STACK_SIZE
];
171 struct lthread_sched
*root_sched
;
172 } __rte_cache_aligned
;
175 * Definition of an lthread
178 struct ctx ctx
; /* cpu context */
180 uint64_t state
; /* current lthread state */
182 struct lthread_sched
*sched
; /* current scheduler */
183 void *stack
; /* ptr to actual stack */
184 size_t stack_size
; /* current stack_size */
185 size_t last_stack_size
; /* last yield stack_size */
186 lthread_func_t fun
; /* func ctx is running */
187 void *arg
; /* func args passed to func */
188 void *per_lthread_data
; /* per lthread user data */
189 lthread_exit_func exit_handler
; /* called when thread exits */
190 uint64_t birth
; /* time lthread was born */
191 struct lthread_queue
*pending_wr_queue
; /* deferred queue to write */
192 struct lthread
*lt_join
; /* lthread to join on */
193 uint64_t join
; /* state for joining */
194 void **lt_exit_ptr
; /* exit ptr for lthread_join */
195 struct lthread_sched
*root_sched
; /* thread was created here*/
196 struct queue_node
*qnode
; /* node when in a queue */
197 struct rte_timer tim
; /* sleep timer */
198 struct lthread_tls
*tls
; /* keys in use by the thread */
199 struct lthread_stack
*stack_container
; /* stack */
200 char funcname
[MAX_LTHREAD_NAME_SIZE
]; /* thread func name */
201 uint64_t diag_ref
; /* ref to user diag data */
202 } __rte_cache_aligned
;
208 #endif /* LTHREAD_INT_H */