Update.
[platform/upstream/glibc.git] / nptl / sysdeps / unix / sysv / linux / powerpc / lowlevellock.h
1 /* Copyright (C) 2003 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
33 /* Initializer for compatibility lock.  */
34 #define LLL_MUTEX_LOCK_INITIALIZER (0)
35
36 #define lll_futex_wait(futexp, val) \
37   ({                                                                          \
38     INTERNAL_SYSCALL_DECL (__err);                                            \
39     long int __ret;                                                           \
40                                                                               \
41     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
42                               (futexp), FUTEX_WAIT, (val), 0);                \
43     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0;                       \
44   })
45
46 #define lll_futex_timed_wait(futexp, val, timespec) \
47   ({                                                                          \
48     INTERNAL_SYSCALL_DECL (__err);                                            \
49     long int __ret;                                                           \
50                                                                               \
51     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
52                               (futexp), FUTEX_WAIT, (val), (timespec));       \
53     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0;                       \
54   })
55
56 #define lll_futex_wake(futexp, nr) \
57   ({                                                                          \
58     INTERNAL_SYSCALL_DECL (__err);                                            \
59     long int __ret;                                                           \
60                                                                               \
61     __ret = INTERNAL_SYSCALL (futex, __err, 4,                                \
62                               (futexp), FUTEX_WAKE, (nr), 0);                 \
63     INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret: 0;                       \
64   })
65
66 #ifdef UP
67 # define __lll_acq_instr        ""
68 # define __lll_rel_instr        ""
69 #else
70 # define __lll_acq_instr        "isync"
71 # define __lll_rel_instr        "sync"
72 #endif
73
74 /* Set *futex to 1 if it is 0, atomically.  Returns the old value */
75 #define __lll_trylock(futex) \
76   ({ int __val;                                                               \
77      __asm __volatile ("1:      lwarx   %0,0,%2\n"                            \
78                        "        cmpwi   0,%0,0\n"                             \
79                        "        bne     2f\n"                                 \
80                        "        stwcx.  %3,0,%2\n"                            \
81                        "        bne-    1b\n"                                 \
82                        "2:      " __lll_acq_instr                             \
83                        : "=&r" (__val), "=m" (*futex)                         \
84                        : "r" (futex), "r" (1), "1" (*futex)                   \
85                        : "cr0", "memory");                                    \
86      __val;                                                                   \
87   })
88
89 #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
90
91
92 extern void __lll_lock_wait (int *futex, int val) attribute_hidden;
93
94 #define lll_mutex_lock(lock) \
95   (void) ({                                                                   \
96     int *__futex = &(lock);                                                   \
97     int __val = atomic_exchange_and_add (__futex, 1);                         \
98     __asm __volatile (__lll_acq_instr ::: "memory");                          \
99     if (__builtin_expect (__val != 0, 0))                                     \
100       __lll_lock_wait (__futex, __val);                                       \
101   })
102
103 extern int __lll_timedlock_wait
104         (int *futex, int val, const struct timespec *) attribute_hidden;
105
106 #define lll_mutex_timedlock(lock, abstime) \
107   ({ int *__futex = &(lock);                                                  \
108      int __val = atomic_exchange_and_add (__futex, 1);                        \
109      __asm __volatile (__lll_acq_instr ::: "memory");                         \
110      if (__builtin_expect (__val != 0, 0))                                    \
111        __val = __lll_timedlock_wait (__futex, __val, (abstime));              \
112      __val;                                                                   \
113   })
114
115 #define lll_mutex_unlock(lock) \
116   ((void) ({                                                                  \
117     int *__futex = &(lock);                                                   \
118     int __val = atomic_exchange (__futex, 0);                                 \
119     if (__builtin_expect (__val > 1, 0))                                      \
120       lll_futex_wake (__futex, 1);                                            \
121   }))
122
123 #define lll_mutex_islocked(futex) \
124   (futex != 0)
125
126
127 /* Our internal lock implementation is identical to the binary-compatible
128    mutex implementation. */
129
130 /* Type for lock object.  */
131 typedef int lll_lock_t;
132
133 /* Initializers for lock.  */
134 #define LLL_LOCK_INITIALIZER            (0)
135 #define LLL_LOCK_INITIALIZER_LOCKED     (1)
136
137 extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
138
139 /* The states of a lock are:
140     0  -  untaken
141     1  -  taken by one user
142    >1  -  taken by more users */
143
144 #define lll_trylock(lock)       lll_mutex_trylock (lock)
145 #define lll_lock(lock)          lll_mutex_lock (lock)
146 #define lll_unlock(lock)        lll_mutex_unlock (lock)
147 #define lll_islocked(lock)      lll_mutex_islocked (lock)
148
149 /* The kernel notifies a process which uses CLONE_CLEARTID via futex
150    wakeup when the clone terminates.  The memory location contains the
151    thread ID while the clone is running and is reset to zero
152    afterwards.  */
153 #define lll_wait_tid(tid) \
154   do {                                                                        \
155     __typeof (tid) __tid;                                                     \
156     while ((__tid = (tid)) != 0)                                              \
157       lll_futex_wait (&(tid), __tid);                                         \
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 */