thread.h (1265B)
1 /* See LICENSE file for copyright and license details. */ 2 #ifndef THREADLIB_H 3 #define THREADLIB_H 4 5 #include <sched.h> 6 #include <stddef.h> 7 8 struct thread_config { 9 size_t stack_size; 10 size_t guard_size; 11 }; 12 13 typedef int spinlock_t; 14 15 struct thread_ctx; 16 17 struct thread_ctx *thread_init(const struct thread_config *tc, int *rval); 18 pid_t thread_register(struct thread_ctx *tctx, int (*fn)(void *), void *arg); 19 int thread_wait(struct thread_ctx *tctx, pid_t pid); 20 int thread_wait_blocking(struct thread_ctx *tctx, pid_t pid); 21 int thread_wait_all_blocking(struct thread_ctx *tctx); 22 void thread_exit(struct thread_ctx *tctx); 23 pid_t thread_id(void); 24 int thread_get_sched_param(struct thread_ctx *tctx, pid_t pid, int *policy, 25 struct sched_param *param); 26 int thread_set_sched_param(struct thread_ctx *tctx, pid_t pid, int policy, 27 const struct sched_param *param); 28 29 static inline void 30 spinlock_init(spinlock_t *spinlock) 31 { 32 __sync_lock_release(spinlock); 33 } 34 35 static inline void 36 acquire(spinlock_t *spinlock) 37 { 38 while (__sync_lock_test_and_set(spinlock, 1) == 1) 39 ; 40 } 41 42 static inline int 43 try_acquire(spinlock_t *spinlock) 44 { 45 return __sync_lock_test_and_set(spinlock, 1); 46 } 47 48 static inline void 49 release(spinlock_t *spinlock) 50 { 51 __sync_lock_release(spinlock); 52 } 53 54 #endif