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