initialize memory location first
[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 #include <assert.h>
12
13 /* global "model" object */
14 #include "model.h"
15 #include "execution.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 //      Thread *th = model->get_pthread(t);
37         ModelExecution *execution = model->get_execution();
38         Thread *th = execution->get_pthread(t);
39
40         model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
41
42         if ( value_ptr ) {
43                 // store return value
44                 void *rtval = th->get_pthread_return();
45                 *value_ptr = rtval;
46         }
47         return 0;
48 }
49
50 int pthread_detach(pthread_t t) {
51         //Doesn't do anything
52         //Return success
53         return 0;
54 }
55
56 void pthread_exit(void *value_ptr) {
57         Thread * th = thread_current();
58         th->set_pthread_return(value_ptr);
59         model->switch_to_master(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th));
60         //Need to exit so we don't return to the program
61         real_pthread_exit(NULL);
62 }
63
64 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
65         cdsc::snapmutex *m = new cdsc::snapmutex();
66
67         if (!model) {
68                 snapshot_system_init(10000, 1024, 1024, 40000);
69                 model = new ModelChecker();
70                 model->startChecker();
71         }
72
73         ModelExecution *execution = model->get_execution();
74         execution->getMutexMap()->put(p_mutex, m);
75
76         return 0;
77 }
78
79 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
80         if (!model) {
81                 snapshot_system_init(10000, 1024, 1024, 40000);
82                 model = new ModelChecker();
83                 model->startChecker();
84         }
85
86
87         ModelExecution *execution = model->get_execution();
88
89         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
90            instead of pthread_mutex_init, or where *p_mutex is not stored
91            in the execution->mutex_map for some reason. */
92         if (!execution->getMutexMap()->contains(p_mutex)) {
93                 pthread_mutex_init(p_mutex, NULL);
94         }
95
96         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
97
98         if (m != NULL) {
99                 m->lock();
100         } else {
101                 printf("ah\n");
102         }
103
104         return 0;
105 }
106
107 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
108         if (!model) {
109                 snapshot_system_init(10000, 1024, 1024, 40000);
110                 model = new ModelChecker();
111                 model->startChecker();
112         }
113
114         ModelExecution *execution = model->get_execution();
115         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
116         return m->try_lock();
117 }
118 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
119         ModelExecution *execution = model->get_execution();
120         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
121
122         if (m != NULL) {
123                 m->unlock();
124         } else {
125                 printf("try to unlock an untracked pthread_mutex\n");
126         }
127
128         return 0;
129 }
130
131 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
132                                                                                                                  const struct timespec *__restrict abstime) {
133 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
134
135 /*
136         ModelExecution *execution = model->get_execution();
137         if (!execution->mutex_map.contains(p_mutex)) {
138                 pthread_mutex_init(p_mutex, NULL);
139         }
140         cdsc::snapmutex *m = execution->mutex_map.get(p_mutex);
141
142         if (m != NULL) {
143                 m->lock();
144         } else {
145                 printf("something is wrong with pthread_mutex_timedlock\n");
146         }
147
148         printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
149  */
150         return 0;
151 }
152
153 pthread_t pthread_self() {
154         Thread* th = model->get_current_thread();
155         return (pthread_t)th->get_id();
156 }
157
158 int pthread_key_delete(pthread_key_t) {
159         model_print("key_delete is called\n");
160         return 0;
161 }
162
163 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
164         cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
165
166         ModelExecution *execution = model->get_execution();
167         execution->getCondMap()->put(p_cond, v);
168         return 0;
169 }
170
171 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
172         ModelExecution *execution = model->get_execution();
173         if ( !execution->getCondMap()->contains(p_cond) )
174                 pthread_cond_init(p_cond, NULL);
175
176         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
177         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
178
179         v->wait(*m);
180         return 0;
181 }
182
183 int pthread_cond_timedwait(pthread_cond_t *p_cond,
184                                                                                                          pthread_mutex_t *p_mutex, const struct timespec *abstime) {
185 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
186         ModelExecution *execution = model->get_execution();
187
188         if ( !execution->getCondMap()->contains(p_cond) )
189                 pthread_cond_init(p_cond, NULL);
190         if ( !execution->getMutexMap()->contains(p_mutex) )
191                 pthread_mutex_init(p_mutex, NULL);
192
193         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
194 //      cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
195
196         model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
197 //      v->wait(*m);
198 //      printf("timed_wait called\n");
199         return 0;
200 }
201
202 int pthread_cond_signal(pthread_cond_t *p_cond) {
203         // notify only one blocked thread
204         ModelExecution *execution = model->get_execution();
205         if ( !execution->getCondMap()->contains(p_cond) )
206                 pthread_cond_init(p_cond, NULL);
207
208         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
209
210         v->notify_one();
211         return 0;
212 }
213
214 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
215         // notify all blocked threads
216         ModelExecution *execution = model->get_execution();
217         if ( !execution->getCondMap()->contains(p_cond) )
218                 pthread_cond_init(p_cond, NULL);
219
220         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
221
222         v->notify_all();
223         return 0;
224 }
225
226 int pthread_cond_destroy(pthread_cond_t *p_cond) {
227         ModelExecution *execution = model->get_execution();
228
229         if (execution->getCondMap()->contains(p_cond)) {
230                 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
231                 delete v;
232                 execution->getCondMap()->remove(p_cond);
233         }
234         return 0;
235 }