+
+///Enter exclusive mode by pausing everyone else
+int pthread_pause_all() {
+ //printf("Pause ALL\n");
+ pthread_user_data_lock();
+ //printf("Pause ALL+\n");
+ //printf("Pause %p == %p\n", (void *)pthread_pause_holder, (void *)pthread_self());
+ pthread_pause_assert_owner();
+ pthread_pause_holder = pthread_self();
+ pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL);
+ //printf("Pause ALL!\n");
+ pthread_user_data_unlock();
+ return 0;
+}
+
+///Leave exclusive mode by unpausing everyone else
+int pthread_unpause_all() {
+ //printf("UnPause ALL\n");
+ pthread_user_data_lock();
+ //printf("UnPause ALL+\n");
+ pthread_pause_assert_owner();
+ pthread_pause_holder = PTHREAD_XNULL;
+ pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL);
+ //printf("UnPause ALL!\n");
+ pthread_user_data_unlock();
+ 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);
+
+ pthread_pause_enable();
+
+ //Register new thread to user data structure
+ //pthread_user_data_internal(pthread_self()); //Perhaps already done in pthread_extra_yield() and pthread_pause_enable()??
+
+ //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());
+ pthread_extra_yield();
+
+ //Run actual task
+ 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);
+}