Marine Library  1.0
C++ library for Linux Networking Development
Public Types | Public Member Functions | Static Public Member Functions | List of all members
CAtomicSync< T > Class Template Reference

GCC __sync_XXX based atomic operations for integral types. More...

#include <atomic_sync.hh>

Public Types

typedef T value_type
 

Public Member Functions

 CAtomicSync (value_type c=value_type())
 Initialize with a value. More...
 
 CAtomicSync (const __Myt &c)
 Initialize from another CAtomicSync. More...
 
value_type load () const volatile
 Get value atomically. More...
 
void store (value_type c) volatile
 Set value atomically. More...
 
value_type fetch_add (value_type c) volatile
 Get value and then ADD atomically. More...
 
value_type add_fetch (value_type c) volatile
 ADD and then get value atomically. More...
 
value_type fetch_sub (value_type c) volatile
 Get value and then SUBTRACT atomically. More...
 
value_type sub_fetch (value_type c) volatile
 SUBTRACT and then get value atomically. More...
 
value_type fetch_or (value_type c) volatile
 Get value and then OR atomically. More...
 
value_type or_fetch (value_type c) volatile
 OR and then get value atomically. More...
 
value_type fetch_and (value_type c) volatile
 Get value and then AND atomically. More...
 
value_type and_fetch (value_type c) volatile
 AND and then get value atomically. More...
 
value_type fetch_xor (value_type c) volatile
 Get value and then XOR atomically. More...
 
value_type xor_fetch (value_type c) volatile
 XOR and then get value atomically. More...
 
value_type swap (value_type c) volatile
 Get value and then set atomically. More...
 
bool compare_swap (value_type expval, value_type newval, value_type *oldval=NULL) volatile
 Compare and set atomically. More...
 
Operators

Those operators just act as what you've already known, except that they are all atomic operations.

value_type operator= (const __Myt &c) volatile
 
value_type operator= (value_type c) volatile
 
 operator value_type () const volatile
 
value_type operator++ () volatile
 
value_type operator-- () volatile
 
value_type operator++ (int) volatile
 
value_type operator-- (int) volatile
 
value_type operator+= (value_type c) volatile
 
value_type operator-= (value_type c) volatile
 
value_type operator|= (value_type c) volatile
 
value_type operator&= (value_type c) volatile
 
value_type operator^= (value_type c) volatile
 

Static Public Member Functions

static value_type load (const value_type *v)
 Get value atomically. More...
 
static void store (value_type *v, value_type c)
 Set value atomically. More...
 
static value_type fetch_add (value_type *v, value_type c)
 Get value and then ADD atomically. More...
 
static value_type add_fetch (value_type *v, value_type c)
 ADD and then get value atomically. More...
 
static value_type fetch_sub (value_type *v, value_type c)
 Get value and then SUBTRACT atomically. More...
 
static value_type sub_fetch (value_type *v, value_type c)
 SUBTRACT and then get value atomically. More...
 
static value_type fetch_or (value_type *v, value_type c)
 Get value and then OR atomically. More...
 
static value_type or_fetch (value_type *v, value_type c)
 OR and then get value atomically. More...
 
static value_type fetch_and (value_type *v, value_type c)
 Get value and then AND atomically. More...
 
static value_type and_fetch (value_type *v, value_type c)
 AND and then get value atomically. More...
 
static value_type fetch_xor (value_type *v, value_type c)
 Get value and then XOR atomically. More...
 
static value_type xor_fetch (value_type *v, value_type c)
 XOR and then get value atomically. More...
 
static value_type swap (value_type *v, value_type c)
 Get value and then set atomically. More...
 
static bool compare_swap (value_type *val, value_type expval, value_type newval, value_type *oldval)
 Compare and set atomically. More...
 

Detailed Description

template<typename T>
class CAtomicSync< T >

GCC __sync_XXX based atomic operations for integral types.

A common use of CAtomicSync is a high performance counter shared between threads or processes.

Template Parameters
TAn integral type, i.e. int, long, etc.

Constructor & Destructor Documentation

template<typename T>
CAtomicSync< T >::CAtomicSync ( value_type  c = value_type())
inlineexplicit

