e462776c024c59e1a82a070242ca7b3b33ad6a7a
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / lowlevellock.h
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@redhat.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 <ia64intrin.h>
27 #include <atomic.h>
28
29 #define __NR_futex              1230
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(futex, val) lll_futex_timed_wait (futex, val, 0)
38
39 #define lll_futex_timed_wait(ftx, val, timespec)                        \
40 ({                                                                      \
41    DO_INLINE_SYSCALL(futex, 4, (long) (ftx), FUTEX_WAIT, (int) (val),   \
42                      (long) (timespec));                                \
43    _r10 == -1 ? -_retval : _retval;                                     \
44 })
45
46 #define lll_futex_wake(ftx, nr)                                         \
47 ({                                                                      \
48    DO_INLINE_SYSCALL(futex, 3, (long) (ftx), FUTEX_WAKE, (int) (nr));   \
49    _r10 == -1 ? -_retval : _retval;                                     \
50 })
51
52 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex)                      \
53 ({                                                                           \
54    DO_INLINE_SYSCALL(futex, 5, (long) (ftx), FUTEX_REQUEUE, (int) (nr_wake), \
55                      (int) (nr_move), (long) (mutex));                       \
56    _r10 == -1 ? -_retval : _retval;                                          \
57 })
58
59
60 #define __lll_mutex_trylock(futex) \
61   (atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0)
62 #define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))
63
64
65 extern void __lll_lock_wait (int *futex) attribute_hidden;
66
67
68 #define __lll_mutex_lock(futex)                                         \
69   ((void) ({                                                            \
70     int *__futex = (futex);                                             \
71     if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0)      \
72       __lll_lock_wait (__futex);                                        \
73   }))
74 #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
75
76
77 #define __lll_mutex_cond_lock(futex)                                    \
78   ((void) ({                                                            \
79     int *__futex = (futex);                                             \
80     if (atomic_compare_and_exchange_bool_acq (__futex, 2, 0) != 0)      \
81       __lll_lock_wait (__futex);                                        \
82   }))
83 #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
84
85
86 extern int __lll_timedlock_wait (int *futex, const struct timespec *)
87      attribute_hidden;
88
89
90 #define __lll_mutex_timedlock(futex, abstime)                           \
91   ({                                                                    \
92      int *__futex = (futex);                                            \
93      int __val = 0;                                                     \
94                                                                         \
95      if (atomic_compare_and_exchange_bool_acq (__futex, 1, 0) != 0)     \
96        __val = __lll_timedlock_wait (__futex, abstime);                 \
97      __val;                                                             \
98   })
99 #define lll_mutex_timedlock(futex, abstime) \
100   __lll_mutex_timedlock (&(futex), abstime)
101
102
103 #define __lll_mutex_unlock(futex)                       \
104   ((void) ({                                            \
105     int *__futex = (futex);                             \
106     int __val = atomic_exchange_rel (__futex, 0);       \
107                                                         \
108     if (__builtin_expect (__val > 1, 0))                \
109       lll_futex_wake (__futex, 1);                      \
110   }))
111 #define lll_mutex_unlock(futex) \
112   __lll_mutex_unlock(&(futex))
113
114
115 #define __lll_mutex_unlock_force(futex)         \
116   ((void) ({                                    \
117     int *__futex = (futex);                     \
118     (void) atomic_exchange_rel (__futex, 0);    \
119     lll_futex_wake (__futex, 1);                \
120   }))
121 #define lll_mutex_unlock_force(futex) \
122   __lll_mutex_unlock_force(&(futex))
123
124
125 #define lll_mutex_islocked(futex) \
126   (futex != 0)
127
128
129 /* We have a separate internal lock implementation which is not tied
130    to binary compatibility.  We can use the lll_mutex_*.  */
131
132 /* Type for lock object.  */
133 typedef int lll_lock_t;
134
135 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
136
137 /* Initializers for lock.  */
138 #define LLL_LOCK_INITIALIZER            (0)
139 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
140
141 #define lll_trylock(futex)      lll_mutex_trylock (futex)
142 #define lll_lock(futex)         lll_mutex_lock (futex)
143 #define lll_unlock(futex)       lll_mutex_unlock (futex)
144 #define lll_islocked(futex)     lll_mutex_islocked (futex)
145
146
147 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
148    wakeup when the clone terminates.  The memory location contains the
149    thread ID while the clone is running and is reset to zero
150    afterwards.  */
151 #define lll_wait_tid(tid) \
152   do                                            \
153     {                                           \
154       __typeof (tid) __tid;                     \
155       while ((__tid = (tid)) != 0)              \
156         lll_futex_wait (&(tid), __tid);         \
157     }                                           \
158   while (0)
159
160 extern int __lll_timedwait_tid (int *, const struct timespec *)
161      attribute_hidden;
162
163 #define lll_timedwait_tid(tid, abstime) \
164   ({                                                    \
165     int __res = 0;                                      \
166     if ((tid) != 0)                                     \
167       __res = __lll_timedwait_tid (&(tid), (abstime));  \
168     __res;                                              \
169   })
170
171
172 /* Conditional variable handling.  */
173
174 extern void __lll_cond_wait (pthread_cond_t *cond)
175      attribute_hidden;
176 extern int __lll_cond_timedwait (pthread_cond_t *cond,
177                                  const struct timespec *abstime)
178      attribute_hidden;
179 extern void __lll_cond_wake (pthread_cond_t *cond)
180      attribute_hidden;
181 extern void __lll_cond_broadcast (pthread_cond_t *cond)
182      attribute_hidden;
183
184 #define lll_cond_wait(cond) \
185   __lll_cond_wait (cond)
186 #define lll_cond_timedwait(cond, abstime) \
187   __lll_cond_timedwait (cond, abstime)
188 #define lll_cond_wake(cond) \
189   __lll_cond_wake (cond)
190 #define lll_cond_broadcast(cond) \
191   __lll_cond_broadcast (cond)
192
193 #endif  /* lowlevellock.h */