medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 7b40c4733d292269f4bae6a29051c15f2d48e56f
parent c7bebce9685ae788c8cda88bc84e9456d95668b8
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu Dec 15 21:11:33 +0200

NONCOPYABLE macro

Diffstat:
benchmark.cc | 5++++-
intrinsics.h | 2++
nonblocking_fixed_spsc_queue.h | 2++
platform_atomic.h | 48++++++++++++++++++++++++++++--------------------
4 files changed, 36 insertions(+), 21 deletions(-)
diff --git a/benchmark.cc b/benchmark.cc @@ -10,6 +10,9 @@ #define MAX_TIMERS 4096 struct Timer { + NONCOPYABLE( Timer ); + Timer() { } + const char * fn; const char * file; int line; @@ -19,7 +22,7 @@ struct Timer { }; static Timer timers[ 4096 ]; -static atomic_u32 num_timers = { 0 }; +static atomic_u32 num_timers( 0 ); ScopedTimer::ScopedTimer( u32 idx ) { initial_clock = __rdtsc(); diff --git a/intrinsics.h b/intrinsics.h @@ -135,6 +135,8 @@ inline void mike_assert( const bool predicate, const char * message ) { #define STATIC_ASSERT( p ) static int CONCAT( STATIC_ASSERT, __COUNTER__ )[ int( p ) - 1 ] #endif +#define NONCOPYABLE( T ) T( const T & ) = delete; void operator=( const T & ) = delete; + template< typename F > struct ScopeExit { ScopeExit( F f_ ) : f( f_ ) { } diff --git a/nonblocking_fixed_spsc_queue.h b/nonblocking_fixed_spsc_queue.h @@ -66,6 +66,8 @@ public: private: enum LastOp { READ, WRITE }; struct Node { + NONCOPYABLE( Node ); + Node() { } T data; atomic_s32 last_op; }; diff --git a/platform_atomic.h b/platform_atomic.h @@ -14,26 +14,6 @@ #if COMPILER_GCCORCLANG && !PLATFORM_RELACY -struct atomic_s32 { - volatile s32 v; - atomic_s32 & operator=( atomic_s32 & rhs ) = delete; -}; - -struct atomic_s64 { - volatile s64 v; - atomic_s64 & operator=( atomic_s64 & rhs ) = delete; -}; - -struct atomic_u32 { - volatile u32 v; - atomic_u32 & operator=( atomic_u32 & rhs ) = delete; -}; - -struct atomic_u64 { - volatile u64 v; - atomic_u64 & operator=( atomic_u64 & rhs ) = delete; -}; - #define load_relaxed( atom ) __atomic_load_n( &( atom )->v, __ATOMIC_RELAXED ) #define store_relaxed( atom, x ) __atomic_store_n( &( atom )->v, ( x ), __ATOMIC_RELAXED ) #define fetch_add_relaxed( atom, x ) __atomic_fetch_add( &( atom )->v, ( x ), __ATOMIC_RELAXED ) @@ -65,6 +45,34 @@ struct atomic_u64 { #define exchange_seqcst( atom, x ) __atomic_exchange_n( &( atom )->v, ( x ), __ATOMIC_SEQ_CST ) #define compare_exchange_seqcst( atom, before, after ) __atomic_compare_exchange_n( &( atom )->v, ( before ), ( after ), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) +struct atomic_s32 { + NONCOPYABLE( atomic_s32 ); + atomic_s32() { store_relaxed( this, 0 ); } + atomic_s32( s32 x ) { store_relaxed( this, x ); } + volatile s32 v; +}; + +struct atomic_s64 { + NONCOPYABLE( atomic_s64 ); + atomic_s64() { store_relaxed( this, 0 ); } + atomic_s64( s64 x ) { store_relaxed( this, x ); } + volatile s64 v; +}; + +struct atomic_u32 { + NONCOPYABLE( atomic_u32 ); + atomic_u32() { store_relaxed( this, 0 ); } + atomic_u32( u32 x ) { store_relaxed( this, x ); } + volatile u32 v; +}; + +struct atomic_u64 { + NONCOPYABLE( atomic_u64 ); + atomic_u64() { store_relaxed( this, 0 ); } + atomic_u64( u64 x ) { store_relaxed( this, x ); } + volatile u64 v; +}; + // TODO: maybe use these instead? // s32 load_relaxed( atomic_s32 * atom ) { // return __atomic_load_n( &atom->v, __ATOMIC_RELAXED );