Initialize with a value.

If no value is provided, default to 0.

Parameters
cA value
template<typename T>
CAtomicSync< T >::CAtomicSync ( const __Myt c)
inline

Initialize from another CAtomicSync.

Parameters
cAnother CAtomicSync

Member Function Documentation

template<typename T>
value_type CAtomicSync< T >::add_fetch ( value_type  c) volatile
inline

ADD and then get value atomically.

This function performs {*this += c; return *this;}

Parameters
cA value
Returns
Current value after ADD'ing
template<typename T>
static value_type CAtomicSync< T >::add_fetch ( value_type *  v,
value_type  c 
)
inlinestatic

ADD and then get value atomically.

This function performs {*v += c; return *v;}

Parameters
vPointer to a value
cA value
Returns
Value after ADD'ing
template<typename T>
value_type CAtomicSync< T >::and_fetch ( value_type  c) volatile
inline

AND and then get value atomically.

This function performs {*this &= c; return *this;}

Parameters
cA value
Returns
Current value after AND'ing
template<typename T>
static value_type CAtomicSync< T >::and_fetch ( value_type *  v,
value_type  c 
)
inlinestatic

AND and then get value atomically.

This function performs {*v &= c; return *v;}

Parameters
vPointer to a value
cA value
Returns
Value after AND'ing
template<typename T>
bool CAtomicSync< T >::compare_swap ( value_type  expval,
value_type  newval,
value_type *  oldval = NULL 
) volatile
inline

Compare and set atomically.

This function performs:

if(*this == expval){
*oldval = *this; *this = newval; return true;
}else{
*oldval = *this; return false;
}
Parameters
[in]expvalExpected value
[in]newvalNew value to set
[out]oldvalPointer to a value to receive the old value before setting; Or NULL if not interested
Returns
true if *this == expval ; otherwise false
Note
Setting operation will be performed if and only if compare operation returns true.
template<typename T>
static bool CAtomicSync< T >::compare_swap ( value_type *  val,
value_type  expval,
value_type  newval,
value_type *  oldval 
)
inlinestatic

Compare and set atomically.

This function performs:

if(*val == expval){
*oldval = *val; *val = newval; return true;
}else{
*oldval = *val; return false;
}
Parameters
[in,out]valPointer to a value
[in]expvalExpected value
[in]newvalNew value to set
[out]oldvalPointer to a value to receive the old value before setting; Or NULL if not interested
Returns
true if *this == expval ; otherwise false
Note
Setting operation will be performed if and only if compare operation returns true.
template<typename T>
value_type CAtomicSync< T >::fetch_add ( value_type  c) volatile
inline

Get value and then ADD atomically.

This function performs {T old = *this; *this += c; return old;}

Parameters
cA value
Returns
Current value before ADD'ing
template<typename T>
static value_type CAtomicSync< T >::fetch_add ( value_type *  v,
value_type  c 
)
inlinestatic

Get value and then ADD atomically.

This function performs {T old = *v; *v += c; return old;}

Parameters
vPointer to a value
cA value
Returns
Value before ADD'ing
template<typename T>
value_type CAtomicSync< T >::fetch_and ( value_type  c) volatile
inline

Get value and then AND atomically.

This function performs {T old = *this; *this &= c; return old;}

Parameters
cA value
Returns
Current value before AND'ing
template<typename T>
static value_type CAtomicSync< T >::fetch_and ( value_type *  v,
value_type  c 
)
inlinestatic

Get value and then AND atomically.

This function performs {T old = *v; *v &= c; return old;}

Parameters
vPointer to a value
cA value
Returns
Value before AND'ing
template<typename T>
value_type CAtomicSync< T >::fetch_or ( value_type  c) volatile
inline

Get value and then OR atomically.

This function performs {T old = *this; *this |= c; return old;}

Parameters
cA value
Returns
Current value before OR'ing
template<typename T>
static value_type CAtomicSync< T >::fetch_or ( value_type *  v,
value_type  c 
)
inlinestatic

Get value and then OR atomically.

This function performs {T old = *v; *v |= c; return old;}

