Merge branch 'newactionlist' of ssh://plrg.eecs.uci.edu:/home/git/random-fuzzer into...
[c11tester.git] / threads.cc
index e8ffb3a9bf2151814c5441e6fefa97f2b2691168..a2d2ed166ba3350400e6e5c13cb9b897fdad628a 100644 (file)
@@ -14,6 +14,7 @@
 #include "model.h"
 #include "execution.h"
 #include "schedule.h"
+#include "clockvector.h"
 
 #ifdef TLS
 #include <dlfcn.h>
@@ -59,13 +60,14 @@ Thread * thread_current(void)
        return model->get_current_thread();
 }
 
-void main_thread_startup() {
-#ifdef TLS
+void modelexit() {
+       model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, thread_current()));
+}
+
+void initMainThread() {
+       atexit(modelexit);
        Thread * curr_thread = thread_current();
-       /* Add dummy "start" action, just to create a first clock vector */
        model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
-#endif
-       thread_startup();
 }
 
 /**
@@ -170,7 +172,7 @@ void real_init_all() {
        }
 
        if (!pthread_exit_p) {
-               pthread_exit_p = (void (*)(void *))dlsym(RTLD_NEXT, "pthread_exit");
+               *((void (**)(void *)) &pthread_exit_p) = (void (*)(void *))dlsym(RTLD_NEXT, "pthread_exit");
                if ((error = dlerror()) != NULL) {
                        fputs(error, stderr);
                        exit(EXIT_FAILURE);
@@ -225,7 +227,7 @@ void * helper_thread(void * ptr) {
 #ifdef TLS
 void tlsdestructor(void *v) {
        uintptr_t count = (uintptr_t) v;
-       if (count > 0) {
+       if (count > 1) {
                if (pthread_setspecific(model->get_execution()->getPthreadKey(), (const void *)(count - 1))) {
                        printf("Destructor setup failed\n");
                        exit(-1);
@@ -285,10 +287,7 @@ int Thread::create_context()
        context.uc_stack.ss_flags = 0;
        context.uc_link = model->get_system_context();
 #ifdef TLS
-       if (model != NULL)
-               makecontext(&context, setup_context, 0);
-       else
-               makecontext(&context, main_thread_startup, 0);
+       makecontext(&context, setup_context, 0);
 #else
        makecontext(&context, thread_startup, 0);
 #endif
@@ -342,7 +341,7 @@ void Thread::complete()
        if (stack)
                stack_free(stack);
 #ifdef TLS
-       if (this != model->getInitThread() && !model->getParams()->threadsnocleanup) {
+       if (this != model->getInitThread()) {
                ASSERT(thread_current()==NULL);
                real_pthread_mutex_unlock(&mutex2);
                real_pthread_join(thread, NULL);
@@ -361,6 +360,7 @@ void Thread::complete()
  */
 Thread::Thread(thread_id_t tid) :
        parent(NULL),
+       acq_fence_cv(new ClockVector()),
        creation(NULL),
        pending(NULL),
        start_routine(NULL),
@@ -386,6 +386,7 @@ Thread::Thread(thread_id_t tid) :
  */
 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
        parent(parent),
+       acq_fence_cv(new ClockVector()),
        creation(NULL),
        pending(NULL),
        start_routine(func),
@@ -418,6 +419,7 @@ Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread
  */
 Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
        parent(parent),
+       acq_fence_cv(new ClockVector()),
        creation(NULL),
        pending(NULL),
        start_routine(NULL),
@@ -446,6 +448,8 @@ Thread::~Thread()
 {
        if (!is_complete())
                complete();
+
+       delete acq_fence_cv;
 }
 
 /** @return The thread_id_t corresponding to this Thread object. */