From 518c2d19e98ecef8b4616085480d66d13d22a702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tomic=CC=8Cevic=CC=81?= <dominik.tomicevic@gmail.com> Date: Sat, 4 Jul 2015 11:50:42 +0200 Subject: [PATCH] moved sync directory to root --- sync/spinlock.hpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 sync/spinlock.hpp diff --git a/sync/spinlock.hpp b/sync/spinlock.hpp new file mode 100644 index 000000000..70559727a --- /dev/null +++ b/sync/spinlock.hpp @@ -0,0 +1,29 @@ +#ifndef MEMGRAPH_UTILS_SYNC_SPINLOCK_HPP +#define MEMGRAPH_UTILS_SYNC_SPINLOCK_HPP + +#include <atomic> +#include <unistd.h> + +class SpinLock +{ +public: + + void lock() + { + // TODO add asm pause and counter first before sleeping + // might be faster, but test this and see + while(lock_flag.test_and_set(std::memory_order_acquire)) + usleep(250); + } + + void unlock() + { + lock_flag.clear(std::memory_order_release); + } + +private: + // guaranteed by standard to be lock free! + std::atomic_flag lock_flag = ATOMIC_FLAG_INIT; +}; + +#endif