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