17 #ifndef __COMMON_THREAD_H__
18 #define __COMMON_THREAD_H__
24 #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
25 int _starpu_pthread_spin_do_lock(starpu_pthread_spinlock_t *lock);
28 #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
30 static inline int _starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock,
int pshared STARPU_ATTRIBUTE_UNUSED)
35 #define starpu_pthread_spin_init _starpu_pthread_spin_init
37 static inline int _starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
42 #define starpu_pthread_spin_destroy _starpu_pthread_spin_destroy
44 static inline int _starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
49 if (STARPU_LIKELY(!lock->taken))
56 starpu_sleep(0.000001);
59 #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
60 if (STARPU_LIKELY(STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0))
64 return _starpu_pthread_spin_do_lock(lock);
69 prev = STARPU_TEST_AND_SET(&lock->taken, 1);
70 if (STARPU_UNLIKELY(prev))
73 while (STARPU_UNLIKELY(prev));
77 #define starpu_pthread_spin_lock _starpu_pthread_spin_lock
79 static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
82 STARPU_ASSERT(lock->taken);
83 #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
84 STARPU_ASSERT(lock->taken == 1 || lock->taken == 2);
86 STARPU_ASSERT(lock->taken);
90 static inline int _starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
93 if (STARPU_UNLIKELY(lock->taken))
97 #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
99 prev = STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1);
100 return (prev == 0)?0:EBUSY;
103 prev = STARPU_TEST_AND_SET(&lock->taken, 1);
104 return (prev == 0)?0:EBUSY;
107 #define starpu_pthread_spin_trylock _starpu_pthread_spin_trylock
109 #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
110 void _starpu_pthread_spin_do_unlock(starpu_pthread_spinlock_t *lock);
113 static inline int _starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
115 #ifdef STARPU_SIMGRID
117 #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
118 STARPU_ASSERT(lock->taken != 0);
119 STARPU_SYNCHRONIZE();
120 unsigned next = STARPU_ATOMIC_ADD(&lock->taken, -1);
121 if (STARPU_LIKELY(next == 0))
124 _starpu_pthread_spin_do_unlock(lock);
126 STARPU_RELEASE(&lock->taken);
130 #define starpu_pthread_spin_unlock _starpu_pthread_spin_unlock
134 static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
136 STARPU_ASSERT(pthread_spin_trylock((pthread_spinlock_t *)lock) != 0);