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