Thread registry is now protected by mutex
authorTomas Mudrunka <tomas@mudrunka.cz>
Wed, 30 Jun 2021 14:36:43 +0000 (16:36 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Wed, 30 Jun 2021 14:36:43 +0000 (16:36 +0200)
c/pthread_extra/pthread_extra.h
c/pthread_extra/pthread_pause.c
c/pthread_extra/pthread_user_data.c

index ca5514d26f6bf04992b88f6b11c54b36e33d032f..efba75e6e46427175e3d9cba51dcede073961e79 100644 (file)
@@ -45,6 +45,7 @@ int pthread_extra_create(pthread_t *restrict thread,
 void pthread_unpause_handler();
 void pthread_pause_handler();
 void pthread_pause_enable();
+void pthread_pause_disable();
 int pthread_pause(pthread_t thread);
 int pthread_unpause(pthread_t thread);
 int pthread_pause_reschedule(pthread_t thread);
index 409d57038ab8d6b69c7233e6ddffedbce879c079..c6b96f6d8a0518907c477971c7f63da366cabaa7 100644 (file)
@@ -37,7 +37,7 @@ void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) {
 
 void pthread_pause_enable() {
        //Add thread to internal registry
-       pthread_user_data_internal(pthread_self());
+       //pthread_user_data_internal(pthread_self());
 
        //Nesting signals too deep is not good for stack
        //You can get runtime stats using following command:
@@ -66,7 +66,7 @@ void pthread_pause_enable() {
 
 void pthread_pause_disable() {
        //Add thread to internal registry
-       pthread_user_data_internal(pthread_self());
+       //pthread_user_data_internal(pthread_self());
 
        //Block signal
        sigset_t sigset;
index 26204a870aa03c55d1d7499266ccc827f10cc4df..e0ba979be0c90751aac8e92429e31f162d6290d3 100644 (file)
@@ -7,24 +7,43 @@
 //Static array with user data for all thread handles
 //TODO: perhaps use something more sophisticated like linked list?
 pthread_user_data_internal_t pthread_user_data[PTHREAD_XTHREADS_MAX+1] = {{.tid=PTHREAD_XNULL}};
+pthread_mutex_t pthread_user_data_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+int pthread_user_data_lock() {
+       pthread_pause_disable();
+       return pthread_mutex_lock(&pthread_user_data_mutex);
+}
+
+int pthread_user_data_unlock() {
+       pthread_mutex_unlock(&pthread_user_data_mutex);
+       pthread_pause_enable();
+       return 0;
+}
 
 //Get pointer to internal record tied to specified thread
 pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) {
        //Return NULL if requested thread handle is NULL
        if(pthread_equal(thread, PTHREAD_XNULL)) return NULL;
 
+       //Lock
+       pthread_user_data_lock(); //TODO: maybe use R/W locking scheme?
+
        //Find if the thread is already registered, add it if not
-       //FIXME: recycle slots of destroyed threads!!!
+       //FIXME: detect overflow of array and cause assert fail!
+       //FIXME: recycle slots of destroyed threads!!! signaled using pthread_user_data_remove();
        pthread_t i;
        for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) {
                if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) {
-                       //FIXME: Should be locking for addition to the array!!!!
                        pthread_user_data[i+1].tid = PTHREAD_XNULL;
                        pthread_user_data[i].tid = thread;
                        break;
                }
                if(pthread_equal(pthread_user_data[i].tid, thread)) break;
        }
+
+       //UnLock
+       pthread_user_data_unlock();
+
        //Return pointer
        return &pthread_user_data[i];
 }
This page took 0.346565 seconds and 4 git commands to generate.