rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rthreads.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2015-2018 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_THREAD_H__
19#define __R_THREAD_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
31#include <rlib/rtypes.h>
33#include <rlib/data/rbitset.h>
34#include <rlib/rref.h>
35
37
60
66#define R_ONCE_INIT { R_ONCE_STATE_INIT, NULL }
78typedef struct
79{
80 rauint state;
82} ROnce;
83
89#define R_TSS_INIT(notify) { (RDestroyNotify)(notify), { NULL } }
100typedef struct
101{
103 union {
104 rpointer ptr;
105 raptr aptr;
106 ruint ui;
107 ruint8 u8;
108 ruint16 u16;
109 ruint32 u32;
110 ruint64 u64;
111 ruint8 data[sizeof (rpointer) * 4];
112 } impl;
113} RTss;
114
123
129typedef rpointer (*RThreadFunc) (rpointer data);
131typedef struct RThread RThread;
132
133
154
/* r_once group */
156
181R_API void r_tss_set (RTss * tss, rpointer data);
187R_API void r_mutex_init (RMutex * mutex);
191R_API void r_mutex_lock (RMutex * mutex);
242R_API void r_cond_init (RCond * cond);
252R_API void r_cond_wait (RCond * cond, RMutex * mutex);
265#define r_thread_new(name, func, data) \
266 r_thread_new_full (name, NULL, func, data)
280 const RBitset * cpuset, RThreadFunc func, rpointer data);
282#define r_thread_ref r_ref_ref
284#define r_thread_unref r_ref_unref
298static inline void r_thread_join_unref (rpointer thread)
299{
300 r_thread_join (thread);
301 r_thread_unref (thread);
302}
307R_API int r_thread_kill (RThread * thread, int sig);
309R_API const rchar * r_thread_get_name (const RThread * thread);
322
336
337#ifdef R_OS_WIN32
342R_API void r_thread_win32_dll_thread_detach (void);
343#endif
/* r_threads group */
347
348R_END_DECLS
349
350#endif /* __R_THREAD_H__ */
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
rpointer RMutex
Opaque mutex handle; initialise with r_mutex_init.
Definition rthreads.h:116
rpointer RRMutex
Opaque recursive-mutex handle; initialise with r_rmutex_init.
Definition rthreads.h:118
struct RThread RThread
Opaque, refcounted thread handle returned by r_thread_new_full.
Definition rthreads.h:131
ROnceState
Lifecycle states of an ROnce.
Definition rthreads.h:55
rpointer(* RThreadFunc)(rpointer data)
Thread entry-point signature.
Definition rthreads.h:129
rpointer RCond
Opaque condition-variable handle; initialise with r_cond_init.
Definition rthreads.h:122
rpointer r_call_once(ROnce *once, RThreadFunc f, rpointer a)
Run f exactly once across all threads, return its result.
rpointer RRWMutex
Opaque read/write-mutex handle; initialise with r_rwmutex_init.
Definition rthreads.h:120
@ R_ONCE_STATE_RUNNING
Definition rthreads.h:57
@ R_ONCE_STATE_INIT
Definition rthreads.h:56
@ R_ONCE_STATE_DONE
Definition rthreads.h:58
void r_mutex_init(RMutex *mutex)
Initialise a fresh RMutex.
void r_thread_exit(rpointer retval)
Exit the calling thread, returning retval to its joiner.
void r_thread_sleep(ruint sec)
Sleep the calling thread for sec whole seconds.
void r_cond_broadcast(RCond *cond)
Wake all threads waiting on cond.
void r_mutex_clear(RMutex *mutex)
Release OS resources held by mutex; must be unlocked first.
void r_rmutex_lock(RRMutex *mutex)
Acquire mutex (recursively for the owning thread).
void r_rmutex_init(RRMutex *mutex)
Initialise a fresh RRMutex.
void r_rwmutex_rdunlock(RRWMutex *mutex)
Release a previously-acquired read lock.
void r_mutex_unlock(RMutex *mutex)
Release mutex; the calling thread must currently hold it.
void r_tss_set(RTss *tss, rpointer data)
Set this thread's TSS value, freeing the previous one via the notify callback if any.
void r_thread_usleep(rulong microsec)
Sleep the calling thread for microsec microseconds.
rboolean r_thread_set_affinity(RThread *thread, const RBitset *cpuset)
Set thread's CPU-affinity mask.
void r_cond_clear(RCond *cond)
Release OS resources held by cond.
void r_cond_wait(RCond *cond, RMutex *mutex)
Atomically release mutex and wait until signalled, then reacquire mutex before returning.
int r_thread_kill(RThread *thread, int sig)
Send signal sig to thread (POSIX-style; thin wrapper over pthread_kill on Unix).
void r_rwmutex_init(RRWMutex *mutex)
Initialise a fresh RRWMutex.
void r_rmutex_unlock(RRMutex *mutex)
Release one nesting level of mutex.
void r_thread_yield(void)
Yield the calling thread's remaining quantum to the scheduler.
const rchar * r_thread_get_name(const RThread *thread)
Return the human-readable name thread was given at creation.
#define r_thread_unref
Drop a reference on thread (alias for r_ref_unref).
Definition rthreads.h:284
rboolean r_rwmutex_trywrlock(RRWMutex *mutex)
Try-lock variant of r_rwmutex_wrlock.
static void r_thread_join_unref(rpointer thread)
RDestroyNotify-compatible "join then unref" helper.
Definition rthreads.h:298
rboolean r_rwmutex_tryrdlock(RRWMutex *mutex)
Try-lock variant of r_rwmutex_rdlock.
RThread * r_thread_new_full(const rchar *name, const RBitset *cpuset, RThreadFunc func, rpointer data)
Spawn a new thread running func(data).
void r_rwmutex_rdlock(RRWMutex *mutex)
Acquire a shared read lock; blocks until no writer holds the lock.
rboolean r_mutex_trylock(RMutex *mutex)
Try to acquire mutex without blocking; TRUE on success.
rboolean r_thread_get_affinity(const RThread *thread, RBitset *cpuset)
Read thread's current CPU-affinity bitset into cpuset.
ruint r_thread_get_id(const RThread *thread)
Return thread's OS-assigned numeric ID.
rpointer r_thread_join(RThread *thread)
Wait for thread to terminate and return its result.
void r_rwmutex_wrunlock(RRWMutex *mutex)
Release a previously-acquired write lock.
void r_rwmutex_wrlock(RRWMutex *mutex)
Acquire an exclusive write lock; blocks until no reader or writer holds the lock.
RThread * r_thread_current(void)
Return the calling thread's RThread handle, or NULL if the calling thread was not created by rlib.
void r_cond_signal(RCond *cond)
Wake at least one thread waiting on cond.
void r_mutex_lock(RMutex *mutex)
Acquire mutex, blocking until available.
void r_cond_init(RCond *cond)
Initialise a fresh RCond.
void r_rmutex_clear(RRMutex *mutex)
Release OS resources held by mutex.
rpointer r_tss_get(RTss *tss)
Retrieve this thread's TSS value, or NULL if unset.
void r_rwmutex_clear(RRWMutex *mutex)
Release OS resources held by mutex.
rboolean r_rmutex_trylock(RRMutex *mutex)
Try-lock variant; see r_mutex_trylock.
unsigned long rulong
Unsigned long.
Definition rtypes.h:155
char rchar
Default character type (char).
Definition rtypes.h:137
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
unsigned int ruint
Unsigned int.
Definition rtypes.h:157
void(* RDestroyNotify)(rpointer ptr)
Destructor callback: free / release the value at ptr.
Definition rtypes.h:401
unsigned int ruint32
Unsigned 32-bit integer.
Definition rtypes.h:192
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
unsigned char ruint8
Unsigned 8-bit integer.
Definition rtypes.h:190
unsigned long ruint64
Unsigned 64-bit integer.
Definition rtypes.h:193
unsigned short ruint16
Unsigned 16-bit integer.
Definition rtypes.h:191
Atomic load / store / exchange / CAS / fetch-modify operations on integer, unsigned-integer and point...
Fixed-width bitset with bit / byte / word accessors and bitwise compound operations.
Refcount base struct shared by every refcounted type in rlib.
Foundational type aliases used by every rlib header.
Fixed-width bitset.
Definition rbitset.h:68
One-shot initialisation guard.
Definition rthreads.h:79
rauint state
Definition rthreads.h:80
rpointer ret
Definition rthreads.h:81
Thread-specific storage slot.
Definition rthreads.h:101
RDestroyNotify notify
Definition rthreads.h:102