demote 'system_thread' to just 'system_context'
[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
26         static int swap(ucontext_t *ctxt, Thread *t);
27         static int swap(Thread *t, ucontext_t *ctxt);
28
29         thread_state get_state() { return state; }
30         void set_state(thread_state s) { state = s; }
31         thread_id_t get_id();
32         thrd_t get_thrd_t() { return *user_thread; }
33 private:
34         int create_context();
35
36         void (*start_routine)();
37         void *arg;
38         ucontext_t context;
39         void *stack;
40         thrd_t *user_thread;
41         thread_id_t id;
42         thread_state state;
43 };
44
45 Thread * thread_current();
46
47 static inline thread_id_t thrd_to_id(thrd_t t)
48 {
49         return t;
50 }
51
52 #endif /* __THREADS_H__ */