#include "pthread.h" #include #include #include #include static struct __pthread main_thread = {0}; struct __pthread *pthread_self(void) { static int init = 0; if (!init) { kern_handle_t self_handle = KERN_HANDLE_INVALID; kern_status_t status = thread_self(&self_handle); if (status != KERN_OK) { /* TODO set an errno value in a way that doesn't result * in a recursive call to pthread_self */ return NULL; } struct __pthread *self = &main_thread; self->thr_self = self; self->thr_handle = self_handle; self->thr_map_base = self; self->thr_map_size = sizeof *self; thread_config_set( self_handle, THREAD_CFG_GSBASE, &self, sizeof(void *)); init = 1; return self; } return __pthread_self(); }