edits
[c11tester.git] / pthread.cc
1 #include "common.h"
2 #include "threads-model.h"
3 #include "action.h"
4 #include "mypthread.h"
5
6 #include "snapshot-interface.h"
7 #include "datarace.h"
8
9 #include "mutex.h"
10 #include <condition_variable>
11
12 /* global "model" object */
13 #include "model.h"
14 #include "execution.h"
15 #include <errno.h>
16
17 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
18                                                                          pthread_start_t start_routine, void * arg) {
19         if (!model) {
20                 snapshot_system_init(10000, 1024, 1024, 40000);
21                 model = new ModelChecker();
22                 model->startChecker();
23         }
24
25         struct pthread_params params = { start_routine, arg };
26
27         /* seq_cst is just a 'don't care' parameter */
28         model->switch_to_master(new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params));
29
30         return 0;
31 }
32
33 int pthread_join(pthread_t t, void **value_ptr) {
34         ModelExecution *execution = model->get_execution();
35         Thread *th = execution->get_pthread(t);
36
37         model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
38
39         if ( value_ptr ) {
40                 // store return value
41                 void *rtval = th->get_pthread_return();
42                 *value_ptr = rtval;
43         }
44         return 0;
45 }
46
47 int pthread_detach(pthread_t t) {
48         //Doesn't do anything
49         //Return success
50         return 0;
51 }
52
53 /* Take care of both pthread_yield and c++ thread yield */
54 int sched_yield() {
55         model->switch_thread(new ModelAction(THREAD_YIELD, std::memory_order_seq_cst, thread_current(), VALUE_NONE));
56         return 0;
57 }
58
59 void pthread_exit(void *value_ptr) {
60         Thread * th = thread_current();
61         th->set_pthread_return(value_ptr);
62         model->switch_to_master(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th));
63         //Need to exit so we don't return to the program
64         real_pthread_exit(NULL);
65 }
66
67 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
68         if (!model) {
69                 snapshot_system_init(10000, 1024, 1024, 40000);
70                 model = new ModelChecker();
71                 model->startChecker();
72         }
73         cdsc::snapmutex *m = new cdsc::snapmutex();
74
75         ModelExecution *execution = model->get_execution();
76         execution->getMutexMap()->put(p_mutex, m);
77
78         return 0;
79 }
80
81 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
82         if (!model) {
83                 snapshot_system_init(10000, 1024, 1024, 40000);
84                 model = new ModelChecker();
85                 model->startChecker();
86         }
87
88         ModelExecution *execution = model->get_execution();
89
90         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
91            instead of pthread_mutex_init, or where *p_mutex is not stored
92            in the execution->mutex_map for some reason. */
93         if (!execution->getMutexMap()->contains(p_mutex)) {
94                 pthread_mutex_init(p_mutex, NULL);
95         }
96
97         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
98
99         if (m != NULL) {
100                 m->lock();
101         } else {
102                 return 1;
103         }
104
105         return 0;
106 }
107
108 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
109         if (!model) {
110                 snapshot_system_init(10000, 1024, 1024, 40000);
111                 model = new ModelChecker();
112                 model->startChecker();
113         }
114
115         ModelExecution *execution = model->get_execution();
116         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
117         return m->try_lock();
118 }
119 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
120         ModelExecution *execution = model->get_execution();
121         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
122
123         if (m != NULL) {
124                 m->unlock();
125         } else {
126                 printf("try to unlock an untracked pthread_mutex\n");
127                 return 1;
128         }
129
130         return 0;
131 }
132
133 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
134                                                                                                                  const struct timespec *__restrict abstime) {
135 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
136
137         if (!model) {
138                 snapshot_system_init(10000, 1024, 1024, 40000);
139                 model = new ModelChecker();
140                 model->startChecker();
141         }
142
143         ModelExecution *execution = model->get_execution();
144
145         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
146            instead of pthread_mutex_init, or where *p_mutex is not stored
147            in the execution->mutex_map for some reason. */
148         if (!execution->getMutexMap()->contains(p_mutex)) {
149                 pthread_mutex_init(p_mutex, NULL);
150         }
151
152         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
153
154         if (m != NULL) {
155                 m->lock();
156                 return 0;
157         }
158
159         return 1;
160 }
161
162 pthread_t pthread_self() {
163         Thread* th = model->get_current_thread();
164         return (pthread_t)th->get_id();
165 }
166
167 int pthread_key_delete(pthread_key_t) {
168         model_print("key_delete is called\n");
169         return 0;
170 }
171
172 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
173         cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
174
175         ModelExecution *execution = model->get_execution();
176         execution->getCondMap()->put(p_cond, v);
177         return 0;
178 }
179
180 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
181         ModelExecution *execution = model->get_execution();
182         if ( !execution->getCondMap()->contains(p_cond) )
183                 pthread_cond_init(p_cond, NULL);
184         if ( !execution->getMutexMap()->contains(p_mutex) )
185                 pthread_mutex_init(p_mutex, NULL);
186
187         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
188         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
189
190         v->wait(*m);
191         return 0;
192 }
193
194 int pthread_cond_timedwait(pthread_cond_t *p_cond,
195                                                                                                          pthread_mutex_t *p_mutex, const struct timespec *abstime) {
196         ModelExecution *execution = model->get_execution();
197
198         if ( !execution->getCondMap()->contains(p_cond) )
199                 pthread_cond_init(p_cond, NULL);
200         if ( !execution->getMutexMap()->contains(p_mutex) )
201                 pthread_mutex_init(p_mutex, NULL);
202
203         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
204         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
205
206         model->switch_thread(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
207         m->lock();
208
209         // model_print("Timed_wait is called\n");
210         return 0;
211 }
212
213 int pthread_cond_signal(pthread_cond_t *p_cond) {
214         // notify only one blocked thread
215         ModelExecution *execution = model->get_execution();
216         if ( !execution->getCondMap()->contains(p_cond) )
217                 pthread_cond_init(p_cond, NULL);
218
219         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
220
221         v->notify_one();
222         return 0;
223 }
224
225 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
226         // notify all blocked threads
227         ModelExecution *execution = model->get_execution();
228         if ( !execution->getCondMap()->contains(p_cond) )
229                 pthread_cond_init(p_cond, NULL);
230
231         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
232
233         v->notify_all();
234         return 0;
235 }
236
237 int pthread_cond_destroy(pthread_cond_t *p_cond) {
238         ModelExecution *execution = model->get_execution();
239
240         if (execution->getCondMap()->contains(p_cond)) {
241                 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
242                 delete v;
243                 execution->getCondMap()->remove(p_cond);
244         }
245         return 0;
246 }