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 HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
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
,
63 static inline int compare_and_swap(long * ptr
, long oldval
, long newval
,
66 return __compare_and_swap(ptr
, oldval
, newval
);
71 extern int __pthread_compare_and_swap(long * ptr
, long oldval
, long newval
,
74 static inline int compare_and_swap(long * ptr
, long oldval
, long newval
,
77 return __pthread_compare_and_swap(ptr
, oldval
, newval
, spinlock
);
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
89 extern void internal_function
__pthread_lock(struct _pthread_fastlock
* lock
,
91 extern int __pthread_unlock(struct _pthread_fastlock
*lock
);
93 static inline void __pthread_init_lock(struct _pthread_fastlock
* lock
)
99 static inline int __pthread_trylock (struct _pthread_fastlock
* lock
)
101 #if defined HAS_COMPARE_AND_SWAP
105 #if defined TEST_FOR_COMPARE_AND_SWAP
106 if (!__pthread_has_cas
)
108 #if !defined HAS_COMPARE_AND_SWAP
110 return (testandset(&lock
->__spinlock
) ? EBUSY
: 0);
114 #if defined HAS_COMPARE_AND_SWAP
116 oldstatus
= lock
->__status
;
117 if (oldstatus
!= 0) return EBUSY
;
118 } while(! compare_and_swap(&lock
->__status
, 0, 1, &lock
->__spinlock
));
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
,
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
)
138 lock
->__spinlock
= 0;
141 static inline int __pthread_alt_trylock (struct _pthread_fastlock
* lock
)
146 oldstatus
= lock
->__status
;
147 if (oldstatus
!= 0) return EBUSY
;
148 } while(! compare_and_swap(&lock
->__status
, 0, 1, &lock
->__spinlock
));
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
)
164 oldval
= pa
->p_count
;
165 } while (!compare_and_swap(&pa
->p_count
, oldval
, oldval
+ 1, &pa
->p_spinlock
));
171 static inline long atomic_decrement(struct pthread_atomic
*pa
)
176 oldval
= pa
->p_count
;
177 } while (!compare_and_swap(&pa
->p_count
, oldval
, oldval
- 1, &pa
->p_spinlock
));
182 #define ATOMIC_INITIALIZER { 0, 0 }