threads: add is_complete() helper function
[model-checker.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         /** @return True if this thread is finished executing */
71         bool is_complete() { return state == THREAD_COMPLETED; }
72
73         friend void thread_startup();
74
75         SNAPSHOTALLOC
76 private:
77         int create_context();
78         Thread *parent;
79         ModelAction *creation;
80
81         void (*start_routine)(void *);
82         void *arg;
83         ucontext_t context;
84         void *stack;
85         thrd_t *user_thread;
86         thread_id_t id;
87         thread_state state;
88
89         /**
90          * The value returned by the last action in this thread
91          * @see Thread::set_return_value()
92          * @see Thread::get_return_value()
93          */
94         uint64_t last_action_val;
95 };
96
97 Thread * thread_current();
98
99 static inline thread_id_t thrd_to_id(thrd_t t)
100 {
101         return t;
102 }
103
104 static inline thread_id_t int_to_id(int i)
105 {
106         return i;
107 }
108
109 static inline int id_to_int(thread_id_t id)
110 {
111         return id;
112 }
113
114 #endif /* __THREADS_H__ */