(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / generic / lowlevellock.h
blob9cffca83e67e3afac708422b7955fb2e92beac32
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <atomic.h>
23 /* Implement generic mutex. Basic futex syscall support is required:
25 lll_futex_wait(futex, value) - call sys_futex with FUTEX_WAIT
26 and third parameter VALUE
28 lll_futex_wake(futex, value) - call sys_futex with FUTEX_WAKE
29 and third parameter VALUE
33 /* Mutex lock counter:
34 bit 31 clear means unlocked;
35 bit 31 set means locked.
37 All code that looks at bit 31 first increases the 'number of
38 interested threads' usage counter, which is in bits 0-30.
40 All negative mutex values indicate that the mutex is still locked. */
43 static inline void
44 __generic_mutex_lock (int *mutex)
46 unsigned int v;
48 /* Bit 31 was clear, we got the mutex. (this is the fastpath). */
49 if (atomic_bit_test_set (mutex, 31) == 0)
50 return;
52 atomic_increment (mutex);
54 while (1)
56 if (atomic_bit_test_set (mutex, 31) == 0)
58 atomic_decrement (mutex);
59 return;
62 /* We have to wait now. First make sure the futex value we are
63 monitoring is truly negative (i.e. locked). */
64 v = *mutex;
65 if (v >= 0)
66 continue;
68 lll_futex_wait (mutex, v);
73 static inline void
74 __generic_mutex_unlock (int *mutex)
76 /* Adding 0x80000000 to the counter results in 0 if and only if
77 there are not other interested threads - we can return (this is
78 the fastpath). */
79 if (atomic_add_zero (0x80000000, mutex))
80 return;
82 /* There are other threads waiting for this mutex, wake one of them
83 up. */
84 lll_futex_wake (mutex, 1);
88 #define lll_mutex_lock(futex) __generic_mutex_lock (&(futex))
89 #define lll_mutex_unlock(futex) __generic_mutex_unlock (&(futex))