librace: format DEBUG() prints properly
[c11tester.git] / threads.h
1 #ifndef __THREADS_H__
2 #define __THREADS_H__
3
4 #include <ucontext.h>
5
6 #include "libthreads.h"
7
8 #define THREAD_ID_T_NONE        -1
9
10 typedef enum thread_state {
11         THREAD_CREATED,
12         THREAD_RUNNING,
13         THREAD_READY,
14         THREAD_COMPLETED
15 } thread_state;
16
17 class Thread {
18 public:
19         void * operator new(size_t size);
20         void operator delete(void *ptr);
21         Thread(thrd_t *t, void (*func)(), void *a);
22         Thread(thrd_t *t);
23         ~Thread();
24         void complete();
25         int swap(Thread *t);
26
27         thread_state get_state() { return state; }
28         void set_state(thread_state s) { state = s; }
29         thread_id_t get_id();
30         thrd_t get_thrd_t() { return *user_thread; }
31 private:
32         int create_context();
33
34         void (*start_routine)();
35         void *arg;
36         ucontext_t context;
37         void *stack;
38         thrd_t *user_thread;
39         thread_id_t id;
40         thread_state state;
41 };
42
43 Thread * thread_current();
44
45 static inline thread_id_t thrd_to_id(thrd_t t)
46 {
47         return t;
48 }
49
50 #endif /* __THREADS_H__ */