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