tas_spin_lock.cpp 796 Bytes
Newer Older
FritzFlorian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "pls/internal/helpers/profiler.h"
#include "pls/internal/base/tas_spin_lock.h"

namespace pls {
    namespace internal {
        namespace base {
            void tas_spin_lock::lock() {
                PROFILE_LOCK("Acquire Lock")
                int tries = 0;
                while (flag_.test_and_set(std::memory_order_acquire)) {
                    tries++;
                    if (tries % yield_at_tries_ == 0) {
                        this_thread::yield();
                    }
                }
            }

            bool tas_spin_lock::try_lock() {
                return flag_.test_and_set(std::memory_order_acquire) == 0;
            }

            void tas_spin_lock::unlock() {
                flag_.clear(std::memory_order_release);
            }
        }
    }
}