3f0a1e4f69e278dcb67449a600922eb6d11df76c
[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 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 }
48
49 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
50         if (!model) {
51           model = new ModelChecker();
52         }
53
54         cdsc::mutex *m = new cdsc::mutex();
55
56         ModelExecution *execution = model->get_execution();
57         execution->getMutexMap()->put(p_mutex, m);
58         return 0;
59 }
60
61 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
62         ModelExecution *execution = model->get_execution();
63
64         /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used 
65            instead of pthread_mutex_init, or where *p_mutex is not stored
66            in the execution->mutex_map for some reason. */
67         if (!execution->getMutexMap()->contains(p_mutex)) {     
68                 pthread_mutex_init(p_mutex, NULL);
69         }
70
71         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
72
73         if (m != NULL) {
74                 m->lock();
75         } else {
76                 printf("ah\n");
77         }
78
79         return 0;
80 }
81
82 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
83         ModelExecution *execution = model->get_execution();
84         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
85         return m->try_lock();
86 }
87 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {    
88         ModelExecution *execution = model->get_execution();
89         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
90
91         if (m != NULL) {
92                 m->unlock();
93         } else {
94                 printf("try to unlock an untracked pthread_mutex\n");
95         }
96
97         return 0;
98 }
99
100 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
101                                 const struct timespec *__restrict abstime) {
102 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
103
104 /*
105         ModelExecution *execution = model->get_execution();
106         if (!execution->mutex_map.contains(p_mutex)) {  
107                 pthread_mutex_init(p_mutex, NULL);
108         }
109         cdsc::mutex *m = execution->mutex_map.get(p_mutex);
110
111         if (m != NULL) {
112                 m->lock();
113         } else {
114                 printf("something is wrong with pthread_mutex_timedlock\n");
115         }
116
117         printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
118 */
119         return 0;
120 }
121
122 pthread_t pthread_self() {
123         Thread* th = model->get_current_thread();
124         return th->get_id();
125 }
126
127 int pthread_key_delete(pthread_key_t) {
128         model_print("key_delete is called\n");
129         return 0;
130 }
131
132 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
133         cdsc::condition_variable *v = new cdsc::condition_variable();
134
135         ModelExecution *execution = model->get_execution();
136         execution->getCondMap()->put(p_cond, v);
137         return 0;
138 }
139
140 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
141         ModelExecution *execution = model->get_execution();
142         if ( !execution->getCondMap()->contains(p_cond) )
143                 pthread_cond_init(p_cond, NULL);
144
145         cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
146         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
147
148         v->wait(*m);
149         return 0;
150 }
151
152 int pthread_cond_timedwait(pthread_cond_t *p_cond, 
153     pthread_mutex_t *p_mutex, const struct timespec *abstime) {
154 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
155         ModelExecution *execution = model->get_execution();
156
157         if ( !execution->getCondMap()->contains(p_cond) )
158                 pthread_cond_init(p_cond, NULL);
159         if ( !execution->getMutexMap()->contains(p_mutex) )
160                 pthread_mutex_init(p_mutex, NULL);
161
162         cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
163         cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
164
165         model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
166 //      v->wait(*m);
167 //      printf("timed_wait called\n");
168         return 0;
169 }
170
171 int pthread_cond_signal(pthread_cond_t *p_cond) {
172         // notify only one blocked thread
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
179         v->notify_one();
180         return 0;
181 }