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