-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
210 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* Copyright (c) 2023 Krisztián Rugási. Subject to the MIT License. */ | ||
|
||
#ifndef GA_UTILITY_ATOMIC_HPP | ||
#define GA_UTILITY_ATOMIC_HPP | ||
|
||
#include <atomic> | ||
#include <memory> | ||
#include <utility> | ||
|
||
namespace gapp::detail | ||
{ | ||
/* | ||
* This is a simple wrapper class around std::atomic with atomic | ||
* initialization, in order to prevent data races between the | ||
* initialization of the variable and later accesses to it. | ||
* | ||
* The wrapper also adds move operators for convenience. | ||
*/ | ||
template<typename T> | ||
class atomic | ||
{ | ||
public: | ||
atomic(std::memory_order order = std::memory_order_seq_cst) noexcept | ||
{ | ||
data_.store(T{}, order); | ||
} | ||
|
||
atomic(T value, std::memory_order order = std::memory_order_seq_cst) noexcept | ||
{ | ||
data_.store(std::move(value), order); | ||
} | ||
|
||
atomic(atomic<T>&& other) noexcept | ||
{ | ||
data_.store(other->load()); | ||
} | ||
|
||
atomic<T>& operator=(T value) noexcept | ||
{ | ||
data_.store(std::move(value)); | ||
return *this; | ||
} | ||
|
||
atomic<T>& operator=(atomic<T>&& other) noexcept | ||
{ | ||
data_.store(other->load()); | ||
return *this; | ||
} | ||
|
||
~atomic() noexcept = default; // maybe needs release? | ||
|
||
std::atomic<T>& operator*() noexcept { return data_; } | ||
std::atomic<T>* operator->() noexcept { return std::addressof(data_); } | ||
const std::atomic<T>& operator*() const noexcept { return data_; } | ||
const std::atomic<T>* operator->() const noexcept { return std::addressof(data_); } | ||
|
||
private: | ||
std::atomic<T> data_; | ||
}; | ||
|
||
|
||
} // namespace gapp::detail | ||
|
||
#endif // !GA_UTILITY_ATOMIC_HPP |
Oops, something went wrong.