yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
stop_token.h
Go to the documentation of this file.
1#ifndef YAZE_UTIL_STOP_TOKEN_H_
2#define YAZE_UTIL_STOP_TOKEN_H_
3
4#include <atomic>
5#include <memory>
6#include <utility>
7
8namespace yaze::util {
9
11inline constexpr NoStopStateTag kNoStopState{};
12
13// Poll-only cancellation for background scans. Some supported libc++ versions
14// lack std::stop_token; the scans do not need stop callbacks or std::jthread.
15class StopToken {
16 public:
17 StopToken() = default;
18
19 bool has_stop_state() const noexcept { return state_ != nullptr; }
20
21 bool stop_requested() const noexcept {
22 return state_ && state_->load(std::memory_order_acquire);
23 }
24
25 private:
26 friend class StopSource;
27 explicit StopToken(std::shared_ptr<std::atomic<bool>> state)
28 : state_(std::move(state)) {}
29
30 std::shared_ptr<std::atomic<bool>> state_;
31};
32
34 public:
35 StopSource() : state_(std::make_shared<std::atomic<bool>>(false)) {}
36 explicit StopSource(NoStopStateTag) noexcept {}
37
38 StopToken get_token() const noexcept { return StopToken(state_); }
39
40 bool request_stop() noexcept {
41 return state_ && !state_->exchange(true, std::memory_order_acq_rel);
42 }
43
44 private:
45 std::shared_ptr<std::atomic<bool>> state_;
46};
47
48} // namespace yaze::util
49
50#endif // YAZE_UTIL_STOP_TOKEN_H_
StopSource(NoStopStateTag) noexcept
Definition stop_token.h:36
StopToken get_token() const noexcept
Definition stop_token.h:38
bool request_stop() noexcept
Definition stop_token.h:40
std::shared_ptr< std::atomic< bool > > state_
Definition stop_token.h:45
std::shared_ptr< std::atomic< bool > > state_
Definition stop_token.h:30
bool stop_requested() const noexcept
Definition stop_token.h:21
bool has_stop_state() const noexcept
Definition stop_token.h:19
StopToken(std::shared_ptr< std::atomic< bool > > state)
Definition stop_token.h:27
constexpr NoStopStateTag kNoStopState
Definition stop_token.h:11