Revert "delete plugins"
[c11tester.git] / pthread.cc
1 #include "common.h"
2 #include "threads-model.h"
3 #include "action.h"
4 #include "pthread.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 static void param_defaults(struct model_params *params)
18 {
19         params->enabledcount = 1;
20         params->bound = 0;
21         params->verbose = !!DBG_ENABLED();
22         params->uninitvalue = 0;
23         params->maxexecutions = 0;
24 }
25
26 static void model_main()
27 {
28         struct model_params params;
29
30         param_defaults(&params);
31
32         //parse_options(&params, main_argc, main_argv);
33
34         //Initialize race detector
35         initRaceDetector();
36
37         snapshot_stack_init();
38
39         model = new ModelChecker(params);       // L: Model thread is created
40 //      install_trace_analyses(model->get_execution());         L: disable plugin
41
42         snapshot_record(0);
43         model->run();
44         delete model;
45
46         DEBUG("Exiting\n");
47 }
48
49 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
50           pthread_start_t start_routine, void * arg) {
51         struct pthread_params params = { start_routine, arg };
52
53         ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params);
54
55         /* seq_cst is just a 'don't care' parameter */
56         model->switch_to_master(act);
57
58         return 0;
59 }
60
61 int pthread_join(pthread_t t, void **value_ptr) {
62 //      Thread *th = model->get_pthread(t);
63         ModelExecution *execution = model->get_execution();
64         Thread *th = execution->get_pthread(t);
65
66         model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
67
68         if ( value_ptr ) {
69                 // store return value
70                 void *rtval = th->get_pthread_return();
71                 *value_ptr = rtval;
72         } 
73         return 0;
74 }
75
76 void pthread_exit(void *value_ptr) {
77         Thread * th = thread_current();
78         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, th));
79 }
80
81 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
82         if (!model) {
83                 snapshot_system_init(10000, 1024, 1024, 40000, &model_main);
84         }
85
86         cdsc::mutex *m = new cdsc::mutex();
87
88         ModelExecution *execution = model->get_execution();
89         execution->getMutexMap()->put(p_mutex, m);
90         return 0;
91 }
92
93 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
94         ModelExecution *execution = model->get_execution();
95
96         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used 
97            instead of pthread_mutex_init, or where *p_mutex is not stored
98            in the execution->mutex_map for some reason. */
99         if (!execution->getMutexMap()->contains(p_mutex)) {     
100                 pthread_mutex_init(p_mutex, NULL);
101         }
102
103         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
104
105         if (m != NULL) {
106                 m->lock();
107         } else {
108                 printf("ah\n");
109         }
110
111         return 0;
112 }
113
114 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
115         ModelExecution *execution = model->get_execution();
116         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
117         return m->try_lock();
118 }
119 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {    
120         ModelExecution *execution = model->get_execution();
121         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
122
123         if (m != NULL) {
124                 m->unlock();
125         } else {
126                 printf("try to unlock an untracked pthread_mutex\n");
127         }
128
129         return 0;
130 }
131
132 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
133                                 const struct timespec *__restrict abstime) {
134 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
135
136 /*
137         ModelExecution *execution = model->get_execution();
138         if (!execution->mutex_map.contains(p_mutex)) {  
139                 pthread_mutex_init(p_mutex, NULL);
140         }
141         cdsc::mutex *m = execution->mutex_map.get(p_mutex);
142
143         if (m != NULL) {
144                 m->lock();
145         } else {
146                 printf("something is wrong with pthread_mutex_timedlock\n");
147         }
148
149         printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
150 */
151         return 0;
152 }
153
154 pthread_t pthread_self() {
155         Thread* th = model->get_current_thread();
156         return th->get_id();
157 }
158
159 int pthread_key_delete(pthread_key_t) {
160         model_print("key_delete is called\n");
161         return 0;
162 }
163
164 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
165         cdsc::condition_variable *v = new cdsc::condition_variable();
166
167         ModelExecution *execution = model->get_execution();
168         execution->getCondMap()->put(p_cond, v);
169         return 0;
170 }
171
172 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
173         ModelExecution *execution = model->get_execution();
174         if ( !execution->getCondMap()->contains(p_cond) )
175                 pthread_cond_init(p_cond, NULL);
176
177         cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
178         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
179
180         v->wait(*m);
181         return 0;
182 }
183
184 int pthread_cond_timedwait(pthread_cond_t *p_cond, 
185     pthread_mutex_t *p_mutex, const struct timespec *abstime) {
186 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
187         ModelExecution *execution = model->get_execution();
188
189         if ( !execution->getCondMap()->contains(p_cond) )
190                 pthread_cond_init(p_cond, NULL);
191         if ( !execution->getMutexMap()->contains(p_mutex) )
192                 pthread_mutex_init(p_mutex, NULL);
193
194         cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
195         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
196
197         model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
198 //      v->wait(*m);
199 //      printf("timed_wait called\n");
200         return 0;
201 }
202
203 int pthread_cond_signal(pthread_cond_t *p_cond) {
204         // notify only one blocked thread
205         ModelExecution *execution = model->get_execution();
206         if ( !execution->getCondMap()->contains(p_cond) )
207                 pthread_cond_init(p_cond, NULL);
208
209         cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
210
211         v->notify_one();
212         return 0;
213 }