Update.
[platform/upstream/glibc.git] / nptl / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
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 Libr    \ary; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
22
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <atomic.h>
27
28
29 #define __NR_futex              221
30 #define FUTEX_WAIT              0
31 #define FUTEX_WAKE              1
32 #define FUTEX_REQUEUE           3
33
34 /* Initializer for compatibility lock.  */
35 #define LLL_MUTEX_LOCK_INITIALIZER (0)
36
37 #define lll_futex_wait(futexp, val) \
38   ({                                                                          \
39     INTERNAL_SYSCALL_DECL (__err);                                            \
40     long int __ret;                                                           \
41                                                                               \
42     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
43                               (futexp), FUTEX_WAIT, (val), 0);                \
44     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
45   })
46
47 #define lll_futex_timed_wait(futexp, val, timespec) \
48   ({                                                                          \
49     INTERNAL_SYSCALL_DECL (__err);                                            \
50     long int __ret;                                                           \
51                                                                               \
52     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
53                               (futexp), FUTEX_WAIT, (val), (timespec));       \
54     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
55   })
56
57 #define lll_futex_wake(futexp, nr) \
58   ({                                                                          \
59     INTERNAL_SYSCALL_DECL (__err);                                            \
60     long int __ret;                                                           \
61                                                                               \
62     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
63                               (futexp), FUTEX_WAKE, (nr), 0);                 \
64     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
65   })
66
67 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex) \
68   ({                                                                          \
69     INTERNAL_SYSCALL_DECL (__err);                                            \
70     long int __ret;                                                           \
71                                                                               \
72     __ret = INTERNAL_SYSCALL (futex, __err, 5,                                \
73                               (futexp), FUTEX_REQUEUE, (nr_wake), (nr_move),  \
74                               (mutex));                                       \
75     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
76   })
77
78 #ifdef UP
79 # define __lll_acq_instr        ""
80 # define __lll_rel_instr        ""
81 #else
82 # define __lll_acq_instr        "isync"
83 # define __lll_rel_instr        "sync"
84 #endif
85
86 /* Set *futex to 1 if it is 0, atomically.  Returns the old value */
87 #define __lll_trylock(futex) \
88   ({ int __val;                                                               \
89      __asm __volatile ("1:      lwarx   %0,0,%2\n"                            \
90                        "        cmpwi   0,%0,0\n"                             \
91                        "        bne     2f\n"                                 \
92                        "        stwcx.  %3,0,%2\n"                            \
93                        "        bne-    1b\n"                                 \
94                        "2:      " __lll_acq_instr                             \
95                        : "=&r" (__val), "=m" (*futex)                         \
96                        : "r" (futex), "r" (1), "1" (*futex)                   \
97                        : "cr0", "memory");                                    \
98      __val;                                                                   \
99   })
100
101 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
102
103 /* Set *futex to 2 if it is 0, atomically.  Returns the old value */
104 #define __lll_cond_trylock(futex) \
105   ({ int __val;                                                               \
106      __asm __volatile ("1:      lwarx   %0,0,%2\n"                            \
107                        "        cmpwi   0,%0,0\n"                             \
108                        "        bne     2f\n"                                 \
109                        "        stwcx.  %3,0,%2\n"                            \
110                        "        bne-    1b\n"                                 \
111                        "2:      " __lll_acq_instr                             \
112                        : "=&r" (__val), "=m" (*futex)                         \
113                        : "r" (futex), "r" (2), "1" (*futex)                   \
114                        : "cr0", "memory");                                    \
115      __val;                                                                   \
116   })
117 #define lll_mutex_cond_trylock(lock)    __lll_cond_trylock (&(lock))
118
119
120 extern void __lll_lock_wait (int *futex) attribute_hidden;
121
122 #define lll_mutex_lock(lock) \
123   (void) ({                                                                   \
124     int *__futex = &(lock);                                                   \
125     if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
126                           0) != 0)                                            \
127       __lll_lock_wait (__futex);                                              \
128   })
129
130 #define lll_mutex_cond_lock(lock) \
131   (void) ({                                                                   \
132     int *__futex = &(lock);                                                   \
133     if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
134                           0) != 0)                                            \
135       __lll_lock_wait (__futex);                                              \
136   })
137
138 extern int __lll_timedlock_wait
139   (int *futex, const struct timespec *) attribute_hidden;
140
141 #define lll_mutex_timedlock(lock, abstime) \
142   ({                                                                          \
143     int *__futex = &(lock);                                                   \
144     int __val = 0;                                                            \
145     if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
146                           0) != 0)                                            \
147       __val = __lll_timedlock_wait (__futex, abstime);                        \
148     __val;                                                                    \
149   })
150
151 #define lll_mutex_unlock(lock) \
152   ((void) ({                                                                  \
153     int *__futex = &(lock);                                                   \
154     int __val = atomic_exchange_rel (__futex, 0);                             \
155     if (__builtin_expect (__val > 1, 0))                                      \
156       lll_futex_wake (__futex, 1);                                            \
157   }))
158
159 #define lll_mutex_unlock_force(lock) \
160   ((void) ({                                                                  \
161     int *__futex = &(lock);                                                   \
162     *__futex = 0;                                                             \
163     __asm __volatile (__lll_rel_instr ::: "memory");                          \
164     lll_futex_wake (__futex, 1);                                              \
165   }))
166
167 #define lll_mutex_islocked(futex) \
168   (futex != 0)
169
170
171 /* Our internal lock implementation is identical to the binary-compatible
172    mutex implementation. */
173
174 /* Type for lock object.  */
175 typedef int lll_lock_t;
176
177 /* Initializers for lock.  */
178 #define LLL_LOCK_INITIALIZER            (0)
179 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
180
181 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
182
183 /* The states of a lock are:
184     0  -  untaken
185     1  -  taken by one user
186    >1  -  taken by more users */
187
188 #define lll_trylock(lock)       lll_mutex_trylock (lock)
189 #define lll_lock(lock)          lll_mutex_lock (lock)
190 #define lll_unlock(lock)        lll_mutex_unlock (lock)
191 #define lll_islocked(lock)      lll_mutex_islocked (lock)
192
193 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
194    wakeup when the clone terminates.  The memory location contains the
195    thread ID while the clone is running and is reset to zero
196    afterwards.  */
197 #define lll_wait_tid(tid) \
198   do {                                                                        \
199     __typeof (tid) __tid;                                                     \
200     while ((__tid = (tid)) != 0)                                              \
201       lll_futex_wait (&(tid), __tid);                                         \
202   } while (0)
203
204 extern int __lll_timedwait_tid (int *, const struct timespec *)
205      attribute_hidden;
206
207 #define lll_timedwait_tid(tid, abstime) \
208   ({                                                                          \
209     int __res = 0;                                                            \
210     if ((tid) != 0)                                                           \
211       __res = __lll_timedwait_tid (&(tid), (abstime));                        \
212     __res;                                                                    \
213   })
214
215
216 /* Conditional variable handling.  */
217
218 extern void __lll_cond_wait (pthread_cond_t *cond)
219      attribute_hidden;
220 extern int __lll_cond_timedwait (pthread_cond_t *cond,
221                                  const struct timespec *abstime)
222      attribute_hidden;
223 extern void __lll_cond_wake (pthread_cond_t *cond)
224      attribute_hidden;
225 extern void __lll_cond_broadcast (pthread_cond_t *cond)
226      attribute_hidden;
227
228 #define lll_cond_wait(cond) \
229   __lll_cond_wait (cond)
230 #define lll_cond_timedwait(cond, abstime) \
231   __lll_cond_timedwait (cond, abstime)
232 #define lll_cond_wake(cond) \
233   __lll_cond_wake (cond)
234 #define lll_cond_broadcast(cond) \
235   __lll_cond_broadcast (cond)
236
237 #endif  /* lowlevellock.h */