-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fiber] Implement stop_token interface for Task
- Loading branch information
Showing
5 changed files
with
346 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright (c) 2024, Niklas Hauser | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
|
||
namespace modm::fiber | ||
{ | ||
|
||
/// @ingroup modm_processing_fiber | ||
/// @{ | ||
|
||
/// @cond | ||
class stop_state | ||
{ | ||
std::atomic_bool requested{false}; | ||
|
||
public: | ||
[[nodiscard]] | ||
bool inline | ||
stop_requested() const | ||
{ | ||
return requested.load(); | ||
} | ||
|
||
bool inline | ||
request_stop() | ||
{ | ||
return not requested.exchange(true); | ||
} | ||
}; | ||
/// @endcond | ||
|
||
/// Implements the `std::stop_token` interface for fibers. | ||
/// @see https://en.cppreference.com/w/cpp/thread/stop_token | ||
class stop_token | ||
{ | ||
friend class Task; | ||
friend class stop_source; | ||
const stop_state *state{nullptr}; | ||
|
||
constexpr explicit stop_token(const stop_state *state) : state(state) {} | ||
public: | ||
constexpr stop_token() = default; | ||
constexpr stop_token(const stop_token&) = default; | ||
constexpr stop_token(stop_token&&) = default; | ||
constexpr ~stop_token() = default; | ||
constexpr stop_token& operator=(const stop_token&) = default; | ||
constexpr stop_token& operator=(stop_token&&) = default; | ||
|
||
[[nodiscard]] | ||
bool inline | ||
stop_possible() const | ||
{ | ||
return state; | ||
} | ||
|
||
[[nodiscard]] | ||
bool inline | ||
stop_requested() const | ||
{ | ||
return stop_possible() and state->stop_requested(); | ||
} | ||
|
||
// void inline | ||
// swap(stop_token& rhs) | ||
// { | ||
// std::swap(state, rhs.state); | ||
// } | ||
|
||
[[nodiscard]] | ||
friend bool inline | ||
operator==(const stop_token& a, const stop_token& b) | ||
{ | ||
return a.state == b.state; | ||
} | ||
|
||
// friend void inline | ||
// swap(stop_token& lhs, stop_token& rhs) | ||
// { | ||
// lhs.swap(rhs); | ||
// } | ||
}; | ||
|
||
/// Implements the `std::stop_source` interface for fibers. | ||
/// @see https://en.cppreference.com/w/cpp/thread/stop_source | ||
class stop_source | ||
{ | ||
friend class Task; | ||
stop_state *state{nullptr}; | ||
explicit constexpr stop_source(stop_state *state) : state(state) {} | ||
|
||
public: | ||
constexpr stop_source() = default; | ||
constexpr stop_source(const stop_source&) = default; | ||
constexpr stop_source(stop_source&&) = default; | ||
constexpr ~stop_source() = default; | ||
constexpr stop_source& operator=(const stop_source&) = default; | ||
constexpr stop_source& operator=(stop_source&&) = default; | ||
|
||
[[nodiscard]] | ||
constexpr bool | ||
stop_possible() const | ||
{ | ||
return state; | ||
} | ||
|
||
[[nodiscard]] | ||
bool inline | ||
stop_requested() const | ||
{ | ||
return stop_possible() and state->stop_requested(); | ||
} | ||
|
||
bool inline | ||
request_stop() | ||
{ | ||
return stop_possible() and state->request_stop(); | ||
} | ||
|
||
[[nodiscard]] | ||
stop_token inline | ||
get_token() const | ||
{ | ||
return stop_token{state}; | ||
} | ||
|
||
// void inline | ||
// swap(stop_token& rhs) | ||
// { | ||
// std::swap(state, rhs.state); | ||
// } | ||
|
||
[[nodiscard]] | ||
friend bool inline | ||
operator==(const stop_source& a, const stop_source& b) | ||
{ | ||
return a.state == b.state; | ||
} | ||
|
||
// friend void inline | ||
// swap(stop_token& lhs, stop_token& rhs) | ||
// { | ||
// lhs.swap(rhs); | ||
// } | ||
}; | ||
|
||
/// @} | ||
|
||
} |
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
Oops, something went wrong.