CNDP  22.08.0
cthread_api.h
Go to the documentation of this file.
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 
92 #ifndef _CTHREAD_API_H
93 #define _CTHREAD_API_H
94 
95 #include <sys/socket.h>
96 #include <fcntl.h>
97 #include <netinet/in.h>
98 
99 #include <cne_cycles.h>
100 #include <cne_log.h>
101 #include <cne_branch_prediction.h>
102 
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
107 struct cthread;
108 struct cthread_cond;
109 struct cthread_sema;
110 struct cthread_mutex;
111 struct cthread_once;
112 struct cthread_barrier;
113 struct cthread_condattr;
114 struct cthread_sched;
115 
120  int32_t cnt;
121 };
122 
127  uint32_t flags;
128 };
129 
130 #define MUTEX_RECURSIVE_ATTR 0x00000001
135 typedef void (*cthread_func_t)(void *);
136 
137 /*
138  * Define the size of stack for an cthread
139  * Then this is the size that will be allocated on cthread creation
140  * This is a fixed size and will not grow.
141  */
142 #define CTHREAD_DEFAULT_STACK_SIZE (1024 * 16)
143 
148 #define CTHREAD_MAX_KEYS 256
149 
154 #define CTHREAD_DESTRUCTOR_ITERATIONS 4
155 
159 #define CTHREAD_MAX_THREADS CNE_MAX_THREADS
160 
171 #define CTHREAD_PREALLOC 64
172 
195 CNDP_API int cthread_num_schedulers_set(int num);
196 
202 CNDP_API int cthread_active_schedulers(void);
203 
219 CNDP_API void cthread_scheduler_shutdown(int thread);
220 
233 CNDP_API void cthread_scheduler_shutdown_all(void);
234 
246 CNDP_API void cthread_run(void);
247 
257 CNDP_API void cthread_sched_stack_size_set(size_t stack_size);
258 
265 CNDP_API size_t cthread_sched_stack_size(void);
266 
275 CNDP_API int cthread_sched_create(size_t stack_size);
276 
295 CNDP_API struct cthread *cthread_create(const char *name, cthread_func_t func, void *arg);
296 
311 CNDP_API int cthread_cancel(struct cthread *ct);
312 
329 CNDP_API int cthread_join(struct cthread *ct, void **ptr);
330 
341 CNDP_API void cthread_detach(void);
342 
358 CNDP_API void cthread_exit(void *val);
359 
374 CNDP_API void cthread_sleep(uint64_t nsecs);
375 
390 CNDP_API void cthread_sleep_msec(uint64_t ms);
391 
406 CNDP_API void cthread_sleep_clks(uint64_t clks);
407 
422 CNDP_API void cthread_sleep_nsecs(uint64_t nsecs);
423 
432 CNDP_API int cthread_timer_expired(struct cthread *ct);
433 
443 CNDP_API void cthread_yield(void);
444 
459 CNDP_API int cthread_set_affinity(int thread);
460 
469 CNDP_API struct cthread *cthread_current(void);
470 
477 CNDP_API void cthread_set_name(const char *f);
478 
487 CNDP_API const char *cthread_get_name(struct cthread *ct);
488 
503 CNDP_API void cthread_set_data(void *data);
504 
516 CNDP_API void *cthread_get_data(void);
517 
518 struct cthread_key;
522 typedef void (*tls_destructor_func)(void *);
523 
551 CNDP_API int cthread_key_create(unsigned int *key, tls_destructor_func destructor);
552 
573 CNDP_API int cthread_key_delete(unsigned int key);
574 
593 CNDP_API void *cthread_getspecific(unsigned int key);
594 
617 CNDP_API int cthread_setspecific(unsigned int key, const void *value);
618 
655 /* start and end of per cthread section */
656 extern char __start_per_dt;
657 extern char __stop_per_dt;
658 
659 #define CNE_DEFINE_PER_CTHREAD(type, name) \
660  __typeof__(type) __attribute((section("per_dt"))) per_dt_##name
661 
665 #define CNE_DECLARE_PER_CTHREAD(type, name) \
666  extern __typeof__(type) __attribute((section("per_dt"))) per_dt_##name
670 #define CNE_PER_CTHREAD(name) \
671  ((typeof(per_dt_##name) *)((char *)cthread_get_data() + \
672  ((char *)&per_dt_##name - &__start_per_dt)))
673 
723 CNDP_API int cthread_mutex_init(const char *name, struct cthread_mutex **mutex,
724  const struct cthread_mutexattr *attr);
725 
742 CNDP_API int cthread_mutex_destroy(struct cthread_mutex *mutex);
743 
765 CNDP_API int cthread_mutex_lock(struct cthread_mutex *mutex);
766 
786 CNDP_API int cthread_mutex_trylock(struct cthread_mutex *mutex);
787 
806 CNDP_API int cthread_mutex_unlock(struct cthread_mutex *mutex);
807 
816 CNDP_API int cthread_mutex_state(struct cthread_mutex *m);
817 
830 CNDP_API int cthread_barrier_init(const char *name, struct cthread_barrier **barr, unsigned count);
831 
840 CNDP_API int cthread_barrier_destroy(struct cthread_barrier *b);
841 
850 CNDP_API int cthread_barrier_wait(struct cthread_barrier *b);
851 
874 CNDP_API int cthread_cond_init(const char *name, struct cthread_cond **c,
875  const struct cthread_condattr *attr);
876 
891 CNDP_API int cthread_cond_destroy(struct cthread_cond *cond);
892 
904 CNDP_API int cthread_cond_reset(struct cthread_cond *cond);
905 
924 CNDP_API int cthread_cond_wait(struct cthread_cond *c, struct cthread_mutex *m);
925 
947 CNDP_API int cthread_cond_timedwait(struct cthread_cond *c, struct cthread_mutex *m,
948  const struct timespec *abstime);
949 
964 CNDP_API int cthread_cond_signal(struct cthread_cond *c);
965 
980 CNDP_API int cthread_cond_broadcast(struct cthread_cond *c);
981 
992 CNDP_API int cthread_cond_broadcast_no_sched(struct cthread_cond *c);
993 
1014 CNDP_API int cthread_sema_init(const char *name, struct cthread_sema **s,
1015  const struct cthread_semaattr *attr);
1016 
1030 CNDP_API int cthread_sema_destroy(struct cthread_sema *sema);
1031 
1041 CNDP_API int cthread_sema_reset(struct cthread_sema *sema);
1042 
1058 CNDP_API int cthread_sema_wait(struct cthread_sema *s, struct cthread_mutex *m);
1059 
1078 CNDP_API int cthread_sema_timedwait(struct cthread_sema *s, struct cthread_mutex *m,
1079  const struct timespec *abstime);
1080 
1092 CNDP_API int cthread_sema_signal(struct cthread_sema *s);
1093 
1105 CNDP_API int cthread_sema_flush(struct cthread_sema *s);
1106 
1118 CNDP_API int cthread_sema_flush_no_sched(struct cthread_sema *s);
1119 
1123 CNDP_API int is_cthread_running(void);
1124 
1128 typedef int (*cthread_cb_t)(struct cthread *c, void *arg, int idx);
1129 
1140 CNDP_API struct cthread *cthread_find(struct cthread_sched *s, int threadid);
1141 
1154 CNDP_API int cthread_foreach(struct cthread_sched *s, cthread_cb_t func, void *arg);
1155 
1164 CNDP_API struct cthread_sched *cthread_get_sched(struct cthread *c);
1165 
1169 typedef int (*sched_cb_t)(struct cthread_sched *s, void *arg, int idx);
1170 
1179 CNDP_API int cthread_sched_id(struct cthread_sched *s);
1180 
1189 CNDP_API struct cthread_sched *cthread_sched_find(int schedid);
1190 
1201 CNDP_API int cthread_sched_foreach(sched_cb_t func, void *arg);
1202 
1211 CNDP_API void *cthread_thread_private(struct cthread *c);
1212 
1224 CNDP_API int cthread_set_thread_private(struct cthread *c, void *arg);
1225 
1234 CNDP_API int cthread_once_init(struct cthread_once **once);
1235 
1244 CNDP_API int cthread_once_destroy(struct cthread_once *once);
1245 
1254 CNDP_API int cthread_once_reset(struct cthread_once *once);
1255 
1268 CNDP_API int cthread_once(struct cthread_once *once, int (*func)(void *), void *arg);
1269 
1270 #ifdef __cplusplus
1271 }
1272 #endif
1273 
1274 #endif /* _CTHREAD_API_H */
CNDP_API int cthread_barrier_wait(struct cthread_barrier *b)
CNDP_API int is_cthread_running(void)
CNDP_API int cthread_setspecific(unsigned int key, const void *value)
CNDP_API struct cthread * cthread_find(struct cthread_sched *s, int threadid)
CNDP_API int cthread_barrier_destroy(struct cthread_barrier *b)
CNDP_API int cthread_cond_init(const char *name, struct cthread_cond **c, const struct cthread_condattr *attr)
CNDP_API struct cthread_sched * cthread_get_sched(struct cthread *c)
CNDP_API size_t cthread_sched_stack_size(void)
CNDP_API struct cthread_sched * cthread_sched_find(int schedid)
CNDP_API int cthread_sched_foreach(sched_cb_t func, void *arg)
CNDP_API int cthread_mutex_destroy(struct cthread_mutex *mutex)
CNDP_API int cthread_mutex_lock(struct cthread_mutex *mutex)
CNDP_API void cthread_scheduler_shutdown(int thread)
CNDP_API int cthread_foreach(struct cthread_sched *s, cthread_cb_t func, void *arg)
CNDP_API int cthread_timer_expired(struct cthread *ct)
CNDP_API int cthread_sema_wait(struct cthread_sema *s, struct cthread_mutex *m)
CNDP_API int cthread_active_schedulers(void)
CNDP_API int cthread_once(struct cthread_once *once, int(*func)(void *), void *arg)
CNDP_API int cthread_sema_timedwait(struct cthread_sema *s, struct cthread_mutex *m, const struct timespec *abstime)
CNDP_API int cthread_mutex_trylock(struct cthread_mutex *mutex)
CNDP_API void cthread_set_name(const char *f)
CNDP_API void cthread_set_data(void *data)
void(* tls_destructor_func)(void *)
Definition: cthread_api.h:522
CNDP_API int cthread_sched_id(struct cthread_sched *s)
CNDP_API void cthread_sleep_clks(uint64_t clks)
CNDP_API void cthread_sleep(uint64_t nsecs)
CNDP_API int cthread_join(struct cthread *ct, void **ptr)
CNDP_API void cthread_sched_stack_size_set(size_t stack_size)
CNDP_API struct cthread * cthread_current(void)
CNDP_API void * cthread_getspecific(unsigned int key)
CNDP_API int cthread_sema_flush_no_sched(struct cthread_sema *s)
CNDP_API int cthread_cond_destroy(struct cthread_cond *cond)
CNDP_API const char * cthread_get_name(struct cthread *ct)
CNDP_API int cthread_key_delete(unsigned int key)
CNDP_API void cthread_run(void)
CNDP_API int cthread_barrier_init(const char *name, struct cthread_barrier **barr, unsigned count)
CNDP_API void cthread_scheduler_shutdown_all(void)
CNDP_API void cthread_yield(void)
CNDP_API int cthread_key_create(unsigned int *key, tls_destructor_func destructor)
CNDP_API int cthread_mutex_unlock(struct cthread_mutex *mutex)
CNDP_API int cthread_cond_broadcast_no_sched(struct cthread_cond *c)
CNDP_API struct cthread * cthread_create(const char *name, cthread_func_t func, void *arg)
CNDP_API int cthread_num_schedulers_set(int num)
void(* cthread_func_t)(void *)
Definition: cthread_api.h:135
int(* sched_cb_t)(struct cthread_sched *s, void *arg, int idx)
Definition: cthread_api.h:1169
CNDP_API int cthread_sema_destroy(struct cthread_sema *sema)
CNDP_API int cthread_sema_init(const char *name, struct cthread_sema **s, const struct cthread_semaattr *attr)
CNDP_API void cthread_detach(void)
CNDP_API void cthread_sleep_nsecs(uint64_t nsecs)
CNDP_API int cthread_sched_create(size_t stack_size)
CNDP_API int cthread_sema_signal(struct cthread_sema *s)
char __start_per_dt
CNDP_API int cthread_sema_reset(struct cthread_sema *sema)
CNDP_API int cthread_set_thread_private(struct cthread *c, void *arg)
CNDP_API int cthread_set_affinity(int thread)
CNDP_API int cthread_cond_broadcast(struct cthread_cond *c)
CNDP_API int cthread_once_destroy(struct cthread_once *once)
int(* cthread_cb_t)(struct cthread *c, void *arg, int idx)
Definition: cthread_api.h:1128
CNDP_API int cthread_cond_wait(struct cthread_cond *c, struct cthread_mutex *m)
CNDP_API int cthread_once_init(struct cthread_once **once)
CNDP_API int cthread_cond_timedwait(struct cthread_cond *c, struct cthread_mutex *m, const struct timespec *abstime)
CNDP_API int cthread_once_reset(struct cthread_once *once)
CNDP_API int cthread_cancel(struct cthread *ct)
CNDP_API int cthread_mutex_state(struct cthread_mutex *m)
CNDP_API void * cthread_thread_private(struct cthread *c)
CNDP_API int cthread_mutex_init(const char *name, struct cthread_mutex **mutex, const struct cthread_mutexattr *attr)
CNDP_API void cthread_exit(void *val)
CNDP_API void * cthread_get_data(void)
CNDP_API int cthread_cond_reset(struct cthread_cond *cond)
CNDP_API int cthread_cond_signal(struct cthread_cond *c)
CNDP_API void cthread_sleep_msec(uint64_t ms)
CNDP_API int cthread_sema_flush(struct cthread_sema *s)
char name[CTHREAD_NAME_SIZE]
Definition: cthread_int.h:183
void * arg
Definition: cthread_int.h:168