Haka
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 
33 
40 
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 
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 
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 
124 
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 */
bool thread_setcanceltype(enum thread_cancel_t type)
Definition: thread.c:114
INLINE void atomic64_destroy(atomic64_t *v)
Definition: thread.h:316
INLINE uint32 atomic_dec(atomic_t *v)
Definition: thread.h:281
Definition: thread.h:96
bool thread_setcancelstate(bool enable)
Definition: thread.c:132
unsigned HAKA_64BIT_TYPE uint64
Definition: types.h:30
INLINE uint32 atomic_inc(atomic_t *v)
Definition: thread.h:274
sem_t semaphore_t
Definition: thread.h:199
unsigned HAKA_32BIT_TYPE uint32
Definition: types.h:29
bool thread_sigmask(int how, sigset_t *set, sigset_t *oldset)
Definition: thread.c:104
void thread_set_packet_capture_cpu_count(int count)
Definition: thread.c:27
pthread_spinlock_t spinlock_t
Definition: thread.h:165
bool thread_equal(thread_t a, thread_t b)
Definition: thread.c:164
void thread_testcancel()
Definition: thread.c:142
void * local_storage_get(local_storage_t *key)
Definition: thread.c:537
INLINE void atomic64_init(atomic64_t *v, uint64 x)
Definition: thread.h:311
pthread_barrier_t barrier_t
Definition: thread.h:213
pthread_mutex_t mutex_t
Definition: thread.h:148
void thread_setid(int id)
Definition: thread.c:54
bool local_storage_init(local_storage_t *key, void(*destructor)(void *))
Definition: thread.c:517
bool local_storage_set(local_storage_t *key, const void *value)
Definition: thread.c:542
thread_t thread_self()
Definition: thread.c:159
int thread_get_cpu_count()
Definition: thread.c:32
void thread_protect(void(*run)(void *), void *runarg, void(*finish)(void *), void *finisharg)
Definition: thread.c:147
thread_cancel_t
Definition: thread.h:94
INLINE uint32 atomic_get(atomic_t *v)
Definition: thread.h:286
pthread_rwlock_t rwlock_t
Definition: thread.h:180
INLINE uint64 atomic64_get(atomic64_t *v)
Definition: thread.h:335
bool thread_kill(thread_t thread, int sig)
Definition: thread.c:169
INLINE void atomic64_set(atomic64_t *v, uint64 x)
Definition: thread.h:306
thread_t thread_current()
Definition: thread.c:89
INLINE uint64 atomic64_inc(atomic64_t *v)
Definition: thread.h:323
volatile uint32 atomic_t
Definition: thread.h:267
volatile uint64 atomic64_t
Definition: thread.h:301
INLINE void atomic_set(atomic_t *v, uint32 x)
Definition: thread.h:291
pthread_key_t local_storage_t
Definition: thread.h:226
thread_t thread_main()
Definition: thread.c:154
pthread_t thread_t
Definition: thread.h:46
bool thread_join(thread_t thread, void **ret)
Definition: thread.c:69
int thread_get_packet_capture_cpu_count()
Definition: thread.c:19
bool thread_signal(thread_t thread, int sig)
Definition: thread.c:94
int thread_getid()
Definition: thread.c:49
bool local_storage_destroy(local_storage_t *key)
Definition: thread.c:527
INLINE uint64 atomic64_dec(atomic64_t *v)
Definition: thread.h:330
Definition: thread.h:95
bool thread_cancel(thread_t thread)
Definition: thread.c:79