X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11tester.git;a=blobdiff_plain;f=pthread.cc;h=63060d898eb2af5c8db5c995dd24a7c5f1e63710;hp=0234cdaedb1abe17001df485c2099a813e437613;hb=c522e1dd1ccc11827ac2fce444764081dca25746;hpb=1a2e39876cbd4a779e7c387a57ba1d26740e9c6a diff --git a/pthread.cc b/pthread.cc index 0234cdae..63060d89 100644 --- a/pthread.cc +++ b/pthread.cc @@ -8,11 +8,31 @@ #include "mutex.h" #include -#include /* global "model" object */ #include "model.h" #include "execution.h" +extern "C" { +int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); +} + +int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { + if (model) { + uint64_t time = rqtp->tv_sec * 1000000000 + rqtp->tv_nsec; + struct timespec currtime; + clock_gettime(CLOCK_MONOTONIC, &currtime); + uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec; + model->switch_to_master(new ModelAction(THREAD_SLEEP, std::memory_order_seq_cst, time, lcurrtime)); + if (rmtp != NULL) { + clock_gettime(CLOCK_MONOTONIC, &currtime); + uint64_t lendtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec; + uint64_t elapsed = lendtime - lcurrtime; + rmtp->tv_sec = elapsed / 1000000000; + rmtp->tv_nsec = elapsed - rmtp->tv_sec * 1000000000; + } + } + return 0; +} int pthread_create(pthread_t *t, const pthread_attr_t * attr, pthread_start_t start_routine, void * arg) { @@ -33,7 +53,6 @@ int pthread_create(pthread_t *t, const pthread_attr_t * attr, } int pthread_join(pthread_t t, void **value_ptr) { -// Thread *th = model->get_pthread(t); ModelExecution *execution = model->get_execution(); Thread *th = execution->get_pthread(t); @@ -57,16 +76,17 @@ void pthread_exit(void *value_ptr) { Thread * th = thread_current(); th->set_pthread_return(value_ptr); model->switch_to_master(new ModelAction(THREADONLY_FINISH, std::memory_order_seq_cst, th)); + //Need to exit so we don't return to the program + real_pthread_exit(NULL); } int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) { - cdsc::snapmutex *m = new cdsc::snapmutex(); - if (!model) { snapshot_system_init(10000, 1024, 1024, 40000); model = new ModelChecker(); model->startChecker(); } + cdsc::snapmutex *m = new cdsc::snapmutex(); ModelExecution *execution = model->get_execution(); execution->getMutexMap()->put(p_mutex, m); @@ -81,7 +101,6 @@ int pthread_mutex_lock(pthread_mutex_t *p_mutex) { model->startChecker(); } - ModelExecution *execution = model->get_execution(); /* to protect the case where PTHREAD_MUTEX_INITIALIZER is used @@ -189,7 +208,7 @@ int pthread_cond_timedwait(pthread_cond_t *p_cond, pthread_mutex_init(p_mutex, NULL); cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond); - cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex); +// cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex); model->switch_to_master(new ModelAction(NOOP, std::memory_order_seq_cst, v)); // v->wait(*m); @@ -220,3 +239,14 @@ int pthread_cond_broadcast(pthread_cond_t *p_cond) { v->notify_all(); return 0; } + +int pthread_cond_destroy(pthread_cond_t *p_cond) { + ModelExecution *execution = model->get_execution(); + + if (execution->getCondMap()->contains(p_cond)) { + cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond); + delete v; + execution->getCondMap()->remove(p_cond); + } + return 0; +}