Haka: include/haka/thread.h Source File

Haka

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

thread.h
Go to the documentation of this file.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
10 #ifndef HAKA_THREAD_H
11 #define HAKA_THREAD_H
12
13 #include <stddef.h>
14 #include <errno.h>
15 #include <pthread.h>
16 #include <semaphore.h>
17
18 #include <haka/types.h>
19 #include <haka/compiler.h>
20
21
32 int thread_get_packet_capture_cpu_count();
33
39 void thread_set_packet_capture_cpu_count(int count);
40
44 int thread_get_cpu_count();
45
46 typedef pthread_t thread_t;
48 #define THREAD_CANCELED PTHREAD_CANCELED
53 bool thread_create(thread_t *thread, void *(*main)(void*), void *param);
54
58 thread_t thread_current();
59
63 bool thread_join(thread_t thread, void **ret);
64
68 bool thread_cancel(thread_t thread);
69
73 int thread_getid();
74
79 void thread_setid(int id);
80
84 bool thread_sigmask(int how, sigset_t *set, sigset_t *oldset);
85
89 bool thread_signal(thread_t thread, int sig);
90
94 enum thread_cancel_t {
95 THREAD_CANCEL_DEFERRED,
96 THREAD_CANCEL_ASYNCHRONOUS
97 };
98
102 bool thread_setcancelstate(bool enable);
103
107 bool thread_setcanceltype(enum thread_cancel_t type);
108
112 void thread_testcancel();
113
118 void thread_protect(void (*run)(void *), void *runarg, void (*finish)(void *), void *finisharg);
119
123 thread_t thread_main();
124
128 thread_t thread_self();
129
133 bool thread_equal(thread_t a, thread_t b);
134
138 bool thread_kill(thread_t thread, int sig);
139
140
148 typedef pthread_mutex_t mutex_t;
150 #define MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
152 bool mutex_init(mutex_t *mutex, bool recursive);
153 bool mutex_destroy(mutex_t *mutex);
154 bool mutex_lock(mutex_t *mutex);
155 bool mutex_trylock(mutex_t *mutex);
156 bool mutex_unlock(mutex_t *mutex);
157
165 typedef pthread_spinlock_t spinlock_t;
167 bool spinlock_init(spinlock_t *lock);
168 bool spinlock_destroy(spinlock_t *lock);
169 bool spinlock_lock(spinlock_t *lock);
170 bool spinlock_trylock(spinlock_t *lock);
171 bool spinlock_unlock(spinlock_t *lock);
172
180 typedef pthread_rwlock_t rwlock_t;
182 #define RWLOCK_INIT PTHREAD_RWLOCK_INITIALIZER
184 bool rwlock_init(rwlock_t *rwlock);
185 bool rwlock_destroy(rwlock_t *rwlock);
186 bool rwlock_readlock(rwlock_t *rwlock);
187 bool rwlock_tryreadlock(rwlock_t *rwlock);
188 bool rwlock_writelock(rwlock_t *rwlock);
189 bool rwlock_trywritelock(rwlock_t *rwlock);
190 bool rwlock_unlock(rwlock_t *rwlock);
191
199 typedef sem_t semaphore_t;
201 bool semaphore_init(semaphore_t *semaphore, uint32 initial);
202 bool semaphore_destroy(semaphore_t *semaphore);
203 bool semaphore_wait(semaphore_t *semaphore);
204 bool semaphore_post(semaphore_t *semaphore);
205
213 typedef pthread_barrier_t barrier_t;
215 bool barrier_init(barrier_t *barrier, uint32 count);
216 bool barrier_destroy(barrier_t *barrier);
217 bool barrier_wait(barrier_t *barrier);
218
226 typedef pthread_key_t local_storage_t;
234 bool local_storage_init(local_storage_t *key, void (*destructor)(void *));
235
241 bool local_storage_destroy(local_storage_t *key);
242
248 void *local_storage_get(local_storage_t *key);
249
255 bool local_storage_set(local_storage_t *key, const void *value);
256
267 typedef volatile uint32 atomic_t;
268
274 INLINE uint32 atomic_inc(atomic_t *v) { return __sync_add_and_fetch(v, 1); }
275
281 INLINE uint32 atomic_dec(atomic_t *v) { return __sync_sub_and_fetch(v, 1); }
282
286 INLINE uint32 atomic_get(atomic_t *v) { return *v; }
287
291 INLINE void atomic_set(atomic_t *v, uint32 x) { *v = x; }
292
293
294 /* Atomic counter (64 bits) */
295
296 #if defined(__x86_64__) || defined(__doxygen__)
297
301 typedef volatile uint64 atomic64_t;
302
306 INLINE void atomic64_set(atomic64_t *v, uint64 x) { *v = x; }
307
311 INLINE void atomic64_init(atomic64_t *v, uint64 x) { atomic64_set(v, x); }
312
316 INLINE void atomic64_destroy(atomic64_t *v) { }
317
323 INLINE uint64 atomic64_inc(atomic64_t *v) { return __sync_add_and_fetch(v, 1); }
324
330 INLINE uint64 atomic64_dec(atomic64_t *v) { return __sync_sub_and_fetch(v, 1); }
331
335 INLINE uint64 atomic64_get(atomic64_t *v) { return *v; }
336
337 #else
338
339 typedef struct {
340 uint64 value;
341 spinlock_t spinlock;
342 } atomic64_t;
343
344 void atomic64_init(atomic64_t *v, uint64 x);
345 void atomic64_destroy(atomic64_t *v);
346 uint64 atomic64_inc(atomic64_t *v);
347 uint64 atomic64_dec(atomic64_t *v);
348 INLINE uint64 atomic64_get(atomic64_t *v) { return v->value; }
349 void atomic64_set(atomic64_t *v, uint64 x);
350
351 #endif
352
355 #endif /* HAKA_THREAD_H */
types.h
thread_setcanceltype
bool thread_setcanceltype(enum thread_cancel_t type)
Definition: thread.c:114
atomic64_destroy
INLINE void atomic64_destroy(atomic64_t *v)
Definition: thread.h:316
atomic_dec
INLINE uint32 atomic_dec(atomic_t *v)
Definition: thread.h:281
THREAD_CANCEL_ASYNCHRONOUS
Definition: thread.h:96
thread_setcancelstate
bool thread_setcancelstate(bool enable)
Definition: thread.c:132
uint64
unsigned HAKA_64BIT_TYPE uint64
Definition: types.h:30
atomic_inc
INLINE uint32 atomic_inc(atomic_t *v)
Definition: thread.h:274
semaphore_t
sem_t semaphore_t
Definition: thread.h:199
uint32
unsigned HAKA_32BIT_TYPE uint32
Definition: types.h:29
thread_sigmask
bool thread_sigmask(int how, sigset_t *set, sigset_t *oldset)
Definition: thread.c:104
thread_set_packet_capture_cpu_count
void thread_set_packet_capture_cpu_count(int count)
Definition: thread.c:27
spinlock_t
pthread_spinlock_t spinlock_t
Definition: thread.h:165
thread_equal
bool thread_equal(thread_t a, thread_t b)
Definition: thread.c:164
thread_testcancel
void thread_testcancel()
Definition: thread.c:142
local_storage_get
void * local_storage_get(local_storage_t *key)
Definition: thread.c:537
atomic64_init
INLINE void atomic64_init(atomic64_t *v, uint64 x)
Definition: thread.h:311
barrier_t
pthread_barrier_t barrier_t
Definition: thread.h:213
mutex_t
pthread_mutex_t mutex_t
Definition: thread.h:148
thread_setid
void thread_setid(int id)
Definition: thread.c:54
local_storage_init
bool local_storage_init(local_storage_t *key, void(*destructor)(void *))
Definition: thread.c:517
local_storage_set
bool local_storage_set(local_storage_t *key, const void *value)
Definition: thread.c:542
thread_self
thread_t thread_self()
Definition: thread.c:159
thread_get_cpu_count
int thread_get_cpu_count()
Definition: thread.c:32
thread_protect
void thread_protect(void(*run)(void *), void *runarg, void(*finish)(void *), void *finisharg)
Definition: thread.c:147
thread_cancel_t
thread_cancel_t
Definition: thread.h:94
atomic_get
INLINE uint32 atomic_get(atomic_t *v)
Definition: thread.h:286
rwlock_t
pthread_rwlock_t rwlock_t
Definition: thread.h:180
atomic64_get
INLINE uint64 atomic64_get(atomic64_t *v)
Definition: thread.h:335
thread_kill
bool thread_kill(thread_t thread, int sig)
Definition: thread.c:169
atomic64_set
INLINE void atomic64_set(atomic64_t *v, uint64 x)
Definition: thread.h:306
thread_current
thread_t thread_current()
Definition: thread.c:89
atomic64_inc
INLINE uint64 atomic64_inc(atomic64_t *v)
Definition: thread.h:323
atomic_t
volatile uint32 atomic_t
Definition: thread.h:267
atomic64_t
volatile uint64 atomic64_t
Definition: thread.h:301
atomic_set
INLINE void atomic_set(atomic_t *v, uint32 x)
Definition: thread.h:291
local_storage_t
pthread_key_t local_storage_t
Definition: thread.h:226
thread_main
thread_t thread_main()
Definition: thread.c:154
thread_t
pthread_t thread_t
Definition: thread.h:46
thread_join
bool thread_join(thread_t thread, void **ret)
Definition: thread.c:69
thread_get_packet_capture_cpu_count
int thread_get_packet_capture_cpu_count()
Definition: thread.c:19
thread_signal
bool thread_signal(thread_t thread, int sig)
Definition: thread.c:94
thread_getid
int thread_getid()
Definition: thread.c:49
local_storage_destroy
bool local_storage_destroy(local_storage_t *key)
Definition: thread.c:527
atomic64_dec
INLINE uint64 atomic64_dec(atomic64_t *v)
Definition: thread.h:330
THREAD_CANCEL_DEFERRED
Definition: thread.h:95
thread_cancel
bool thread_cancel(thread_t thread)
Definition: thread.c:79