Commit cbda47f6 authored by Jonas Stienen's avatar Jonas Stienen

Updating GCC built-in atomics as impl for ITA atomic ops / primitives. And...

Updating GCC built-in atomics as impl for ITA atomic ops / primitives. And some more tests with VistaTicker.
parent 3fce2f65
/*
+-----------------------------------------------------------------------+
| |
| ITAToolkit |
| |
| (c) Copyright Institut für technische Akustik (ITA) |
| Aachen university of technology (RWTH), 2005-2008 |
| |
+-----------------------------------------------------------------------+
| |
| File: ITAAtomicOps.cpp |
| Purpose: Atomare Grundfunktionen für non-blocking concurrency |
| Authors: Frank Wefers (Frank.Wefers@akustik.rwth-aachen.de |
| |
+-----------------------------------------------------------------------+
*/
// $Id: ITAAtomicOpsGCCBuiltinsImpl.cpp 3454 2013-08-21 14:40:16Z stienen $
#include "ITAAtomicOps.h"
#include <stdint.h>
typedef union {
typedef union
{
int32_t i32Value;
float floatValue;
} ConvI32Float;
int atomic_read_int(volatile const int* src)
int atomic_read_int( volatile const int* src )
{
/*
* Trick: Realisierung mittels CAS!
* Falls der Wert schon 0, dann wird er gegen 0 ausgetauscht...
* Also keine Änderung!
*/
return __sync_val_compare_and_swap( (int*) src, 0, 0 );
* Trick: Realisierung mittels CAS!
* Falls der Wert schon 0, dann wird er gegen 0 ausgetauscht...
* Also keine Änderung!
*/
return __sync_val_compare_and_swap( ( int* ) src, 0, 0 );
}
float atomic_read_float(volatile const float* src) {
long atomic_read_long( volatile const long* src )
{
/*
* Trick: Realisierung mittels CAS!
* Falls der Wert schon 0, dann wird er gegen 0 ausgetauscht...
* Also keine Änderung!
*/
return __sync_val_compare_and_swap( ( long* ) src, 0, 0 );
}
float atomic_read_float( volatile const float* src )
{
/*
* Trick: Realisierung mittels CAS!
* Falls der Wert schon 0, dann wird er gegen 0 ausgetauscht...
......@@ -45,59 +40,74 @@ float atomic_read_float(volatile const float* src) {
*/
ConvI32Float c;
c.i32Value = __sync_val_compare_and_swap((volatile int32_t*) src, 0, 0);
c.i32Value = __sync_val_compare_and_swap( ( volatile int32_t* ) src, 0, 0 );
return c.floatValue;
}
void* atomic_read_ptr(volatile const void** src) {
void* atomic_read_ptr( volatile const void** src )
{
/*
* Trick: Realisierung mittels CAS!
* Falls der Wert schon 0, dann wird er gegen 0 ausgetauscht...
* Also keine Änderung!
*/
return __sync_val_compare_and_swap((void**) src, 0, 0);
return __sync_val_compare_and_swap( ( void** ) src, 0, 0 );
}
void atomic_write_int(volatile int* dest, int value) {
__sync_lock_test_and_set(dest, value);
void atomic_write_int( volatile int* dest, int value )
{
__sync_lock_test_and_set( dest, value );
}
void atomic_write_float(volatile float* dest, float value) {
void atomic_write_int( volatile long* dest, long value )
{
__sync_lock_test_and_set( dest, value );
}
void atomic_write_float( volatile float* dest, float value )
{
ConvI32Float c;
c.floatValue = value;
__sync_lock_test_and_set((volatile int32_t*) dest, c.i32Value);
__sync_lock_test_and_set( ( volatile int32_t* ) dest, c.i32Value );
}
void atomic_write_ptr(volatile void** dest, void* value) {
__sync_lock_test_and_set(dest, value);
void atomic_write_ptr( volatile void** dest, void* value )
{
__sync_lock_test_and_set( dest, value );
}
bool atomic_cas_int(volatile int* dest, int expected_value, int new_value) {
return __sync_bool_compare_and_swap(dest, expected_value, new_value);
bool atomic_cas_int( volatile int* dest, int expected_value, int new_value )
{
return __sync_bool_compare_and_swap( dest, expected_value, new_value );
}
/*
bool atomic_cas_double(volatile double* dest, double expected_value, double new_value) {
return __sync_bool_compare_and_swap(dest, expected_value, new_value);
return __sync_bool_compare_and_swap(dest, expected_value, new_value);
}
*/
bool atomic_cas_ptr(volatile void** dest, void* expected_value, void* new_value) {
return __sync_bool_compare_and_swap(dest, expected_value, new_value);
bool atomic_cas_ptr( volatile void** dest, void* expected_value, void* new_value )
{
return __sync_bool_compare_and_swap( dest, expected_value, new_value );
}
void atomic_read32(volatile void* src, void* dest) {
*((int32_t*) dest) = __sync_val_compare_and_swap((volatile int32_t*) src, 0, 0);
void atomic_read32( volatile void* src, void* dest )
{
*( ( int32_t* ) dest ) = __sync_val_compare_and_swap( ( volatile int32_t* ) src, 0, 0 );
}
bool atomic_cas32(volatile void* dest, void* expected_value, void* new_value) {
return __sync_bool_compare_and_swap((volatile int32_t*)dest, *(int32_t*)expected_value, *(int32_t*)new_value);
bool atomic_cas32( volatile void* dest, void* expected_value, void* new_value )
{
return __sync_bool_compare_and_swap( ( volatile int32_t* ) dest, *( int32_t* ) expected_value, *( int32_t* ) new_value );
}
int atomic_inc_int(volatile int* dest) {
return __sync_fetch_and_add(dest, 1);
int atomic_inc_int( volatile int* dest )
{
return __sync_fetch_and_add( dest, 1 );
}
int atomic_dec_int(volatile int* dest) {
return __sync_fetch_and_sub(dest, 1);
int atomic_dec_int( volatile int* dest )
{
return __sync_fetch_and_sub( dest, 1 );
}
......@@ -10,11 +10,12 @@ class TickerCallback : public VistaTicker::AfterPulseFunctor
public:
inline bool operator()()
{
cout << "Ticker callback called" << endl;
cout << "Ticker callback called, text is: " << sText << endl;
return true;
};
int nGranularityMS;
std::string sText;
};
int main( int, char** )
......@@ -22,6 +23,7 @@ int main( int, char** )
VistaTicker oTicker;
TickerCallback oCallback;
oCallback.sText = "First call";
oCallback.nGranularityMS = oTicker.GetGranularity();
VistaTicker::TriggerContext oTickerContext( oTicker.GetGranularity(), true );
......@@ -30,11 +32,22 @@ int main( int, char** )
oTicker.StartTicker();
VistaTimeUtils::Sleep( int( 5*1e3 ) );
VistaTimeUtils::Sleep( int( 2*1e3 ) );
oTicker.StopTicker();
oTicker.RemTrigger( &oTickerContext );
oTicker.SetAfterPulseFunctor( NULL ); // Otherwise Ticker will delete callback instance
oCallback.sText = "Second call";
VistaTicker* pTicker = new VistaTicker( 100 );
pTicker->AddTrigger( new VistaTicker::TriggerContext( 100, true ) );
pTicker->SetAfterPulseFunctor( &oCallback );
pTicker->StartTicker();
VistaTimeUtils::Sleep( int( 2 * 1e3 ) );
pTicker->StopTicker();
pTicker->SetAfterPulseFunctor( NULL ); // Otherwise Ticker will delete callback instance
delete pTicker;
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment