ITAAtomicPrimitives.h 10.2 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
/*
2 3 4 5
 * ----------------------------------------------------------------
 *
 *		ITA core libs
 *		(c) Copyright Institute of Technical Acoustics (ITA)
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
6
 *		RWTH Aachen University, Germany, 2015-2020
7 8 9 10 11 12 13 14 15 16 17
 *
 * ----------------------------------------------------------------
 *				    ____  __________  _______
 *				   //  / //__   ___/ //  _   |
 *				  //  /    //  /    //  /_|  |
 *				 //  /    //  /    //  ___   |
 *				//__/    //__/    //__/   |__|
 *
 * ----------------------------------------------------------------
 *
 */
Jonas Stienen's avatar
Jonas Stienen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

#ifndef INCLUDE_WATCHER_ITA_ATOMIC_PRIMITIVES
#define INCLUDE_WATCHER_ITA_ATOMIC_PRIMITIVES

#include <ITABaseDefinitions.h>

#include <ITAAtomicOps.h>
#include <ITATypes.h>

#include <assert.h>


class ITAAtomicInt
{
public:
	//! Standard-Konstruktor
	inline ITAAtomicInt() : m_iValue( 0 ) {};

	//! Initialisierungs-Konstruktor
	inline ITAAtomicInt( int iInitialValue ) : m_iValue( iInitialValue ) {};

	//! Getter
	inline int get() const {
		return atomic_read_int((volatile const int*) &m_iValue);
	};

	//! Setter
	inline void set( const int iValue ) {
		atomic_write_int(&m_iValue, iValue);
	};

