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