CNDP 22.08.0
Loading...
Searching...
No Matches
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>
102
103#ifdef __cplusplus
104extern "C" {
105#endif
106
107struct cthread;
108struct cthread_cond;
109struct cthread_sema;
110struct cthread_mutex;
111struct cthread_once;
112struct cthread_barrier;
113struct cthread_condattr;
114struct cthread_sched;
115
120 int32_t cnt;
121};
122
127 uint32_t flags;
128};
129
130#define MUTEX_RECURSIVE_ATTR 0x00000001
131
135typedef 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
195CNDP_API int cthread_num_schedulers_set(int num);
196
202CNDP_API int cthread_active_schedulers(void);
203
219CNDP_API void cthread_scheduler_shutdown(int thread);
220
234
245
246CNDP_API void cthread_run(void);
247
257CNDP_API void cthread_sched_stack_size_set(size_t stack_size);
258
265CNDP_API size_t cthread_sched_stack_size(void);
266
275CNDP_API int cthread_sched_create(size_t stack_size);
276
295CNDP_API struct cthread *cthread_create(const char *name, cthread_func_t func, void *arg);
296
311CNDP_API int cthread_cancel(struct cthread *ct);
312
329CNDP_API int cthread_join(struct cthread *ct, void **ptr);
330
341CNDP_API void cthread_detach(void);
342
358CNDP_API void cthread_exit(void *val);
359
374CNDP_API void cthread_sleep(uint64_t nsecs);
375
390CNDP_API void cthread_sleep_msec(uint64_t ms);
391
406CNDP_API void cthread_sleep_clks(uint64_t clks);
407
422CNDP_API void cthread_sleep_nsecs(uint64_t nsecs);
423
432CNDP_API int cthread_timer_expired(struct cthread *ct);
433
443CNDP_API void cthread_yield(void);
444
459CNDP_API int cthread_set_affinity(int thread);
460
469CNDP_API struct cthread *cthread_current(void);
470
477CNDP_API void cthread_set_name(const char *f);
478
487CNDP_API const char *cthread_get_name(struct cthread *ct);
488
503CNDP_API void cthread_set_data(void *data);
504
516CNDP_API void *cthread_get_data(void);
517
518struct cthread_key;
522typedef void (*tls_destructor_func)(void *);
523
551CNDP_API int cthread_key_create(unsigned int *key, tls_destructor_func destructor);
552
573CNDP_API int cthread_key_delete(unsigned int key);
574
593CNDP_API void *cthread_getspecific(unsigned int key);
594
616
617CNDP_API int cthread_setspecific(unsigned int key, const void *value);
618
655/* start and end of per cthread section */
656extern char __start_per_dt;
657extern 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
667
670#define CNE_PER_CTHREAD(name) \
671 ((typeof(per_dt_##name) *)((char *)cthread_get_data() + \
672 ((char *)&per_dt_##name - &__start_per_dt)))
673
722
723CNDP_API int cthread_mutex_init(const char *name, struct cthread_mutex **mutex,
724 const struct cthread_mutexattr *attr);
725
742CNDP_API int cthread_mutex_destroy(struct cthread_mutex *mutex);
743
764
765CNDP_API int cthread_mutex_lock(struct cthread_mutex *mutex);
766
786CNDP_API int cthread_mutex_trylock(struct cthread_mutex *mutex);
787
806CNDP_API int cthread_mutex_unlock(struct cthread_mutex *mutex);
807
816CNDP_API int cthread_mutex_state(struct cthread_mutex *m);
817
830CNDP_API int cthread_barrier_init(const char *name, struct cthread_barrier **barr, unsigned count);
831
840CNDP_API int cthread_barrier_destroy(struct cthread_barrier *b);
841
850CNDP_API int cthread_barrier_wait(struct cthread_barrier *b);
851
874CNDP_API int cthread_cond_init(const char *name, struct cthread_cond **c,
875 const struct cthread_condattr *attr);
876
891CNDP_API int cthread_cond_destroy(struct cthread_cond *cond);
892
904CNDP_API int cthread_cond_reset(struct cthread_cond *cond);
905
924CNDP_API int cthread_cond_wait(struct cthread_cond *c, struct cthread_mutex *m);
925
947CNDP_API int cthread_cond_timedwait(struct cthread_cond *c, struct cthread_mutex *m,
948 const struct timespec *abstime);
949
964CNDP_API int cthread_cond_signal(struct cthread_cond *c);
965
980CNDP_API int cthread_cond_broadcast(struct cthread_cond *c);
981
992CNDP_API int cthread_cond_broadcast_no_sched(struct cthread_cond *c);
993
1014CNDP_API int cthread_sema_init(const char *name, struct cthread_sema **s,
1015 const struct cthread_semaattr *attr);
1016
1030CNDP_API int cthread_sema_destroy(struct cthread_sema *sema);
1031
1041CNDP_API int cthread_sema_reset(struct cthread_sema *sema);
1042
1058CNDP_API int cthread_sema_wait(struct cthread_sema *s, struct cthread_mutex *m);
1059
1078CNDP_API int cthread_sema_timedwait(struct cthread_sema *s, struct cthread_mutex *m,
1079 const struct timespec *abstime);
1080
1092CNDP_API int cthread_sema_signal(struct cthread_sema *s);
1093
1105CNDP_API int cthread_sema_flush(struct cthread_sema *s);
1106
1118CNDP_API int cthread_sema_flush_no_sched(struct cthread_sema *s);
1119
1123CNDP_API int is_cthread_running(void);
1124
1128typedef int (*cthread_cb_t)(struct cthread *c, void *arg, int idx);
1129
1140CNDP_API struct cthread *cthread_find(struct cthread_sched *s, int threadid);
1141
1154CNDP_API int cthread_foreach(struct cthread_sched *s, cthread_cb_t func, void *arg);
1155
1164CNDP_API struct cthread_sched *cthread_get_sched(struct cthread *c);
1165
1169typedef int (*sched_cb_t)(struct cthread_sched *s, void *arg, int idx);
1170
1179CNDP_API int cthread_sched_id(struct cthread_sched *s);
1180
1189CNDP_API struct cthread_sched *cthread_sched_find(int schedid);
1190
1201CNDP_API int cthread_sched_foreach(sched_cb_t func, void *arg);
1202
1211CNDP_API void *cthread_thread_private(struct cthread *c);
1212
1224CNDP_API int cthread_set_thread_private(struct cthread *c, void *arg);
1225
1234CNDP_API int cthread_once_init(struct cthread_once **once);
1235
1244CNDP_API int cthread_once_destroy(struct cthread_once *once);
1245
1254CNDP_API int cthread_once_reset(struct cthread_once *once);
1255
1268CNDP_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)
void(*) tls_destructor_func(void *)
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 size_t cthread_sched_stack_size(void)
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_sema_timedwait(struct cthread_sema *s, struct cthread_mutex *m, const struct timespec *abstime)
CNDP_API struct cthread_sched * cthread_sched_find(int schedid)
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)
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 int cthread_sema_flush_no_sched(struct cthread_sema *s)
CNDP_API int cthread_cond_destroy(struct cthread_cond *cond)
CNDP_API void * cthread_getspecific(unsigned int key)
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 const char * cthread_get_name(struct cthread *ct)
CNDP_API void cthread_scheduler_shutdown_all(void)
CNDP_API void cthread_yield(void)
int(*) cthread_cb_t(struct cthread *c, void *arg, int idx)
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_sched * cthread_get_sched(struct cthread *c)
CNDP_API int cthread_num_schedulers_set(int num)
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)
void(*) cthread_func_t(void *)
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)
CNDP_API void * cthread_get_data(void)
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)
CNDP_API int cthread_cond_wait(struct cthread_cond *c, struct cthread_mutex *m)
CNDP_API struct cthread * cthread_create(const char *name, cthread_func_t func, void *arg)
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 struct cthread * cthread_current(void)
CNDP_API int cthread_cancel(struct cthread *ct)
int(*) sched_cb_t(struct cthread_sched *s, void *arg, int idx)
CNDP_API int cthread_mutex_state(struct cthread_mutex *m)
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 int cthread_cond_reset(struct cthread_cond *cond)
CNDP_API void * cthread_thread_private(struct cthread *c)
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]
void * arg