Offers atomic operations for type integers.
The barrier wait until all previous read and write operations are done and it ensures that all future read and write operations cannot be moved before the barrier.
All future read and writer operations cannot be moved before the barrier. Previous read and write operations may not be done yet.
Ensures that all all previous read and write operations are done. But future read and writer operations could be moved before the barrier.
AtomicOps | Offers atomic operations for type integers. |
Copyright | This program is free software. |
Files | |
C-kern/ | Header file AtomicOps. |
C-kern/ | Implementation file AtomicOps impl. |
Functions | |
test | |
unittest_math_int_atomic | Test functionality of atomic operations. |
int_t | Offers some atomic operations on integers. |
atomicops | |
atomicread_int | Reads last written value from memory location i. |
atomicwrite_int | Writes newval into memory located at i. |
atomicadd_int | Add increment to i and return old value atomically. |
atomicsub_int | Sub decrement from i and return old value atomically. |
atomicswap_int | If *i is equal to oldval change it into newval atomically else do nothing. |
atomicset_int | Sets flag to value != 0 and returns previous value. |
atomicclear_int | Sets flag to value 0. |
inline implementation | |
Macros | |
atomicadd_int | Implements int_t.atomicadd_int. |
atomicclear_int | Implements int_t.atomicclear_int. |
atomicread_int | Implements int_t.atomicread_int. |
atomicsub_int | Implements int_t.atomicsub_int. |
atomicset_int | Implements int_t.atomicset_int. |
atomicswap_int | Implements int_t.atomicswap_int. |
atomicwrite_int | Implements int_t.atomicwrite_int. |
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2013 Jörg Seebohn
Header file AtomicOps.
Implementation file AtomicOps impl.
test | |
unittest_math_int_atomic | Test functionality of atomic operations. |
struct int_t
Offers some atomic operations on integers. If a processor does not support atomic ops then this implementation relys one the compiler to emulate them.
atomicops | |
atomicread_int | Reads last written value from memory location i. |
atomicwrite_int | Writes newval into memory located at i. |
atomicadd_int | Add increment to i and return old value atomically. |
atomicsub_int | Sub decrement from i and return old value atomically. |
atomicswap_int | If *i is equal to oldval change it into newval atomically else do nothing. |
atomicset_int | Sets flag to value != 0 and returns previous value. |
atomicclear_int | Sets flag to value 0. |
int atomicread_int( int * i )
Reads last written value from memory location i. The value must be written with atomicwrite_int or any other atomic operation. This operation ensures also an acquire memory barrier. After this operation completed this thread will see the newest values written by other threads.
int atomicset_int( uint8_t * flag )
Sets flag to value != 0 and returns previous value. A return value of 0 means flag was not set before therefore the lock is acquired. A return value of != 0 means flag was set before therefore the lock is not acquired. This operation ensures also an acquire memory barrier. The thread will see all values written by other threads before this operation if it reads them after this operation. Other threads may not see the values written by this thread.
void atomicclear_int( uint8_t * flag )
Sets flag to value 0. Call this function only if atomicset_int returned true. This operation ensures also a release memory barrier. Other threads will see all values written by this thread before this operation (at least after they have executed an acquire barrier). This thread may not see the values written by other threads.
Macros | |
atomicadd_int | Implements int_t.atomicadd_int. |
atomicclear_int | Implements int_t.atomicclear_int. |
atomicread_int | Implements int_t.atomicread_int. |
atomicsub_int | Implements int_t.atomicsub_int. |
atomicset_int | Implements int_t.atomicset_int. |
atomicswap_int | Implements int_t.atomicswap_int. |
atomicwrite_int | Implements int_t.atomicwrite_int. |
#define atomicadd_int( i, increment ) (__sync_fetch_and_add((i), (increment)))
Implements int_t.atomicadd_int.
#define atomicclear_int( flag ) (__sync_lock_release(flag))
Implements int_t.atomicclear_int.
#define atomicread_int( i ) (__sync_fetch_and_add((i), 0))
Implements int_t.atomicread_int.
#define atomicsub_int( i, decrement ) (__sync_fetch_and_sub((i), (decrement)))
Implements int_t.atomicsub_int.
#define atomicset_int( flag ) (__sync_lock_test_and_set(flag, 1))
Implements int_t.atomicset_int.
#define atomicswap_int( i, oldval, newval ) (__sync_val_compare_and_swap(i, oldval, newval))
Implements int_t.atomicswap_int.
#define atomicwrite_int( i, newval ) ( __extension__ ({ typeof(i) _i = (i) ; typeof(*_i) _new = (newval) ; typeof(*_i) _old = *_i ; typeof(*_i) _old2 ; for (;;) { _old2 = __sync_val_compare_and_swap( _i, _old, _new) ; if (_old2 == _old) break ; _old = _old2 ; } _old ; }))
Implements int_t.atomicwrite_int. TODO: Replace atomicswap (full memory barrier) with store fence.
Test functionality of atomic operations.
int unittest_math_int_atomic( void )
Offers some atomic operations on integers.
struct int_t
Reads last written value from memory location i.
int atomicread_int( int * i )
Writes newval into memory located at i.
void atomicwrite_int( int * i, int newval )
Add increment to i and return old value atomically.
int atomicadd_int( int * i, uint32_t increment )
Sub decrement from i and return old value atomically.
int atomicsub_int( int * i, uint32_t decrement )
If *i is equal to oldval change it into newval atomically else do nothing.
int atomicswap_int( int * i, int oldval, int newval )
Sets flag to value != 0 and returns previous value.
int atomicset_int( uint8_t * flag )
Sets flag to value 0.
void atomicclear_int( uint8_t * flag )
Implements int_t.atomicadd_int.
#define atomicadd_int( i, increment ) (__sync_fetch_and_add((i), (increment)))
Implements int_t.atomicclear_int.
#define atomicclear_int( flag ) (__sync_lock_release(flag))
Implements int_t.atomicread_int.
#define atomicread_int( i ) (__sync_fetch_and_add((i), 0))
Implements int_t.atomicsub_int.
#define atomicsub_int( i, decrement ) (__sync_fetch_and_sub((i), (decrement)))
Implements int_t.atomicset_int.
#define atomicset_int( flag ) (__sync_lock_test_and_set(flag, 1))
Implements int_t.atomicswap_int.
#define atomicswap_int( i, oldval, newval ) (__sync_val_compare_and_swap(i, oldval, newval))
Implements int_t.atomicwrite_int.
#define atomicwrite_int( i, newval ) ( __extension__ ({ typeof(i) _i = (i) ; typeof(*_i) _new = (newval) ; typeof(*_i) _old = *_i ; typeof(*_i) _old2 ; for (;;) { _old2 = __sync_val_compare_and_swap( _i, _old, _new) ; if (_old2 == _old) break ; _old = _old2 ; } _old ; }))