summaryrefslogtreecommitdiffstats
path: root/raul
diff options
context:
space:
mode:
Diffstat (limited to 'raul')
-rw-r--r--raul/AtomicInt.hpp96
-rw-r--r--raul/AtomicPtr.hpp64
-rw-r--r--raul/DoubleBuffer.hpp9
-rw-r--r--raul/Maid.hpp17
-rw-r--r--raul/SRMWQueue.hpp40
-rw-r--r--raul/SRSWQueue.hpp18
6 files changed, 41 insertions, 203 deletions
diff --git a/raul/AtomicInt.hpp b/raul/AtomicInt.hpp
deleted file mode 100644
index 172af6b..0000000
--- a/raul/AtomicInt.hpp
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- This file is part of Raul.
- Copyright 2007-2012 David Robillard <http://drobilla.net>
-
- Raul is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- Raul is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Raul. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef RAUL_ATOMIC_INT_HPP
-#define RAUL_ATOMIC_INT_HPP
-
-#include "raul/barrier.hpp"
-
-namespace Raul {
-
-/** Atomic integer.
- * \ingroup raul
- */
-class AtomicInt {
-public:
- inline AtomicInt(int val) : _val(val) {}
-
- inline AtomicInt(const AtomicInt& copy) : _val(copy.get()) {}
-
- inline int get() const {
- Raul::barrier();
- return _val;
- }
-
- inline void operator=(int val) {
- _val = val;
- Raul::barrier();
- }
-
- inline void operator=(const AtomicInt& val) {
- _val = val.get();
- Raul::barrier();
- }
-
- inline AtomicInt& operator++() { return operator+=(1); }
- inline AtomicInt& operator--() { return operator-=(1); }
- inline bool operator==(int val) const { return get() == val; }
- inline int operator+(int val) const { return get() + val; }
- inline int operator-(int val) const { return get() - val; }
-
- inline bool operator==(const AtomicInt& other) const {
- Raul::barrier();
- return _val == other._val;
- }
-
- inline AtomicInt& operator+=(int val) {
- __sync_fetch_and_add(&_val, val);
- return *this;
- }
-
- inline AtomicInt& operator-=(int val) {
- __sync_fetch_and_sub(&_val, val);
- return *this;
- }
-
- /** Set value to @a val iff current value is @a old.
- * @return true iff set succeeded.
- */
- inline bool compare_and_exchange(int old, int val) {
- return __sync_bool_compare_and_swap(&_val, old, val);
- }
-
- /** Add val to value.
- * @return value immediately before addition took place.
- */
- inline int exchange_and_add(int val) {
- return __sync_fetch_and_add(&_val, val);
- }
-
- /** Decrement value.
- * @return true if value is now 0, otherwise false.
- */
- inline bool decrement_and_test() {
- return __sync_sub_and_fetch(&_val, 1) == 0;
- }
-
-private:
- int _val;
-};
-
-} // namespace Raul
-
-#endif // RAUL_ATOMIC_INT_HPP
diff --git a/raul/AtomicPtr.hpp b/raul/AtomicPtr.hpp
deleted file mode 100644
index 0dc369b..0000000
--- a/raul/AtomicPtr.hpp
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- This file is part of Raul.
- Copyright 2007-2012 David Robillard <http://drobilla.net>
-
- Raul is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- Raul is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Raul. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef RAUL_ATOMIC_PTR_HPP
-#define RAUL_ATOMIC_PTR_HPP
-
-#include <cstddef>
-
-#include "raul/barrier.hpp"
-
-namespace Raul {
-
-/** Atomic pointer.
- * \ingroup raul
- */
-template<typename T>
-class AtomicPtr {
-public:
- inline AtomicPtr() : _val(NULL) {}
-
- inline AtomicPtr(const AtomicPtr& copy) : _val(copy.get()) {}
-
- inline T* get() const {
- Raul::barrier();
- return static_cast<T*>(_val);
- }
-
- inline bool operator==(const AtomicPtr& other) const {
- Raul::barrier();
- return _val == other._val;
- }
-
- inline void operator=(T* val) {
- _val = val;
- Raul::barrier();
- }
-
- /** Set value to @a val iff current value is @a old.
- * @return true iff set succeeded.
- */
- inline bool compare_and_exchange(void* old, void* val) {
- return __sync_bool_compare_and_swap(&_val, old, val);
- }
-
-private:
- void* _val;
-};
-
-} // namespace Raul
-
-#endif // RAUL_ATOMIC_PTR_HPP
diff --git a/raul/DoubleBuffer.hpp b/raul/DoubleBuffer.hpp
index 28080d6..092d529 100644
--- a/raul/DoubleBuffer.hpp
+++ b/raul/DoubleBuffer.hpp
@@ -17,8 +17,7 @@
#ifndef RAUL_DOUBLE_BUFFER_HPP
#define RAUL_DOUBLE_BUFFER_HPP
-#include "raul/AtomicInt.hpp"
-#include "raul/AtomicPtr.hpp"
+#include <atomic>
namespace Raul {
@@ -88,9 +87,9 @@ private:
RAUL_DB_LOCK_READ
};
- AtomicInt _state;
- AtomicPtr<T> _read_val;
- T _vals[2];
+ std::atomic<States> _state;
+ std::atomic<T*> _read_val;
+ T _vals[2];
};
} // namespace Raul
diff --git a/raul/Maid.hpp b/raul/Maid.hpp
index f7870ec..337d7f9 100644
--- a/raul/Maid.hpp
+++ b/raul/Maid.hpp
@@ -17,7 +17,8 @@
#ifndef RAUL_MAID_HPP
#define RAUL_MAID_HPP
-#include "raul/AtomicPtr.hpp"
+#include <atomic>
+
#include "raul/Disposable.hpp"
#include "raul/Manageable.hpp"
#include "raul/Noncopyable.hpp"
@@ -50,8 +51,8 @@ public:
inline void dispose(Disposable* obj) {
if (obj) {
while (true) {
- obj->_maid_next = _disposed.get();
- if (_disposed.compare_and_exchange(obj->_maid_next, obj)) {
+ obj->_maid_next = _disposed.load();
+ if (_disposed.compare_exchange_strong(obj->_maid_next, obj)) {
return;
}
}
@@ -83,14 +84,14 @@ public:
// Atomically get the head of the disposed list
Disposable* disposed;
while (true) {
- disposed = _disposed.get();
- if (_disposed.compare_and_exchange(disposed, NULL)) {
+ disposed = _disposed.load();
+ if (_disposed.compare_exchange_strong(disposed, NULL)) {
break;
}
}
// Free the disposed list
- for (Disposable* obj = _disposed.get(); obj;) {
+ for (Disposable* obj = _disposed.load(); obj;) {
Disposable* const next = obj->_maid_next;
delete obj;
obj = next;
@@ -107,8 +108,8 @@ public:
}
private:
- AtomicPtr<Disposable> _disposed;
- SharedPtr<Manageable> _managed;
+ std::atomic<Disposable*> _disposed;
+ SharedPtr<Manageable> _managed;
};
} // namespace Raul
diff --git a/raul/SRMWQueue.hpp b/raul/SRMWQueue.hpp
index ac790ea..12b4b19 100644
--- a/raul/SRMWQueue.hpp
+++ b/raul/SRMWQueue.hpp
@@ -21,7 +21,6 @@
#include <cstdlib>
#include <cmath>
-#include "raul/AtomicInt.hpp"
#include "raul/Noncopyable.hpp"
namespace Raul {
@@ -71,16 +70,15 @@ public:
private:
- // Note that _front doesn't need to be an AtomicInt since it's only accessed
- // by the (single) reader thread
+ // Note that _front needn't be atomic since it's only used by reader
- unsigned _front; ///< Circular index of element at front of queue (READER ONLY)
- AtomicInt _back; ///< Circular index 1 past element at back of queue (WRITERS ONLY)
- AtomicInt _write_space; ///< Remaining free space for new elements (all threads)
- const unsigned _size; ///< Size of @ref _objects (you can store _size-1 objects)
+ unsigned _front; ///< Circular index of element at front of queue (READER ONLY)
+ std::atomic<int> _back; ///< Circular index 1 past element at back of queue (WRITERS ONLY)
+ std::atomic<int> _write_space; ///< Remaining free space for new elements (all threads)
+ const unsigned _size; ///< Size of @ref _objects (you can store _size-1 objects)
- T* const _objects; ///< Fixed array containing queued elements
- AtomicInt* const _valid; ///< Parallel array to _objects, whether loc is written or not
+ T* const _objects; ///< Fixed array containing queued elements
+ std::atomic<bool>* const _valid; ///< Parallel array to _objects, whether loc is written or not
};
template<typename T>
@@ -90,20 +88,21 @@ SRMWQueue<T>::SRMWQueue(size_t size)
, _write_space(size)
, _size(size+1)
, _objects((T*)calloc(_size, sizeof(T)))
- , _valid((AtomicInt*)calloc(_size, sizeof(AtomicInt)))
+ , _valid(new std::atomic<bool>[_size])
{
assert(log2(size) - (int)log2(size) == 0);
assert(size > 1);
- assert(_size-1 == (unsigned)_write_space.get());
+ assert(_size-1 == (unsigned)_write_space.load());
for (unsigned i = 0; i < _size; ++i) {
- assert(_valid[i].get() == 0);
+ assert(!_valid[i]);
}
}
template <typename T>
SRMWQueue<T>::~SRMWQueue()
{
+ delete _valid;
free(_objects);
}
@@ -115,7 +114,7 @@ template <typename T>
inline bool
SRMWQueue<T>::full() const
{
- return (_write_space.get() <= 0);
+ return (_write_space.load() <= 0);
}
/** Push an item onto the back of the SRMWQueue - realtime-safe, not thread-safe.
@@ -129,7 +128,7 @@ template <typename T>
inline bool
SRMWQueue<T>::push(const T& elem)
{
- const int old_write_space = _write_space.exchange_and_add(-1);
+ const int old_write_space = _write_space--;
const bool already_full = (old_write_space <= 0);
/* Technically right here pop could be called in the reader thread and
@@ -146,11 +145,11 @@ SRMWQueue<T>::push(const T& elem)
} else {
// Note: _size must be a power of 2 for this to not explode when _back overflows
- const unsigned write_index = (unsigned)_back.exchange_and_add(1) % _size;
+ const unsigned write_index = _back++ % _size;
- assert(_valid[write_index] == 0);
+ assert(!_valid[write_index]);
_objects[write_index] = elem;
- ++(_valid[write_index]);
+ _valid[write_index] = true;
return true;
}
@@ -164,7 +163,7 @@ template <typename T>
inline bool
SRMWQueue<T>::empty() const
{
- return (_valid[_front].get() == 0);
+ return (!_valid[_front].load());
}
/** Return the element at the front of the queue without removing it.
@@ -190,12 +189,11 @@ template <typename T>
inline void
SRMWQueue<T>::pop()
{
- assert(_valid[_front] == 1);
- --(_valid[_front]);
+ _valid[_front] = false;
_front = (_front + 1) % (_size);
- if (_write_space.get() < 0)
+ if (_write_space.load() < 0)
_write_space = 1;
else
++_write_space;
diff --git a/raul/SRSWQueue.hpp b/raul/SRSWQueue.hpp
index 409497b..b9554a6 100644
--- a/raul/SRSWQueue.hpp
+++ b/raul/SRSWQueue.hpp
@@ -17,9 +17,9 @@
#ifndef RAUL_SRSW_QUEUE_HPP
#define RAUL_SRSW_QUEUE_HPP
+#include <atomic>
#include <cassert>
-#include "raul/AtomicInt.hpp"
#include "raul/Noncopyable.hpp"
namespace Raul {
@@ -60,9 +60,9 @@ public:
inline void pop();
private:
- AtomicInt _front; ///< Index to front of queue (circular)
- AtomicInt _back; ///< Index to back of queue (one past last element) (circular)
- const size_t _size; ///< Size of @ref _objects (you can store _size-1 objects)
+ std::atomic<size_t> _front; ///< Index to front of queue (circular)
+ std::atomic<size_t> _back; ///< Index to back of queue (one past last element) (circular)
+ const size_t _size; ///< Size of @ref _objects (you can store _size-1 objects)
T* const _objects; ///< Fixed array containing queued elements
};
@@ -88,7 +88,7 @@ template <typename T>
inline bool
SRSWQueue<T>::empty() const
{
- return (_back.get() == _front.get());
+ return (_back.load() == _front.load());
}
/** Return whether or not the queue is full.
@@ -97,7 +97,7 @@ template <typename T>
inline bool
SRSWQueue<T>::full() const
{
- return (((_front.get() - _back.get() + _size) % _size) == 1);
+ return (((_front.load() - _back.load() + _size) % _size) == 1);
}
/** Return the element at the front of the queue without removing it
@@ -106,7 +106,7 @@ template <typename T>
inline T&
SRSWQueue<T>::front() const
{
- return _objects[_front.get()];
+ return _objects[_front.load()];
}
/** Push an item onto the back of the SRSWQueue - realtime-safe, not thread-safe.
@@ -121,7 +121,7 @@ SRSWQueue<T>::push(const T& elem)
if (full()) {
return false;
} else {
- unsigned back = _back.get();
+ unsigned back = _back.load();
_objects[back] = elem;
_back = (back + 1) % _size;
return true;
@@ -141,7 +141,7 @@ SRSWQueue<T>::pop()
assert(!empty());
assert(_size > 0);
- _front = (_front.get() + 1) % (_size);
+ _front = (_front.load() + 1) % (_size);
}
} // namespace Raul