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