]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/examples/performance-thread/common/lthread_int.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / dpdk / examples / performance-thread / common / lthread_int.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33
34 /*
35 * Some portions of this software may have been derived from the
36 * https://github.com/halayli/lthread which carrys the following license.
37 *
38 * Copyright (C) 2012, Hasan Alayli <halayli@gmail.com>
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 */
61 #ifndef LTHREAD_INT_H
62 #include <lthread_api.h>
63 #define LTHREAD_INT_H
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 #include <stdint.h>
70 #include <sys/time.h>
71 #include <sys/types.h>
72 #include <errno.h>
73 #include <pthread.h>
74 #include <time.h>
75
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>
82 #include <ctx.h>
83
84 #include <lthread_api.h>
85 #include "lthread.h"
86 #include "lthread_diag.h"
87 #include "lthread_tls.h"
88
89 struct lthread;
90 struct lthread_sched;
91 struct lthread_cond;
92 struct lthread_mutex;
93 struct lthread_key;
94
95 struct key_pool;
96 struct qnode;
97 struct qnode_pool;
98 struct lthread_sched;
99 struct lthread_tls;
100
101
102 #define BIT(x) (1 << (x))
103 #define CLEARBIT(x) ~(1 << (x))
104
105 #define POSIX_ERRNO(x) (x)
106
107 #define MAX_LTHREAD_NAME_SIZE 64
108
109 #define RTE_LOGTYPE_LTHREAD RTE_LOGTYPE_USER1
110
111
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
115
116 /*
117 * Definition of an scheduler struct
118 */
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 */
136 size_t stack_size;
137 uint64_t diag_ref; /* diag ref */
138 } __rte_cache_aligned;
139
140 RTE_DECLARE_PER_LCORE(struct lthread_sched *, this_sched);
141
142
143 /*
144 * State for an lthread
145 */
146 enum lthread_st {
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 */
154 };
155
156 /*
157 * lthread sub states for exit/join
158 */
159 enum join_st {
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 */
165 };
166
167 /* defnition of an lthread stack object */
168 struct lthread_stack {
169 uint8_t stack[LTHREAD_MAX_STACK_SIZE];
170 size_t stack_size;
171 struct lthread_sched *root_sched;
172 } __rte_cache_aligned;
173
174 /*
175 * Definition of an lthread
176 */
177 struct lthread {
178 struct ctx ctx; /* cpu context */
179
180 uint64_t state; /* current lthread state */
181
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;
203
204 #ifdef __cplusplus
205 }
206 #endif
207
208 #endif /* LTHREAD_INT_H */