Cleanup scaffolds
authorTomas Mudrunka <tomas@mudrunka.cz>
Tue, 29 Jun 2021 14:23:39 +0000 (16:23 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Tue, 29 Jun 2021 14:23:39 +0000 (16:23 +0200)
c/pthread_extra/pthread_extra.h
c/pthread_extra/pthread_pause.c
c/pthread_extra/pthread_user_data.c
c/pthread_extra/test_pause.c

index faffb3d7165ee4897e4b80cbc032f558193ad7a8..15e4d8bd39c88fe49391c4e51ab56342092e0977 100644 (file)
@@ -28,6 +28,7 @@ pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread);
 void** pthread_user_data_ptr(pthread_t thread);
 void*  pthread_user_data_get(pthread_t thread);
 void   pthread_user_data_set(pthread_t thread, void *usr);
+void   pthread_user_data_cleanup(void * arg);
 
 // Pausing
 
@@ -36,11 +37,16 @@ void   pthread_user_data_set(pthread_t thread, void *usr);
 #define PTHREAD_XSIG_CONT (SIGRTMIN+1)
 #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal
 
+int pthread_extra_create(pthread_t *restrict thread,
+                          const pthread_attr_t *restrict attr,
+                          void *(*start_routine)(void *),
+                          void *restrict arg);
 void pthread_unpause_handler();
 void pthread_pause_handler();
 void pthread_pause_enable();
 int pthread_pause(pthread_t thread);
 int pthread_unpause(pthread_t thread);
+int pthread_pause_reschedule(pthread_t thread);
 
 // Message queues
 
index 1eacdf498d049aa2f50e73219f0042375612b11d..c0d43507d12702e4b6174d99d299e8cbbb3700df 100644 (file)
@@ -7,6 +7,8 @@
 #include <unistd.h>
 #include <sys/resource.h>
 //#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
 //#include <sys/time.h>
 
 void pthread_pause_handler() {
@@ -55,16 +57,62 @@ void pthread_pause_disable() {
        pthread_sigmask(SIG_BLOCK, &sigset, NULL);
 }
 
+int pthread_pause_reschedule(pthread_t thread) {
+       //Send signal to initiate pause handler
+       while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
+       return 0;
+}
+
 int pthread_pause(pthread_t thread) {
        //Set thread as paused and notify it via signal (wait when queue full)
        pthread_user_data_internal(thread)->running = 0;
-       while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
+       pthread_pause_reschedule(thread);
        return 0;
 }
 
 int pthread_unpause(pthread_t thread) {
        //Set thread as running and notify it via signal (wait when queue full)
        pthread_user_data_internal(thread)->running = 1;
-       while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
+       pthread_pause_reschedule(thread);
        return 0;
 }
+
+
+
+// Wrappers ///////////////////////////////////////////////////////////
+
+
+typedef struct pthread_extra_wrapper_t {
+       void *(*start_routine)(void *);
+       void *restrict arg;
+} pthread_extra_wrapper_t;
+
+void *pthread_extra_thread_wrapper(void *arg) {
+       pthread_extra_wrapper_t task = *((pthread_extra_wrapper_t*)arg);
+       free(arg);
+
+       //Register new thread to user data structure
+       pthread_user_data_internal(pthread_self());
+
+       //TODO: user_data should do this automaticaly?
+       pthread_cleanup_push(pthread_user_data_cleanup, (void *)pthread_self());
+
+       //Check if we should be running according to pthread_pause sub-scheduler
+       pthread_pause_reschedule(pthread_self());
+
+       return task.start_routine(task.arg);
+
+       pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro
+}
+
+int pthread_extra_create(pthread_t *restrict thread,
+                          const pthread_attr_t *restrict attr,
+                          void *(*start_routine)(void *),
+                          void *restrict arg) {
+
+       pthread_extra_wrapper_t *task = malloc(sizeof(pthread_extra_wrapper_t));
+       assert(task != NULL);
+       task->start_routine=start_routine;
+       task->arg=arg;
+       return pthread_create(thread, attr, pthread_extra_thread_wrapper, task);
+}
index b86c4a3291a300247e7bd70046eb6d22682fa9f5..26204a870aa03c55d1d7499266ccc827f10cc4df 100644 (file)
@@ -1,5 +1,6 @@
 #define __PTHREAD_EXTRA_INTERNAL
 
+#include <assert.h>
 #include <pthread.h>
 #include <pthread_extra.h>
 
@@ -17,8 +18,9 @@ pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) {
        pthread_t i;
        for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) {
                if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) {
-                       pthread_user_data[i].tid = thread;
+                       //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;
@@ -41,3 +43,17 @@ void pthread_user_data_set(pthread_t thread, void *usr) {
 void* pthread_user_data_get(pthread_t thread) {
        return *(pthread_user_data_ptr(thread));
 }
+
+//Remove canceled thread from user data registry
+int pthread_user_data_remove(pthread_t thread) {
+       //FIXME: not implemented yet!
+       (void) thread;
+       return 0;
+}
+
+//User data cleanup handler to be registered with pthread_cleanup_push()
+void pthread_user_data_cleanup( void * arg ) {
+       pthread_t t = (pthread_t)arg;
+       assert(pthread_equal(t, pthread_self()) && "Pthread_self() is not working in cleanup context!");
+       pthread_user_data_remove(t);
+}
index 467328940c87c786f61b024436de2506a4fa8dc6..f17be8a3de72c378780be4903f8e7ea8be9eb2b1 100644 (file)
@@ -20,8 +20,8 @@ int main() {
        pthread_t a, b;
        pthread_pause_enable(); //Will get inherited by all threads from now on
        //That way you can be sure it is pausable immediately
-       pthread_create(&a, NULL, thread_test, " A");
-       pthread_create(&b, NULL, thread_test, " B");
+       pthread_extra_create(&a, NULL, thread_test, " A");
+       pthread_extra_create(&b, NULL, thread_test, " B");
        //sleep(1);
 
        //printf("OK\n");
This page took 0.238919 seconds and 4 git commands to generate.