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>
67 #include <sys/types.h>
72 #include <rte_memory.h>
73 #include <rte_cycles.h>
74 #include <rte_per_lcore.h>
75 #include <rte_timer.h>
76 #include <rte_atomic_64.h>
77 #include <rte_spinlock.h>
80 #include <lthread_api.h>
82 #include "lthread_diag.h"
83 #include "lthread_tls.h"
98 #define BIT(x) (1 << (x))
99 #define CLEARBIT(x) ~(1 << (x))
101 #define POSIX_ERRNO(x) (x)
103 #define MAX_LTHREAD_NAME_SIZE 64
105 #define RTE_LOGTYPE_LTHREAD RTE_LOGTYPE_USER1
108 /* define some shorthand for current scheduler and current thread */
109 #define THIS_SCHED RTE_PER_LCORE(this_sched)
110 #define THIS_LTHREAD RTE_PER_LCORE(this_sched)->current_lthread
113 * Definition of an scheduler struct
115 struct lthread_sched
{
116 struct ctx ctx
; /* cpu context */
117 uint64_t birth
; /* time created */
118 struct lthread
*current_lthread
; /* running thread */
119 unsigned lcore_id
; /* this sched lcore */
120 int run_flag
; /* sched shutdown */
121 uint64_t nb_blocked_threads
; /* blocked threads */
122 struct lthread_queue
*ready
; /* local ready queue */
123 struct lthread_queue
*pready
; /* peer ready queue */
124 struct lthread_objcache
*lthread_cache
; /* free lthreads */
125 struct lthread_objcache
*stack_cache
; /* free stacks */
126 struct lthread_objcache
*per_lthread_cache
; /* free per lthread */
127 struct lthread_objcache
*tls_cache
; /* free TLS */
128 struct lthread_objcache
*cond_cache
; /* free cond vars */
129 struct lthread_objcache
*mutex_cache
; /* free mutexes */
130 struct qnode_pool
*qnode_pool
; /* pool of queue nodes */
131 struct key_pool
*key_pool
; /* pool of free TLS keys */
133 uint64_t diag_ref
; /* diag ref */
134 } __rte_cache_aligned
;
136 RTE_DECLARE_PER_LCORE(struct lthread_sched
*, this_sched
);
140 * State for an lthread
143 ST_LT_INIT
, /* initial state */
144 ST_LT_READY
, /* lthread is ready to run */
145 ST_LT_SLEEPING
, /* lthread is sleeping */
146 ST_LT_EXPIRED
, /* lthread timeout has expired */
147 ST_LT_EXITED
, /* lthread has exited and needs cleanup */
148 ST_LT_DETACH
, /* lthread frees on exit*/
149 ST_LT_CANCELLED
, /* lthread has been cancelled */
153 * lthread sub states for exit/join
156 LT_JOIN_INITIAL
, /* initial state */
157 LT_JOIN_EXITING
, /* thread is exiting */
158 LT_JOIN_THREAD_SET
, /* joining thread has been set */
159 LT_JOIN_EXIT_VAL_SET
, /* exiting thread has set ret val */
160 LT_JOIN_EXIT_VAL_READ
, /* joining thread has collected ret val */
163 /* defnition of an lthread stack object */
164 struct lthread_stack
{
165 uint8_t stack
[LTHREAD_MAX_STACK_SIZE
];
167 struct lthread_sched
*root_sched
;
168 } __rte_cache_aligned
;
171 * Definition of an lthread
174 struct ctx ctx
; /* cpu context */
176 uint64_t state
; /* current lthread state */
178 struct lthread_sched
*sched
; /* current scheduler */
179 void *stack
; /* ptr to actual stack */
180 size_t stack_size
; /* current stack_size */
181 size_t last_stack_size
; /* last yield stack_size */
182 lthread_func_t fun
; /* func ctx is running */
183 void *arg
; /* func args passed to func */
184 void *per_lthread_data
; /* per lthread user data */
185 lthread_exit_func exit_handler
; /* called when thread exits */
186 uint64_t birth
; /* time lthread was born */
187 struct lthread_queue
*pending_wr_queue
; /* deferred queue to write */
188 struct lthread
*lt_join
; /* lthread to join on */
189 uint64_t join
; /* state for joining */
190 void **lt_exit_ptr
; /* exit ptr for lthread_join */
191 struct lthread_sched
*root_sched
; /* thread was created here*/
192 struct queue_node
*qnode
; /* node when in a queue */
193 struct rte_timer tim
; /* sleep timer */
194 struct lthread_tls
*tls
; /* keys in use by the thread */
195 struct lthread_stack
*stack_container
; /* stack */
196 char funcname
[MAX_LTHREAD_NAME_SIZE
]; /* thread func name */
197 uint64_t diag_ref
; /* ref to user diag data */
198 } __rte_cache_aligned
;
200 #endif /* LTHREAD_INT_H */