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
#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
#include <unistd.h>
#include <sys/resource.h>
//#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
//#include <sys/time.h>
void pthread_pause_handler() {
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);
+}
#define __PTHREAD_EXTRA_INTERNAL
+#include <assert.h>
#include <pthread.h>
#include <pthread_extra.h>
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;
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);
+}
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");