Parameters
vPointer to a value
cA value
Returns
Value before OR'ing
template<typename T>
value_type CAtomicSync< T >::fetch_sub ( value_type  c) volatile
inline

Get value and then SUBTRACT atomically.

This function performs {T old = *this; *this -= c; return old;}

Parameters
cA value
Returns
Current value before SUBTRACT'ing
template<typename T>
static value_type CAtomicSync< T >::fetch_sub ( value_type *  v,
value_type  c 
)
inlinestatic

Get value and then SUBTRACT atomically.

This function performs {T old = *v; *v -= c; return old;}

Parameters
vPointer to a value
cA value
Returns
Value before SUBTRACT'ing
template<typename T>
value_type CAtomicSync< T >::fetch_xor ( value_type  c) volatile
inline

Get value and then XOR atomically.

This function performs {T old = *this; *this ^= c; return old;}

Parameters
cA value
Returns
Current value before XOR'ing
template<typename T>
static value_type CAtomicSync< T >::fetch_xor ( value_type *  v,
value_type  c 
)
inlinestatic

Get value and then XOR atomically.

This function performs {T old = *v; *v ^= c; return old;}

Parameters
vPointer to a value
cA value
Returns
Value before XOR'ing
template<typename T>
value_type CAtomicSync< T >::load ( ) const volatile
inline

Get value atomically.

Returns
Current value, i.e. *this
template<typename T>
static value_type CAtomicSync< T >::load ( const value_type *  v)
inlinestatic

Get value atomically.

Parameters
vPointer to a value
Returns
Current value of v, i.e. *v
template<typename T>
value_type CAtomicSync< T >::or_fetch ( value_type  c) volatile
inline

OR and then get value atomically.

This function performs {*this |= c; return *this;}

Parameters
cA value
Returns
Current value after OR'ing
template<typename T>
static value_type CAtomicSync< T >::or_fetch ( value_type *  v,
value_type  c 
)
inlinestatic

OR and then get value atomically.

This function performs {*v |= c; return *v;}

Parameters
vPointer to a value
cA value
Returns
Value after OR'ing
template<typename T>
void CAtomicSync< T >::store ( value_type  c) volatile
inline

Set value atomically.

This function performs {*this = c}.

Parameters
cA Value
template<typename T>
static void CAtomicSync< T >::store ( value_type *  v,
value_type  c 
)
inlinestatic

Set value atomically.

This function performs {*v = c}.

Parameters
vPointer to a value
cA Value
template<typename T>
value_type CAtomicSync< T >::sub_fetch ( value_type  c) volatile
inline

SUBTRACT and then get value atomically.

This function performs {*this -= c; return *this;}

Parameters
cA value
Returns
Current value after SUBTRACT'ing
template<typename T>
static value_type CAtomicSync< T >::sub_fetch ( value_type *  v,
value_type  c 
)
inlinestatic

SUBTRACT and then get value atomically.

This function performs {*v -= c; return *v;}

Parameters
vPointer to a value
cA value
Returns
Value after SUBTRACT'ing
template<typename T>
value_type CAtomicSync< T >::swap ( value_type  c) volatile
inline

Get value and then set atomically.

This function performs {T old = *this; *this = c; return old;}

Parameters
cA value
Returns
Value before setting
template<typename T>
static value_type CAtomicSync< T >::swap ( value_type *  v,
value_type  c 
)
inlinestatic

Get value and then set atomically.

This function performs {T old = *v; *v = c; return old;}

Parameters
vPointer to a value
cA value
Returns
Value before setting
template<typename T>
value_type CAtomicSync< T >::xor_fetch ( value_type  c) volatile
inline

XOR and then get value atomically.

This function performs {*this ^= c; return *this;}

Parameters
cA value
Returns
Current value after XOR'ing
template<typename T>
static value_type CAtomicSync< T >::xor_fetch ( value_type *  v,
value_type  c 
)
inlinestatic

XOR and then get value atomically.

This function performs {*v ^= c; return *v;}

Parameters
vPointer to a value
cA value
Returns
Value after XOR'ing

The documentation for this class was generated from the following file: