[BZ #2505]
[platform/upstream/glibc.git] / nptl / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
1 /* Copyright (C) 2003, 2004, 2006 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 Library; 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 #ifndef __NR_futex
30 # define __NR_futex             221
31 #endif
32 #define FUTEX_WAIT              0
33 #define FUTEX_WAKE              1
34 #define FUTEX_REQUEUE           3
35 #define FUTEX_CMP_REQUEUE       4
36 #define FUTEX_WAKE_OP           5
37 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE   ((4 << 24) | 1)
38
39 /* Initializer for compatibility lock.  */
40 #define LLL_MUTEX_LOCK_INITIALIZER (0)
41
42 #define lll_futex_wait(futexp, val) \
43   ({                                                                          \
44     INTERNAL_SYSCALL_DECL (__err);                                            \
45     long int __ret;                                                           \
46                                                                               \
47     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
48                               (futexp), FUTEX_WAIT, (val), 0);                \
49     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
50   })
51
52 #define lll_futex_timed_wait(futexp, val, timespec) \
53   ({                                                                          \
54     INTERNAL_SYSCALL_DECL (__err);                                            \
55     long int __ret;                                                           \
56                                                                               \
57     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
58                               (futexp), FUTEX_WAIT, (val), (timespec));       \
59     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
60   })
61
62 #define lll_futex_wake(futexp, nr) \
63   ({                                                                          \
64     INTERNAL_SYSCALL_DECL (__err);                                            \
65     long int __ret;                                                           \
66                                                                               \
67     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
68                               (futexp), FUTEX_WAKE, (nr), 0);                 \
69     INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;                 \
70   })
71
72 #define lll_robust_mutex_dead(futexv) \
73   do                                                                          \
74     {                                                                         \
75       INTERNAL_SYSCALL_DECL (__err);                                          \
76       int *__futexp = &(futexv);                                              \
77                                                                               \
78       atomic_or (__futexp, FUTEX_OWNER_DIED);                                 \
79       INTERNAL_SYSCALL (futex, __err, 4, __futexp, FUTEX_WAKE, 1, 0);         \
80     }                                                                         \
81   while (0)
82
83 /* Returns non-zero if error happened, zero if success.  */
84 #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
85   ({                                                                          \
86     INTERNAL_SYSCALL_DECL (__err);                                            \
87     long int __ret;                                                           \
88                                                                               \
89     __ret = INTERNAL_SYSCALL (futex, __err, 6,                                \
90                               (futexp), FUTEX_CMP_REQUEUE, (nr_wake),         \
91                               (nr_move), (mutex), (val));                     \
92     INTERNAL_SYSCALL_ERROR_P (__ret, __err);                                  \
93   })
94
95 /* Returns non-zero if error happened, zero if success.  */
96 #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
97   ({                                                                          \
98     INTERNAL_SYSCALL_DECL (__err);                                            \
99     long int __ret;                                                           \
100                                                                               \
101     __ret = INTERNAL_SYSCALL (futex, __err, 6,                                \
102                               (futexp), FUTEX_WAKE_OP, (nr_wake),             \
103                               (nr_wake2), (futexp2),                          \
104                               FUTEX_OP_CLEAR_WAKE_IF_GT_ONE);                 \
105     INTERNAL_SYSCALL_ERROR_P (__ret, __err);                                  \
106   })
107
108 #ifdef UP
109 # define __lll_acq_instr        ""
110 # define __lll_rel_instr        ""
111 #else
112 # define __lll_acq_instr        "isync"
113 # ifdef _ARCH_PWR4
114 /*
115  * Newer powerpc64 processors support the new "light weight" sync (lwsync)
116  * So if the build is using -mcpu=[power4,power5,power5+,970] we can
117  * safely use lwsync.
118  */
119 #  define __lll_rel_instr       "lwsync"
120 # else
121 /*
122  * Older powerpc32 processors don't support the new "light weight"
123  * sync (lwsync).  So the only safe option is to use normal sync
124  * for all powerpc32 applications.
125  */
126 #  define __lll_rel_instr       "sync"
127 # endif
128 #endif
129
130 /* Set *futex to ID if it is 0, atomically.  Returns the old value */
131 #define __lll_robust_trylock(futex, id) \
132   ({ int __val;                                                               \
133      __asm __volatile ("1:      lwarx   %0,0,%2\n"                            \
134                        "        cmpwi   0,%0,0\n"                             \
135                        "        bne     2f\n"                                 \
136                        "        stwcx.  %3,0,%2\n"                            \
137                        "        bne-    1b\n"                                 \
138                        "2:      " __lll_acq_instr                             \
139                        : "=&r" (__val), "=m" (*futex)                         \
140                        : "r" (futex), "r" (id), "m" (*futex)                  \
141                        : "cr0", "memory");                                    \
142      __val;                                                                   \
143   })
144
145 #define lll_robust_mutex_trylock(lock, id) __lll_robust_trylock (&(lock), id)
146
147 /* Set *futex to 1 if it is 0, atomically.  Returns the old value */
148 #define __lll_trylock(futex) __lll_robust_trylock (futex, 1)
149
150 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
151
152 /* Set *futex to 2 if it is 0, atomically.  Returns the old value */
153 #define __lll_cond_trylock(futex) __lll_robust_trylock (futex, 2)
154
155 #define lll_mutex_cond_trylock(lock)    __lll_cond_trylock (&(lock))
156
157
158 extern void __lll_lock_wait (int *futex) attribute_hidden;
159 extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
160
161 #define lll_mutex_lock(lock) \
162   (void) ({                                                                   \
163     int *__futex = &(lock);                                                   \
164     if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
165                           0) != 0)                                            \
166       __lll_lock_wait (__futex);                                              \
167   })
168
169 #define lll_robust_mutex_lock(lock, id) \
170   ({                                                                          \
171     int *__futex = &(lock);                                                   \
172     int __val = 0;                                                            \
173     if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id,  \
174                                                                 0), 0))       \
175       __val = __lll_robust_lock_wait (__futex);                               \
176     __val;                                                                    \
177   })
178
179 #define lll_mutex_cond_lock(lock) \
180   (void) ({                                                                   \
181     int *__futex = &(lock);                                                   \
182     if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
183                           0) != 0)                                            \
184       __lll_lock_wait (__futex);                                              \
185   })
186
187 #define lll_robust_mutex_cond_lock(lock, id) \
188   ({                                                                          \
189     int *__futex = &(lock);                                                   \
190     int __val = 0;                                                            \
191     int __id = id | FUTEX_WAITERS;                                            \
192     if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
193                                                                 0), 0))       \
194       __val = __lll_robust_lock_wait (__futex);                               \
195     __val;                                                                    \
196   })
197
198
199 extern int __lll_timedlock_wait
200   (int *futex, const struct timespec *) attribute_hidden;
201 extern int __lll_robust_timedlock_wait
202   (int *futex, const struct timespec *) attribute_hidden;
203
204 #define lll_mutex_timedlock(lock, abstime) \
205   ({                                                                          \
206     int *__futex = &(lock);                                                   \
207     int __val = 0;                                                            \
208     if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
209                           0) != 0)                                            \
210       __val = __lll_timedlock_wait (__futex, abstime);                        \
211     __val;                                                                    \
212   })
213
214 #define lll_robust_mutex_timedlock(lock, abstime, id) \
215   ({                                                                          \
216     int *__futex = &(lock);                                                   \
217     int __val = 0;                                                            \
218     if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id,  \
219                                                                 0), 0))       \
220       __val = __lll_robust_timedlock_wait (__futex, abstime);                 \
221     __val;                                                                    \
222   })
223
224 #define lll_mutex_unlock(lock) \
225   ((void) ({                                                                  \
226     int *__futex = &(lock);                                                   \
227     int __val = atomic_exchange_rel (__futex, 0);                             \
228     if (__builtin_expect (__val > 1, 0))                                      \
229       lll_futex_wake (__futex, 1);                                            \
230   }))
231
232 #define lll_robust_mutex_unlock(lock) \
233   ((void) ({                                                                  \
234     int *__futex = &(lock);                                                   \
235     int __val = atomic_exchange_rel (__futex, 0);                             \
236     if (__builtin_expect (__val & FUTEX_WAITERS, 0))                          \
237       lll_futex_wake (__futex, 1);                                            \
238   }))
239
240 #define lll_mutex_unlock_force(lock) \
241   ((void) ({                                                                  \
242     int *__futex = &(lock);                                                   \
243     *__futex = 0;                                                             \
244     __asm __volatile (__lll_rel_instr ::: "memory");                          \
245     lll_futex_wake (__futex, 1);                                              \
246   }))
247
248 #define lll_mutex_islocked(futex) \
249   (futex != 0)
250
251
252 /* Our internal lock implementation is identical to the binary-compatible
253    mutex implementation. */
254
255 /* Type for lock object.  */
256 typedef int lll_lock_t;
257
258 /* Initializers for lock.  */
259 #define LLL_LOCK_INITIALIZER            (0)
260 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
261
262 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
263
264 /* The states of a lock are:
265     0  -  untaken
266     1  -  taken by one user
267    >1  -  taken by more users */
268
269 #define lll_trylock(lock)       lll_mutex_trylock (lock)
270 #define lll_lock(lock)          lll_mutex_lock (lock)
271 #define lll_unlock(lock)        lll_mutex_unlock (lock)
272 #define lll_islocked(lock)      lll_mutex_islocked (lock)
273
274 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
275    wakeup when the clone terminates.  The memory location contains the
276    thread ID while the clone is running and is reset to zero
277    afterwards.  */
278 #define lll_wait_tid(tid) \
279   do {                                                                        \
280     __typeof (tid) __tid;                                                     \
281     while ((__tid = (tid)) != 0)                                              \
282       lll_futex_wait (&(tid), __tid);                                         \
283   } while (0)
284
285 extern int __lll_timedwait_tid (int *, const struct timespec *)
286      attribute_hidden;
287
288 #define lll_timedwait_tid(tid, abstime) \
289   ({                                                                          \
290     int __res = 0;                                                            \
291     if ((tid) != 0)                                                           \
292       __res = __lll_timedwait_tid (&(tid), (abstime));                        \
293     __res;                                                                    \
294   })
295
296
297 /* Conditional variable handling.  */
298
299 extern void __lll_cond_wait (pthread_cond_t *cond)
300      attribute_hidden;
301 extern int __lll_cond_timedwait (pthread_cond_t *cond,
302                                  const struct timespec *abstime)
303      attribute_hidden;
304 extern void __lll_cond_wake (pthread_cond_t *cond)
305      attribute_hidden;
306 extern void __lll_cond_broadcast (pthread_cond_t *cond)
307      attribute_hidden;
308
309 #define lll_cond_wait(cond) \
310   __lll_cond_wait (cond)
311 #define lll_cond_timedwait(cond, abstime) \
312   __lll_cond_timedwait (cond, abstime)
313 #define lll_cond_wake(cond) \
314   __lll_cond_wake (cond)
315 #define lll_cond_broadcast(cond) \
316   __lll_cond_broadcast (cond)
317
318 #endif  /* lowlevellock.h */