add support for condition variable
[c11tester.git] / pthread.cc
1 #include "common.h"
2 #include "threads-model.h"
3 #include "action.h"
4 #include "pthread.h"
5 #include <mutex>
6 #include <condition_variable>
7 #include <assert.h>
8
9 /* global "model" object */
10 #include "model.h"
11 #include "execution.h"
12
13 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
14           pthread_start_t start_routine, void * arg) {
15         struct pthread_params params = { start_routine, arg };
16
17         ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params);
18
19         /* seq_cst is just a 'don't care' parameter */
20         model->switch_to_master(act);
21
22         return 0;
23 }
24
25 int pthread_join(pthread_t t, void **value_ptr) {
26 //      Thread *th = model->get_pthread(t);
27         ModelExecution *execution = model->get_execution();
28         Thread *th = execution->get_pthread(t);
29
30         model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
31
32         if ( value_ptr ) {
33                 // store return value
34                 void *rtval = th->get_pthread_return();
35                 *value_ptr = rtval;
36         } 
37         return 0;
38 }
39
40 void pthread_exit(void *value_ptr) {
41         Thread * th = thread_current();
42         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, th));
43 }
44
45 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
46         std::mutex *m = new std::mutex();
47
48         ModelExecution *execution = model->get_execution();
49         execution->mutex_map.put(p_mutex, m);
50         return 0;
51 }
52
53 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
54         ModelExecution *execution = model->get_execution();
55         std::mutex *m = execution->mutex_map.get(p_mutex);
56         m->lock();
57         /* error message? */
58         return 0;
59 }
60 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
61         ModelExecution *execution = model->get_execution();
62         std::mutex *m = execution->mutex_map.get(p_mutex);
63         return m->try_lock();
64 }
65 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {    
66         ModelExecution *execution = model->get_execution();
67         std::mutex *m = execution->mutex_map.get(p_mutex);
68         m->unlock();
69         return 0;
70 }
71
72 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
73                                 const struct timespec *__restrict abstime) {
74         ModelExecution *execution = model->get_execution();
75         std::mutex *m = execution->mutex_map.get(p_mutex);
76         m->lock();
77         return 0;
78 }
79
80 pthread_t pthread_self() {
81         Thread* th = model->get_current_thread();
82         return th->get_id();
83 }
84
85 int pthread_key_delete(pthread_key_t) {
86         model_print("key_delete is called\n");
87         return 0;
88 }
89
90 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
91         std::condition_variable *v = new std::condition_variable();
92
93         ModelExecution *execution = model->get_execution();
94         execution->cond_map.put(p_cond, v);
95         return 0;
96 }
97
98 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
99         ModelExecution *execution = model->get_execution();
100         std::condition_variable *v = execution->cond_map.get(p_cond);
101         std::mutex *m = execution->mutex_map.get(p_mutex);
102
103         v->wait(*m);
104         return 0;
105
106 }
107
108 int pthread_cond_timedwait(pthread_cond_t *p_cond, 
109     pthread_mutex_t *p_mutex, const struct timespec *abstime) {
110         ModelExecution *execution = model->get_execution();
111         std::condition_variable *v = execution->cond_map.get(p_cond);
112         std::mutex *m = execution->mutex_map.get(p_mutex);
113
114         v->wait(*m);
115         return 0;
116 }
117
118 int pthread_cond_signal(pthread_cond_t *p_cond) {
119         // notify only one blocked thread
120         ModelExecution *execution = model->get_execution();
121         std::condition_variable *v = execution->cond_map.get(p_cond);
122
123         v->notify_one();
124         return 0;
125 }