diff --git a/headers/private/libroot/libroot_private.h b/headers/private/libroot/libroot_private.h index 20d035a10e..eaa9bf2276 100644 --- a/headers/private/libroot/libroot_private.h +++ b/headers/private/libroot/libroot_private.h @@ -41,6 +41,8 @@ void __init_pwd_backend(void); void __reinit_pwd_backend_after_fork(void); void* __arch_get_caller(void); +void __init_pthread(void); + #ifdef __cplusplus } diff --git a/src/system/libroot/libroot_init.c b/src/system/libroot/libroot_init.c index 7739b01828..4b65efa306 100644 --- a/src/system/libroot/libroot_init.c +++ b/src/system/libroot/libroot_init.c @@ -60,6 +60,7 @@ initialize_before(image_id imageID) __init_env(__gRuntimeLoader->program_args); __init_heap_post_env(); __init_pwd_backend(); + __init_pthread(); } diff --git a/src/system/libroot/os/thread.c b/src/system/libroot/os/thread.c index fc014204f5..7b11cd70d2 100644 --- a/src/system/libroot/os/thread.c +++ b/src/system/libroot/os/thread.c @@ -99,7 +99,10 @@ spawn_thread(thread_func entry, const char *name, int32 priority, void *data) attributes.stack_address = NULL; attributes.stack_size = 0; - return _kern_spawn_thread(&attributes); + thread->id = _kern_spawn_thread(&attributes); + if (thread->id < 0) + free(thread); + return thread->id; } diff --git a/src/system/libroot/posix/pthread/pthread.c b/src/system/libroot/posix/pthread/pthread.c index 5dcabc03b3..b67408c1c9 100644 --- a/src/system/libroot/posix/pthread/pthread.c +++ b/src/system/libroot/posix/pthread/pthread.c @@ -5,7 +5,8 @@ */ -#include "pthread_private.h" +#include +#include #include #include @@ -93,6 +94,13 @@ __allocate_pthread(void *data) } +void +__init_pthread(void) +{ + sMainThread.id = find_thread(NULL); +} + + // #pragma mark - public API