d23c4900f6382b62ed39c70c434e852c1e4f22ab
[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         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, th));
59         while(1) ;//make warning goaway
60 }
61
62 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
63         cdsc::snapmutex *m = new cdsc::snapmutex();
64
65         if (!model) {
66                 snapshot_system_init(10000, 1024, 1024, 40000);
67                 model = new ModelChecker();
68                 model->startChecker();
69         }
70
71         ModelExecution *execution = model->get_execution();
72         execution->getMutexMap()->put(p_mutex, m);
73
74         return 0;
75 }
76
77 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
78         if (!model) {
79                 snapshot_system_init(10000, 1024, 1024, 40000);
80                 model = new ModelChecker();
81                 model->startChecker();
82         }
83
84
85         ModelExecution *execution = model->get_execution();
86
87         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
88            instead of pthread_mutex_init, or where *p_mutex is not stored
89            in the execution->mutex_map for some reason. */
90         if (!execution->getMutexMap()->contains(p_mutex)) {
91                 pthread_mutex_init(p_mutex, NULL);
92         }
93
94         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
95
96         if (m != NULL) {
97                 m->lock();
98         } else {
99                 printf("ah\n");
100         }
101
102         return 0;
103 }
104
105 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
106         if (!model) {
107                 snapshot_system_init(10000, 1024, 1024, 40000);
108                 model = new ModelChecker();
109                 model->startChecker();
110         }
111         
112         ModelExecution *execution = model->get_execution();
113         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
114         return m->try_lock();
115 }
116 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
117         ModelExecution *execution = model->get_execution();
118         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
119
120         if (m != NULL) {
121                 m->unlock();
122         } else {
123                 printf("try to unlock an untracked pthread_mutex\n");
124         }
125
126         return 0;
127 }
128
129 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
130                                                                                                                  const struct timespec *__restrict abstime) {
131 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
132
133 /*
134         ModelExecution *execution = model->get_execution();
135         if (!execution->mutex_map.contains(p_mutex)) {
136                 pthread_mutex_init(p_mutex, NULL);
137         }
138         cdsc::snapmutex *m = execution->mutex_map.get(p_mutex);
139
140         if (m != NULL) {
141                 m->lock();
142         } else {
143                 printf("something is wrong with pthread_mutex_timedlock\n");
144         }
145
146         printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
147  */
148         return 0;
149 }
150
151 pthread_t pthread_self() {
152         Thread* th = model->get_current_thread();
153         return (pthread_t)th->get_id();
154 }
155
156 int pthread_key_delete(pthread_key_t) {
157         model_print("key_delete is called\n");
158         return 0;
159 }
160
161 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
162         cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
163
164         ModelExecution *execution = model->get_execution();
165         execution->getCondMap()->put(p_cond, v);
166         return 0;
167 }
168
169 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
170         ModelExecution *execution = model->get_execution();
171         if ( !execution->getCondMap()->contains(p_cond) )
172                 pthread_cond_init(p_cond, NULL);
173
174         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
175         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
176
177         v->wait(*m);
178         return 0;
179 }
180
181 int pthread_cond_timedwait(pthread_cond_t *p_cond,
182                                                                                                          pthread_mutex_t *p_mutex, const struct timespec *abstime) {
183 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
184         ModelExecution *execution = model->get_execution();
185
186         if ( !execution->getCondMap()->contains(p_cond) )
187                 pthread_cond_init(p_cond, NULL);
188         if ( !execution->getMutexMap()->contains(p_mutex) )
189                 pthread_mutex_init(p_mutex, NULL);
190
191         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
192         cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
193
194         model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
195 //      v->wait(*m);
196 //      printf("timed_wait called\n");
197         return 0;
198 }
199
200 int pthread_cond_signal(pthread_cond_t *p_cond) {
201         // notify only one blocked thread
202         ModelExecution *execution = model->get_execution();
203         if ( !execution->getCondMap()->contains(p_cond) )
204                 pthread_cond_init(p_cond, NULL);
205
206         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
207
208         v->notify_one();
209         return 0;
210 }
211
212 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
213         // notify all blocked threads
214         ModelExecution *execution = model->get_execution();
215         if ( !execution->getCondMap()->contains(p_cond) )
216                 pthread_cond_init(p_cond, NULL);
217
218         cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
219
220         v->notify_all();
221         return 0;
222 }