Adding support to return global segments of the process using the /proc/maps
[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 *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   MEMALLOC
34 private:
35         int create_context();
36         Thread *parent;
37
38         void (*start_routine)();
39         void *arg;
40         ucontext_t context;
41         void *stack;
42         thrd_t *user_thread;
43         thread_id_t id;
44         thread_state state;
45 };
46
47 Thread * thread_current();
48
49 static inline thread_id_t thrd_to_id(thrd_t t)
50 {
51         return t;
52 }
53
54 static inline thread_id_t int_to_id(int i)
55 {
56         return i;
57 }
58
59 static inline int id_to_int(thread_id_t id)
60 {
61         return id;
62 }
63
64 #endif /* __THREADS_H__ */