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_thread(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_thread(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_thread(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 * attr) {
69 snapshot_system_init(10000, 1024, 1024, 40000);
70 model = new ModelChecker();
71 model->startChecker();
74 int mutex_type = PTHREAD_MUTEX_DEFAULT;
76 pthread_mutexattr_gettype(attr, &mutex_type);
78 cdsc::snapmutex *m = new cdsc::snapmutex(mutex_type);
80 ModelExecution *execution = model->get_execution();
81 execution->getMutexMap()->put(p_mutex, m);
86 int pthread_mutex_lock(pthread_mutex_t *p_mutex) {
88 snapshot_system_init(10000, 1024, 1024, 40000);
89 model = new ModelChecker();
90 model->startChecker();
93 ModelExecution *execution = model->get_execution();
95 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
96 instead of pthread_mutex_init, or where *p_mutex is not stored
97 in the execution->mutex_map for some reason. */
98 if (!execution->getMutexMap()->contains(p_mutex)) {
99 pthread_mutex_init(p_mutex, NULL);
102 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
113 int pthread_mutex_trylock(pthread_mutex_t *p_mutex) {
115 snapshot_system_init(10000, 1024, 1024, 40000);
116 model = new ModelChecker();
117 model->startChecker();
120 ModelExecution *execution = model->get_execution();
121 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
122 return m->try_lock();
124 int pthread_mutex_unlock(pthread_mutex_t *p_mutex) {
125 ModelExecution *execution = model->get_execution();
126 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
131 printf("try to unlock an untracked pthread_mutex\n");
138 int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
139 const struct timespec *__restrict abstime) {
140 // timedlock just gives the option of giving up the lock, so return and let the scheduler decide which thread goes next
143 snapshot_system_init(10000, 1024, 1024, 40000);
144 model = new ModelChecker();
145 model->startChecker();
148 ModelExecution *execution = model->get_execution();
150 /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used
151 instead of pthread_mutex_init, or where *p_mutex is not stored
152 in the execution->mutex_map for some reason. */
153 if (!execution->getMutexMap()->contains(p_mutex)) {
154 pthread_mutex_init(p_mutex, NULL);
157 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
167 pthread_t pthread_self() {
169 snapshot_system_init(10000, 1024, 1024, 40000);
170 model = new ModelChecker();
171 model->startChecker();
174 Thread* th = model->get_current_thread();
175 return (pthread_t)th->get_id();
178 int pthread_key_delete(pthread_key_t) {
179 model_print("key_delete is called\n");
183 int pthread_cond_init(pthread_cond_t *p_cond, const pthread_condattr_t *attr) {
184 cdsc::snapcondition_variable *v = new cdsc::snapcondition_variable();
186 ModelExecution *execution = model->get_execution();
187 execution->getCondMap()->put(p_cond, v);
191 int pthread_cond_wait(pthread_cond_t *p_cond, pthread_mutex_t *p_mutex) {
192 ModelExecution *execution = model->get_execution();
193 if ( !execution->getCondMap()->contains(p_cond) )
194 pthread_cond_init(p_cond, NULL);
195 if ( !execution->getMutexMap()->contains(p_mutex) )
196 pthread_mutex_init(p_mutex, NULL);
198 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
199 cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
205 int pthread_cond_timedwait(pthread_cond_t *p_cond,
206 pthread_mutex_t *p_mutex, const struct timespec *abstime) {
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_thread(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
220 // model_print("Timed_wait is called\n");
224 int pthread_cond_signal(pthread_cond_t *p_cond) {
225 // notify only one blocked thread
226 ModelExecution *execution = model->get_execution();
227 if ( !execution->getCondMap()->contains(p_cond) )
228 pthread_cond_init(p_cond, NULL);
230 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
236 int pthread_cond_broadcast(pthread_cond_t *p_cond) {
237 // notify all blocked threads
238 ModelExecution *execution = model->get_execution();
239 if ( !execution->getCondMap()->contains(p_cond) )
240 pthread_cond_init(p_cond, NULL);
242 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
248 int pthread_cond_destroy(pthread_cond_t *p_cond) {
249 ModelExecution *execution = model->get_execution();
251 if (execution->getCondMap()->contains(p_cond)) {
252 cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
254 execution->getCondMap()->remove(p_cond);
259 /* https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c */
260 int pthread_getattr_np(pthread_t t, pthread_attr_t *attr)
262 ModelExecution *execution = model->get_execution();
263 Thread *th = execution->get_pthread(t);
265 struct pthread_attr *iattr = (struct pthread_attr *) attr;
267 /* The sizes are subject to alignment. */
269 #if _STACK_GROWS_DOWN
272 iattr->stackaddr = (char *) th->get_stack_addr();
282 int pthread_setname_np(pthread_t t, const char *name)
284 ModelExecution *execution = model->get_execution();
285 Thread *th = execution->get_pthread(t);