123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #include <bits/initspin.h>
- #ifndef HAS_COMPARE_AND_SWAP
- #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
- #define HAS_COMPARE_AND_SWAP
- #endif
- #endif
- #if defined(TEST_FOR_COMPARE_AND_SWAP)
- extern int __pthread_has_cas;
- extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
- int * spinlock);
- static __inline__ int compare_and_swap(long * ptr, long oldval, long newval,
- int * spinlock)
- {
- if (__builtin_expect (__pthread_has_cas, 1))
- return __compare_and_swap(ptr, oldval, newval);
- else
- return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
- }
- #elif defined(HAS_COMPARE_AND_SWAP)
- #ifdef IMPLEMENT_TAS_WITH_CAS
- #define testandset(p) !__compare_and_swap((long int *) p, 0, 1)
- #endif
- #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
- static __inline__ int
- compare_and_swap_with_release_semantics (long * ptr, long oldval,
- long newval, int * spinlock)
- {
- return __compare_and_swap_with_release_semantics (ptr, oldval,
- newval);
- }
- #endif
- static __inline__ int compare_and_swap(long * ptr, long oldval, long newval,
- int * spinlock)
- {
- return __compare_and_swap(ptr, oldval, newval);
- }
- #else
- extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
- int * spinlock);
- static __inline__ int compare_and_swap(long * ptr, long oldval, long newval,
- int * spinlock)
- {
- return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
- }
- #endif
- #ifndef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
- #define compare_and_swap_with_release_semantics compare_and_swap
- #define __compare_and_swap_with_release_semantics __compare_and_swap
- #endif
- extern void internal_function __pthread_lock(struct _pthread_fastlock * lock,
- pthread_descr self);
- extern int __pthread_unlock(struct _pthread_fastlock *lock);
- static __inline__ void __pthread_init_lock(struct _pthread_fastlock * lock)
- {
- lock->__status = 0;
- lock->__spinlock = __LT_SPINLOCK_INIT;
- }
- static __inline__ int __pthread_trylock (struct _pthread_fastlock * lock)
- {
- #if defined TEST_FOR_COMPARE_AND_SWAP
- if (!__pthread_has_cas)
- #endif
- #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
- {
- return (testandset(&lock->__spinlock) ? EBUSY : 0);
- }
- #endif
- #if defined HAS_COMPARE_AND_SWAP
- do {
- if (lock->__status != 0) return EBUSY;
- } while(! __compare_and_swap(&lock->__status, 0, 1));
- return 0;
- #endif
- }
- extern void __pthread_alt_lock(struct _pthread_fastlock * lock,
- pthread_descr self);
- extern int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
- pthread_descr self, const struct timespec *abstime);
- extern void __pthread_alt_unlock(struct _pthread_fastlock *lock);
- static __inline__ void __pthread_alt_init_lock(struct _pthread_fastlock * lock)
- {
- lock->__status = 0;
- lock->__spinlock = __LT_SPINLOCK_INIT;
- }
- static __inline__ int __pthread_alt_trylock (struct _pthread_fastlock * lock)
- {
- #if defined TEST_FOR_COMPARE_AND_SWAP
- if (!__pthread_has_cas)
- #endif
- #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
- {
- int res = EBUSY;
- if (testandset(&lock->__spinlock) == 0)
- {
- if (lock->__status == 0)
- {
- lock->__status = 1;
- WRITE_MEMORY_BARRIER();
- res = 0;
- }
- lock->__spinlock = __LT_SPINLOCK_INIT;
- }
- return res;
- }
- #endif
- #if defined HAS_COMPARE_AND_SWAP
- do {
- if (lock->__status != 0) return EBUSY;
- } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
- return 0;
- #endif
- }
- static __inline__ long atomic_increment(struct pthread_atomic *pa)
- {
- long oldval;
- do {
- oldval = pa->p_count;
- } while (!compare_and_swap(&pa->p_count, oldval, oldval + 1, &pa->p_spinlock));
- return oldval;
- }
- static __inline__ long atomic_decrement(struct pthread_atomic *pa)
- {
- long oldval;
- do {
- oldval = pa->p_count;
- } while (!compare_and_swap(&pa->p_count, oldval, oldval - 1, &pa->p_spinlock));
- return oldval;
- }
- static __inline__ void
- __pthread_set_own_extricate_if (pthread_descr self, pthread_extricate_if *peif)
- {
-
- if (peif == NULL
- || THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
- {
-
- if (peif == NULL)
- __pthread_lock (THREAD_GETMEM(self, p_lock), self);
- THREAD_SETMEM(self, p_extricate, peif);
- if (peif == NULL)
- __pthread_unlock (THREAD_GETMEM(self, p_lock));
- }
- }
|