| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #pragma once
- #include <atomic>
- namespace BansheeEngine
- {
- /** @addtogroup Threading
- * @{
- */
- /**
- * Synchronization primitive with low overhead.
- *
- * @note
- * However it will actively block the thread waiting for the lock, not allowing any other work to be done, so it is
- * best used for short locks.
- */
- class SpinLock
- {
- public:
- SpinLock()
- {
- mLock.clear(std::memory_order_relaxed);
- }
- /** Lock any following operations with the spin lock, not allowing any other thread to access them. */
- void lock()
- {
- while(mLock.test_and_set(std::memory_order_acquire))
- { }
- }
- /** Release the lock and allow other threads to acquire the lock. */
- void unlock()
- {
- mLock.clear(std::memory_order_release);
- }
- private:
- std::atomic_flag mLock;
- };
- /**
- * Provides a safer method for locking a spin lock as the lock will get automatically locked when this objected is
- * created and unlocked as soon as it goes out of scope.
- */
- class ScopedSpinLock
- {
- public:
- ScopedSpinLock(SpinLock& spinLock)
- :mSpinLock(spinLock)
- {
- mSpinLock.lock();
- }
- ~ScopedSpinLock()
- {
- mSpinLock.unlock();
- }
- private:
- SpinLock& mSpinLock;
- };
- /** @} */
- }
|