7 * Copyright (c) 2007, 2008, 2009, 2010, 2011, ETH Zurich.
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
15 #ifndef LIBBARRELFISH_THREADS_H
16 #define LIBBARRELFISH_THREADS_H
19 #include <sys/cdefs.h>
21 #include <barrelfish/caddr.h> // for struct capref.
22 #include <barrelfish/thread_sync.h>
23 #include <barrelfish_kpi/registers_arch.h>
24 #include <barrelfish_kpi/dispatcher_handle.h>
25 #include <errors/errno.h>
26 #include <barrelfish/waitset.h>
30 typedef int (*thread_func_t)(void *);
32 /// Default size of a thread's stack
33 #define THREADS_DEFAULT_STACK_BYTES (64 * 1024)
35 struct thread *thread_create(thread_func_t start_func, void *data);
36 struct thread *thread_create_varstack(thread_func_t start_func, void *arg,
38 void thread_yield(void);
39 void thread_yield_dispatcher(struct capref endpoint);
40 void thread_exit(int status);
41 struct thread *thread_self(void);
42 struct thread *thread_self_disabled(void);
43 errval_t thread_join(struct thread *thread, int *retval);
44 errval_t thread_detach(struct thread *thread);
46 void thread_pause(struct thread *thread);
47 void thread_pause_and_capture_state(struct thread *thread,
48 arch_registers_state_t **ret_regs,
49 arch_registers_fpu_state_t **ret_fpuregs);
50 void thread_resume(struct thread *thread);
52 void thread_mutex_init(struct thread_mutex *mutex);
53 void thread_mutex_lock(struct thread_mutex *mutex);
54 bool thread_mutex_trylock(struct thread_mutex *mutex);
55 void thread_mutex_lock_nested(struct thread_mutex *mutex);
56 void thread_mutex_unlock(struct thread_mutex *mutex);
57 struct thread *thread_mutex_unlock_disabled(dispatcher_handle_t handle,
58 struct thread_mutex *mutex);
60 void thread_cond_init(struct thread_cond *cond);
61 void thread_cond_signal(struct thread_cond *cond);
62 void thread_cond_broadcast(struct thread_cond *cond);
63 void thread_cond_wait(struct thread_cond *cond, struct thread_mutex *mutex);
65 void thread_sem_init(struct thread_sem *sem, unsigned int value);
66 void thread_sem_wait(struct thread_sem *sem);
67 bool thread_sem_trywait(struct thread_sem *sem);
68 void thread_sem_post(struct thread_sem *sem);
70 void thread_set_tls(void *);
71 void *thread_get_tls(void);
73 void thread_set_tls_key(int, void *);
74 void *thread_get_tls_key(int);
76 uintptr_t thread_id(void);
77 uintptr_t thread_get_id(struct thread *t);
78 void thread_set_id(uintptr_t id);
80 uint32_t thread_set_token(struct waitset_chanstate *channel);
81 void thread_clear_token(struct waitset_chanstate *channel);
82 uint32_t thread_current_token(void);
84 void thread_set_outgoing_token(uint32_t token);
85 void thread_get_outgoing_token(uint32_t *token);
87 struct flounder_rpc_context;
89 void thread_set_rpc_in_progress(bool v);
90 bool thread_get_rpc_in_progress(void);
91 void thread_set_async_error(errval_t e);
92 errval_t thread_get_async_error(void);
94 extern __thread thread_once_t thread_once_local_epoch;
95 extern void thread_once_internal(thread_once_t *control, void (*func)(void));
98 * \brief Run a routine exactly once; use this for thread-safe initialization.
100 * \param control Control word - should be initialized with THREAD_ONCE_INIT.
101 * \param func Callback to be invoked.
103 static inline void thread_once(thread_once_t *control, void (*func)(void)) {
104 assert(control != NULL);
105 assert(func != NULL);
106 thread_once_t x = *control; // unprotected access
107 if (x > thread_once_local_epoch) {
108 thread_once_internal(control, func);
114 #endif // LIBBARRELFISH_THREADS_H