From ea77094b2449099039430f34aed5b70a08b01482 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 29 Nov 2009 11:17:53 +0000 Subject: [PATCH] Replaced the hown-grown recursive lock implementation by a shared lazy recursive lock. I haven't investigated it closer, but the previous implementation was even broken -- "strace /bin/true" showed two release_sem() calls, but no acquire_sem(). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34342 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/stdlib/exit.c | 40 ++++++++------------------ 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/system/libroot/posix/stdlib/exit.c b/src/system/libroot/posix/stdlib/exit.c index 40873ea45e..8777d29f91 100644 --- a/src/system/libroot/posix/stdlib/exit.c +++ b/src/system/libroot/posix/stdlib/exit.c @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -24,44 +25,26 @@ extern void _IO_cleanup(void); extern void _thread_do_exit_notification(void); struct exit_stack_info { - void (*exit_stack[ATEXIT_MAX])(void); - int32 stack_size; - sem_id lock; - vint32 lock_count; - thread_id lock_owner; - size_t recursion_count; + void (*exit_stack[ATEXIT_MAX])(void); + int32 stack_size; + lazy_recursive_lock lock; }; -static struct exit_stack_info sExitStackInfo = { {}, 0, -1, 0, -1, 0 }; +static struct exit_stack_info sExitStackInfo = { {}, 0, {} }; -static void +static void inline _exit_stack_lock() { - thread_id self = find_thread(NULL); - if (self != sExitStackInfo.lock_owner) { - if (atomic_add(&sExitStackInfo.lock_count, 1) > 0) { - while (acquire_sem(sExitStackInfo.lock) != B_OK) - ; - } - sExitStackInfo.lock_owner = self; - } - sExitStackInfo.recursion_count++; + lazy_recursive_lock_lock(&sExitStackInfo.lock); } -static void +static void inline _exit_stack_unlock() { - if (sExitStackInfo.lock_owner != find_thread(NULL)) - debugger("exit stack lock not owned"); - - if (sExitStackInfo.recursion_count-- == 1) { - sExitStackInfo.lock_owner = -1; - if (atomic_add(&sExitStackInfo.lock_count, -1) == 1) - release_sem(sExitStackInfo.lock); - } + lazy_recursive_lock_unlock(&sExitStackInfo.lock); } @@ -103,8 +86,9 @@ _call_atexit_hooks_for_range(addr_t start, addr_t size) void __init_exit_stack_lock(void) { - sExitStackInfo.lock = create_sem(0, "exit stack lock"); - if (sExitStackInfo.lock < 0) + status_t error = lazy_recursive_lock_init(&sExitStackInfo.lock, + "exit stack lock"); + if (error != B_OK) debugger("failed to create exit stack lock"); }