* NEWS: Update 2.4 items.
[glibc.git] / nptl / lowlevellock.h
blob338da399908e182ad2888aef5cdf683e53713c9a
1 /* Low level locking macros used in NPTL implementation. Stub version.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <atomic.h>
24 /* Implement generic mutex. Basic futex syscall support is required:
26 lll_futex_wait(futex, value) - call sys_futex with FUTEX_WAIT
27 and third parameter VALUE
29 lll_futex_wake(futex, value) - call sys_futex with FUTEX_WAKE
30 and third parameter VALUE
34 /* Mutex lock counter:
35 bit 31 clear means unlocked;
36 bit 31 set means locked.
38 All code that looks at bit 31 first increases the 'number of
39 interested threads' usage counter, which is in bits 0-30.
41 All negative mutex values indicate that the mutex is still locked. */
44 static inline void
45 __generic_mutex_lock (int *mutex)
47 unsigned int v;
49 /* Bit 31 was clear, we got the mutex. (this is the fastpath). */
50 if (atomic_bit_test_set (mutex, 31) == 0)
51 return;
53 atomic_increment (mutex);
55 while (1)
57 if (atomic_bit_test_set (mutex, 31) == 0)
59 atomic_decrement (mutex);
60 return;
63 /* We have to wait now. First make sure the futex value we are
64 monitoring is truly negative (i.e. locked). */
65 v = *mutex;
66 if (v >= 0)
67 continue;
69 lll_futex_wait (mutex, v);
74 static inline void
75 __generic_mutex_unlock (int *mutex)
77 /* Adding 0x80000000 to the counter results in 0 if and only if
78 there are not other interested threads - we can return (this is
79 the fastpath). */
80 if (atomic_add_zero (mutex, 0x80000000))
81 return;
83 /* There are other threads waiting for this mutex, wake one of them
84 up. */
85 lll_futex_wake (mutex, 1);
89 #define lll_mutex_lock(futex) __generic_mutex_lock (&(futex))
90 #define lll_mutex_unlock(futex) __generic_mutex_unlock (&(futex))