Add atomic store and load operations

Added abstractions for storing and loading 32-bit and 64-bit values
atomically. The functions currently use the GCC __atomic builtin atomics.
This commit is contained in:
Markus Mäkelä 2017-04-14 05:10:49 +03:00 committed by Markus Mäkelä
parent 8174690f77
commit 122337569c
2 changed files with 56 additions and 0 deletions

View File

@ -36,6 +36,32 @@ int atomic_add(int *variable, int value);
int64_t atomic_add_int64(int64_t *variable, int64_t value);
uint64_t atomic_add_uint64(uint64_t *variable, int64_t value);
/**
* Implementation of an atomic load operation for the GCC environment.
*
* Loads a value from the contents of a location pointed to by the first parameter.
* The load operation is atomic and it uses the strongest memory ordering.
*
* @param variable Pointer the the variable to load from
* @return The stored value
*/
int atomic_read(int *variable);
int64_t atomic_read_int64(int64_t *variable);
uint64_t atomic_read_uint64(uint64_t *variable);
/**
* Implementation of an atomic store operation for the GCC environment.
*
* Stores a value to the contents of a location pointed to by the first parameter.
* The store operation is atomic and it uses the strongest memory ordering.
*
* @param variable Pointer the the variable to store to
* @param value Value to be stored
*/
void atomic_write(int *variable, int value);
void atomic_write_int64(int64_t *variable, int64_t value);
void atomic_write_uint64(uint64_t *variable, uint64_t value);
/**
* @brief Impose a full memory barrier
*

View File

@ -31,3 +31,33 @@ uint64_t atomic_add_uint64(uint64_t *variable, int64_t value)
{
return __sync_fetch_and_add(variable, value);
}
int atomic_read(int *variable)
{
return __atomic_load_n(variable, __ATOMIC_SEQ_CST);
}
int64_t atomic_read_int64(int64_t *variable)
{
return __atomic_load_n(variable, __ATOMIC_SEQ_CST);
}
uint64_t atomic_read_uint64(uint64_t *variable)
{
return __atomic_load_n(variable, __ATOMIC_SEQ_CST);
}
void atomic_write(int *variable, int value)
{
return __atomic_store_n(variable, value, __ATOMIC_SEQ_CST);
}
void atomic_write_int64(int64_t *variable, int64_t value)
{
return __atomic_store_n(variable, value, __ATOMIC_SEQ_CST);
}
void atomic_write_uint64(uint64_t *variable, uint64_t value)
{
return __atomic_store_n(variable, value, __ATOMIC_SEQ_CST);
}