Blog
Community
Development
Documentation
CNDP
22.08.0
Toggle main menu visibility
Loading...
Searching...
No Matches
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
{
73
STAILQ_ENTRY
(
cthread_sched
) next;
74
struct
cthread
*
current_cthread
;
75
cne_spinlock_recursive_t
lock
;
76
int
sched_id
;
77
int
run_flag
;
78
struct
ctx
ctx
;
79
uint64_t
birth
;
80
u_id_t
*
uid_pool
;
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
;
95
struct
qnode_pool
*
qnode_pool
;
96
struct
key_pool
*
key_pool
;
97
size_t
stack_size
;
98
}
__cne_cache_aligned
;
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
150
struct
cthread_stack
{
151
struct
cthread_sched
*sched;
152
size_t
stack_size;
153
uint8_t stack_start[0];
154
}
__cne_cache_aligned
;
155
159
struct
cthread
{
160
struct
ctx
ctx
;
161
int
cthread_id
;
162
uint64_t
state
;
163
void
*
private_data
;
164
void
*
stack
;
165
size_t
stack_size
;
166
size_t
last_stack_size
;
167
cthread_func_t
fun
;
168
void
*
arg
;
169
void
*
per_cthread_data
;
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
;
176
struct
cthread_sched
*
sched
;
177
struct
queue_node *
qnode
;
178
struct
cne_timer
tim
;
179
struct
cthread_tls *
tls
;
180
struct
cthread_stack
*
stack_container
;
181
struct
cthread_cond *
cond
;
182
struct
cthread_sema *
sema
;
183
char
name
[CTHREAD_NAME_SIZE];
184
STAILQ_ENTRY
(
cthread
) next;
185
}
__cne_cache_aligned
;
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 */
__cne_cache_aligned
#define __cne_cache_aligned
Definition
cne_common.h:379
cne_per_thread.h
CNE_DECLARE_PER_THREAD
#define CNE_DECLARE_PER_THREAD(type, name)
Definition
cne_per_thread.h:35
cne_spinlock.h
cne_timer.h
cthread_func_t
void(*) cthread_func_t(void *)
Definition
cthread_api.h:135
cne_spinlock_recursive_t
Definition
cne_spinlock.h:183
cne_timer
Definition
cne_timer.h:89
cthread_sched
Definition
cthread_int.h:72
cthread_sched::tls_cache
struct cthread_objcache * tls_cache
Definition
cthread_int.h:89
cthread_sched::once_cache
struct cthread_objcache * once_cache
Definition
cthread_int.h:94
cthread_sched::nb_blocked_threads
uint64_t nb_blocked_threads
Definition
cthread_int.h:81
cthread_sched::ready
struct cthread_queue * ready
Definition
cthread_int.h:84
cthread_sched::cthread_cache
struct cthread_objcache * cthread_cache
Definition
cthread_int.h:86
cthread_sched::birth
uint64_t birth
Definition
cthread_int.h:79
cthread_sched::ctx
struct ctx ctx
Definition
cthread_int.h:78
cthread_sched::sched_id
int sched_id
Definition
cthread_int.h:76
cthread_sched::STAILQ_ENTRY
STAILQ_ENTRY(cthread_sched) next
cthread_sched::current_cthread
struct cthread * current_cthread
Definition
cthread_int.h:74
cthread_sched::STAILQ_HEAD
STAILQ_HEAD(, cthread) threads
cthread_sched::key_pool
struct key_pool * key_pool
Definition
cthread_int.h:96
cthread_sched::stack_size
size_t stack_size
Definition
cthread_int.h:97
cthread_sched::pready
struct cthread_queue * pready
Definition
cthread_int.h:85
cthread_sched::CNE_ATOMIC
CNE_ATOMIC(uint_least32_t) thread_count
cthread_sched::barr_cache
struct cthread_objcache * barr_cache
Definition
cthread_int.h:92
cthread_sched::cond_cache
struct cthread_objcache * cond_cache
Definition
cthread_int.h:90
cthread_sched::stack_cache
struct cthread_objcache * stack_cache
Definition
cthread_int.h:87
cthread_sched::mutex_cache
struct cthread_objcache * mutex_cache
Definition
cthread_int.h:93
cthread_sched::sema_cache
struct cthread_objcache * sema_cache
Definition
cthread_int.h:91
cthread_sched::qnode_pool
struct qnode_pool * qnode_pool
Definition
cthread_int.h:95
cthread_sched::lock
cne_spinlock_recursive_t lock
Definition
cthread_int.h:75
cthread_sched::per_cthread_cache
struct cthread_objcache * per_cthread_cache
Definition
cthread_int.h:88
cthread_sched::uid_pool
u_id_t * uid_pool
Definition
cthread_int.h:80
cthread_sched::run_flag
int run_flag
Definition
cthread_int.h:77
cthread_stack
Definition
cthread_int.h:150
cthread
Definition
cthread_int.h:159
cthread::fun
cthread_func_t fun
Definition
cthread_int.h:167
cthread::dt_join
struct cthread * dt_join
Definition
cthread_int.h:173
cthread::CNE_ATOMIC
CNE_ATOMIC(uint_least64_t) join
cthread::name
char name[CTHREAD_NAME_SIZE]
Definition
cthread_int.h:183
cthread::pending_wr_queue
struct cthread_queue * pending_wr_queue
Definition
cthread_int.h:172
cthread::birth
uint64_t birth
Definition
cthread_int.h:171
cthread::sema
struct cthread_sema * sema
Definition
cthread_int.h:182
cthread::ctx
struct ctx ctx
Definition
cthread_int.h:160
cthread::per_cthread_data
void * per_cthread_data
Definition
cthread_int.h:169
cthread::stack_container
struct cthread_stack * stack_container
Definition
cthread_int.h:180
cthread::last_stack_size
size_t last_stack_size
Definition
cthread_int.h:166
cthread::tim
struct cne_timer tim
Definition
cthread_int.h:178
cthread::stack_size
size_t stack_size
Definition
cthread_int.h:165
cthread::arg
void * arg
Definition
cthread_int.h:168
cthread::tls
struct cthread_tls * tls
Definition
cthread_int.h:179
cthread::state
uint64_t state
Definition
cthread_int.h:162
cthread::private_data
void * private_data
Definition
cthread_int.h:163
cthread::qnode
struct queue_node * qnode
Definition
cthread_int.h:177
cthread::sched
struct cthread_sched * sched
Definition
cthread_int.h:176
cthread::cthread_id
int cthread_id
Definition
cthread_int.h:161
cthread::STAILQ_ENTRY
STAILQ_ENTRY(cthread) next
cthread::dt_exit_ptr
void ** dt_exit_ptr
Definition
cthread_int.h:175
cthread::cond
struct cthread_cond * cond
Definition
cthread_int.h:181
cthread::stack
void * stack
Definition
cthread_int.h:164
cthread::exit_handler
cthread_exit_func exit_handler
Definition
cthread_int.h:170
uid.h
u_id_t
void * u_id_t
Definition
uid.h:26
lib
usr
clib
cthread
cthread_int.h
Generated by
1.18.0