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();
178 if ( !execution->getCondMap()->contains(p_cond) )
179 pthread_cond_init(p_cond, NULL);
180 if ( !execution->getMutexMap()->contains(p_mutex) )
181 pthread_mutex_init(p_mutex, NULL);
183 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
184 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
186 model->switch_thread(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
189 // model_print("Timed_wait is called\n");
193 int pthread_cond_signal(pthread_cond_t *p_cond) {
194 // notify only one blocked thread
195 ModelExecution *execution = model->get_execution();
196 if ( !execution->getCondMap()->contains(p_cond) )
197 pthread_cond_init(p_cond, NULL);
199 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
205 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
206 // notify all blocked threads
207 ModelExecution *execution = model->get_execution();
208 if ( !execution->getCondMap()->contains(p_cond) )
209 pthread_cond_init(p_cond, NULL);
211 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
217 int pthread_cond_destroy(pthread_cond_t *p_cond) {
218 ModelExecution *execution = model->get_execution();
220 if (execution->getCondMap()->contains(p_cond)) {
221 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
223 execution->getCondMap()->remove(p_cond);
228 /* https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c */
229 int pthread_getattr_np(pthread_t t, pthread_attr_t *attr)
231 ModelExecution *execution = model->get_execution();
232 Thread *th = execution->get_pthread(t);
234 struct pthread_attr *iattr = (struct pthread_attr *) attr;
236 /* The sizes are subject to alignment. */
238 #if _STACK_GROWS_DOWN
241 iattr->stackaddr = (char *) th->get_stack_addr();
251 int pthread_setname_np(pthread_t t, const char *name)
253 ModelExecution *execution = model->get_execution();
254 Thread *th = execution->get_pthread(t);