]>
Commit | Line | Data |
---|---|---|
a334319f | 1 | /* Copyright (C) 2002 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
19 | ||
20 | #include <atomic.h> | |
21 | ||
22 | ||
23 | /* Implement generic mutex. Basic futex syscall support is required: | |
24 | ||
25 | lll_futex_wait(futex, value) - call sys_futex with FUTEX_WAIT | |
26 | and third parameter VALUE | |
27 | ||
28 | lll_futex_wake(futex, value) - call sys_futex with FUTEX_WAKE | |
29 | and third parameter VALUE | |
30 | */ | |
31 | ||
32 | ||
33 | /* Mutex lock counter: | |
34 | bit 31 clear means unlocked; | |
35 | bit 31 set means locked. | |
36 | ||
37 | All code that looks at bit 31 first increases the 'number of | |
38 | interested threads' usage counter, which is in bits 0-30. | |
39 | ||
40 | All negative mutex values indicate that the mutex is still locked. */ | |
41 | ||
42 | ||
43 | static inline void | |
44 | __generic_mutex_lock (int *mutex) | |
45 | { | |
46 | unsigned int v; | |
47 | ||
48 | /* Bit 31 was clear, we got the mutex. (this is the fastpath). */ | |
49 | if (atomic_bit_test_set (mutex, 31) == 0) | |
50 | return; | |
51 | ||
52 | atomic_increment (mutex); | |
53 | ||
54 | while (1) | |
55 | { | |
56 | if (atomic_bit_test_set (mutex, 31) == 0) | |
57 | { | |
58 | atomic_decrement (mutex); | |
59 | return; | |
60 | } | |
61 | ||
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; | |
67 | ||
68 | lll_futex_wait (mutex, v); | |
69 | } | |
70 | } | |
71 | ||
72 | ||
73 | static inline void | |
74 | __generic_mutex_unlock (int *mutex) | |
75 | { | |
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). */ | |
a334319f | 79 | if (atomic_add_zero (0x80000000, mutex)) |
76a50749 UD |
80 | return; |
81 | ||
82 | /* There are other threads waiting for this mutex, wake one of them | |
83 | up. */ | |
84 | lll_futex_wake (mutex, 1); | |
85 | } | |
86 | ||
87 | ||
88 | #define lll_mutex_lock(futex) __generic_mutex_lock (&(futex)) | |
89 | #define lll_mutex_unlock(futex) __generic_mutex_unlock (&(futex)) |