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