Update.
[glibc.git] / linuxthreads / spinlock.h
blob6609ef71c95c321c6b92476d95111c2f92a0d63d
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 HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
53 static inline int
54 compare_and_swap_with_release_semantics (long * ptr, long oldval,
55 long newval, int * spinlock)
57 return __compare_and_swap_with_release_semantics (ptr, oldval,
58 newval);
61 #endif
63 static inline int compare_and_swap(long * ptr, long oldval, long newval,
64 int * spinlock)
66 return __compare_and_swap(ptr, oldval, newval);
69 #else
71 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
72 int * spinlock);
74 static inline int compare_and_swap(long * ptr, long oldval, long newval,
75 int * spinlock)
77 return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
80 #endif
82 #ifndef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
83 #define compare_and_swap_with_release_semantics compare_and_swap
84 #define __compare_and_swap_with_release_semantics __compare_and_swap
85 #endif
87 /* Internal locks */
89 extern void internal_function __pthread_lock(struct _pthread_fastlock * lock,
90 pthread_descr self);
91 extern int __pthread_unlock(struct _pthread_fastlock *lock);
93 static inline void __pthread_init_lock(struct _pthread_fastlock * lock)
95 lock->__status = 0;
96 lock->__spinlock = 0;
99 static inline int __pthread_trylock (struct _pthread_fastlock * lock)
101 #if defined HAS_COMPARE_AND_SWAP
102 long oldstatus;
103 #endif
105 #if defined TEST_FOR_COMPARE_AND_SWAP
106 if (!__pthread_has_cas)
107 #endif
108 #if !defined HAS_COMPARE_AND_SWAP
110 return (testandset(&lock->__spinlock) ? EBUSY : 0);
112 #endif
114 #if defined HAS_COMPARE_AND_SWAP
115 do {
116 oldstatus = lock->__status;
117 if (oldstatus != 0) return EBUSY;
118 } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
119 return 0;
120 #endif
123 /* Variation of internal lock used for pthread_mutex_t, supporting
124 timed-out waits. Warning: do not mix these operations with the above ones
125 over the same lock object! */
127 extern void __pthread_alt_lock(struct _pthread_fastlock * lock,
128 pthread_descr self);
130 extern int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
131 pthread_descr self, const struct timespec *abstime);
133 extern void __pthread_alt_unlock(struct _pthread_fastlock *lock);
135 static inline void __pthread_alt_init_lock(struct _pthread_fastlock * lock)
137 lock->__status = 0;
138 lock->__spinlock = 0;
141 static inline int __pthread_alt_trylock (struct _pthread_fastlock * lock)
143 long oldstatus;
145 do {
146 oldstatus = lock->__status;
147 if (oldstatus != 0) return EBUSY;
148 } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
149 return 0;
152 /* Initializers for both lock variants */
154 #define LOCK_INITIALIZER {0, 0}
155 #define ALT_LOCK_INITIALIZER {0, 0}
157 /* Operations on pthread_atomic, which is defined in internals.h */
159 static inline long atomic_increment(struct pthread_atomic *pa)
161 long oldval;
163 do {
164 oldval = pa->p_count;
165 } while (!compare_and_swap(&pa->p_count, oldval, oldval + 1, &pa->p_spinlock));
167 return oldval;
171 static inline long atomic_decrement(struct pthread_atomic *pa)
173 long oldval;
175 do {
176 oldval = pa->p_count;
177 } while (!compare_and_swap(&pa->p_count, oldval, oldval - 1, &pa->p_spinlock));
179 return oldval;
182 #define ATOMIC_INITIALIZER { 0, 0 }