AtomicOps

Offers atomic operations for type integers.

Full Memory Barrier

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.

Acquire Memory Barrier

All future read and writer operations cannot be moved before the barrier.  Previous read and write operations may not be done yet.

Release Memory Barrier

Ensures that all all previous read and write operations are done.  But future read and writer operations could be moved before the barrier.

Summary
AtomicOpsOffers atomic operations for type integers.
CopyrightThis program is free software.
Files
C-kern/api/math/int/atomic.hHeader file AtomicOps.
C-kern/math/int/atomic.cImplementation file AtomicOps impl.
Functions
test
unittest_math_int_atomicTest functionality of atomic operations.
int_tOffers some atomic operations on integers.
atomicops
atomicread_intReads last written value from memory location i.
atomicwrite_intWrites newval into memory located at i.
atomicadd_intAdd increment to i and return old value atomically.
atomicsub_intSub decrement from i and return old value atomically.
atomicswap_intIf *i is equal to oldval change it into newval atomically else do nothing.
atomicset_intSets flag to value != 0 and returns previous value.
atomicclear_intSets flag to value 0.
inline implementation
Macros
atomicadd_intImplements int_t.atomicadd_int.
atomicclear_intImplements int_t.atomicclear_int.
atomicread_intImplements int_t.atomicread_int.
atomicsub_intImplements int_t.atomicsub_int.
atomicset_intImplements int_t.atomicset_int.
atomicswap_intImplements int_t.atomicswap_int.
atomicwrite_intImplements int_t.atomicwrite_int.

Copyright

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.

Author

© 2013 Jörg Seebohn

Files

C-kern/api/math/int/atomic.h

Header file AtomicOps.

C-kern/math/int/atomic.c

Implementation file AtomicOps impl.

Functions

Summary
test
unittest_math_int_atomicTest functionality of atomic operations.

test

unittest_math_int_atomic

int unittest_math_int_atomic(void)

Test functionality of atomic operations.

int_t

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.

Summary
atomicops
atomicread_intReads last written value from memory location i.
atomicwrite_intWrites newval into memory located at i.
atomicadd_intAdd increment to i and return old value atomically.
atomicsub_intSub decrement from i and return old value atomically.
atomicswap_intIf *i is equal to oldval change it into newval atomically else do nothing.
atomicset_intSets flag to value != 0 and returns previous value.
atomicclear_intSets flag to value 0.

atomicops

atomicread_int

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.

atomicwrite_int

void atomicwrite_int(int *i,
int newval)

Writes newval into memory located at i.  This operation ensures also a release memory barrier.  After this operation has completed other threads will see all write operations done by this thread before this atomic write operation.

atomicadd_int

int atomicadd_int(int *i,
uint32_t increment)

Add increment to i and return old value atomically.  This operation ensures also a full memory barrier.

atomicsub_int

int atomicsub_int(int *i,
uint32_t decrement)

Sub decrement from i and return old value atomically.  This operation ensures also a full memory barrier.

atomicswap_int

int atomicswap_int(int *i,
int oldval,
int newval)

If *i is equal to oldval change it into newval atomically else do nothing.  The operation returns the old value of i.  If the returned value equals oldval the operation was successful.  This operation ensures also a full memory barrier.

atomicset_int

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.

atomicclear_int

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

#define atomicadd_int(i,
increment) (__sync_fetch_and_add((i), (increment)))

Implements int_t.atomicadd_int.

atomicclear_int

#define atomicclear_int(flag) (__sync_lock_release(flag))

Implements int_t.atomicclear_int.

atomicread_int

#define atomicread_int(i) (__sync_fetch_and_add((i), 0))

Implements int_t.atomicread_int.

atomicsub_int

#define atomicsub_int(i,
decrement) (__sync_fetch_and_sub((i), (decrement)))

Implements int_t.atomicsub_int.

atomicset_int

#define atomicset_int(flag) (__sync_lock_test_and_set(flag, 1))

Implements int_t.atomicset_int.

atomicswap_int

#define atomicswap_int(i,
oldval,
newval) (__sync_val_compare_and_swap(i, oldval, newval))

Implements int_t.atomicswap_int.

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 ; }))

Implements int_t.atomicwrite_int.  TODO: Replace atomicswap (full memory barrier) with store fence.

Offers atomic operations for type integers.
Implements AtomicOps.
int unittest_math_int_atomic(void)
Test functionality of atomic operations.
struct int_t
Offers some atomic operations on integers.
int atomicread_int(int *i)
Reads last written value from memory location i.
void atomicwrite_int(int *i,
int newval)
Writes newval into memory located at i.
int atomicadd_int(int *i,
uint32_t increment)
Add increment to i and return old value atomically.
int atomicsub_int(int *i,
uint32_t decrement)
Sub decrement from i and return old value atomically.
int atomicswap_int(int *i,
int oldval,
int newval)
If *i is equal to oldval change it into newval atomically else do nothing.
int atomicset_int(uint8_t *flag)
Sets flag to value != 0 and returns previous value.
void atomicclear_int(uint8_t *flag)
Sets flag to value 0.
#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.
Close