2 #include "threads-model.h"
6 #include "snapshot-interface.h"
10 #include <condition_variable>
13 /* global "model" object */
15 #include "execution.h"
17 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
20 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) {
22 uint64_t time = rqtp->tv_sec * 1000000000 + rqtp->tv_nsec;
23 struct timespec currtime;
24 clock_gettime(CLOCK_MONOTONIC, &currtime);
25 uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
26 model->switch_to_master(new ModelAction(THREAD_SLEEP, std::memory_order_seq_cst, time, lcurrtime));
28 clock_gettime(CLOCK_MONOTONIC, &currtime);
29 uint64_t lendtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
30 uint64_t elapsed = lendtime - lcurrtime;
31 rmtp->tv_sec = elapsed / 1000000000;
32 rmtp->tv_nsec = elapsed - rmtp->tv_sec * 1000000000;
38 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
39 pthread_start_t start_routine, void * arg) {
41 snapshot_system_init(10000, 1024, 1024, 40000);
42 model = new ModelChecker();
43 model->startChecker();
46 struct pthread_params params = { start_routine, arg };
48 ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)¶ms);
50 /* seq_cst is just a 'don't care' parameter */
51 model->switch_to_master(act);
56 int pthread_join(pthread_t t, void **value_ptr) {
57 // Thread *th = model->get_pthread(t);
58 ModelExecution *execution = model->get_execution();
59 Thread *th = execution->get_pthread(t);
61 model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
65 void *rtval = th->get_pthread_return();
71 int pthread_detach(pthread_t t) {
77 void pthread_exit(void *value_ptr) {
78 Thread * th = thread_current();
79 th->set_pthread_return(value_ptr);
80 model->switch_to_master(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th));
81 //Need to exit so we don't return to the program
82 real_pthread_exit(NULL);
85 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
86 cdsc::snapmutex *m = new cdsc::snapmutex();
89 snapshot_system_init(10000, 1024, 1024, 40000);
90 model = new ModelChecker();
91 model->startChecker();
94 ModelExecution *execution = model->get_execution();
95 execution->getMutexMap()->put(p_mutex, m);
100 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
102 snapshot_system_init(10000, 1024, 1024, 40000);
103 model = new ModelChecker();
104 model->startChecker();
108 ModelExecution *execution = model->get_execution();
110 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
111 instead of pthread_mutex_init, or where *p_mutex is not stored
112 in the execution->mutex_map for some reason. */
113 if (!execution->getMutexMap()->contains(p_mutex)) {
114 pthread_mutex_init(p_mutex, NULL);
117 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
128 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
130 snapshot_system_init(10000, 1024, 1024, 40000);
131 model = new ModelChecker();
132 model->startChecker();
135 ModelExecution *execution = model->get_execution();
136 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
137 return m->try_lock();
139 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
140 ModelExecution *execution = model->get_execution();
141 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
146 printf("try to unlock an untracked pthread_mutex\n");
152 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
153 const struct timespec *__restrict abstime) {
154 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
157 ModelExecution *execution = model->get_execution();
158 if (!execution->mutex_map.contains(p_mutex)) {
159 pthread_mutex_init(p_mutex, NULL);
161 cdsc::snapmutex *m = execution->mutex_map.get(p_mutex);
166 printf("something is wrong with pthread_mutex_timedlock\n");
169 printf("pthread_mutex_timedlock is called. It is currently implemented as a normal lock operation without no timeout\n");
174 pthread_t pthread_self() {
175 Thread* th = model->get_current_thread();
176 return (pthread_t)th->get_id();
179 int pthread_key_delete(pthread_key_t) {
180 model_print("key_delete is called\n");
184 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
185 cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
187 ModelExecution *execution = model->get_execution();
188 execution->getCondMap()->put(p_cond, v);
192 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
193 ModelExecution *execution = model->get_execution();
194 if ( !execution->getCondMap()->contains(p_cond) )
195 pthread_cond_init(p_cond, NULL);
197 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
198 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
204 int pthread_cond_timedwait(pthread_cond_t *p_cond,
205 pthread_mutex_t *p_mutex, const struct timespec *abstime) {
206 // implement cond_timedwait as a noop and let the scheduler decide which thread goes next
207 ModelExecution *execution = model->get_execution();
209 if ( !execution->getCondMap()->contains(p_cond) )
210 pthread_cond_init(p_cond, NULL);
211 if ( !execution->getMutexMap()->contains(p_mutex) )
212 pthread_mutex_init(p_mutex, NULL);
214 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
215 // cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
217 model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v));
219 // printf("timed_wait called\n");
223 int pthread_cond_signal(pthread_cond_t *p_cond) {
224 // notify only one blocked thread
225 ModelExecution *execution = model->get_execution();
226 if ( !execution->getCondMap()->contains(p_cond) )
227 pthread_cond_init(p_cond, NULL);
229 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
235 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
236 // notify all blocked threads
237 ModelExecution *execution = model->get_execution();
238 if ( !execution->getCondMap()->contains(p_cond) )
239 pthread_cond_init(p_cond, NULL);
241 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);