16 #include <boost/noncopyable.hpp> 18 namespace duds {
namespace general {
88 while (af.test_and_set(std::memory_order_acquire)) { }
100 while (af.test_and_set(std::memory_order_acquire)) {
101 std::this_thread::yield();
123 return !af.test_and_set(std::memory_order_acquire);
131 template <
class Clock,
class Duration>
135 while ((res = af.test_and_set(std::memory_order_acquire)) &&
136 (time <= std::chrono::time_point<Clock,Duration>::now())
146 template <
class Clock,
class Duration>
150 while ((res = af.test_and_set(std::memory_order_acquire)) &&
151 (time <= std::chrono::time_point<Clock,Duration>::clock::now())
153 std::this_thread::yield();
163 template <
class Clock,
class Duration>
177 template <
class Rep,
class Period>
187 template <
class Rep,
class Period>
197 template <
class Rep,
class Period>
209 af.clear(std::memory_order_release);
255 template <
class Clock,
class Duration>
259 template <
class Rep,
class Period>
272 typedef std::lock_guard<duds::general::SpinlockYieldingWrapper>
279 typedef std::unique_lock<duds::general::SpinlockYieldingWrapper>
284 #endif // #ifndef SPINLOCK_HPP bool try_lock()
A single attempt at gaining ownership of the lock.
void lockAlwaysYield()
A yielding wait that ends with ownership of the lock.
bool try_lock_until(const std::chrono::time_point< Clock, Duration > &time)
A spiny busy wait or a yielding wait that ends with ownership of the lock if ownership can be granted...
std::unique_lock< duds::general::Spinlock > UniqueSpinLock
A convenience typedef for a std::unique_lock using the Spinlock object.
void lock()
A spiny busy or yielding wait that ends with ownership of the lock.
A simple wrapper around a Spinlock object that implements the timed lockable concept such that attemp...
std::unique_lock< duds::general::SpinlockYieldingWrapper > UniqueYieldingSpinLock
A convenience typedef for a std::unique_lock using the Spinlock yielding wrapper. ...
bool tryLockAlwaysYeildFor(const std::chrono::duration< Rep, Period > &duration)
A yielding wait that ends with ownership of the lock if ownership can be granted within duration...
bool tryLockAlwaysYeildUntil(const std::chrono::time_point< Clock, Duration > &time)
A yielding wait that ends with ownership of the lock if ownership can be granted before time...
SpinlockYieldingWrapper(Spinlock &l)
void lockNeverYield()
A spiny busy wait that ends with ownership of the lock.
~Spinlock()
Locks the spinlock before destruction to delay destruction in case of a lock.
void unlock()
Releases ownership of the lock.
std::atomic_flag af
Used like a mutually exclusive semaphore, but doesn't involve the operating system.
static bool useYield
True when lock() should call yield in its loop; good for uniprocessor, unicore systems.
A simple spinlock following the lockable and timed lockable concepts so that it can be used with std:...
Spinlock()
Makes a Spinlock in the unlocked state.
bool tryLockNeverYeildFor(const std::chrono::duration< Rep, Period > &duration)
A spiny busy wait that ends with ownership of the lock if ownership can be granted within duration...
bool try_lock_for(const std::chrono::duration< Rep, Period > &duration)
bool try_lock_until(const std::chrono::time_point< Clock, Duration > &time)
bool tryLockNeverYeildUntil(const std::chrono::time_point< Clock, Duration > &time)
A spiny busy wait that ends with ownership of the lock if ownership can be granted before time...
std::lock_guard< duds::general::SpinlockYieldingWrapper > YieldingSpinLockGuard
A convenience typedef for a std::lock_guard using the Spinlock yielding wrapper.
bool try_lock_for(const std::chrono::duration< Rep, Period > &duration)
A spiny busy wait or yielding wait that ends with ownership of the lock if ownership can be granted w...
std::lock_guard< duds::general::Spinlock > SpinLockGuard
A convenience typedef for a std::lock_guard using the Spinlock object.