From e4103b1b92852e3fc293c8e3b8857e5bd58e8dc3 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Tue, 25 Dec 2018 20:52:57 +0100 Subject: [PATCH] libroot: Introduce thread specific heap init/exit hooks. This allows heap implementations to initialize and clean up any thread specific structures. The current default hoard heap does not use these. Note that the thread exit hook will not be called for the main thread as the heap may be needed during process termination (__cxa_finalize for example). Change-Id: I703fbd34dec0d9029d619a2125c5b19d8c1933aa Reviewed-on: https://review.haiku-os.org/799 Reviewed-by: waddlesplash --- headers/private/libroot/libroot_private.h | 2 ++ src/system/libroot/os/thread.c | 7 +++++-- src/system/libroot/posix/malloc/wrapper.cpp | 12 ++++++++++++ .../libroot/posix/malloc_debug/malloc_debug_api.cpp | 12 ++++++++++++ src/system/libroot/posix/pthread/pthread.cpp | 2 ++ src/system/libroot/stubbed/libroot_stubs.c | 2 ++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/headers/private/libroot/libroot_private.h b/headers/private/libroot/libroot_private.h index 79ec524e6f..caa9be1cce 100644 --- a/headers/private/libroot/libroot_private.h +++ b/headers/private/libroot/libroot_private.h @@ -39,6 +39,8 @@ void __heap_terminate_after(void); void __heap_before_fork(void); void __heap_after_fork_child(void); void __heap_after_fork_parent(void); +void __heap_thread_init(void); +void __heap_thread_exit(void); void __init_time(addr_t commPageTable); void __arch_init_time(struct real_time_data *data, bool setDefaults); diff --git a/src/system/libroot/os/thread.c b/src/system/libroot/os/thread.c index 41e105e4c5..c92ce075f3 100644 --- a/src/system/libroot/os/thread.c +++ b/src/system/libroot/os/thread.c @@ -37,11 +37,13 @@ thread_entry(void* _entry, void* _thread) { thread_func entry = (thread_func)_entry; pthread_thread* thread = (pthread_thread*)_thread; - status_t returnCode; - returnCode = entry(thread->entry_argument); + __heap_thread_init(); + + status_t returnCode = entry(thread->entry_argument); _thread_do_exit_work(); + __heap_thread_exit(); return returnCode; } @@ -168,6 +170,7 @@ void exit_thread(status_t status) { _thread_do_exit_work(); + __heap_thread_exit(); _kern_exit_thread(status); } diff --git a/src/system/libroot/posix/malloc/wrapper.cpp b/src/system/libroot/posix/malloc/wrapper.cpp index 4465f512ea..ead5d22b4b 100644 --- a/src/system/libroot/posix/malloc/wrapper.cpp +++ b/src/system/libroot/posix/malloc/wrapper.cpp @@ -285,6 +285,18 @@ __heap_after_fork_parent(void) } +extern "C" void +__heap_thread_init(void) +{ +} + + +extern "C" void +__heap_thread_exit(void) +{ +} + + // #pragma mark - public functions diff --git a/src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp b/src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp index bf38d4c9e7..4a0f87022a 100644 --- a/src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp +++ b/src/system/libroot/posix/malloc_debug/malloc_debug_api.cpp @@ -220,6 +220,18 @@ __heap_after_fork_parent(void) } +extern "C" void +__heap_thread_init(void) +{ +} + + +extern "C" void +__heap_thread_exit(void) +{ +} + + // #pragma mark - Public API diff --git a/src/system/libroot/posix/pthread/pthread.cpp b/src/system/libroot/posix/pthread/pthread.cpp index 967fb70024..6bd62f9f36 100644 --- a/src/system/libroot/posix/pthread/pthread.cpp +++ b/src/system/libroot/posix/pthread/pthread.cpp @@ -40,6 +40,8 @@ pthread_thread_entry(void*, void* _thread) { pthread_thread* thread = (pthread_thread*)_thread; + __heap_thread_init(); + pthread_exit(thread->entry(thread->entry_argument)); return 0; } diff --git a/src/system/libroot/stubbed/libroot_stubs.c b/src/system/libroot/stubbed/libroot_stubs.c index 370a85126b..cd05405dcf 100644 --- a/src/system/libroot/stubbed/libroot_stubs.c +++ b/src/system/libroot/stubbed/libroot_stubs.c @@ -880,6 +880,8 @@ void __heap_after_fork_child() {} void __heap_after_fork_parent() {} void __heap_before_fork() {} void __heap_terminate_after() {} +void __heap_thread_init() {} +void __heap_thread_exit() {} void __hypot() {} void __hypotf() {} void __hypotl() {}