30 #ifndef ABSL_BASE_INTERNAL_SPINLOCK_H_ 31 #define ABSL_BASE_INTERNAL_SPINLOCK_H_ 34 #include <sys/types.h> 49 namespace base_internal {
96 bool res = TryLockImpl();
98 this, __tsan_mutex_try_lock | (res ? 0 : __tsan_mutex_try_lock_failed),
106 uint32_t lock_value = lockword_.load(std::memory_order_relaxed);
107 lock_value = lockword_.exchange(lock_value & kSpinLockCooperative,
108 std::memory_order_release);
110 if ((lock_value & kSpinLockDisabledScheduling) != 0) {
113 if ((lock_value & kWaitTimeMask) != 0) {
117 SlowUnlock(lock_value);
126 return (lockword_.load(std::memory_order_relaxed) & kSpinLockHeld) != 0;
134 static uint32_t EncodeWaitCycles(int64_t wait_start_time,
135 int64_t wait_end_time);
138 static uint64_t DecodeWaitCycles(uint32_t lock_value);
150 enum { kSpinLockHeld = 1 };
151 enum { kSpinLockCooperative = 2 };
152 enum { kSpinLockDisabledScheduling = 4 };
153 enum { kSpinLockSleeper = 8 };
154 enum { kWaitTimeMask =
155 ~(kSpinLockHeld | kSpinLockCooperative | kSpinLockDisabledScheduling) };
163 uint32_t TryLockInternal(uint32_t lock_value, uint32_t wait_cycles);
164 void InitLinkerInitializedAndCooperative();
170 uint32_t lock_value = lockword_.load(std::memory_order_relaxed);
171 return (TryLockInternal(lock_value, 0) & kSpinLockHeld) == 0;
205 int64_t wait_cycles));
214 uint32_t wait_cycles) {
215 if ((lock_value & kSpinLockHeld) != 0) {
219 uint32_t sched_disabled_bit = 0;
220 if ((lock_value & kSpinLockCooperative) == 0) {
224 sched_disabled_bit = kSpinLockDisabledScheduling;
228 if (lockword_.compare_exchange_strong(
230 kSpinLockHeld | lock_value | wait_cycles | sched_disabled_bit,
231 std::memory_order_acquire, std::memory_order_relaxed)) {
242 #endif // ABSL_BASE_INTERNAL_SPINLOCK_H_ #define ABSL_TSAN_MUTEX_POST_UNLOCK(...)
void Lock() EXCLUSIVE_LOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
uint32_t TryLockInternal(uint32_t lock_value, uint32_t wait_cycles)
void RegisterSpinLockProfiler(void(*fn)(const void *contendedlock, int64_t wait_cycles))
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
~SpinLockHolder() UNLOCK_FUNCTION()
std::atomic< uint32_t > lockword_
void Unlock() UNLOCK_FUNCTION()
bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
SpinLockHolder(SpinLock *l) EXCLUSIVE_LOCK_FUNCTION(l)
static bool DisableRescheduling()
#define ABSL_TSAN_MUTEX_PRE_LOCK(...)
#define UNLOCK_FUNCTION(...)
SpinLock(base_internal::LinkerInitialized)
#define ABSL_TSAN_MUTEX_DESTROY(...)
#define ABSL_TSAN_MUTEX_POST_LOCK(...)
#define ABSL_TSAN_MUTEX_CREATE(...)
static constexpr bool IsCooperative(base_internal::SchedulingMode scheduling_mode)
#define ABSL_TSAN_MUTEX_PRE_UNLOCK(...)
static void EnableRescheduling(bool disable_result)
#define ABSL_ATTRIBUTE_COLD