* sysdeps/unix/sysv/linux/powerpc/lowlevellock.h (FUTEX_WAKE_OP,
[glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / lowlevellock.h
blob4219fe2716d48ce61cbc0340dd9e779cf8595435
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
23 #include <time.h>
24 #include <sys/param.h>
25 #include <bits/pthreadtypes.h>
26 #include <ia64intrin.h>
27 #include <atomic.h>
29 #define __NR_futex 1230
30 #define FUTEX_WAIT 0
31 #define FUTEX_WAKE 1
32 #define FUTEX_REQUEUE 3
33 #define FUTEX_CMP_REQUEUE 4
34 #define FUTEX_WAKE_OP 5
35 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
37 /* Delay in spinlock loop. */
38 #define BUSY_WAIT_NOP asm ("hint @pause")
40 /* Initializer for compatibility lock. */
41 #define LLL_MUTEX_LOCK_INITIALIZER (0)
43 #define lll_futex_wait(futex, val) lll_futex_timed_wait (futex, val, 0)
45 #define lll_futex_timed_wait(ftx, val, timespec) \
46 ({ \
47 DO_INLINE_SYSCALL(futex, 4, (long) (ftx), FUTEX_WAIT, (int) (val), \
48 (long) (timespec)); \
49 _r10 == -1 ? -_retval : _retval; \
52 #define lll_futex_wake(ftx, nr) \
53 ({ \
54 DO_INLINE_SYSCALL(futex, 3, (long) (ftx), FUTEX_WAKE, (int) (nr)); \
55 _r10 == -1 ? -_retval : _retval; \
58 /* Returns non-zero if error happened, zero if success. */
59 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val) \
60 ({ \
61 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), FUTEX_CMP_REQUEUE, \
62 (int) (nr_wake), (int) (nr_move), (long) (mutex), \
63 (int) val); \
64 _r10 == -1; \
67 /* Returns non-zero if error happened, zero if success. */
68 #define lll_futex_wake_unlock(ftx, nr_wake, nr_wake2, ftx2) \
69 ({ \
70 DO_INLINE_SYSCALL(futex, 6, (long) (ftx), FUTEX_WAKE_OP, \
71 (int) (nr_wake), (int) (nr_wake2), (long) (ftx2), \
72 FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
73 _r10 == -1; \
77 #define __lll_mutex_trylock(futex) \
78 (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
79 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
82 #define __lll_mutex_cond_trylock(futex) \
83 (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
84 #define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))
87 extern void __lll_lock_wait (int *futex) attribute_hidden;
90 #define __lll_mutex_lock(futex) \
91 ((void) ({ \
92 int *__futex = (futex); \
93 if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0) \
94 __lll_lock_wait (__futex); \
95 }))
96 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
99 #define __lll_mutex_cond_lock(futex) \
100 ((void) ({ \
101 int *__futex = (futex); \
102 if (atomic_compare_and_exchange_bool_acq (__futex, 2, 0) != 0) \
103 __lll_lock_wait (__futex); \
105 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
108 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
109 attribute_hidden;
112 #define __lll_mutex_timedlock(futex, abstime) \
113 ({ \
114 int *__futex = (futex); \
115 int __val = 0; \
117 if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0) \
118 __val = __lll_timedlock_wait (__futex, abstime); \
119 __val; \
121 #define lll_mutex_timedlock(futex, abstime) \
122 __lll_mutex_timedlock (&(futex), abstime)
125 #define __lll_mutex_unlock(futex) \
126 ((void) ({ \
127 int *__futex = (futex); \
128 int __val = atomic_exchange_rel (__futex, 0); \
130 if (__builtin_expect (__val > 1, 0)) \
131 lll_futex_wake (__futex, 1); \
133 #define lll_mutex_unlock(futex) \
134 __lll_mutex_unlock(&(futex))
137 #define __lll_mutex_unlock_force(futex) \
138 ((void) ({ \
139 int *__futex = (futex); \
140 (void) atomic_exchange_rel (__futex, 0); \
141 lll_futex_wake (__futex, 1); \
143 #define lll_mutex_unlock_force(futex) \
144 __lll_mutex_unlock_force(&(futex))
147 #define lll_mutex_islocked(futex) \
148 (futex != 0)
151 /* We have a separate internal lock implementation which is not tied
152 to binary compatibility. We can use the lll_mutex_*. */
154 /* Type for lock object. */
155 typedef int lll_lock_t;
157 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
159 /* Initializers for lock. */
160 #define LLL_LOCK_INITIALIZER (0)
161 #define LLL_LOCK_INITIALIZER_LOCKED (1)
163 #define lll_trylock(futex) lll_mutex_trylock (futex)
164 #define lll_lock(futex) lll_mutex_lock (futex)
165 #define lll_unlock(futex) lll_mutex_unlock (futex)
166 #define lll_islocked(futex) lll_mutex_islocked (futex)
169 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
170 wakeup when the clone terminates. The memory location contains the
171 thread ID while the clone is running and is reset to zero
172 afterwards. */
173 #define lll_wait_tid(tid) \
174 do \
176 __typeof (tid) __tid; \
177 while ((__tid = (tid)) != 0) \
178 lll_futex_wait (&(tid), __tid); \
180 while (0)
182 extern int __lll_timedwait_tid (int *, const struct timespec *)
183 attribute_hidden;
185 #define lll_timedwait_tid(tid, abstime) \
186 ({ \
187 int __res = 0; \
188 if ((tid) != 0) \
189 __res = __lll_timedwait_tid (&(tid), (abstime)); \
190 __res; \
194 /* Conditional variable handling. */
196 extern void __lll_cond_wait (pthread_cond_t *cond)
197 attribute_hidden;
198 extern int __lll_cond_timedwait (pthread_cond_t *cond,
199 const struct timespec *abstime)
200 attribute_hidden;
201 extern void __lll_cond_wake (pthread_cond_t *cond)
202 attribute_hidden;
203 extern void __lll_cond_broadcast (pthread_cond_t *cond)
204 attribute_hidden;
206 #define lll_cond_wait(cond) \
207 __lll_cond_wait (cond)
208 #define lll_cond_timedwait(cond, abstime) \
209 __lll_cond_timedwait (cond, abstime)
210 #define lll_cond_wake(cond) \
211 __lll_cond_wake (cond)
212 #define lll_cond_broadcast(cond) \
213 __lll_cond_broadcast (cond)
215 #endif /* lowlevellock.h */