Commit 7778e32d authored by NICLAUSSE Nicolas's avatar NICLAUSSE Nicolas
Browse files

add dtkSpinLock

parent 5156871f
......@@ -32,7 +32,8 @@ set(${PROJECT_NAME}_HEADERS
dtkCoreSettings
dtkCoreSettings.h
dtkArrayData
dtkArrayData.h)
dtkArrayData.h
dtkSpinLock.h)
set(${PROJECT_NAME}_SOURCES
dtkCoreSettings.cpp
......
/* dtkSpinLock.h ---
*
* Author: Nicolas Niclausse
* Created: 29 jul 2014
*/
#pragma once
#include <QAtomicInt>
//
// https://gist.github.com/aperezdc/5582452
class dtkSpinLock: private QAtomicInt
{
public:
class Acquire {
public:
Acquire(dtkSpinLock& spinLock): m_spinLock(spinLock)
{ m_spinLock.lock(); }
~Acquire()
{ m_spinLock.unlock(); }
private:
dtkSpinLock& m_spinLock;
// Disable copy constructor and assignment operator
Acquire& operator=(const Acquire&);
Acquire(const Acquire&);
};
dtkSpinLock(): QAtomicInt(Unlocked) {}
void lock() {
while (!testAndSetOrdered(Unlocked, Locked));
}
void unlock() {
while (!testAndSetOrdered(Locked, Unlocked));
}
bool tryLock() {
return testAndSetOrdered(Unlocked, Locked);
}
private:
static const int Unlocked = 1;
static const int Locked = 0;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment