750a302ceb454439306403805e0fceb287e652b1
[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 #include "execution.h"
10
11 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
12           pthread_start_t start_routine, void * arg) {
13         struct pthread_params params = { start_routine, arg };
14
15         ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params);
16
17         /* seq_cst is just a 'don't care' parameter */
18         model->switch_to_master(act);
19
20         return 0;
21 }
22
23 int pthread_join(pthread_t t, void **value_ptr) {
24 //      Thread *th = model->get_pthread(t);
25         ModelExecution *execution = model->get_execution();
26         Thread *th = execution->get_pthread(t);
27
28         model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
29
30         if ( value_ptr ) {
31                 // store return value
32                 void *rtval = th->get_pthread_return();
33                 *value_ptr = rtval;
34         } 
35         return 0;
36 }
37
38 void pthread_exit(void *value_ptr) {
39         Thread * th = thread_current();
40         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, th));
41 }
42
43 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
44         std::mutex *m = new std::mutex();
45
46         ModelExecution *execution = model->get_execution();
47         execution->mutex_map.put(p_mutex, m);
48         return 0;
49 }
50
51 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
52         ModelExecution *execution = model->get_execution();
53         std::mutex *m = execution->mutex_map.get(p_mutex);
54         m->lock();
55         /* error message? */
56         return 0;
57 }
58 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
59         ModelExecution *execution = model->get_execution();
60         std::mutex *m = execution->mutex_map.get(p_mutex);
61         return m->try_lock();
62
63         /* error message?  */
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
70         return 0;
71 }
72
73 pthread_t pthread_self() {
74         Thread* th = model->get_current_thread();
75         return th->get_id();
76 }
77
78 int pthread_key_delete(pthread_key_t) {
79         model_print("key_delete is called\n");
80         return 0;
81 }