changes
[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_ASSERTED,
31         /** Thread has completed its execution */
32         THREAD_COMPLETED
33 } thread_state;
34
35 class ModelAction;
36
37 /** @brief A Thread is created for each user-space thread */
38 class Thread {
39 public:
40         Thread(thrd_t *t, void (*func)(void *), void *a);
41         ~Thread();
42         void complete();
43
44         static int swap(ucontext_t *ctxt, Thread *t);
45         static int swap(Thread *t, ucontext_t *ctxt);
46
47         thread_state get_state() { return state; }
48         void set_state(thread_state s) { state = s; }
49         thread_id_t get_id();
50         thrd_t get_thrd_t() { return *user_thread; }
51         Thread * get_parent() { return parent; }
52
53         void set_creation(ModelAction *act) { creation = act; }
54         ModelAction * get_creation() { return creation; }
55
56         /**
57          * Set a return value for the last action in this thread (e.g., for an
58          * atomic read).
59          * @param value The value to return
60          */
61         void set_return_value(uint64_t value) { last_action_val = value; }
62
63         /**
64          * Retrieve a return value for the last action in this thread. Used,
65          * for instance, for an atomic read to return the 'read' value. Should
66          * be called from a user context.
67          * @return The value 'returned' by the action
68          */
69         uint64_t get_return_value() { return last_action_val; }
70
71         friend void thread_startup();
72
73         SNAPSHOTALLOC
74 private:
75         int create_context();
76         Thread *parent;
77         ModelAction *creation;
78
79         void (*start_routine)(void *);
80         void *arg;
81         ucontext_t context;
82         void *stack;
83         thrd_t *user_thread;
84         thread_id_t id;
85         thread_state state;
86
87         /**
88          * The value returned by the last action in this thread
89          * @see Thread::set_return_value()
90          * @see Thread::get_return_value()
91          */
92         uint64_t last_action_val;
93 };
94
95 Thread * thread_current();
96
97 static inline thread_id_t thrd_to_id(thrd_t t)
98 {
99         return t;
100 }
101
102 static inline thread_id_t int_to_id(int i)
103 {
104         return i;
105 }
106
107 static inline int id_to_int(thread_id_t id)
108 {
109         return id;
110 }
111
112 #endif /* __THREADS_H__ */