run.sh: provide gdb option
[c11tester.git] / threads.h
1 #ifndef __THREADS_H__
2 #define __THREADS_H__
3
4 #include <ucontext.h>
5 #include "mymemory.h"
6 #include "libthreads.h"
7
8 typedef int thread_id_t;
9
10 #define THREAD_ID_T_NONE        -1
11
12 typedef enum thread_state {
13         THREAD_CREATED,
14         THREAD_RUNNING,
15         THREAD_READY,
16         THREAD_COMPLETED
17 } thread_state;
18
19 class Thread {
20 public:
21         Thread(thrd_t *t, void (*func)(void *), 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         Thread * get_parent() { return parent; }
33   friend void thread_startup();
34   MEMALLOC
35 private:
36         int create_context();
37         Thread *parent;
38
39         void (*start_routine)(void *);
40         void *arg;
41         ucontext_t context;
42         void *stack;
43         thrd_t *user_thread;
44         thread_id_t id;
45         thread_state state;
46 };
47
48 Thread * thread_current();
49
50 static inline thread_id_t thrd_to_id(thrd_t t)
51 {
52         return t;
53 }
54
55 static inline thread_id_t int_to_id(int i)
56 {
57         return i;
58 }
59
60 static inline int id_to_int(thread_id_t id)
61 {
62         return id;
63 }
64
65 #endif /* __THREADS_H__ */