Add pthread functions
[c11tester.git] / pthread.cc
index b5fb7ce964200750a108412c63041de7cea56c3d..706f1b75602ef6ae33e2876be5064dcd10ab18a3 100644 (file)
@@ -162,6 +162,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();
 }
@@ -246,3 +252,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;
+}