small changes
[c11tester.git] / pthread.cc
index e453f1b27e45b2188c73231fbac3d436cd15ac6a..e95fb37a3c9b079599b19ca90dd9f00d74fd66b3 100644 (file)
@@ -25,7 +25,7 @@ int pthread_create(pthread_t *t, const pthread_attr_t * attr,
        struct pthread_params params = { start_routine, arg };
 
        /* seq_cst is just a 'don't care' parameter */
-       model->switch_to_master(new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params));
+       model->switch_thread(new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params));
 
        return 0;
 }
@@ -34,7 +34,7 @@ int pthread_join(pthread_t t, void **value_ptr) {
        ModelExecution *execution = model->get_execution();
        Thread *th = execution->get_pthread(t);
 
-       model->switch_to_master(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
+       model->switch_thread(new ModelAction(PTHREAD_JOIN, std::memory_order_seq_cst, th, id_to_int(th->get_id())));
 
        if ( value_ptr ) {
                // store return value
@@ -59,18 +59,23 @@ int sched_yield() {
 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));
+       model->switch_thread(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 *) {
+int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t * attr) {
        if (!model) {
                snapshot_system_init(10000, 1024, 1024, 40000);
                model = new ModelChecker();
                model->startChecker();
        }
-       cdsc::snapmutex *m = new cdsc::snapmutex();
+
+       int mutex_type = PTHREAD_MUTEX_DEFAULT;
+       if (attr != NULL)
+               pthread_mutexattr_gettype(attr, &mutex_type);
+
+       cdsc::snapmutex *m = new cdsc::snapmutex(mutex_type);
 
        ModelExecution *execution = model->get_execution();
        execution->getMutexMap()->put(p_mutex, m);
@@ -160,6 +165,12 @@ int pthread_mutex_timedlock (pthread_mutex_t *__restrict p_mutex,
 }
 
 pthread_t pthread_self() {
+       if (!model) {
+               snapshot_system_init(10000, 1024, 1024, 40000);
+               model = new ModelChecker();
+               model->startChecker();
+       }
+
        Thread* th = model->get_current_thread();
        return (pthread_t)th->get_id();
 }
@@ -244,3 +255,37 @@ int pthread_cond_destroy(pthread_cond_t *p_cond) {
        }
        return 0;
 }
+
+/* https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c */
+int pthread_getattr_np(pthread_t t, pthread_attr_t *attr)
+{
+       ModelExecution *execution = model->get_execution();
+       Thread *th = execution->get_pthread(t);
+
+       struct pthread_attr *iattr = (struct pthread_attr *) attr;
+
+       /* The sizes are subject to alignment.  */
+       if (th != NULL) {
+#if _STACK_GROWS_DOWN
+               ASSERT(false);
+#else
+               iattr->stackaddr = (char *) th->get_stack_addr();
+#endif
+
+       } else {
+               ASSERT(false);
+       }
+
+       return 0;
+}
+
+int pthread_setname_np(pthread_t t, const char *name)
+{
+       ModelExecution *execution = model->get_execution();
+       Thread *th = execution->get_pthread(t);
+
+       if (th != NULL)
+               return 0;
+
+       return 1;
+}