	//! Compare-and-swap (CAS)
	inline bool cas( int iExpectedValue, int iNewValue ) {
		return atomic_cas_int(&m_iValue, iExpectedValue, iNewValue);
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicInt = ITAAtomicInt')
	inline ITAAtomicInt& operator=( const ITAAtomicInt& iNewValue ) {
		set(iNewValue.get());
		return *this;
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicInt = int')
	inline ITAAtomicInt& operator=( const int iNewValue ) {
		set(iNewValue);
		return *this;
	};

	//! Cast to int operator (required for assignments of the form 'int = ITAAtomicInt')
	inline  operator int() const { return get(); }

	//! Increment
	inline 	ITAAtomicInt& operator++( ) {
		atomic_inc_int(&m_iValue);
		return *this;
	};

	//! Decrement
	inline ITAAtomicInt& operator--( ) {
		atomic_dec_int(&m_iValue);
		return *this;
	};

	// TODO: Mehr Operatoren nach Bedarf
	inline bool operator==( const ITAAtomicInt& rhs ) const { return ( get() == rhs.get() ); };
	inline bool operator!=( const ITAAtomicInt& rhs ) const { return ( get() != rhs.get() ); };

	inline bool operator==( const int& rhs ) const { return ( get() == rhs ); };
	inline bool operator!=( const int& rhs ) const { return ( get() != rhs ); };

private:
	volatile int m_iValue;
};

class ITAAtomicLong
{
public:
	//! Standard-Konstruktor
	inline ITAAtomicLong() : m_lValue( 0 ) {};

	//! Initialisierungs-Konstruktor
	inline ITAAtomicLong( long lInitialValue ) : m_lValue( lInitialValue ) {};

	//! Getter
	inline long get() const {
		return atomic_read_long((volatile const long*) &m_lValue);
	};

	//! Setter
	inline void set( const long lValue ) {
		atomic_write_long((volatile long*) &m_lValue, lValue);
	};

	//! Compare-and-swap (CAS)
	inline bool cas( long lExpectedValue, long lNewValue ) {
		return atomic_cas_long((volatile long*) &m_lValue, lExpectedValue, lNewValue);
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicLong = ITAAtomicLong')
	inline ITAAtomicLong& operator=( const ITAAtomicLong& lNewValue ) {
		set(lNewValue.get());
		return *this;
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicLong = long')
	inline ITAAtomicLong& operator=( const long lNewValue ) {
		set(lNewValue);
		return *this;
	};

	//! Cast to long operator (required for assignments of the form 'long = ITAAtomicLong')
	inline  operator long() const { return get(); };

	// TODO: Mehr Operatoren nach Bedarf
	inline bool operator==( const ITAAtomicLong& rhs ) const { return ( get() == rhs.get() ); };
	inline bool operator!=( const ITAAtomicLong& rhs ) const { return ( get() != rhs.get() ); };

	inline bool operator==( const long& rhs ) const { return ( get() == rhs ); };
	inline bool operator!=( const long& rhs ) const { return ( get() != rhs ); };
	
	inline bool operator==( const int& rhs ) const { return ( get() == rhs ); };
	inline bool operator!=( const int& rhs ) const { return ( get() != rhs ); };

private:
	volatile long m_lValue;
};

class ITAAtomicBool
{
public:
	/*
	 *  Hinweis: Boolesche Typen werden hier mittels Integers umgesetzt
	 */

	//! Standard-Konstruktor
	inline ITAAtomicBool() : m_iState( false ) {};

	//! Initialisierungs-Konstruktor
	inline ITAAtomicBool( bool bInitialState ) : m_iState( bInitialState ) {};

	//! Getter
	inline bool get() const {
		return (atomic_read_int((volatile const int*) &m_iState) != 0);
	};

	//! Setter
	inline void set( const bool bState ) {
		atomic_write_int(&m_iState, bState);
	}

	//! Compare-and-swap (CAS)
	inline bool cas( bool bExpectedState, bool bNewState ) {
		return atomic_cas_int(&m_iState, bExpectedState, bNewState);
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicBool = ITAAtomicBool')
	inline ITAAtomicBool& operator=( const ITAAtomicBool& bNewState ) {
		set(bNewState.get());
		return *this;
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicBool = bool')
	inline ITAAtomicBool& operator=( const bool bNewState ) {
		set(bNewState);
		return *this;
	};

	//! Cast to int operator (required for assignments of the form 'bool = ITAAtomicBool')
	inline operator bool() const { return get(); };

	// TODO: Mehr Operatoren nach Bedarf
	inline bool operator==( const ITAAtomicBool& rhs ) const { return ( get() == rhs.get() ); };
	inline bool operator!=( const ITAAtomicBool& rhs ) const { return ( get() != rhs.get() ); };

	inline bool operator==( const bool& rhs ) const { return ( get() == rhs ); };
	inline bool operator!=( const bool& rhs ) const { return ( get() != rhs ); };

private:
	volatile int m_iState;
};

class ITAAtomicFloat
{
public:
	//! Standard-Konstruktor
	inline ITAAtomicFloat() : m_fValue( 0 ) {};

	//! Initialisierungs-Konstruktor
	inline ITAAtomicFloat( float fInitialValue ) : m_fValue( fInitialValue ) {};

	//! Getter
	inline float get() const {
		return atomic_read_float((volatile const float*) &m_fValue);
	};

	//! Setter
	inline void set( const float fValue ) {
		atomic_write_float(&m_fValue, fValue);
	};

	/* TODO:
	//! Compare-and-swap (CAS)
	inline bool cas(float fExpectedValue, float fNewValue) {
		return atomic_cas_float(&m_fValue, fExpectedValue, fNewValue);
	};
	*/

	//! Assignment operator (required for assignments of the form 'ITAAtomicFloat = ITAAtomicFloat')
	inline ITAAtomicFloat& operator=( const ITAAtomicFloat& fNewValue ) {
		set(fNewValue.get());
		return *this;
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicFloat = float')
	inline ITAAtomicFloat& operator=( const float fNewValue ) {
		set(fNewValue);
		return *this;
	};

	//! Cast to float operator (required for assignments of the form 'float = ITAAtomicFloat')
	inline  operator float() const {
		return get(); 
	};

	//! Compare operator (ITAAtomicFloat == ITAAtomicFloat)
	inline bool operator==( const ITAAtomicFloat& rhs ) const {
		return (get() == rhs.get()); 
	};

	//! Complementary compare operator (ITAAtomicFloat != ITAAtomicFloat)
	inline bool operator!=( const ITAAtomicFloat& rhs ) const {
		return (get() != rhs.get()); 
	};

	//! Compare operator (ITAAtomicFloat == float)
	inline bool operator==( const float& rhs ) const { return ( get() == rhs ); }
	
	//! Complementary compare operator (ITAAtomicFloat != float)
	inline bool operator!=( const float& rhs ) const {
		return (get() != rhs); 
	};

	//! Add and Assign operator (ITAAtomicFloat += ITAAtomicFloat)
	inline ITAAtomicFloat& operator+=( const ITAAtomicFloat& rhs ) {
		set(get() + rhs.get()); 
		return *this; 
	};

	//! Add and Assign operator (ITAAtomicFloat += float)
	inline ITAAtomicFloat& operator+=( const float& rhs ) {
		set(get() + rhs);
		return *this; 
	};

	//! Subtract and Assign operator (ITAAtomicFloat -= ITAAtomicFloat)
	inline ITAAtomicFloat& operator-=( const ITAAtomicFloat& rhs ) {
		set(get() - rhs.get()); 
		return *this; 
	};

	//! Subtract and Assign operator (ITAAtomicFloat -= float)
	inline ITAAtomicFloat& operator-=( const float& rhs ) {
		set(get() - rhs); 
		return *this; 
	};

	//! Devide and Assign operator (ITAAtomicFloat /= ITAAtomicFloat)
	inline ITAAtomicFloat& operator/=( const ITAAtomicFloat& rhs ) {
		assert( rhs.get() != 0.0f ); // Give hint on devision-by-zero in debug mode
		set(get() / rhs.get()); 
		return *this; 
	};

	//! Devide and Assign operator (ITAAtomicFloat /= float)
	inline ITAAtomicFloat& operator/=( const float& rhs ) {
		assert( rhs != 0.0f ); // Give hint on devision-by-zero in debug mode
		set(get() / rhs); 
		return *this; 
	};

private:
	volatile float m_fValue;
};

template <class T>
class ITAAtomicPtr
{
public:
	//! Standard-Konstruktor
	inline ITAAtomicPtr() : m_pPtr( NULL ) {};

	//! Initialisierungs-Konstruktor
	inline ITAAtomicPtr( T* pInitialPtr ) : m_pPtr( pInitialPtr ) {};

	//! Getter
	inline T* get() const {
		return (T*) atomic_read_ptr((volatile const void**) &m_pPtr);
	};

	//! Setter
	inline void set( const T* pPtr ) {
		atomic_write_ptr(&m_pPtr, (void*) pPtr);
	};

	//! Compare-and-swap (CAS)
	inline bool compareAndSwap( T* iExpectedPtr, T* iNewPtr ) {
		return atomic_cas_ptr(&m_pPtr, iExpectedPtr, iNewPtr);
	};

	inline //! Assignment operator (required for assignments of the form 'ITAAtomicPtr = ITAAtomicPtr')
	ITAAtomicPtr& operator=(const ITAAtomicPtr& pNewPtr) { 
		set(pNewPtr.get());
		return *this;
	};

	//! Assignment operator (required for assignments of the form 'ITAAtomicPtr = int')
	inline ITAAtomicPtr& operator=( const T* pNewPtr ) {
		set(pNewPtr);
		return *this;
	};

	//! Cast to int operator (required for assignments of the form 'int = ITAAtomicPtr')
	inline operator T*( ) const { return get(); };

	// TODO: Mehr Operatoren nach Bedarf
	inline bool operator==( const ITAAtomicPtr& rhs ) const { return ( get() == rhs.get() ); };
	inline bool operator!=( const ITAAtomicPtr& rhs ) const { return ( get() != rhs.get() ); };

	inline bool operator==( const void*& rhs ) const { return ( get() == rhs ); };
	inline bool operator!=( const void*& rhs ) const { return ( get() != rhs ); };

	inline const T& operator*( ) const { return *get(); };
	inline T& operator*( ) { return *get(); };
	inline const T* operator->( ) const { return get(); };
	inline T* operator->( ) { return get(); };

private:
	volatile void* m_pPtr;
};


#endif // INCLUDE_WATCHER_ITA_ATOMIC_PRIMITIVES