-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Spinner manager #198
Open
filippobrizzi
wants to merge
4
commits into
main
Choose a base branch
from
async_sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Spinner manager #198
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions
38
modules/concurrency/include/hephaestus/concurrency/spinner_manager.h
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,38 @@ | ||
//================================================================================================= | ||
// Copyright (C) 2023-2024 HEPHAESTUS Contributors | ||
//================================================================================================= | ||
|
||
#pragma once | ||
|
||
#include <future> | ||
#include <vector> | ||
|
||
#include "hephaestus/concurrency/spinner.h" | ||
|
||
namespace heph::concurrency { | ||
|
||
/// @brief SpinnersManager allows to orchestrate the execution of multiple spinners. | ||
/// The main feature is `waitAny` which allows to unblock as soon as one of the spinners is done. | ||
/// This allows to catch errors in spinner execution as soon as possible and stop the others. | ||
/// NOTE: right now we do not have any concept of error for the spinners: we cannot know if a spinner | ||
/// terminated with an error or not. If an exception is thrown inside the runner, it will be re-throwed when | ||
/// we call runner.stop().get(). We leave it to the user to handle it outside of the runner manager. | ||
/// NOTE: this logic is quite generic and can be extended to any type of object that has `wait()` and `stop()` | ||
/// methods. To be faitful to the principle of implement only what you need now, we limit the scope to | ||
/// spinners and consider to expand the scope when an use case arises. | ||
class SpinnersManager { | ||
public: | ||
explicit SpinnersManager(std::vector<Spinner*> spinners); | ||
|
||
void startAll(); | ||
void waitAll(); | ||
void waitAny(); | ||
void stopAll(); | ||
|
||
private: | ||
std::vector<Spinner*> spinners_; | ||
|
||
std::vector<std::future<void>> wait_futures_; | ||
}; | ||
|
||
} // namespace heph::concurrency |
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,56 @@ | ||
//================================================================================================= | ||
// Copyright (C) 2023-2024 HEPHAESTUS Contributors | ||
//================================================================================================= | ||
|
||
#include "hephaestus/concurrency/spinner_manager.h" | ||
|
||
#include <algorithm> | ||
#include <atomic> | ||
#include <future> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <fmt/core.h> | ||
|
||
#include "hephaestus/concurrency/spinner.h" | ||
|
||
namespace heph::concurrency { | ||
SpinnersManager::SpinnersManager(std::vector<Spinner*> spinners) : spinners_{ std::move(spinners) } { | ||
} | ||
|
||
void SpinnersManager::startAll() { | ||
std::ranges::for_each(spinners_, [](auto* runner) { runner->start(); }); | ||
} | ||
|
||
void SpinnersManager::waitAll() { | ||
std::ranges::for_each(spinners_, [](auto* runner) { runner->wait(); }); | ||
} | ||
|
||
void SpinnersManager::waitAny() { | ||
std::atomic_flag flag = ATOMIC_FLAG_INIT; | ||
wait_futures_.reserve(spinners_.size()); | ||
for (auto* runner : spinners_) { | ||
auto future = std::async(std::launch::async, [runner, &flag]() { | ||
runner->wait(); | ||
flag.test_and_set(); | ||
flag.notify_all(); | ||
}); | ||
wait_futures_.push_back(std::move(future)); | ||
} | ||
|
||
flag.wait(false); | ||
} | ||
|
||
void SpinnersManager::stopAll() { | ||
std::vector<std::future<void>> futures; | ||
futures.reserve(spinners_.size()); | ||
|
||
std::ranges::for_each(spinners_, | ||
[&futures](auto* runner) { futures.push_back(std::move(runner->stop())); }); | ||
|
||
// If we called `waitAny`, we need to wait for the remaining futures. | ||
std::ranges::for_each(wait_futures_, [](auto& future) { future.get(); }); | ||
|
||
std::ranges::for_each(futures, [](auto& f) { f.get(); }); | ||
} | ||
} // namespace heph::concurrency |
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,97 @@ | ||
//================================================================================================= | ||
// Copyright (C) 2023-2024 HEPHAESTUS Contributors | ||
//================================================================================================= | ||
|
||
#include <atomic> | ||
#include <stdexcept> | ||
|
||
#include <fmt/core.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "hephaestus/concurrency/spinner.h" | ||
#include "hephaestus/concurrency/spinner_manager.h" | ||
|
||
namespace heph::concurrency::tests { | ||
|
||
TEST(SpinnersManager, Empty) { | ||
SpinnersManager runner_manager{ {} }; | ||
runner_manager.startAll(); | ||
runner_manager.waitAll(); | ||
runner_manager.stopAll(); | ||
} | ||
|
||
TEST(SpinnersManager, OneSpinnerSuccessful) { | ||
std::atomic_bool flag = false; | ||
Spinner spinner{ Spinner::StoppableCallback{ [&flag]() -> Spinner::SpinResult { | ||
flag = true; | ||
return Spinner::SpinResult::STOP; | ||
} } }; | ||
|
||
SpinnersManager runner_manager{ { &spinner } }; | ||
runner_manager.startAll(); | ||
runner_manager.waitAll(); | ||
runner_manager.stopAll(); | ||
|
||
EXPECT_TRUE(flag); | ||
} | ||
|
||
TEST(SpinnersManager, OneSpinnerError) { | ||
Spinner spinner{ []() { throw std::runtime_error("fail"); } }; | ||
|
||
SpinnersManager runner_manager{ { &spinner } }; | ||
runner_manager.startAll(); | ||
runner_manager.waitAll(); | ||
EXPECT_THROW(runner_manager.stopAll(), std::runtime_error); | ||
} | ||
|
||
TEST(SpinnersManager, MultipleSpinnersSuccessful) { | ||
std::atomic_bool flag1 = false; | ||
Spinner spinner1{ Spinner::StoppableCallback{ [&flag1]() -> Spinner::SpinResult { | ||
flag1 = true; | ||
return Spinner::SpinResult::STOP; | ||
} } }; | ||
|
||
std::atomic_bool flag2 = false; | ||
Spinner spinner2{ Spinner::StoppableCallback{ [&flag2]() -> Spinner::SpinResult { | ||
flag2 = true; | ||
return Spinner::SpinResult::STOP; | ||
} } }; | ||
|
||
SpinnersManager runner_manager{ { &spinner1, &spinner2 } }; | ||
runner_manager.startAll(); | ||
runner_manager.waitAll(); | ||
runner_manager.stopAll(); | ||
|
||
EXPECT_TRUE(flag1); | ||
EXPECT_TRUE(flag2); | ||
} | ||
|
||
TEST(SpinnersManager, MultipleSpinnersWaitAny) { | ||
Spinner spinner1{ []() {} }; // Run indefinitely until stopped | ||
|
||
std::atomic_bool flag = false; | ||
Spinner spinner2{ Spinner::StoppableCallback{ [&flag]() -> Spinner::SpinResult { | ||
flag = true; | ||
return Spinner::SpinResult::STOP; | ||
} } }; | ||
|
||
SpinnersManager runner_manager{ { &spinner1, &spinner2 } }; | ||
runner_manager.startAll(); | ||
runner_manager.waitAny(); | ||
runner_manager.stopAll(); | ||
|
||
EXPECT_TRUE(flag); | ||
} | ||
|
||
TEST(SpinnersManager, MultipleSpinnersOneError) { | ||
Spinner spinner1{ []() {} }; // Run indefinitely until stopped | ||
|
||
Spinner spinner2{ []() { throw std::runtime_error("fail"); } }; | ||
|
||
SpinnersManager runner_manager{ { &spinner1, &spinner2 } }; | ||
runner_manager.startAll(); | ||
runner_manager.waitAny(); | ||
EXPECT_THROW(runner_manager.stopAll(), std::runtime_error); | ||
} | ||
|
||
} // namespace heph::concurrency::tests |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming is clear, but given this is somewhat complex async behavior, I would add these two lines. I'm not sure waitAll is useful tbh, as it only is useful if the spinner terminates on its own AND does never throw, which seems a non-existing case. While it is elegant and adds completeness, I feel like removing it.