Files
memgraph/include/threading/sync/spinlock.hpp
sale 99b8a4f234 Added Skiplist ReverseIterator, Distance Approximation Prototype and documented some stuff
Summary: Skiplist ReverseIterator and Distance Approximation

Test Plan: manual

Reviewers: florijan, buda

Reviewed By: buda

Subscribers: pullbot, florijan, buda

Differential Revision: https://phabricator.memgraph.io/D44
2017-01-31 15:16:38 +01:00

29 lines
692 B
C++

#pragma once
#include <unistd.h>
#include <atomic>
#include "utils/cpu_relax.hpp"
/**
* @class SpinLock
*
* @brief
* Spinlock is used as an locking mechanism based on an atomic flag and
* waiting loops. It uses the cpu_relax "asm pause" command to optimize wasted
* time while the threads are waiting.
*
*/
class SpinLock {
public:
void lock() { // Before was memory_order_acquire
while (lock_flag.test_and_set(std::memory_order_seq_cst)) cpu_relax();
}
// Before was memory_order_release
void unlock() { lock_flag.clear(std::memory_order_seq_cst); }
private:
// guaranteed by standard to be lock free!
mutable std::atomic_flag lock_flag = ATOMIC_FLAG_INIT;
};