pthread_join is able to return values
[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
7 /* global "model" object */
8 #include "model.h"
9
10 unsigned int counter = 0;       // counter does not to be reset to zero. It is 
11                                 // find as long as it is unique.
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         *t = counter;
18         counter++;
19
20         ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params);
21         model->pthread_map[*t] = act;
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         ModelAction *act = model->pthread_map[t];
31         Thread *th = act->get_thread_operand();
32
33         model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
34
35         // store return value
36         void *rtval = th->get_pthread_return();
37         *value_ptr = rtval;
38
39         return 0;
40 }
41
42 void pthread_exit(void *value_ptr) {
43         Thread * th = thread_current();
44         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, th));
45 }
46
47 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
48         if (model->mutex_map.find(p_mutex) != model->mutex_map.end() ) {
49                 model_print("Reinitialize a lock\n");
50                 // return 1;    // 0 means success; 1 means failure
51         }
52
53         std::mutex *m = new std::mutex();
54         m->initialize();
55         model->mutex_map[p_mutex] = m;
56         return 0;
57 }
58
59 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
60         std::mutex *m = model->mutex_map[p_mutex];
61         m->lock();
62         /* error message? */
63         return 0;
64 }
65 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
66         std::mutex *m = model->mutex_map[p_mutex];
67         return m->try_lock();
68         /* error message?  */
69 }
70 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {    
71         std::mutex *m = model->mutex_map[p_mutex];
72         m->unlock();
73         return 0;
74 }
75
76 void check() {
77         for (std::map<pthread_t, ModelAction*>::iterator it = model->pthread_map.begin(); it != model->pthread_map.end(); it++) {
78                 model_print("id: %d\n", it->first);
79         }
80 }