2 #include "threads-model.h"
6 #include "snapshot-interface.h"
10 #include <condition_variable>
12 /* global "model" object */
14 #include "execution.h"
16 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
19 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) {
21 uint64_t time = rqtp->tv_sec * 1000000000 + rqtp->tv_nsec;
22 struct timespec currtime;
23 clock_gettime(CLOCK_MONOTONIC, &currtime);
24 uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
25 model->switch_to_master(new ModelAction(THREAD_SLEEP, std::memory_order_seq_cst, time, lcurrtime));
27 clock_gettime(CLOCK_MONOTONIC, &currtime);
28 uint64_t lendtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
29 uint64_t elapsed = lendtime - lcurrtime;
30 rmtp->tv_sec = elapsed / 1000000000;
31 rmtp->tv_nsec = elapsed - rmtp->tv_sec * 1000000000;
37 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
38 pthread_start_t start_routine, void * arg) {
40 snapshot_system_init(10000, 1024, 1024, 40000);
41 model = new ModelChecker();
42 model->startChecker();
45 struct pthread_params params = { start_routine, arg };
47 ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)¶ms);
49 /* seq_cst is just a 'don't care' parameter */
50 model->switch_to_master(act);
55 int pthread_join(pthread_t t, void **value_ptr) {
56 ModelExecution *execution = model->get_execution();
57 Thread *th = execution->get_pthread(t);
59 model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
63 void *rtval = th->get_pthread_return();
69 int pthread_detach(pthread_t t) {
75 void pthread_exit(void *value_ptr) {
76 Thread * th = thread_current();
77 th->set_pthread_return(value_ptr);
78 model->switch_to_master(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th));
79 //Need to exit so we don't return to the program
80 real_pthread_exit(NULL);
83 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
85 snapshot_system_init(10000, 1024, 1024, 40000);
86 model = new ModelChecker();
87 model->startChecker();
89 cdsc::snapmutex *m = new cdsc::snapmutex();
91 ModelExecution *execution = model->get_execution();
92 execution->getMutexMap()->put(p_mutex, m);
97 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
99 snapshot_system_init(10000, 1024, 1024, 40000);
100 model = new ModelChecker();
101 model->startChecker();
104 ModelExecution *execution = model->get_execution();
106 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
107 instead of pthread_mutex_init, or where *p_mutex is not stored
108 in the execution->mutex_map for some reason. */
109 if (!execution->getMutexMap()->contains(p_mutex)) {
110 pthread_mutex_init(p_mutex, NULL);
113 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
124 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
126 snapshot_system_init(10000, 1024, 1024, 40000);
127 model = new ModelChecker();
128 model->startChecker();
131 ModelExecution *execution = model->get_execution();
132 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
133 return m->try_lock();
135 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
136 ModelExecution *execution = model->get_execution();
137 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
142 printf("try to unlock an untracked pthread_mutex\n");
148 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
149 const struct timespec *__restrict abstime) {
150 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
153 ModelExecution *execution = model->get_execution();
154 if (!execution->mutex_map.contains(p_mutex)) {
155 pthread_mutex_init(p_mutex, NULL);
157 cdsc::snapmutex *m = execution->mutex_map.get(p_mutex);
162 printf("something is wrong with pthread_mutex_timedlock\n");
165 printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
170 pthread_t pthread_self() {
171 Thread* th = model->get_current_thread();
172 return (pthread_t)th->get_id();
175 int pthread_key_delete(pthread_key_t) {
176 model_print("key_delete is called\n");
180 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
181 cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
183 ModelExecution *execution = model->get_execution();
184 execution->getCondMap()->put(p_cond, v);
188 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
189 ModelExecution *execution = model->get_execution();
190 if ( !execution->getCondMap()->contains(p_cond) )
191 pthread_cond_init(p_cond, NULL);
193 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
194 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
200 int pthread_cond_timedwait(pthread_cond_t *p_cond,
201 pthread_mutex_t *p_mutex, const struct timespec *abstime) {
202 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
203 ModelExecution *execution = model->get_execution();
205 if ( !execution->getCondMap()->contains(p_cond) )
206 pthread_cond_init(p_cond, NULL);
207 if ( !execution->getMutexMap()->contains(p_mutex) )
208 pthread_mutex_init(p_mutex, NULL);
210 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
211 // cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
213 model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
215 // printf("timed_wait called\n");
219 int pthread_cond_signal(pthread_cond_t *p_cond) {
220 // notify only one blocked thread
221 ModelExecution *execution = model->get_execution();
222 if ( !execution->getCondMap()->contains(p_cond) )
223 pthread_cond_init(p_cond, NULL);
225 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
231 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
232 // notify all blocked threads
233 ModelExecution *execution = model->get_execution();
234 if ( !execution->getCondMap()->contains(p_cond) )
235 pthread_cond_init(p_cond, NULL);
237 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
243 int pthread_cond_destroy(pthread_cond_t *p_cond) {
244 ModelExecution *execution = model->get_execution();
246 if (execution->getCondMap()->contains(p_cond)) {
247 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
249 execution->getCondMap()->remove(p_cond);