CNDP 22.08.0
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
44extern "C" {
45#endif
46
47struct cthread;
48struct cthread_sched;
49struct cthread_cond;
50struct cthread_mutex;
51struct cthread_key;
52
53struct key_pool;
54struct qnode;
55struct qnode_pool;
56struct cthread_sched;
57struct 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
78 struct ctx ctx;
79 uint64_t birth;
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;
97 size_t stack_size;
99
100CNE_DECLARE_PER_THREAD(struct cthread_sched *, this_sched);
101
105enum 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
139enum 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
159struct cthread {
160 struct ctx ctx;
162 uint64_t state;
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 */
194void _cthread_wait(void);
195
202void _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 *)
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
struct cthread * dt_join
CNE_ATOMIC(uint_least64_t) join
char name[CTHREAD_NAME_SIZE]
struct cthread_queue * pending_wr_queue
uint64_t birth
struct cthread_sema * sema
struct ctx ctx
void * per_cthread_data
struct cthread_stack * stack_container
size_t last_stack_size
struct cne_timer tim
size_t stack_size
void * arg
struct cthread_tls * tls
uint64_t state
void * private_data
struct queue_node * qnode
struct cthread_sched * sched
int cthread_id
STAILQ_ENTRY(cthread) next
void ** dt_exit_ptr
struct cthread_cond * cond
void * stack
cthread_exit_func exit_handler
void * u_id_t
Definition uid.h:26