2 #include "threads-model.h"
6 #include "snapshot-interface.h"
10 #include <condition_variable>
12 /* global "model" object */
14 #include "execution.h"
17 int pthread_create(pthread_t *t, const pthread_attr_t * attr,
18 pthread_start_t start_routine, void * arg) {
19 createModelIfNotExist();
20 struct pthread_params params = { start_routine, arg };
22 /* seq_cst is just a 'don't care' parameter */
23 model->switch_thread(new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)¶ms));
28 int pthread_join(pthread_t t, void **value_ptr) {
29 ModelExecution *execution = model->get_execution();
30 Thread *th = execution->get_pthread(t);
32 model->switch_thread(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
36 void *rtval = th->get_pthread_return();
42 int pthread_detach(pthread_t t) {
48 /* Take care of both pthread_yield and c++ thread yield */
50 model->switch_thread(new ModelAction(THREAD_YIELD, std::memory_order_seq_cst, thread_current(), VALUE_NONE));
54 void pthread_exit(void *value_ptr) {
55 Thread * th = thread_current();
56 th->set_pthread_return(value_ptr);
57 model->switch_thread(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th));
58 //Need to exit so we don't return to the program
59 real_pthread_exit(NULL);
62 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t * attr) {
63 createModelIfNotExist();
64 int mutex_type = PTHREAD_MUTEX_DEFAULT;
66 pthread_mutexattr_gettype(attr, &mutex_type);
68 cdsc::snapmutex *m = new cdsc::snapmutex(mutex_type);
70 ModelExecution *execution = model->get_execution();
71 execution->getMutexMap()->put(p_mutex, m);
76 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
77 createModelIfNotExist();
78 ModelExecution *execution = model->get_execution();
80 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
81 instead of pthread_mutex_init, or where *p_mutex is not stored
82 in the execution->mutex_map for some reason. */
83 if (!execution->getMutexMap()->contains(p_mutex)) {
84 pthread_mutex_init(p_mutex, NULL);
87 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
98 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
99 createModelIfNotExist();
100 ModelExecution *execution = model->get_execution();
101 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
102 return m->try_lock() ? 0 : EBUSY;
104 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
105 ModelExecution *execution = model->get_execution();
106 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
111 printf("try to unlock an untracked pthread_mutex\n");
118 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
119 const struct timespec *__restrict abstime) {
120 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
121 createModelIfNotExist();
122 ModelExecution *execution = model->get_execution();
124 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
125 instead of pthread_mutex_init, or where *p_mutex is not stored
126 in the execution->mutex_map for some reason. */
127 if (!execution->getMutexMap()->contains(p_mutex)) {
128 pthread_mutex_init(p_mutex, NULL);
131 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
141 pthread_t pthread_self() {
142 createModelIfNotExist();
143 Thread* th = model->get_current_thread();
144 return (pthread_t)th->get_id();
147 int pthread_key_delete(pthread_key_t) {
148 model_print("key_delete is called\n");
152 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
153 cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
155 ModelExecution *execution = model->get_execution();
156 execution->getCondMap()->put(p_cond, v);
160 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
161 ModelExecution *execution = model->get_execution();
162 if ( !execution->getCondMap()->contains(p_cond) )
163 pthread_cond_init(p_cond, NULL);
164 if ( !execution->getMutexMap()->contains(p_mutex) )
165 pthread_mutex_init(p_mutex, NULL);
167 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
168 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
174 int pthread_cond_timedwait(pthread_cond_t *p_cond,
175 pthread_mutex_t *p_mutex, const struct timespec *abstime) {
176 ModelExecution *execution = model->get_execution();
177 if ( !execution->getCondMap()->contains(p_cond) )
178 pthread_cond_init(p_cond, NULL);
179 if ( !execution->getMutexMap()->contains(p_mutex) )
180 pthread_mutex_init(p_mutex, NULL);
182 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
183 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
185 uint64_t time = abstime->tv_sec * 1000000000 + abstime->tv_nsec;
186 ModelAction * timed_wait = new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m);
187 timed_wait->set_time(time);
188 if (model->switch_thread(timed_wait) == ETIMEDOUT) {
189 //model_print("thread %u wait timedout\n", thread_current_id());
196 int pthread_cond_signal(pthread_cond_t *p_cond) {
197 // notify only one blocked thread
198 ModelExecution *execution = model->get_execution();
199 if ( !execution->getCondMap()->contains(p_cond) )
200 pthread_cond_init(p_cond, NULL);
202 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
208 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
209 // notify all blocked threads
210 ModelExecution *execution = model->get_execution();
211 if ( !execution->getCondMap()->contains(p_cond) )
212 pthread_cond_init(p_cond, NULL);
214 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
220 int pthread_cond_destroy(pthread_cond_t *p_cond) {
221 ModelExecution *execution = model->get_execution();
223 if (execution->getCondMap()->contains(p_cond)) {
224 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
226 execution->getCondMap()->remove(p_cond);
231 /* https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c */
232 int pthread_getattr_np(pthread_t t, pthread_attr_t *attr)
234 ModelExecution *execution = model->get_execution();
235 Thread *th = execution->get_pthread(t);
237 struct pthread_attr *iattr = (struct pthread_attr *) attr;
239 /* The sizes are subject to alignment. */
241 #if _STACK_GROWS_DOWN
244 iattr->stackaddr = (char *) th->get_stack_addr();
254 int pthread_setname_np(pthread_t t, const char *name)
256 ModelExecution *execution = model->get_execution();
257 Thread *th = execution->get_pthread(t);