17#ifndef __COMMON_THREAD_H__
18#define __COMMON_THREAD_H__
25#pragma GCC visibility push(hidden)
27#if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
28int _starpu_pthread_spin_do_lock(starpu_pthread_spinlock_t *lock) STARPU_ATTRIBUTE_VISIBILITY_DEFAULT;
31#if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
33static inline int _starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock,
int pshared STARPU_ATTRIBUTE_UNUSED)
38#define starpu_pthread_spin_init _starpu_pthread_spin_init
40static inline int _starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
45#define starpu_pthread_spin_destroy _starpu_pthread_spin_destroy
47static inline int _starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
50 if (STARPU_LIKELY(!lock->taken))
56#ifdef STARPU_HAVE_S4U_ON_TIME_ADVANCE_CB
58 starpu_sleep(0.000001);
60 if (STARPU_LIKELY(!lock->taken))
67 STARPU_PTHREAD_MUTEX_LOCK(&_starpu_simgrid_time_advance_mutex);
71#ifdef STARPU_HAVE_S4U_ON_TIME_ADVANCE_CB
72 STARPU_PTHREAD_COND_WAIT(&_starpu_simgrid_time_advance_cond, &_starpu_simgrid_time_advance_mutex);
76 starpu_sleep(0.000001);
81#ifdef STARPU_HAVE_S4U_ON_TIME_ADVANCE_CB
82 STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_simgrid_time_advance_mutex);
85#elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
86 if (STARPU_LIKELY(STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0))
90 return _starpu_pthread_spin_do_lock(lock);
95 prev = STARPU_TEST_AND_SET(&lock->taken, 1);
96 if (STARPU_UNLIKELY(prev))
99 while (STARPU_UNLIKELY(prev));
103#define starpu_pthread_spin_lock _starpu_pthread_spin_lock
105static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
108 STARPU_ASSERT(lock->taken);
109#elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
110 STARPU_ASSERT(lock->taken == 1 || lock->taken == 2);
112 STARPU_ASSERT(lock->taken);
116static inline int _starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
119 if (STARPU_UNLIKELY(lock->taken))
123#elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
125 prev = STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1);
126 return (prev == 0)?0:EBUSY;
129 prev = STARPU_TEST_AND_SET(&lock->taken, 1);
130 return (prev == 0)?0:EBUSY;
133#define starpu_pthread_spin_trylock _starpu_pthread_spin_trylock
135#if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
136void _starpu_pthread_spin_do_unlock(starpu_pthread_spinlock_t *lock) STARPU_ATTRIBUTE_VISIBILITY_DEFAULT;
139static inline int _starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
143#elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
144 STARPU_ASSERT(lock->taken != 0);
145 STARPU_SYNCHRONIZE();
146 unsigned next = STARPU_ATOMIC_ADD(&lock->taken, -1);
147 if (STARPU_LIKELY(next == 0))
150 _starpu_pthread_spin_do_unlock(lock);
152 STARPU_RELEASE(&lock->taken);
156#define starpu_pthread_spin_unlock _starpu_pthread_spin_unlock
160static inline void _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
162 STARPU_ASSERT(pthread_spin_trylock((pthread_spinlock_t *)lock) != 0);
168#pragma GCC visibility pop