Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / lowlevellock.h
blob7d1913a58df0d3b0886931f90659b4fbce1d3ef0
1 /* Low level locking macros used in NPTL implementation. Stub version.
2 Copyright (C) 2002-2014 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <atomic.h>
23 /* Mutex lock counter:
24 bit 31 clear means unlocked;
25 bit 31 set means locked.
27 All code that looks at bit 31 first increases the 'number of
28 interested threads' usage counter, which is in bits 0-30.
30 All negative mutex values indicate that the mutex is still locked. */
33 static inline void
34 __generic_mutex_lock (int *mutex)
36 unsigned int v;
38 /* Bit 31 was clear, we got the mutex. (this is the fastpath). */
39 if (atomic_bit_test_set (mutex, 31) == 0)
40 return;
42 atomic_increment (mutex);
44 while (1)
46 if (atomic_bit_test_set (mutex, 31) == 0)
48 atomic_decrement (mutex);
49 return;
52 /* We have to wait now. First make sure the futex value we are
53 monitoring is truly negative (i.e. locked). */
54 v = *mutex;
55 if (v >= 0)
56 continue;
58 lll_futex_wait (mutex, v,
59 // XYZ check mutex flag
60 LLL_SHARED);
65 static inline void
66 __generic_mutex_unlock (int *mutex)
68 /* Adding 0x80000000 to the counter results in 0 if and only if
69 there are not other interested threads - we can return (this is
70 the fastpath). */
71 if (atomic_add_zero (mutex, 0x80000000))
72 return;
74 /* There are other threads waiting for this mutex, wake one of them
75 up. */
76 lll_futex_wake (mutex, 1,
77 // XYZ check mutex flag
78 LLL_SHARED);
82 #define lll_mutex_lock(futex) __generic_mutex_lock (&(futex))
83 #define lll_mutex_unlock(futex) __generic_mutex_unlock (&(futex))