finish promise support
[c11tester.git] / threads.h
1 /** @file threads.h
2  *  @brief Model Checker Thread class.
3  */
4
5 #ifndef __THREADS_H__
6 #define __THREADS_H__
7
8 #include <ucontext.h>
9 #include <stdint.h>
10
11 #include "mymemory.h"
12 #include "libthreads.h"
13
14 typedef int thread_id_t;
15
16 #define THREAD_ID_T_NONE        -1
17
18 /** @brief Represents the state of a user Thread */
19 typedef enum thread_state {
20         /** Thread was just created and hasn't run yet */
21         THREAD_CREATED,
22         /** Thread is running */
23         THREAD_RUNNING,
24         /**
25          * Thread has yielded to the model-checker but is ready to run. Used
26          * during an action that caused a context switch to the model-checking
27          * context.
28          */
29         THREAD_READY,
30         /** Thread has completed its execution */
31         THREAD_COMPLETED
32 } thread_state;
33
34 class ModelAction;
35
36 /** @brief A Thread is created for each user-space thread */
37 class Thread {
38 public:
39         Thread(thrd_t *t, void (*func)(void *), void *a);
40         ~Thread();
41         void complete();
42
43         static int swap(ucontext_t *ctxt, Thread *t);
44         static int swap(Thread *t, ucontext_t *ctxt);
45
46         thread_state get_state() { return state; }
47         void set_state(thread_state s) { state = s; }
48         thread_id_t get_id();
49         thrd_t get_thrd_t() { return *user_thread; }
50         Thread * get_parent() { return parent; }
51
52         void set_creation(ModelAction *act) { creation = act; }
53         ModelAction * get_creation() { return creation; }
54
55         /**
56          * Set a return value for the last action in this thread (e.g., for an
57          * atomic read).
58          * @param value The value to return
59          */
60         void set_return_value(uint64_t value) { last_action_val = value; }
61
62         /**
63          * Retrieve a return value for the last action in this thread. Used,
64          * for instance, for an atomic read to return the 'read' value. Should
65          * be called from a user context.
66          * @return The value 'returned' by the action
67          */
68         uint64_t get_return_value() { return last_action_val; }
69
70         friend void thread_startup();
71
72         SNAPSHOTALLOC
73 private:
74         int create_context();
75         Thread *parent;
76         ModelAction *creation;
77
78         void (*start_routine)(void *);
79         void *arg;
80         ucontext_t context;
81         void *stack;
82         thrd_t *user_thread;
83         thread_id_t id;
84         thread_state state;
85
86         /**
87          * The value returned by the last action in this thread
88          * @see Thread::set_return_value()
89          * @see Thread::get_return_value()
90          */
91         uint64_t last_action_val;
92 };
93
94 Thread * thread_current();
95
96 static inline thread_id_t thrd_to_id(thrd_t t)
97 {
98         return t;
99 }
100
101 static inline thread_id_t int_to_id(int i)
102 {
103         return i;
104 }
105
106 static inline int id_to_int(thread_id_t id)
107 {
108         return id;
109 }
110
111 #endif /* __THREADS_H__ */