ladivic
Loading...
Searching...
No Matches
ldvc_atomic.hpp File Reference

Provides utilities for atomic operations with mutex protection in C++. More...

#include <atomic>
#include <mutex>
Include dependency graph for ldvc_atomic.hpp:

Go to the source code of this file.

Functions

template<typename T >
void ldvc_atomic_create (std::atomic< T > &var, std::mutex &mutex, T value)
 Initializes an std::atomic variable with the specified initial value under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_delete (std::atomic< T > &var, std::mutex &mutex)
 Resets an std::atomic variable to its default-constructed value under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_inc (std::atomic< T > &var, std::mutex &mutex, T arg)
 Atomically increments the value of an std::atomic variable by the specified amount under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_dec (std::atomic< T > &var, std::mutex &mutex, T arg)
 Atomically decrements the value of an std::atomic variable by the specified amount under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_and (std::atomic< T > &var, std::mutex &mutex, T arg)
 Performs a bitwise AND operation on an std::atomic variable with the specified value under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_or (std::atomic< T > &var, std::mutex &mutex, T arg)
 Performs a bitwise OR operation on an std::atomic variable with the specified value under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_xor (std::atomic< T > &var, std::mutex &mutex, T arg)
 Performs a bitwise XOR operation on an std::atomic variable with the specified value under the protection of a mutex.
 
template<typename T >
T ldvc_atomic_exchange (std::atomic< T > &var, std::mutex &mutex, T new_value)
 Atomically exchanges the value of an std::atomic variable with a new value under the protection of a mutex.
 
template<typename T >
T ldvc_atomic_load (const std::atomic< T > &var, std::mutex &mutex)
 Atomically loads the value of an std::atomic variable under the protection of a mutex.
 
template<typename T >
void ldvc_atomic_store (std::atomic< T > &var, std::mutex &mutex, T new_value)
 Atomically stores a new value in an std::atomic variable under the protection of a mutex.
 

Detailed Description

Provides utilities for atomic operations with mutex protection in C++.

This header file defines functions for performing atomic operations on std::atomic variables with the protection of a mutex. These functions ensure thread safety while allowing atomicity for operations such as incrementing, decrementing, bitwise operations, exchange, load, and store.

Author
Nathanne Isip

Definition in file ldvc_atomic.hpp.

Function Documentation

◆ ldvc_atomic_and()

template<typename T >
void ldvc_atomic_and ( std::atomic< T > & var,
std::mutex & mutex,
T arg )

Performs a bitwise AND operation on an std::atomic variable with the specified value under the protection of a mutex.

This function performs a bitwise AND operation on an std::atomic variable var with the specified value arg under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be bitwise ANDed.
mutexThe mutex for thread safety.
argThe value for the bitwise AND operation.

Definition at line 151 of file ldvc_atomic.hpp.

152{
153 std::lock_guard<std::mutex> lock(mutex);
154 var.fetch_and(arg, std::memory_order_relaxed);
155}
T * ldvc_malloc(usize size)
Allocates memory for an array of elements.
Definition ldvc_mem.hpp:61

◆ ldvc_atomic_create()

template<typename T >
void ldvc_atomic_create ( std::atomic< T > & var,
std::mutex & mutex,
T value )

Initializes an std::atomic variable with the specified initial value under the protection of a mutex.

This function initializes an std::atomic variable var with the specified initial value value under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be initialized.
mutexThe mutex for thread safety.
valueThe initial value for the atomic variable.

Definition at line 59 of file ldvc_atomic.hpp.

60{
61 std::lock_guard<std::mutex> lock(mutex);
62 var.store(value, std::memory_order_relaxed);
63}

◆ ldvc_atomic_dec()

template<typename T >
void ldvc_atomic_dec ( std::atomic< T > & var,
std::mutex & mutex,
T arg )

Atomically decrements the value of an std::atomic variable by the specified amount under the protection of a mutex.

This function atomically decrements the value of an std::atomic variable var by the specified amount arg under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be decremented.
mutexThe mutex for thread safety.
argThe amount by which to decrement the atomic variable.

Definition at line 127 of file ldvc_atomic.hpp.

128{
129 std::lock_guard<std::mutex> lock(mutex);
130 var.fetch_sub(arg, std::memory_order_relaxed);
131}

◆ ldvc_atomic_delete()

template<typename T >
void ldvc_atomic_delete ( std::atomic< T > & var,
std::mutex & mutex )

