Rizin
unix-like reverse engineering framework and cli tools
rz_th.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2017 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2021-2022 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #ifndef RZ_TH_H
6 #define RZ_TH_H
7 
8 #ifdef _GNU_SOURCE
9 #undef _GNU_SOURCE
10 #endif
11 #define _GNU_SOURCE
12 #include <rz_types.h>
13 #include <rz_list.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #define RZ_THREAD_POOL_ALL_CORES (0)
20 #define RZ_THREAD_QUEUE_UNLIMITED (0)
21 
22 typedef struct rz_th_sem_t RzThreadSemaphore;
23 typedef struct rz_th_lock_t RzThreadLock;
24 typedef struct rz_th_cond_t RzThreadCond;
25 typedef struct rz_th_t RzThread;
26 typedef struct rz_th_pool_t RzThreadPool;
27 typedef struct rz_th_queue_t RzThreadQueue;
28 typedef void *(*RzThreadFunction)(void *user);
29 
30 typedef struct rz_atomic_bool_t RzAtomicBool;
31 
32 #ifdef RZ_API
40 RZ_API bool rz_th_set_affinity(RZ_NONNULL RzThread *th, int cpuid);
41 RZ_API bool rz_th_yield(void);
42 
43 RZ_API RZ_OWN RzThreadSemaphore *rz_th_sem_new(unsigned int initial);
47 
48 RZ_API RZ_OWN RzThreadLock *rz_th_lock_new(bool recursive);
53 
59 
61 RZ_API size_t rz_th_request_physical_cores(size_t max_cores);
62 
63 RZ_API RZ_OWN RzThreadPool *rz_th_pool_new(size_t max_threads);
69 
72 RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail);
77 
82 
83 #endif
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif
size_t len
Definition: 6502dis.c:15
static int value
Definition: cmd_api.c:93
#define RZ_API
RZ_API void rz_th_free(RZ_NULLABLE RzThread *th)
Frees a RzThread structure.
Definition: thread.c:246
RZ_API RZ_OWN void * rz_th_get_user(RZ_NONNULL RzThread *th)
Returns user pointer of thread.
Definition: thread.c:263
RZ_API bool rz_th_yield(void)
Yield the processor.
Definition: thread.c:285
RZ_API bool rz_th_set_affinity(RZ_NONNULL RzThread *th, int cpuid)
Sets the thread cpu affinity.
Definition: thread.c:122
RZ_API bool rz_th_get_name(RZ_NONNULL RzThread *th, RZ_NONNULL RZ_OUT char *name, size_t len)
Gets the name of the thread and writes it into the output buffer.
Definition: thread.c:86
RZ_API RZ_OWN RzThread * rz_th_new(RZ_NONNULL RzThreadFunction function, RZ_NULLABLE void *user)
Creates and starts a new thread.
Definition: thread.c:198
RZ_API RZ_OWN void * rz_th_get_retv(RZ_NONNULL RzThread *th)
Returns return value of the thread.
Definition: thread.c:275
RZ_API bool rz_th_wait(RZ_NONNULL RzThread *th)
Awaits indefinetely for a thread to join.
Definition: thread.c:231
RZ_API bool rz_th_set_name(RZ_NONNULL RzThread *th, RZ_NONNULL const char *name)
Sets the name of the thread.
Definition: thread.c:44
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
void *(* RzThreadFunction)(void *user)
Definition: rz_th.h:28
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_BORROW
Definition: rz_types.h:63
#define cond(bop, top, mask, flags)
Definition: z80asm.h:102
RzThreadPool is a structure which handles n-threads threads.
Definition: thread_pool.c:12
RzThreadQueue is a thread-safe queue that can be listened on from multiple threads.
Definition: thread_queue.c:17
Definition: thread.h:84
RZ_API void rz_th_cond_signal_all(RZ_NONNULL RzThreadCond *cond)
This function shall unblock all threads currently blocked on the specified condition.
Definition: thread_cond.c:48
RZ_API void rz_th_cond_free(RZ_NULLABLE RzThreadCond *cond)
Frees a RzThreadCond struct.
Definition: thread_cond.c:77
RZ_API void rz_th_cond_signal(RZ_NONNULL RzThreadCond *cond)
This function shall unblock at least one of the threads that are blocked on the specified condition.
Definition: thread_cond.c:34
RZ_API void rz_th_cond_wait(RZ_NONNULL RzThreadCond *cond, RZ_NONNULL RzThreadLock *lock)
The function shall block on a condition variable and shall be called with RzThreadLock locked by the ...
Definition: thread_cond.c:63
RZ_API RZ_OWN RzThreadCond * rz_th_cond_new(void)
Condition variables are intended to be used to communicate changes in the state of data shared betwee...
Definition: thread_cond.c:13
RZ_API void rz_th_lock_leave(RZ_NONNULL RzThreadLock *thl)
Releases a RzThreadLock structure.
Definition: thread_lock.c:75
RZ_API void rz_th_lock_free(RZ_NULLABLE RzThreadLock *thl)
Frees a RzThreadLock structure.
Definition: thread_lock.c:89
RZ_API RZ_OWN RzThreadLock * rz_th_lock_new(bool recursive)
Allocates and initialize a RzThreadLock structure.
Definition: thread_lock.c:14
RZ_API bool rz_th_lock_tryenter(RZ_NONNULL RzThreadLock *thl)
Tries to acquire a RzThreadLock structure.
Definition: thread_lock.c:61
RZ_API void rz_th_lock_enter(RZ_NONNULL RzThreadLock *thl)
Acquires a RzThreadLock structure.
Definition: thread_lock.c:45
RZ_API size_t rz_th_request_physical_cores(size_t max_cores)
Returns the maximum number of cores available regardless of the number of cores requested....
Definition: thread_pool.c:78
RZ_API void rz_th_pool_free(RZ_NULLABLE RzThreadPool *pool)
Kills (and frees) the threads and frees the RzThreadPool struct.
Definition: thread_pool.c:117
RZ_API size_t rz_th_physical_core_number()
Returns the number of available physical cores of the host machine.
Definition: thread_pool.c:22
RZ_API RZ_BORROW RzThread * rz_th_pool_get_thread(RZ_NONNULL RzThreadPool *pool, size_t index)
Returns the n-th thread in the thread pool.
Definition: thread_pool.c:158
RZ_API bool rz_th_pool_wait(RZ_NONNULL RzThreadPool *pool)
Waits the end of all the threads in the thread pool.
Definition: thread_pool.c:170
RZ_API bool rz_th_pool_add_thread(RZ_NONNULL RzThreadPool *pool, RZ_NONNULL RzThread *thread)
Adds a thread to the thread pool.
Definition: thread_pool.c:138
RZ_API size_t rz_th_pool_size(RZ_NONNULL RzThreadPool *pool)
Returns the thread pool size.
Definition: thread_pool.c:189
RZ_API RZ_OWN RzThreadPool * rz_th_pool_new(size_t max_threads)
returns a new RzThreadPool structure with a pool of thread
Definition: thread_pool.c:96
RZ_API bool rz_th_queue_is_full(RZ_NONNULL RzThreadQueue *queue)
Returns true if the queue is full and when the size is not RZ_THREAD_QUEUE_UNLIMITED (thread-safe)
Definition: thread_queue.c:164
RZ_API bool rz_th_queue_is_empty(RZ_NONNULL RzThreadQueue *queue)
Returns true if the queue is empty (thread-safe)
Definition: thread_queue.c:148
RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail)
Pushes a new element into the queue.
Definition: thread_queue.c:75
RZ_API void rz_th_queue_free(RZ_NULLABLE RzThreadQueue *queue)
Frees a RzThreadQueue structure.
Definition: thread_queue.c:55
RZ_API RZ_OWN void * rz_th_queue_wait_pop(RZ_NONNULL RzThreadQueue *queue, bool tail)
Removes an element from the queue, but yields the thread till not empty.
Definition: thread_queue.c:124
RZ_API RZ_OWN void * rz_th_queue_pop(RZ_NONNULL RzThreadQueue *queue, bool tail)
Removes an element from the queue, but does not awaits when empty.
Definition: thread_queue.c:102
RZ_API RZ_OWN RzThreadQueue * rz_th_queue_new(size_t max_size, RZ_NULLABLE RzListFree qfree)
Allocates and initializes a new fifo queue.
Definition: thread_queue.c:32
RZ_API RZ_OWN RzThreadSemaphore * rz_th_sem_new(unsigned int initial)
Allocates and initialize a RzThreadSemaphore structure.
Definition: thread_sem.c:26
RZ_API void rz_th_sem_free(RZ_NULLABLE RzThreadSemaphore *sem)
Frees a RzThreadSemaphore struct.
Definition: thread_sem.c:73
RZ_API void rz_th_sem_post(RZ_NONNULL RzThreadSemaphore *sem)
increments (releases) a semaphore
Definition: thread_sem.c:97
RZ_API void rz_th_sem_wait(RZ_NONNULL RzThreadSemaphore *sem)
Decrements (acquires) the semaphore (waits indefinetely)
Definition: thread_sem.c:111
RZ_API RZ_OWN RzAtomicBool * rz_atomic_bool_new(bool value)
Initialize a thread safe bool type container.
Definition: thread_types.c:24
RZ_API bool rz_atomic_bool_get(RZ_NONNULL RzAtomicBool *tbool)
Gets the current value hold by the RzAtomicBool structure.
Definition: thread_types.c:54
RZ_API void rz_atomic_bool_free(RZ_NULLABLE RzAtomicBool *tbool)
Frees a RzAtomicBool structure.
Definition: thread_types.c:39
RZ_API void rz_atomic_bool_set(RZ_NONNULL RzAtomicBool *tbool, bool value)
Sets the value int the RzAtomicBool structure.
Definition: thread_types.c:68
static void lock(volatile int *lk)
Definition: malloc.c:61
uv_pipe_t queue
Definition: worker.c:9