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) {
20 snapshot_system_init(10000, 1024, 1024, 40000);
21 model = new ModelChecker();
22 model->startChecker();
25 struct pthread_params params = { start_routine, arg };
27 /* seq_cst is just a 'don't care' parameter */
28 model->switch_to_master(new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)¶ms));
33 int pthread_join(pthread_t t, void **value_ptr) {
34 ModelExecution *execution = model->get_execution();
35 Thread *th = execution->get_pthread(t);
37 model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
41 void *rtval = th->get_pthread_return();
47 int pthread_detach(pthread_t t) {
53 /* Take care of both pthread_yield and c++ thread yield */
55 model->switch_thread(new ModelAction(THREAD_YIELD, std::memory_order_seq_cst, thread_current(), VALUE_NONE));
59 void pthread_exit(void *value_ptr) {
60 Thread * th = thread_current();
61 th->set_pthread_return(value_ptr);
62 model->switch_to_master(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th));
63 //Need to exit so we don't return to the program
64 real_pthread_exit(NULL);
67 int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
69 snapshot_system_init(10000, 1024, 1024, 40000);
70 model = new ModelChecker();
71 model->startChecker();
73 cdsc::snapmutex *m = new cdsc::snapmutex();
75 ModelExecution *execution = model->get_execution();
76 execution->getMutexMap()->put(p_mutex, m);
81 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
83 snapshot_system_init(10000, 1024, 1024, 40000);
84 model = new ModelChecker();
85 model->startChecker();
88 ModelExecution *execution = model->get_execution();
90 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
91 instead of pthread_mutex_init, or where *p_mutex is not stored
92 in the execution->mutex_map for some reason. */
93 if (!execution->getMutexMap()->contains(p_mutex)) {
94 pthread_mutex_init(p_mutex, NULL);
97 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
108 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
110 snapshot_system_init(10000, 1024, 1024, 40000);
111 model = new ModelChecker();
112 model->startChecker();
115 ModelExecution *execution = model->get_execution();
116 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
117 return m->try_lock();
119 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
120 ModelExecution *execution = model->get_execution();
121 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
126 printf("try to unlock an untracked pthread_mutex\n");
133 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
134 const struct timespec *__restrict abstime) {
135 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
138 snapshot_system_init(10000, 1024, 1024, 40000);
139 model = new ModelChecker();
140 model->startChecker();
143 ModelExecution *execution = model->get_execution();
145 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
146 instead of pthread_mutex_init, or where *p_mutex is not stored
147 in the execution->mutex_map for some reason. */
148 if (!execution->getMutexMap()->contains(p_mutex)) {
149 pthread_mutex_init(p_mutex, NULL);
152 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
162 pthread_t pthread_self() {
163 Thread* th = model->get_current_thread();
164 return (pthread_t)th->get_id();
167 int pthread_key_delete(pthread_key_t) {
168 model_print("key_delete is called\n");
172 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
173 cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
175 ModelExecution *execution = model->get_execution();
176 execution->getCondMap()->put(p_cond, v);
180 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
181 ModelExecution *execution = model->get_execution();
182 if ( !execution->getCondMap()->contains(p_cond) )
183 pthread_cond_init(p_cond, NULL);
184 if ( !execution->getMutexMap()->contains(p_mutex) )
185 pthread_mutex_init(p_mutex, NULL);
187 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
188 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
194 int pthread_cond_timedwait(pthread_cond_t *p_cond,
195 pthread_mutex_t *p_mutex, const struct timespec *abstime) {
196 ModelExecution *execution = model->get_execution();
198 if ( !execution->getCondMap()->contains(p_cond) )
199 pthread_cond_init(p_cond, NULL);
200 if ( !execution->getMutexMap()->contains(p_mutex) )
201 pthread_mutex_init(p_mutex, NULL);
203 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
204 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
206 model->switch_thread(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
209 // model_print("Timed_wait is called\n");
213 int pthread_cond_signal(pthread_cond_t *p_cond) {
214 // notify only one blocked thread
215 ModelExecution *execution = model->get_execution();
216 if ( !execution->getCondMap()->contains(p_cond) )
217 pthread_cond_init(p_cond, NULL);
219 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
225 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
226 // notify all blocked threads
227 ModelExecution *execution = model->get_execution();
228 if ( !execution->getCondMap()->contains(p_cond) )
229 pthread_cond_init(p_cond, NULL);
231 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
237 int pthread_cond_destroy(pthread_cond_t *p_cond) {
238 ModelExecution *execution = model->get_execution();
240 if (execution->getCondMap()->contains(p_cond)) {
241 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
243 execution->getCondMap()->remove(p_cond);