lib: c: pthread: implement pthread_self

This commit is contained in:
2026-03-22 19:08:42 +00:00
parent 33888e364b
commit dd46a378b3
3 changed files with 40 additions and 3 deletions

View File

@@ -0,0 +1,36 @@
#include "pthread.h"
#include <mango/task.h>
#include <mango/types.h>
#include <string.h>
#include <sys/mman.h>
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();
}