CNDP  22.08.0
cthread_int.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2019-2022 Intel Corporation
3  */
4 
5 /*
6  * Some portions of this software may have been derived from the
7  * https://github.com/halayli/lthread which carrys the following license.
8  *
9  * Copyright (c) 2012, Hasan Alayli <halayli@gmail.com>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 #ifndef _CTHREAD_INT_H
33 #define _CTHREAD_INT_H
34 
35 #include <sys/queue.h>
36 #include <cne_per_thread.h>
37 #include <cne_spinlock.h>
38 #include <cne_timer.h>
39 #include <uid.h>
40 #include "cthread.h"
41 #include "ctx.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 struct cthread;
48 struct cthread_sched;
49 struct cthread_cond;
50 struct cthread_mutex;
51 struct cthread_key;
52 
53 struct key_pool;
54 struct qnode;
55 struct qnode_pool;
56 struct cthread_sched;
57 struct cthread_tls;
58 
59 #define BIT(x) (1ULL << (x))
60 
61 #define POSIX_ERRNO(x) (x)
62 
63 #define CTHREAD_NAME_SIZE 64
64 
65 /* define some shorthand for current scheduler and current thread */
66 #define THIS_SCHED CNE_PER_THREAD(this_sched)
67 #define THIS_CTHREAD CNE_PER_THREAD(this_sched)->current_cthread
68 
72 struct cthread_sched {
76  int sched_id;
77  int run_flag;
78  struct ctx ctx;
79  uint64_t birth;
81  uint64_t nb_blocked_threads;
82  STAILQ_HEAD(, cthread) threads;
83  CNE_ATOMIC(uint_least32_t) thread_count;
84  struct cthread_queue *ready;
85  struct cthread_queue *pready;
86  struct cthread_objcache *cthread_cache;
87  struct cthread_objcache *stack_cache;
88  struct cthread_objcache *per_cthread_cache;
89  struct cthread_objcache *tls_cache;
90  struct cthread_objcache *cond_cache;
91  struct cthread_objcache *sema_cache;
92  struct cthread_objcache *barr_cache;
93  struct cthread_objcache *mutex_cache;
94  struct cthread_objcache *once_cache;
96  struct key_pool *key_pool;
97  size_t stack_size;
99 
100 CNE_DECLARE_PER_THREAD(struct cthread_sched *, this_sched);
101 
105 enum cthread_st {
106  CT_STATE_INIT, /* initial state */
107  CT_STATE_READY, /* cthread is ready to run */
108  CT_STATE_EXITED, /* cthread has exited and needs cleanup */
109  CT_STATE_DETACH, /* cthread frees on exit*/
110  CT_STATE_CANCELLED, /* cthread has been cancelled */
111  CT_STATE_COND_WAITING, /* cthread is blocked on condition var */
112  CT_STATE_MUTEX_WAITING, /* cthread is blocked on mutex */
113  CT_STATE_EXPIRED, /* cthread timeout has expired */
114  CT_STATE_SLEEPING, /* cthread is sleeping */
115  NUM_STATES
116 };
117 
118 #define CLEAR_STATE_BITS \
119  (BIT(CT_STATE_COND_WAITING) | BIT(CT_STATE_MUTEX_WAITING) | BIT(CT_STATE_SLEEPING))
120 
121 // clang-format off
122 #define CTHREAD_STATES { \
123  "INIT", \
124  "READY", \
125  "EXITED", \
126  "DETACH", \
127  "CANCELLED", \
128  "COND-WAITING", \
129  "MUTEX-WAITING", \
130  "EXPIRED", \
131  "SLEEPING", \
132  NULL \
133  }
134 // clang-format on
135 
139 enum join_st {
140  CT_JOIN_INITIAL, /* initial state */
141  CT_JOIN_EXITING, /* thread is exiting */
142  CT_JOIN_THREAD_SET, /* joining thread has been set */
143  CT_JOIN_EXIT_VAL_SET, /* exiting thread has set ret val */
144  CT_JOIN_EXIT_VAL_READ /* joining thread has collected ret val */
145 };
146 
151  struct cthread_sched *sched;
152  size_t stack_size;
153  uint8_t stack_start[0];
155 
159 struct cthread {
160  struct ctx ctx;
162  uint64_t state;
163  void *private_data;
164  void *stack;
165  size_t stack_size;
168  void *arg;
170  cthread_exit_func exit_handler;
171  uint64_t birth;
172  struct cthread_queue *pending_wr_queue;
173  struct cthread *dt_join;
174  CNE_ATOMIC(uint_least64_t) join;
175  void **dt_exit_ptr;
177  struct queue_node *qnode;
178  struct cne_timer tim;
179  struct cthread_tls *tls;
181  struct cthread_cond *cond;
182  struct cthread_sema *sema;
183  char name[CTHREAD_NAME_SIZE];
186 
187 /* Internal functions */
194 void _cthread_wait(void);
195 
202 void _cthread_mutex_wait(void);
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #endif /* _CTHREAD_INT_H */
#define __cne_cache_aligned
Definition: cne_common.h:379
#define CNE_DECLARE_PER_THREAD(type, name)
void(* cthread_func_t)(void *)
Definition: cthread_api.h:135
struct cthread_objcache * tls_cache
Definition: cthread_int.h:89
struct cthread_objcache * once_cache
Definition: cthread_int.h:94
uint64_t nb_blocked_threads
Definition: cthread_int.h:81
struct cthread_queue * ready
Definition: cthread_int.h:84
struct cthread_objcache * cthread_cache
Definition: cthread_int.h:86
uint64_t birth
Definition: cthread_int.h:79
struct ctx ctx
Definition: cthread_int.h:78
STAILQ_ENTRY(cthread_sched) next
struct cthread * current_cthread
Definition: cthread_int.h:74
STAILQ_HEAD(, cthread) threads
struct key_pool * key_pool
Definition: cthread_int.h:96
size_t stack_size
Definition: cthread_int.h:97
struct cthread_queue * pready
Definition: cthread_int.h:85
CNE_ATOMIC(uint_least32_t) thread_count
struct cthread_objcache * barr_cache
Definition: cthread_int.h:92
struct cthread_objcache * cond_cache
Definition: cthread_int.h:90
struct cthread_objcache * stack_cache
Definition: cthread_int.h:87
struct cthread_objcache * mutex_cache
Definition: cthread_int.h:93
struct cthread_objcache * sema_cache
Definition: cthread_int.h:91
struct qnode_pool * qnode_pool
Definition: cthread_int.h:95
cne_spinlock_recursive_t lock
Definition: cthread_int.h:75
struct cthread_objcache * per_cthread_cache
Definition: cthread_int.h:88
u_id_t * uid_pool
Definition: cthread_int.h:80
cthread_func_t fun
Definition: cthread_int.h:167
struct cthread * dt_join
Definition: cthread_int.h:173
CNE_ATOMIC(uint_least64_t) join
char name[CTHREAD_NAME_SIZE]
Definition: cthread_int.h:183
struct cthread_queue * pending_wr_queue
Definition: cthread_int.h:172
uint64_t birth
Definition: cthread_int.h:171
struct cthread_sema * sema
Definition: cthread_int.h:182
struct ctx ctx
Definition: cthread_int.h:160
void * per_cthread_data
Definition: cthread_int.h:169
struct cthread_stack * stack_container
Definition: cthread_int.h:180
size_t last_stack_size
Definition: cthread_int.h:166
struct cne_timer tim
Definition: cthread_int.h:178
size_t stack_size
Definition: cthread_int.h:165
void * arg
Definition: cthread_int.h:168
struct cthread_tls * tls
Definition: cthread_int.h:179
uint64_t state
Definition: cthread_int.h:162
void * private_data
Definition: cthread_int.h:163
struct queue_node * qnode
Definition: cthread_int.h:177
struct cthread_sched * sched
Definition: cthread_int.h:176
int cthread_id
Definition: cthread_int.h:161
STAILQ_ENTRY(cthread) next
void ** dt_exit_ptr
Definition: cthread_int.h:175
struct cthread_cond * cond
Definition: cthread_int.h:181
void * stack
Definition: cthread_int.h:164
cthread_exit_func exit_handler
Definition: cthread_int.h:170
void * u_id_t
Definition: uid.h:26