threads: add id_to_int() and int_to_id() inline functions
[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();
23         void complete();
24
25         static int swap(ucontext_t *ctxt, Thread *t);
26         static int swap(Thread *t, ucontext_t *ctxt);
27
28         thread_state get_state() { return state; }
29         void set_state(thread_state s) { state = s; }
30         thread_id_t get_id();
31         thrd_t get_thrd_t() { return *user_thread; }
32 private:
33         int create_context();
34
35         void (*start_routine)();
36         void *arg;
37         ucontext_t context;
38         void *stack;
39         thrd_t *user_thread;
40         thread_id_t id;
41         thread_state state;
42 };
43
44 Thread * thread_current();
45
46 static inline thread_id_t thrd_to_id(thrd_t t)
47 {
48         return t;
49 }
50
51 static inline thread_id_t int_to_id(int i)
52 {
53         return i;
54 }
55
56 static inline int id_to_int(thread_id_t id)
57 {
58         return id;
59 }
60
61 #endif /* __THREADS_H__ */