From af370c702a1374fd8997cedd5f2ce5d5f153dcb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Mon, 7 Jul 2008 16:17:34 +0000 Subject: [PATCH] * Added a recursive_lock_trylock() function. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26297 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/fs_shell/fssh_api_wrapper.h | 1 + headers/private/fs_shell/fssh_lock.h | 1 + headers/private/kernel/lock.h | 1 + src/system/kernel/lock.cpp | 27 ++++++++++++++++++++- src/tools/fs_shell/lock.cpp | 18 ++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/headers/private/fs_shell/fssh_api_wrapper.h b/headers/private/fs_shell/fssh_api_wrapper.h index 9daea616b5..bd4ac10d60 100644 --- a/headers/private/fs_shell/fssh_api_wrapper.h +++ b/headers/private/fs_shell/fssh_api_wrapper.h @@ -975,6 +975,7 @@ #define recursive_lock_init_etc fssh_recursive_lock_init_etc #define recursive_lock_destroy fssh_recursive_lock_destroy #define recursive_lock_lock fssh_recursive_lock_lock +#define recursive_lock_trylock fssh_recursive_lock_trylock #define recursive_lock_unlock fssh_recursive_lock_unlock #define recursive_lock_get_recursion fssh_recursive_lock_get_recursion diff --git a/headers/private/fs_shell/fssh_lock.h b/headers/private/fs_shell/fssh_lock.h index c44c84ad68..8e591d5a2c 100644 --- a/headers/private/fs_shell/fssh_lock.h +++ b/headers/private/fs_shell/fssh_lock.h @@ -55,6 +55,7 @@ extern void fssh_recursive_lock_init_etc(fssh_recursive_lock *lock, const char * uint32_t flags); extern void fssh_recursive_lock_destroy(fssh_recursive_lock *lock); extern fssh_status_t fssh_recursive_lock_lock(fssh_recursive_lock *lock); +extern fssh_status_t fssh_recursive_lock_trylock(fssh_recursive_lock *lock); extern void fssh_recursive_lock_unlock(fssh_recursive_lock *lock); extern int32_t fssh_recursive_lock_get_recursion(fssh_recursive_lock *lock); diff --git a/headers/private/kernel/lock.h b/headers/private/kernel/lock.h index a6dcb625df..308dfeb323 100644 --- a/headers/private/kernel/lock.h +++ b/headers/private/kernel/lock.h @@ -84,6 +84,7 @@ extern void recursive_lock_init_etc(recursive_lock *lock, const char *name, uint32 flags); extern void recursive_lock_destroy(recursive_lock *lock); extern status_t recursive_lock_lock(recursive_lock *lock); +extern status_t recursive_lock_trylock(recursive_lock *lock); extern void recursive_lock_unlock(recursive_lock *lock); extern int32 recursive_lock_get_recursion(recursive_lock *lock); diff --git a/src/system/kernel/lock.cpp b/src/system/kernel/lock.cpp index 6a8371dd11..f8165249f0 100644 --- a/src/system/kernel/lock.cpp +++ b/src/system/kernel/lock.cpp @@ -92,12 +92,37 @@ recursive_lock_lock(recursive_lock *lock) { thread_id thread = thread_get_current_thread_id(); + if (!kernel_startup && !are_interrupts_enabled()) { + panic("recursive_lock_lock: called with interrupts disabled for lock " + "%p (\"%s\")\n", lock, lock->lock.name); + } + + if (thread != RECURSIVE_LOCK_HOLDER(lock)) { + mutex_lock(&lock->lock); +#ifndef KDEBUG + lock->holder = thread; +#endif + } + + lock->recursion++; + return B_OK; +} + + +status_t +recursive_lock_trylock(recursive_lock *lock) +{ + thread_id thread = thread_get_current_thread_id(); + if (!kernel_startup && !are_interrupts_enabled()) panic("recursive_lock_lock: called with interrupts disabled for lock " "%p (\"%s\")\n", lock, lock->lock.name); if (thread != RECURSIVE_LOCK_HOLDER(lock)) { - mutex_lock(&lock->lock); + status_t status = mutex_trylock(&lock->lock); + if (status != B_OK) + return status; + #ifndef KDEBUG lock->holder = thread; #endif diff --git a/src/tools/fs_shell/lock.cpp b/src/tools/fs_shell/lock.cpp index 7e223f01dd..ae57764158 100644 --- a/src/tools/fs_shell/lock.cpp +++ b/src/tools/fs_shell/lock.cpp @@ -71,6 +71,24 @@ fssh_recursive_lock_lock(fssh_recursive_lock *lock) } +extern "C" fssh_status_t +fssh_recursive_lock_trylock(fssh_recursive_lock *lock) +{ + fssh_thread_id thread = fssh_find_thread(NULL); + + if (thread != lock->holder) { + fssh_status_t status = fssh_acquire_sem_etc(lock->sem, 1, + FSSH_B_RELATIVE_TIMEOUT, 0); + if (status < FSSH_B_OK) + return status; + + lock->holder = thread; + } + lock->recursion++; + return FSSH_B_OK; +} + + extern "C" void fssh_recursive_lock_unlock(fssh_recursive_lock *lock) {