Update.
[glibc.git] / linuxthreads / spinlock.h
blobb3d02314bfafdbb1cb2a60b72fb014c508a8a972
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. */
16 /* There are 2 compare and swap synchronization primitives with
17 different semantics:
19 1. compare_and_swap, which has acquire semantics (i.e. it
20 completes befor subsequent writes.)
21 2. compare_and_swap_with_release_semantics, which has release
22 semantics (it completes after previous writes.)
24 For those platforms on which they are the same. HAS_COMPARE_AND_SWAP
25 should be defined. For those platforms on which they are different,
26 HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS has to be defined. */
28 #ifndef HAS_COMPARE_AND_SWAP
29 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
30 #define HAS_COMPARE_AND_SWAP
31 #endif
32 #endif
34 #if defined(TEST_FOR_COMPARE_AND_SWAP)
36 extern int __pthread_has_cas;
37 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
38 int * spinlock);
40 static inline int compare_and_swap(long * ptr, long oldval, long newval,
41 int * spinlock)
43 if (__builtin_expect (__pthread_has_cas, 1))
44 return __compare_and_swap(ptr, oldval, newval);
45 else
46 return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
49 #elif defined(HAS_COMPARE_AND_SWAP)
51 #ifdef IMPLEMENT_TAS_WITH_CAS
52 #define testandset(p) !__compare_and_swap((long int *) p, 0, 1)
53 #endif
55 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
57 static inline int
58 compare_and_swap_with_release_semantics (long * ptr, long oldval,
59 long newval, int * spinlock)
61 return __compare_and_swap_with_release_semantics (ptr, oldval,
62 newval);
65 #endif
67 static inline int compare_and_swap(long * ptr, long oldval, long newval,
68 int * spinlock)
70 return __compare_and_swap(ptr, oldval, newval);
73 #else
75 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
76 int * spinlock);
78 static inline int compare_and_swap(long * ptr, long oldval, long newval,
79 int * spinlock)
81 return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
84 #endif
86 #ifndef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
87 #define compare_and_swap_with_release_semantics compare_and_swap
88 #define __compare_and_swap_with_release_semantics __compare_and_swap
89 #endif
91 /* Internal locks */
93 extern void internal_function __pthread_lock(struct _pthread_fastlock * lock,
94 pthread_descr self);
95 extern int __pthread_unlock(struct _pthread_fastlock *lock);
97 static inline void __pthread_init_lock(struct _pthread_fastlock * lock)
99 lock->__status = 0;
100 lock->__spinlock = 0;
103 static inline int __pthread_trylock (struct _pthread_fastlock * lock)
105 #if defined HAS_COMPARE_AND_SWAP
106 long oldstatus;
107 #endif
109 #if defined TEST_FOR_COMPARE_AND_SWAP
110 if (!__pthread_has_cas)
111 #endif
112 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
114 return (testandset(&lock->__spinlock) ? EBUSY : 0);
116 #endif
118 #if defined HAS_COMPARE_AND_SWAP
119 do {
120 oldstatus = lock->__status;
121 if (oldstatus != 0) return EBUSY;
122 } while(! __compare_and_swap(&lock->__status, 0, 1));
123 return 0;
124 #endif
127 /* Variation of internal lock used for pthread_mutex_t, supporting
128 timed-out waits. Warning: do not mix these operations with the above ones
129 over the same lock object! */
131 extern void __pthread_alt_lock(struct _pthread_fastlock * lock,
132 pthread_descr self);
134 extern int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
135 pthread_descr self, const struct timespec *abstime);
137 extern void __pthread_alt_unlock(struct _pthread_fastlock *lock);
139 static inline void __pthread_alt_init_lock(struct _pthread_fastlock * lock)
141 lock->__status = 0;
142 lock->__spinlock = 0;
145 static inline int __pthread_alt_trylock (struct _pthread_fastlock * lock)
147 #if defined HAS_COMPARE_AND_SWAP
148 long oldstatus;
149 #endif
151 #if defined TEST_FOR_COMPARE_AND_SWAP
152 if (!__pthread_has_cas)
153 #endif
154 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
156 int res = EBUSY;
158 if (testandset(&lock->__spinlock) == 0)
160 if (lock->__status == 0)
162 lock->__status = 1;
163 WRITE_MEMORY_BARRIER();
164 res = 0;
166 lock->__spinlock = 0;
168 return res;
170 #endif
172 #if defined HAS_COMPARE_AND_SWAP
173 do {
174 oldstatus = lock->__status;
175 if (oldstatus != 0) return EBUSY;
176 } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
177 return 0;
178 #endif
181 /* Initializers for both lock variants */
183 #define LOCK_INITIALIZER {0, 0}
184 #define ALT_LOCK_INITIALIZER {0, 0}
186 /* Operations on pthread_atomic, which is defined in internals.h */
188 static inline long atomic_increment(struct pthread_atomic *pa)
190 long oldval;
192 do {
193 oldval = pa->p_count;
194 } while (!compare_and_swap(&pa->p_count, oldval, oldval + 1, &pa->p_spinlock));
196 return oldval;
200 static inline long atomic_decrement(struct pthread_atomic *pa)
202 long oldval;
204 do {
205 oldval = pa->p_count;
206 } while (!compare_and_swap(&pa->p_count, oldval, oldval - 1, &pa->p_spinlock));
208 return oldval;
211 #define ATOMIC_INITIALIZER { 0, 0 }
214 static inline void
215 __pthread_set_own_extricate_if(pthread_descr self, pthread_extricate_if *peif)
217 #if 0
218 __pthread_lock(THREAD_GETMEM(self, p_lock), self);
219 THREAD_SETMEM(self, p_extricate, peif);
220 __pthread_unlock(THREAD_GETMEM (self, p_lock));
221 #else
222 /* I don't think that getting the lock is necessary. All we do is an
223 atomic write. */
224 THREAD_SETMEM(self, p_extricate, peif);
225 #endif