(res_isourserver): Fix cast.
[glibc/pb-stable.git] / linuxthreads / spinlock.h
blob0dbaa54e2da0b6443ead5e508166c5c1ececa02f
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1998 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
9 /* */
10 /* This program 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 */
13 /* GNU Library General Public License for more details. */
15 #include <bits/initspin.h>
18 /* There are 2 compare and swap synchronization primitives with
19 different semantics:
21 1. compare_and_swap, which has acquire semantics (i.e. it
22 completes befor subsequent writes.)
23 2. compare_and_swap_with_release_semantics, which has release
24 semantics (it completes after previous writes.)
26 For those platforms on which they are the same. HAS_COMPARE_AND_SWAP
27 should be defined. For those platforms on which they are different,
28 HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS has to be defined. */
30 #ifndef HAS_COMPARE_AND_SWAP
31 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
32 #define HAS_COMPARE_AND_SWAP
33 #endif
34 #endif
36 #if defined(TEST_FOR_COMPARE_AND_SWAP)
38 extern int __pthread_has_cas;
39 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
40 int * spinlock);
42 static inline int compare_and_swap(long * ptr, long oldval, long newval,
43 int * spinlock)
45 if (__builtin_expect (__pthread_has_cas, 1))
46 return __compare_and_swap(ptr, oldval, newval);
47 else
48 return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
51 #elif defined(HAS_COMPARE_AND_SWAP)
53 #ifdef IMPLEMENT_TAS_WITH_CAS
54 #define testandset(p) !__compare_and_swap((long int *) p, 0, 1)
55 #endif
57 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
59 static inline int
60 compare_and_swap_with_release_semantics (long * ptr, long oldval,
61 long newval, int * spinlock)
63 return __compare_and_swap_with_release_semantics (ptr, oldval,
64 newval);
67 #endif
69 static inline int compare_and_swap(long * ptr, long oldval, long newval,
70 int * spinlock)
72 return __compare_and_swap(ptr, oldval, newval);
75 #else
77 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
78 int * spinlock);
80 static inline int compare_and_swap(long * ptr, long oldval, long newval,
81 int * spinlock)
83 return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
86 #endif
88 #ifndef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
89 #define compare_and_swap_with_release_semantics compare_and_swap
90 #define __compare_and_swap_with_release_semantics __compare_and_swap
91 #endif
93 /* Internal locks */
95 extern void internal_function __pthread_lock(struct _pthread_fastlock * lock,
96 pthread_descr self);
97 extern int __pthread_unlock(struct _pthread_fastlock *lock);
99 static inline void __pthread_init_lock(struct _pthread_fastlock * lock)
101 lock->__status = 0;
102 lock->__spinlock = __LT_SPINLOCK_INIT;
105 static inline int __pthread_trylock (struct _pthread_fastlock * lock)
107 #if defined HAS_COMPARE_AND_SWAP
108 long oldstatus;
109 #endif
111 #if defined TEST_FOR_COMPARE_AND_SWAP
112 if (!__pthread_has_cas)
113 #endif
114 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
116 return (testandset(&lock->__spinlock) ? EBUSY : 0);
118 #endif
120 #if defined HAS_COMPARE_AND_SWAP
121 do {
122 oldstatus = lock->__status;
123 if (oldstatus != 0) return EBUSY;
124 } while(! __compare_and_swap(&lock->__status, 0, 1));
125 return 0;
126 #endif
129 /* Variation of internal lock used for pthread_mutex_t, supporting
130 timed-out waits. Warning: do not mix these operations with the above ones
131 over the same lock object! */
133 extern void __pthread_alt_lock(struct _pthread_fastlock * lock,
134 pthread_descr self);
136 extern int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
137 pthread_descr self, const struct timespec *abstime);
139 extern void __pthread_alt_unlock(struct _pthread_fastlock *lock);
141 static inline void __pthread_alt_init_lock(struct _pthread_fastlock * lock)
143 lock->__status = 0;
144 lock->__spinlock = __LT_SPINLOCK_INIT;
147 static inline int __pthread_alt_trylock (struct _pthread_fastlock * lock)
149 #if defined HAS_COMPARE_AND_SWAP
150 long oldstatus;
151 #endif
153 #if defined TEST_FOR_COMPARE_AND_SWAP
154 if (!__pthread_has_cas)
155 #endif
156 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
158 int res = EBUSY;
160 if (testandset(&lock->__spinlock) == 0)
162 if (lock->__status == 0)
164 lock->__status = 1;
165 WRITE_MEMORY_BARRIER();
166 res = 0;
168 lock->__spinlock = __LT_SPINLOCK_INIT;
170 return res;
172 #endif
174 #if defined HAS_COMPARE_AND_SWAP
175 do {
176 oldstatus = lock->__status;
177 if (oldstatus != 0) return EBUSY;
178 } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
179 return 0;
180 #endif
183 /* Operations on pthread_atomic, which is defined in internals.h */
185 static inline long atomic_increment(struct pthread_atomic *pa)
187 long oldval;
189 do {
190 oldval = pa->p_count;
191 } while (!compare_and_swap(&pa->p_count, oldval, oldval + 1, &pa->p_spinlock));
193 return oldval;
197 static inline long atomic_decrement(struct pthread_atomic *pa)
199 long oldval;
201 do {
202 oldval = pa->p_count;
203 } while (!compare_and_swap(&pa->p_count, oldval, oldval - 1, &pa->p_spinlock));
205 return oldval;
209 static inline void
210 __pthread_set_own_extricate_if (pthread_descr self, pthread_extricate_if *peif)
212 /* Only store a non-null peif if the thread has cancellation enabled.
213 Otherwise pthread_cancel will unconditionally call the extricate handler,
214 and restart the thread giving rise to forbidden spurious wakeups. */
215 if (peif == NULL
216 || THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
218 /* If we are removing the extricate interface, we need to synchronize
219 against pthread_cancel so that it does not continue with a pointer
220 to a deallocated pthread_extricate_if struct! The thread lock
221 is (ab)used for this synchronization purpose. */
222 if (peif == NULL)
223 __pthread_lock (THREAD_GETMEM(self, p_lock), self);
224 THREAD_SETMEM(self, p_extricate, peif);
225 if (peif == NULL)
226 __pthread_unlock (THREAD_GETMEM(self, p_lock));