* Kill the thread only if is alive. * Remove rz_th_lock_guard. * Use rz_sys_usleep since pthread_yield/sched_yield are not portable. * Kill threads only on error. * Avoid killing already-dead threads. * RzThreadFunction now returns void* and added test_thread_queue. * Removed pthread_exit
83 lines
3.4 KiB
C
83 lines
3.4 KiB
C
// SPDX-FileCopyrightText: 2009-2017 pancake <pancake@nopcode.org>
|
|
// SPDX-FileCopyrightText: 2021-2022 deroad <wargio@libero.it>
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
#ifndef RZ_TH_H
|
|
#define RZ_TH_H
|
|
|
|
#ifdef _GNU_SOURCE
|
|
#undef _GNU_SOURCE
|
|
#endif
|
|
#define _GNU_SOURCE
|
|
#include <rz_types.h>
|
|
#include <rz_list.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#define RZ_THREAD_POOL_ALL_CORES (0)
|
|
#define RZ_THREAD_QUEUE_UNLIMITED (0)
|
|
|
|
typedef struct rz_th_sem_t RzThreadSemaphore;
|
|
typedef struct rz_th_lock_t RzThreadLock;
|
|
typedef struct rz_th_cond_t RzThreadCond;
|
|
typedef struct rz_th_t RzThread;
|
|
typedef struct rz_th_pool_t RzThreadPool;
|
|
typedef struct rz_th_queue_t RzThreadQueue;
|
|
typedef void *(*RzThreadFunction)(void *user);
|
|
|
|
#ifdef RZ_API
|
|
RZ_API RZ_OWN RzThread *rz_th_new(RZ_NONNULL RzThreadFunction function, RZ_NULLABLE void *user);
|
|
RZ_API RZ_OWN void *rz_th_get_user(RZ_NONNULL RzThread *th);
|
|
RZ_API RZ_OWN void *rz_th_get_retv(RZ_NONNULL RzThread *th);
|
|
RZ_API bool rz_th_wait(RZ_NONNULL RzThread *th);
|
|
RZ_API void rz_th_free(RZ_NULLABLE RzThread *th);
|
|
RZ_API void rz_th_kill(RZ_NONNULL RzThread *th);
|
|
RZ_API void rz_th_kill_free(RZ_NONNULL RzThread *th);
|
|
RZ_API bool rz_th_set_name(RZ_NONNULL RzThread *th, RZ_NONNULL const char *name);
|
|
RZ_API bool rz_th_get_name(RZ_NONNULL RzThread *th, RZ_NONNULL RZ_OUT char *name, size_t len);
|
|
RZ_API bool rz_th_set_affinity(RZ_NONNULL RzThread *th, int cpuid);
|
|
RZ_API bool rz_th_yield(void);
|
|
|
|
RZ_API RZ_OWN RzThreadSemaphore *rz_th_sem_new(unsigned int initial);
|
|
RZ_API void rz_th_sem_free(RZ_NULLABLE RzThreadSemaphore *sem);
|
|
RZ_API void rz_th_sem_post(RZ_NONNULL RzThreadSemaphore *sem);
|
|
RZ_API void rz_th_sem_wait(RZ_NONNULL RzThreadSemaphore *sem);
|
|
|
|
RZ_API RZ_OWN RzThreadLock *rz_th_lock_new(bool recursive);
|
|
RZ_API bool rz_th_lock_tryenter(RZ_NONNULL RzThreadLock *thl);
|
|
RZ_API void rz_th_lock_enter(RZ_NONNULL RzThreadLock *thl);
|
|
RZ_API void rz_th_lock_leave(RZ_NONNULL RzThreadLock *thl);
|
|
RZ_API void rz_th_lock_free(RZ_NULLABLE RzThreadLock *thl);
|
|
|
|
RZ_API RZ_OWN RzThreadCond *rz_th_cond_new(void);
|
|
RZ_API void rz_th_cond_signal(RZ_NONNULL RzThreadCond *cond);
|
|
RZ_API void rz_th_cond_signal_all(RZ_NONNULL RzThreadCond *cond);
|
|
RZ_API void rz_th_cond_wait(RZ_NONNULL RzThreadCond *cond, RZ_NONNULL RzThreadLock *lock);
|
|
RZ_API void rz_th_cond_free(RZ_NULLABLE RzThreadCond *cond);
|
|
|
|
RZ_API size_t rz_th_physical_core_number();
|
|
RZ_API RZ_OWN RzThreadPool *rz_th_pool_new(size_t max_threads);
|
|
RZ_API void rz_th_pool_free(RZ_NULLABLE RzThreadPool *pool);
|
|
RZ_API bool rz_th_pool_add_thread(RZ_NONNULL RzThreadPool *pool, RZ_NONNULL RzThread *thread);
|
|
RZ_API RZ_BORROW RzThread *rz_th_pool_get_thread(RZ_NONNULL RzThreadPool *pool, size_t index);
|
|
RZ_API bool rz_th_pool_wait(RZ_NONNULL RzThreadPool *pool);
|
|
RZ_API bool rz_th_pool_kill(RZ_NONNULL RzThreadPool *pool);
|
|
RZ_API size_t rz_th_pool_size(RZ_NULLABLE RzThreadPool *pool);
|
|
|
|
RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new(size_t max_size, RZ_NULLABLE RzListFree qfree);
|
|
RZ_API void rz_th_queue_free(RZ_NULLABLE RzThreadQueue *queue);
|
|
RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail);
|
|
RZ_API RZ_OWN void *rz_th_queue_pop(RZ_NONNULL RzThreadQueue *queue, bool tail);
|
|
RZ_API RZ_OWN void *rz_th_queue_wait_pop(RZ_NONNULL RzThreadQueue *queue, bool tail);
|
|
RZ_API bool rz_th_queue_is_empty(RZ_NULLABLE RzThreadQueue *queue);
|
|
RZ_API bool rz_th_queue_is_full(RZ_NULLABLE RzThreadQueue *queue);
|
|
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|