spin_lock.cpp 866 Bytes
Newer Older
1
#include "pls/internal/helpers/profiler.h"
2 3 4 5 6
#include "pls/internal/base/spin_lock.h"

namespace pls {
    namespace internal {
        namespace base {
7 8 9
            // TODO: Research/Measure how the memory fences/orders influence this.
            //       For now we simply try to be safe by forcing this lock to
            //       also act as a strict memory fence.
10
            void spin_lock::lock() {
11
                PROFILE_LOCK("Acquire Lock")
12
                int tries = 0;
13
                while (flag_.test_and_set(std::memory_order_seq_cst)) {
14 15 16 17 18 19 20 21
                    tries++;
                    if (tries % yield_at_tries_ == 0) {
                        this_thread::yield();
                    }
                }
            }

            void spin_lock::unlock() {
22
                flag_.clear(std::memory_order_seq_cst);
23
            }
24 25 26
        }
    }
}