Resets an std::atomic variable to its default-constructed value under the protection of a mutex.

This function resets an std::atomic variable var to its default-constructed value under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be reset.
mutexThe mutex for thread safety.

Definition at line 80 of file ldvc_atomic.hpp.

81{
82 std::lock_guard<std::mutex> lock(mutex);
83 var.store(T{}, std::memory_order_relaxed);
84}

◆ ldvc_atomic_exchange()

template<typename T >
T ldvc_atomic_exchange ( std::atomic< T > & var,
std::mutex & mutex,
T new_value )

Atomically exchanges the value of an std::atomic variable with a new value under the protection of a mutex.

This function atomically exchanges the value of an std::atomic variable var with the specified new value new_value under the protection of the provided mutex mutex. It returns the previous value of the atomic variable.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be exchanged.
mutexThe mutex for thread safety.
new_valueThe new value to be stored in the atomic variable.
Returns
T The previous value of the atomic variable.

Definition at line 223 of file ldvc_atomic.hpp.

224{
225 std::lock_guard<std::mutex> lock(mutex);
226 return var.exchange(new_value, std::memory_order_relaxed);
227}

◆ ldvc_atomic_inc()

template<typename T >
void ldvc_atomic_inc ( std::atomic< T > & var,
std::mutex & mutex,
T arg )

Atomically increments the value of an std::atomic variable by the specified amount under the protection of a mutex.

This function atomically increments the value of an std::atomic variable var by the specified amount arg under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be incremented.
mutexThe mutex for thread safety.
argThe amount by which to increment the atomic variable.

Definition at line 103 of file ldvc_atomic.hpp.

104{
105 std::lock_guard<std::mutex> lock(mutex);
106 var.fetch_add(arg, std::memory_order_relaxed);
107}

◆ ldvc_atomic_load()

template<typename T >
T ldvc_atomic_load ( const std::atomic< T > & var,
std::mutex & mutex )

Atomically loads the value of an std::atomic variable under the protection of a mutex.

This function atomically loads the value of an std::atomic variable var under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be loaded.
mutexThe mutex for thread safety.
Returns
T The value of the atomic variable.

Definition at line 245 of file ldvc_atomic.hpp.

246{
247 std::lock_guard<std::mutex> lock(mutex);
248 return var.load(std::memory_order_relaxed);
249}

◆ ldvc_atomic_or()

template<typename T >
void ldvc_atomic_or ( std::atomic< T > & var,
std::mutex & mutex,
T arg )

Performs a bitwise OR operation on an std::atomic variable with the specified value under the protection of a mutex.

This function performs a bitwise OR operation on an std::atomic variable var with the specified value arg under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be bitwise ORed.
mutexThe mutex for thread safety.
argThe value for the bitwise OR operation.

Definition at line 174 of file ldvc_atomic.hpp.

175{
176 std::lock_guard<std::mutex> lock(mutex);
177 var.fetch_or(arg, std::memory_order_relaxed);
178}

◆ ldvc_atomic_store()

template<typename T >
void ldvc_atomic_store ( std::atomic< T > & var,
std::mutex & mutex,
T new_value )

Atomically stores a new value in an std::atomic variable under the protection of a mutex.

This function atomically stores the specified new value new_value in an std::atomic variable var under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be stored.
mutexThe mutex for thread safety.
new_valueThe new value to be stored in the atomic variable.

Definition at line 268 of file ldvc_atomic.hpp.

269{
270 std::lock_guard<std::mutex> lock(mutex);
271 var.store(new_value, std::memory_order_relaxed);
272}

◆ ldvc_atomic_xor()

template<typename T >
void ldvc_atomic_xor ( std::atomic< T > & var,
std::mutex & mutex,
T arg )

Performs a bitwise XOR operation on an std::atomic variable with the specified value under the protection of a mutex.

This function performs a bitwise XOR operation on an std::atomic variable var with the specified value arg under the protection of the provided mutex mutex.

Template Parameters
TThe type of the atomic variable.
Parameters
varThe atomic variable to be bitwise XORed.
mutexThe mutex for thread safety.
argThe value for the bitwise XOR operation.

Definition at line 198 of file ldvc_atomic.hpp.

199{
200 std::lock_guard<std::mutex> lock(mutex);
201 var.fetch_xor(arg, std::memory_order_relaxed);
202}