Mercurial > octave
diff liboctave/util/oct-refcount.h @ 27447:396996f1dad0
use std::atomic to implement refcount class
* oct-atomic.h, oct-atomic.c: New files.
* liboctave/util/module.mk: Update.
* dim-vector.h (dim_vector::increment_count,
dim_vector::decrement_count): New functions. Use them instead of
OCTAVE_ATOMIC_INCREMENT and OCTAVE_ATOMIC_DECREMENT macros.
* Array.h (Array<T>::jit_ref_count): Return int, not pointer. Change
all uses.
* jit-typeinfo.h, jit-typeinfo.cc (jit_array::m_ref_count): Store
integer, not pointer. Change all uses.
* oct-refcount.h (OCTAVE_ATOMIC_INCREMENT, OCTAVE_ATOMIC_DECREMENT,
OCTAVE_ATOMIC_POST_INCREMENT, OCTAVE_ATOMIC_POST_DECREMENT):
Delete macros.
(refcount::m_count): Now std::atomic<T> instead of T.
(refcount::operator++, refcount::operator--): Define using std::atomic
operators instead of increment macros.
(refcount::get): Delete.
(class refcount): Explicitly delete assignment operator and copy
constructor. Explicitly use default destructor. Eliminate all uses
of assignment as directly setting the reference count value apart from
initialization should not be needed.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Sep 2019 17:57:08 -0400 |
parents | 8498dccc15c7 |
children | b442ec6dda5c |
line wrap: on
line diff
--- a/liboctave/util/oct-refcount.h Thu Sep 26 11:32:52 2019 -0400 +++ b/liboctave/util/oct-refcount.h Wed Sep 25 17:57:08 2019 -0400 @@ -25,48 +25,10 @@ #include "octave-config.h" -#if (defined (OCTAVE_ENABLE_ATOMIC_REFCOUNT) \ - && (defined (__GNUC__) || defined (_MSC_VER))) - -# if defined (__GNUC__) - -# define OCTAVE_ATOMIC_INCREMENT(x) __sync_add_and_fetch (x, 1) -# define OCTAVE_ATOMIC_DECREMENT(x) __sync_add_and_fetch (x, -1) -# define OCTAVE_ATOMIC_POST_INCREMENT(x) __sync_fetch_and_add (x, 1) -# define OCTAVE_ATOMIC_POST_DECREMENT(x) __sync_fetch_and_add (x, -1) - -# elif defined (_MSC_VER) - -# include <intrin.h> - -# define OCTAVE_ATOMIC_INCREMENT(x) \ - _InterlockedIncrement (static_cast<long *> (x)) - -# define OCTAVE_ATOMIC_DECREMENT(x) \ - _InterlockedDecrement (static_cast<long *> (x)) - -# define OCTAVE_ATOMIC_POST_INCREMENT(x) \ - _InterlockedExchangeAdd (static_cast<long *> (x)) - -# define OCTAVE_ATOMIC_POST_DECREMENT(x) \ - _InterlockedExchangeAdd (static_cast<long *> (x)) - -# endif - -#else - -// Generic non-locking versions - -# define OCTAVE_ATOMIC_INCREMENT(x) ++(*(x)) -# define OCTAVE_ATOMIC_DECREMENT(x) --(*(x)) -# define OCTAVE_ATOMIC_POST_INCREMENT(x) (*(x))++ -# define OCTAVE_ATOMIC_POST_DECREMENT(x) (*(x))-- - -#endif +#include <atomic> namespace octave { - // Encapsulates a reference counter. template <typename T> @@ -80,30 +42,36 @@ : m_count (initial_count) { } + refcount (const refcount&) = delete; + + refcount& operator = (const refcount&) = delete; + + ~refcount (void) = default; + // Increment/Decrement. int is postfix. count_type operator++ (void) { - return OCTAVE_ATOMIC_INCREMENT (&m_count); + return ++m_count; } count_type operator++ (int) { - return OCTAVE_ATOMIC_POST_INCREMENT (&m_count); + return m_count++; } count_type operator-- (void) { - return OCTAVE_ATOMIC_DECREMENT (&m_count); + return --m_count; } count_type operator-- (int) { - return OCTAVE_ATOMIC_POST_DECREMENT (&m_count); + return m_count--; } count_type value (void) const { - return static_cast<count_type const volatile&> (m_count); + return m_count.load (); } operator count_type (void) const @@ -111,14 +79,9 @@ return value (); } - count_type * get (void) - { - return &m_count; - } - private: - count_type m_count; + std::atomic<T> m_count; }; }