2 #include "threads-model.h"
6 #include "snapshot-interface.h"
10 #include <condition_variable>
13 /* global "model" object */
15 #include "execution.h"
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 };
21 ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)¶ms);
23 /* seq_cst is just a 'don't care' parameter */
24 model->switch_to_master(act);
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);
34 model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
38 void *rtval = th->get_pthread_return();
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
50 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
51 cdsc::mutex *m = new cdsc::mutex();
54 snapshot_system_init(10000, 1024, 1024, 40000);
55 model = new ModelChecker();
58 ModelExecution *execution = model->get_execution();
59 execution->getMutexMap()->put(p_mutex, m);
64 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
65 ModelExecution *execution = model->get_execution();
67 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
68 instead of pthread_mutex_init, or where *p_mutex is not stored
69 in the execution->mutex_map for some reason. */
70 if (!execution->getMutexMap()->contains(p_mutex)) {
71 pthread_mutex_init(p_mutex, NULL);
74 cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
85 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
86 ModelExecution *execution = model->get_execution();
87 cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
90 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
91 ModelExecution *execution = model->get_execution();
92 cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
97 printf("try to unlock an untracked pthread_mutex\n");
103 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
104 const struct timespec *__restrict abstime) {
105 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
108 ModelExecution *execution = model->get_execution();
109 if (!execution->mutex_map.contains(p_mutex)) {
110 pthread_mutex_init(p_mutex, NULL);
112 cdsc::mutex *m = execution->mutex_map.get(p_mutex);
117 printf("something is wrong with pthread_mutex_timedlock\n");
120 printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
125 pthread_t pthread_self() {
126 Thread* th = model->get_current_thread();
127 return (pthread_t)th->get_id();
130 int pthread_key_delete(pthread_key_t) {
131 model_print("key_delete is called\n");
135 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
136 cdsc::condition_variable *v = new cdsc::condition_variable();
138 ModelExecution *execution = model->get_execution();
139 execution->getCondMap()->put(p_cond, v);
143 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
144 ModelExecution *execution = model->get_execution();
145 if ( !execution->getCondMap()->contains(p_cond) )
146 pthread_cond_init(p_cond, NULL);
148 cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
149 cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
155 int pthread_cond_timedwait(pthread_cond_t *p_cond,
156 pthread_mutex_t *p_mutex, const struct timespec *abstime) {
157 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
158 ModelExecution *execution = model->get_execution();
160 if ( !execution->getCondMap()->contains(p_cond) )
161 pthread_cond_init(p_cond, NULL);
162 if ( !execution->getMutexMap()->contains(p_mutex) )
163 pthread_mutex_init(p_mutex, NULL);
165 cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);
166 cdsc::mutex *m = execution->getMutexMap()->get(p_mutex);
168 model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
170 // printf("timed_wait called\n");
174 int pthread_cond_signal(pthread_cond_t *p_cond) {
175 // notify only one blocked thread
176 ModelExecution *execution = model->get_execution();
177 if ( !execution->getCondMap()->contains(p_cond) )
178 pthread_cond_init(p_cond, NULL);
180 cdsc::condition_variable *v = execution->getCondMap()->get(p_cond);