libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / system / libroot / os / locks / recursive_lock.cpp
blob6401eb7a307f27138d57b6fe16e2027f06ec06ec
1 /*
2 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3 * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
5 * Distributed under the terms of the MIT License.
7 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
8 * Distributed under the terms of the NewOS License.
9 */
12 #include <locks.h>
14 #include <OS.h>
17 // #pragma mark - recursive lock
20 int32
21 __recursive_lock_get_recursion(recursive_lock *lock)
23 if (lock->holder == find_thread(NULL))
24 return lock->recursion;
26 return -1;
30 void
31 __recursive_lock_init(recursive_lock *lock, const char *name)
33 recursive_lock_init_etc(lock, name, 0);
37 void
38 __recursive_lock_init_etc(recursive_lock *lock, const char *name, uint32 flags)
40 lock->holder = -1;
41 lock->recursion = 0;
42 mutex_init_etc(&lock->lock, name, flags);
46 void
47 __recursive_lock_destroy(recursive_lock *lock)
49 if (lock == NULL)
50 return;
52 mutex_destroy(&lock->lock);
56 status_t
57 __recursive_lock_lock(recursive_lock *lock)
59 thread_id thread = find_thread(NULL);
61 if (thread != lock->holder) {
62 mutex_lock(&lock->lock);
63 lock->holder = thread;
66 lock->recursion++;
67 return B_OK;
71 void
72 __recursive_lock_unlock(recursive_lock *lock)
74 if (find_thread(NULL) != lock->holder) {
75 debugger("recursive_lock unlocked by non-holder thread!\n");
76 return;
79 if (--lock->recursion == 0) {
80 lock->holder = -1;
81 mutex_unlock(&lock->lock);