2 #include "threads-model.h"
6 #include <condition_variable>
9 /* global "model" object */
11 #include "execution.h"
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 };
17 ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)¶ms);
19 /* seq_cst is just a 'don't care' parameter */
20 model->switch_to_master(act);
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);
30 model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
34 void *rtval = th->get_pthread_return();
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));
45 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
46 std::mutex *m = new std::mutex();
48 ModelExecution *execution = model->get_execution();
49 execution->mutex_map.put(p_mutex, m);
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);
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);
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);
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);
80 pthread_t pthread_self() {
81 Thread* th = model->get_current_thread();
85 int pthread_key_delete(pthread_key_t) {
86 model_print("key_delete is called\n");
90 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
91 std::condition_variable *v = new std::condition_variable();
93 ModelExecution *execution = model->get_execution();
94 execution->cond_map.put(p_cond, v);
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);
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);
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);