1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1998 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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
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
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
,
40 static inline int compare_and_swap(long * ptr
, long oldval
, long newval
,
43 if (__builtin_expect (__pthread_has_cas
, 1))
44 return __compare_and_swap(ptr
, oldval
, newval
);
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)
55 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
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
,
67 static inline int compare_and_swap(long * ptr
, long oldval
, long newval
,
70 return __compare_and_swap(ptr
, oldval
, newval
);
75 extern int __pthread_compare_and_swap(long * ptr
, long oldval
, long newval
,
78 static inline int compare_and_swap(long * ptr
, long oldval
, long newval
,
81 return __pthread_compare_and_swap(ptr
, oldval
, newval
, spinlock
);
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
93 extern void internal_function
__pthread_lock(struct _pthread_fastlock
* lock
,
95 extern int __pthread_unlock(struct _pthread_fastlock
*lock
);
97 static inline void __pthread_init_lock(struct _pthread_fastlock
* lock
)
100 lock
->__spinlock
= 0;
103 static inline int __pthread_trylock (struct _pthread_fastlock
* lock
)
105 #if defined HAS_COMPARE_AND_SWAP
109 #if defined TEST_FOR_COMPARE_AND_SWAP
110 if (!__pthread_has_cas
)
112 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
114 return (testandset(&lock
->__spinlock
) ? EBUSY
: 0);
118 #if defined HAS_COMPARE_AND_SWAP
120 oldstatus
= lock
->__status
;
121 if (oldstatus
!= 0) return EBUSY
;
122 } while(! __compare_and_swap(&lock
->__status
, 0, 1));
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
,
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
)
142 lock
->__spinlock
= 0;
145 static inline int __pthread_alt_trylock (struct _pthread_fastlock
* lock
)
147 #if defined HAS_COMPARE_AND_SWAP
151 #if defined TEST_FOR_COMPARE_AND_SWAP
152 if (!__pthread_has_cas
)
154 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
158 if (testandset(&lock
->__spinlock
) == 0)
160 if (lock
->__status
== 0)
163 WRITE_MEMORY_BARRIER();
166 lock
->__spinlock
= 0;
172 #if defined HAS_COMPARE_AND_SWAP
174 oldstatus
= lock
->__status
;
175 if (oldstatus
!= 0) return EBUSY
;
176 } while(! compare_and_swap(&lock
->__status
, 0, 1, &lock
->__spinlock
));
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
)
193 oldval
= pa
->p_count
;
194 } while (!compare_and_swap(&pa
->p_count
, oldval
, oldval
+ 1, &pa
->p_spinlock
));
200 static inline long atomic_decrement(struct pthread_atomic
*pa
)
205 oldval
= pa
->p_count
;
206 } while (!compare_and_swap(&pa
->p_count
, oldval
, oldval
- 1, &pa
->p_spinlock
));
211 #define ATOMIC_INITIALIZER { 0, 0 }
215 __pthread_set_own_extricate_if(pthread_descr self
, pthread_extricate_if
*peif
)
218 __pthread_lock(THREAD_GETMEM(self
, p_lock
), self
);
219 THREAD_SETMEM(self
, p_extricate
, peif
);
220 __pthread_unlock(THREAD_GETMEM (self
, p_lock
));
222 /* I don't think that getting the lock is necessary. All we do is an
224 THREAD_SETMEM(self
, p_extricate
, peif
);