spin_lock.cpp 699 Bytes
Newer Older
1 2 3 4 5
#include "pls/internal/base/spin_lock.h"

namespace pls {
    namespace internal {
        namespace base {
6 7 8 9 10 11 12 13
            void spin_lock::lock() {
                int tries = 0;
                while (flag_.test_and_set(std::memory_order_acquire)) {
                    tries++;
                    if (tries % yield_at_tries_ == 0) {
                        this_thread::yield();
                    }
                }
14
                std::atomic_thread_fence(std::memory_order_seq_cst);
15 16 17 18
            }

            void spin_lock::unlock() {
                flag_.clear(std::memory_order_release);
19
                std::atomic_thread_fence(std::memory_order_seq_cst);
20
            }
21 22 23
        }
    }
}