C++ (Qt)#ifndef SPINLOCK_H#define SPINLOCK_H #include <assert.h>#include <cstdatomic> template <class T>class SpinLock{public: typedef atomic<int> AtomicInt; SpinLock() : m_count_readers(0), m_state_write(-1) {} void setData(const T &data) { while (!acquireWrite(m_count_readers, m_state_write)); m_data = data; releaseWrite(m_state_write); } T getData() const { while (!acquireRead(m_count_readers, m_state_write)); T data = m_data; releaseRead(m_count_readers, m_state_write); return data; }private: AtomicInt m_count_readers; AtomicInt m_state_write; T m_data; static bool acquireWrite(AtomicInt &readers, AtomicInt &state_write) { bool tmp = (++state_write == 0); state_write = 0; return (tmp && !readers); } //------------------------------------------------------------------------- static void releaseWrite(AtomicInt &state_write) { state_write = -1; } //------------------------------------------------------------------------- static bool acquireRead(AtomicInt &readers, AtomicInt &state_write) { readers++; bool tmp = (++state_write == 0); state_write = 0; if (tmp) { state_write = -1; return true; } readers--; return false; } //------------------------------------------------------------------------- static void releaseRead(AtomicInt &readers, AtomicInt &state_write) { if(--readers == 0) state_write = -1; } //-------------------------------------------------------------------------}; #endif // SPINLOCK_H
C++ (Qt) static bool acquireWrite(AtomicInt &readers, AtomicInt &state_write) { bool tmp = (++state_write == 0); state_write = 0; return (tmp && !readers); } static void releaseRead(AtomicInt &readers, AtomicInt &state_write) { if(--readers == 0) state_write = -1; }};
C++ (Qt)static bool acquireWrite(AtomicInt &readers, AtomicInt &state_write) { bool tmp = (++state_write == 0); if (tmp && !readers) return true; state_write = 0; return false; }
C++ (Qt)#ifndef SPINLOCK_H#define SPINLOCK_H #include <assert.h>#include <cstdatomic> template <class T>class SpinLock{public: typedef atomic<int> AtomicInt; SpinLock() : m_key(-1), m_state(STATE_FREE) {} void setData(const T &data) { while (!acquireWrite(m_key, m_state)); m_data = data; releaseWrite(m_key, m_state); } T getData() const { while (!acquireRead(m_key, m_state)); T data = m_data; releaseRead(m_key, m_state); return data; }private: enum { STATE_FREE = 0, STATE_READ = 1, STATE_WRITE = 2, STATE_READ_AND_WRITE = 3 }; AtomicInt m_key; AtomicInt m_state; T m_data; //------------------------------------------------------------------------- static bool acquireWrite(AtomicInt &key, AtomicInt &state) { int tmp = (state |= STATE_WRITE); if (tmp == STATE_READ_AND_WRITE) { state = STATE_READ; return false; } bool tmp2 = (++key == 0); key = 0; return tmp2; } //------------------------------------------------------------------------- static void releaseWrite(AtomicInt &key, AtomicInt &state) { state = STATE_FREE; key = -1; } //------------------------------------------------------------------------- static bool acquireRead(AtomicInt &/*key*/, AtomicInt &state) { int tmp = (state |= STATE_READ); if (tmp == STATE_READ) return true; state = STATE_WRITE; return false; } //------------------------------------------------------------------------- static void releaseRead(AtomicInt &/*key*/, AtomicInt &state) { state = STATE_FREE; } //-------------------------------------------------------------------------}; #endif // SPINLOCK_H
C++ (Qt)#ifndef SPINLOCK_H#define SPINLOCK_H #include <assert.h>#include <cstdatomic> template <class T>class SpinLock{public: typedef atomic<int> AtomicInt; SpinLock() : m_key(-1), m_state(STATE_FREE), m_counter(0) {} void setData(const T &data) { while (!acquireWrite(m_key, m_state)); m_data = data; releaseWrite(m_key, m_state); } T getData() const { while (!acquireRead(m_counter, m_state)); T data = m_data; releaseRead(m_counter, m_state); return data; }private: enum { STATE_FREE = 0, STATE_READ = 1, STATE_WRITE = 2, STATE_READ_AND_WRITE = 3 }; AtomicInt m_key; AtomicInt m_state; AtomicInt m_counter; T m_data; //------------------------------------------------------------------------- static bool acquireWrite(AtomicInt &key, AtomicInt &state) { int tmp = (state |= STATE_WRITE); if (tmp == STATE_READ_AND_WRITE) { state = STATE_READ; return false; } bool tmp2 = (++key == 0); key = 0; return tmp2; } //------------------------------------------------------------------------- static void releaseWrite(AtomicInt &key, AtomicInt &state) { state = STATE_FREE; key = -1; } //------------------------------------------------------------------------- static bool acquireRead(AtomicInt &counter, AtomicInt &state) { int tmp = (state |= STATE_READ); if (tmp == STATE_READ) { counter++; return true; } state = STATE_WRITE; return false; } //------------------------------------------------------------------------- static void releaseRead(AtomicInt &counter, AtomicInt &state) { if (--counter == 0) state = STATE_FREE; } //-------------------------------------------------------------------------}; #endif // SPINLOCK_H
C++ (Qt) static bool acquireRead(AtomicInt &counter, AtomicInt &state) { int tmp = (state |= STATE_READ); if (tmp == STATE_READ) {
C++ (Qt) static bool acquireWrite(AtomicInt &key, AtomicInt &state) { int tmp = (state |= STATE_WRITE); if (tmp == STATE_READ_AND_WRITE) { state = STATE_READ; return false; }