Simplify code
[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_thread(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_thread(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_thread(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 * attr) {
68         if (!model) {
69                 snapshot_system_init(10000, 1024, 1024, 40000);
70                 model = new ModelChecker();
71                 model->startChecker();
72         }
73
74         int mutex_type = PTHREAD_MUTEX_DEFAULT;
75         if (attr != NULL)
76                 pthread_mutexattr_gettype(attr, &mutex_type);
77
78         cdsc::snapmutex *m = new cdsc::snapmutex(mutex_type);
79
80         ModelExecution *execution = model->get_execution();
81         execution->getMutexMap()->put(p_mutex, m);
82
83         return 0;
84 }
85
86 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
87         if (!model) {
88                 snapshot_system_init(10000, 1024, 1024, 40000);
89                 model = new ModelChecker();
90                 model->startChecker();
91         }
92
93         ModelExecution *execution = model->get_execution();
94
95         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
96            instead of pthread_mutex_init, or where *p_mutex is not stored
97            in the execution->mutex_map for some reason. */
98         if (!execution->getMutexMap()->contains(p_mutex)) {
99                 pthread_mutex_init(p_mutex, NULL);
100         }
101
102         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
103
104         if (m != NULL) {
105                 m->lock();
106         } else {
107                 return 1;
108         }
109
110         return 0;
111 }
112
113 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
114         if (!model) {
115                 snapshot_system_init(10000, 1024, 1024, 40000);
116                 model = new ModelChecker();
117                 model->startChecker();
118         }
119
120         ModelExecution *execution = model->get_execution();
121         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
122         return m->try_lock();
123 }
124 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
125         ModelExecution *execution = model->get_execution();
126         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
127
128         if (m != NULL) {
129                 m->unlock();
130         } else {
131                 printf("try to unlock an untracked pthread_mutex\n");
132                 return 1;
133         }
134
135         return 0;
136 }
137
138 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
139                                                                                                                  const struct timespec *__restrict abstime) {
140 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
141
142         if (!model) {
143                 snapshot_system_init(10000, 1024, 1024, 40000);
144                 model = new ModelChecker();
145                 model->startChecker();
146         }
147
148         ModelExecution *execution = model->get_execution();
149
150         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
151            instead of pthread_mutex_init, or where *p_mutex is not stored
152            in the execution->mutex_map for some reason. */
153         if (!execution->getMutexMap()->contains(p_mutex)) {
154                 pthread_mutex_init(p_mutex, NULL);
155         }
156
157         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
158
159         if (m != NULL) {
160                 m->lock();
161                 return 0;
162         }
163
164         return 1;
165 }
166
167 pthread_t pthread_self() {
168         if (!model) {
169                 snapshot_system_init(10000, 1024, 1024, 40000);
170                 model = new ModelChecker();
171                 model->startChecker();
172         }
173
174         Thread* th = model->get_current_thread();
175         return (pthread_t)th->get_id();
176 }
177
178 int pthread_key_delete(pthread_key_t) {
179         model_print("key_delete is called\n");
180         return 0;
181 }
182
183 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
184         cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
185
186         ModelExecution *execution = model->get_execution();
187         execution->getCondMap()->put(p_cond, v);
188         return 0;
189 }
190
191 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
192         ModelExecution *execution = model->get_execution();
193         if ( !execution->getCondMap()->contains(p_cond) )
194                 pthread_cond_init(p_cond, NULL);
195         if ( !execution->getMutexMap()->contains(p_mutex) )
196                 pthread_mutex_init(p_mutex, NULL);
197
198         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
199         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
200
201         v->wait(*m);
202         return 0;
203 }
204
205 int pthread_cond_timedwait(pthread_cond_t *p_cond,
206                                                                                                          pthread_mutex_t *p_mutex, const struct timespec *abstime) {
207         ModelExecution *execution = model->get_execution();
208
209         if ( !execution->getCondMap()->contains(p_cond) )
210                 pthread_cond_init(p_cond, NULL);
211         if ( !execution->getMutexMap()->contains(p_mutex) )
212                 pthread_mutex_init(p_mutex, NULL);
213
214         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
215         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
216
217         model->switch_thread(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
218         m->lock();
219
220         // model_print("Timed_wait is called\n");
221         return 0;
222 }
223
224 int pthread_cond_signal(pthread_cond_t *p_cond) {
225         // notify only one blocked thread
226         ModelExecution *execution = model->get_execution();
227         if ( !execution->getCondMap()->contains(p_cond) )
228                 pthread_cond_init(p_cond, NULL);
229
230         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
231
232         v->notify_one();
233         return 0;
234 }
235
236 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
237         // notify all blocked threads
238         ModelExecution *execution = model->get_execution();
239         if ( !execution->getCondMap()->contains(p_cond) )
240                 pthread_cond_init(p_cond, NULL);
241
242         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
243
244         v->notify_all();
245         return 0;
246 }
247
248 int pthread_cond_destroy(pthread_cond_t *p_cond) {
249         ModelExecution *execution = model->get_execution();
250
251         if (execution->getCondMap()->contains(p_cond)) {
252                 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
253                 delete v;
254                 execution->getCondMap()->remove(p_cond);
255         }
256         return 0;
257 }
258
259 /* https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c */
260 int pthread_getattr_np(pthread_t t, pthread_attr_t *attr)
261 {
262         ModelExecution *execution = model->get_execution();
263         Thread *th = execution->get_pthread(t);
264
265         struct pthread_attr *iattr = (struct pthread_attr *) attr;
266
267         /* The sizes are subject to alignment.  */
268         if (th != NULL) {
269 #if _STACK_GROWS_DOWN
270                 ASSERT(false);
271 #else
272                 iattr->stackaddr = (char *) th->get_stack_addr();
273 #endif
274
275         } else {
276                 ASSERT(false);
277         }
278
279         return 0;
280 }
281
282 int pthread_setname_np(pthread_t t, const char *name)
283 {
284         ModelExecution *execution = model->get_execution();
285         Thread *th = execution->get_pthread(t);
286
287         if (th != NULL)
288                 return 0;
289
290         return 1;
291 }