-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes swallowed exceptions in computation thread (#1685)
* Fixes swallowed exceptions in computation thread Exceptions are now transferred from the computation thread to the main one using std::future mechanisms. Only the first encountered exception is thrown in the main thread. Signed-off-by: Sylvain Leclerc <[email protected]> * - transfer exceptions occuring in zip_writer too - some cleanup - an integration test to check behaviour in simulation Signed-off-by: Sylvain Leclerc <[email protected]> * Take into account review Signed-off-by: Sylvain Leclerc <[email protected]> * - Separate thread pool for zip IO - Addition of a flush method for writers, to synchronize - Addition of tests for zip writer Signed-off-by: Sylvain Leclerc <[email protected]> * Some comments Signed-off-by: Sylvain Leclerc <[email protected]> * Throw more exceptions from zip writer Signed-off-by: Sylvain Leclerc <[email protected]> * Fix headers Signed-off-by: Sylvain Leclerc <[email protected]> * Fix missing header (windows build) Signed-off-by: Sylvain Leclerc <[email protected]> * Take into account review Signed-off-by: Sylvain Leclerc <[email protected]> * Fix windows build again Signed-off-by: Sylvain Leclerc <[email protected]> --------- Signed-off-by: Sylvain Leclerc <[email protected]>
- Loading branch information
Showing
23 changed files
with
568 additions
and
45 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,9 @@ | ||
|
||
|
||
add_library(concurrency) | ||
add_library(Antares::concurrency ALIAS concurrency) | ||
|
||
target_sources(concurrency PRIVATE concurrency.cpp) | ||
target_include_directories(concurrency PUBLIC include) | ||
|
||
target_link_libraries(concurrency yuni-static-core) |
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,83 @@ | ||
/* | ||
** Copyright 2007-2023 RTE | ||
** Authors: Antares_Simulator Team | ||
** | ||
** This file is part of Antares_Simulator. | ||
** | ||
** Antares_Simulator is free software: you can redistribute it and/or modify | ||
** it under the terms of the GNU General Public License as published by | ||
** the Free Software Foundation, either version 3 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** There are special exceptions to the terms and conditions of the | ||
** license as they are applied to this software. View the full text of | ||
** the exceptions in file COPYING.txt in the directory of this software | ||
** distribution | ||
** | ||
** Antares_Simulator is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** GNU General Public License for more details. | ||
** | ||
** You should have received a copy of the GNU General Public License | ||
** along with Antares_Simulator. If not, see <http://www.gnu.org/licenses/>. | ||
** | ||
** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions | ||
*/ | ||
#include <memory> | ||
#include "yuni/job/job.h" | ||
#include "antares/concurrency/concurrency.h" | ||
|
||
namespace Antares::Concurrency | ||
{ | ||
|
||
namespace { | ||
|
||
/*! | ||
* Just wraps an arbitrary task as a yuni job, and allows to retrieve the corresponding future. | ||
*/ | ||
class PackagedJob : public Yuni::Job::IJob { | ||
public: | ||
PackagedJob(const Task& task) : task_(task) {} | ||
|
||
TaskFuture getFuture() { | ||
return task_.get_future(); | ||
} | ||
|
||
protected: | ||
void onExecute() override { | ||
task_(); | ||
} | ||
|
||
private: | ||
std::packaged_task<void()> task_; | ||
}; | ||
|
||
} | ||
|
||
std::future<void> AddTask(Yuni::Job::QueueService& threadPool, | ||
const Task& task, | ||
Yuni::Job::Priority priority) { | ||
auto job = std::make_unique<PackagedJob>(task); | ||
auto future = job->getFuture(); | ||
threadPool.add(job.release(), priority); | ||
return future; | ||
} | ||
|
||
void FutureSet::add(TaskFuture&& f) { | ||
std::lock_guard lock(mutex_); | ||
futures_.push_back(std::move(f)); | ||
} | ||
|
||
void FutureSet::join() { | ||
std::vector<TaskFuture> toBeJoined; | ||
{ | ||
std::lock_guard lock(mutex_); | ||
std::swap(futures_, toBeJoined); | ||
} | ||
for (auto& f: toBeJoined) { | ||
f.get(); | ||
} | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/libs/antares/concurrency/include/antares/concurrency/concurrency.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,88 @@ | ||
/* | ||
** Copyright 2007-2023 RTE | ||
** Authors: Antares_Simulator Team | ||
** | ||
** This file is part of Antares_Simulator. | ||
** | ||
** Antares_Simulator is free software: you can redistribute it and/or modify | ||
** it under the terms of the GNU General Public License as published by | ||
** the Free Software Foundation, either version 3 of the License, or | ||
** (at your option) any later version. | ||
** | ||
** There are special exceptions to the terms and conditions of the | ||
** license as they are applied to this software. View the full text of | ||
** the exceptions in file COPYING.txt in the directory of this software | ||
** distribution | ||
** | ||
** Antares_Simulator is distributed in the hope that it will be useful, | ||
** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
** GNU General Public License for more details. | ||
** | ||
** You should have received a copy of the GNU General Public License | ||
** along with Antares_Simulator. If not, see <http://www.gnu.org/licenses/>. | ||
** | ||
** SPDX-License-Identifier: licenceRef-GPL3_WITH_RTE-Exceptions | ||
*/ | ||
#ifndef ANTARES_CONCURRENCY_H | ||
#define ANTARES_CONCURRENCY_H | ||
|
||
#include <future> | ||
#include "yuni/job/queue/service.h" | ||
|
||
namespace Antares::Concurrency | ||
{ | ||
|
||
using Task = std::function<void()>; | ||
using TaskFuture = std::future<void>; | ||
|
||
/*! | ||
* \brief Queues the provided function and returns the corresponding std::future. | ||
* | ||
* This allows to handle exceptions occuring in the underlying task, | ||
* as opposite to Yuni::Job::QueueService::add which swallows them. | ||
*/ | ||
[[nodiscard]] TaskFuture AddTask(Yuni::Job::QueueService& threadPool, | ||
const Task& task, | ||
Yuni::Job::Priority priority = Yuni::Job::priorityDefault); | ||
|
||
/*! | ||
* \brief Utility class to gather futures to wait for. | ||
*/ | ||
class FutureSet | ||
{ | ||
public: | ||
FutureSet() = default; | ||
~FutureSet() = default; | ||
|
||
FutureSet(const FutureSet&) = delete; | ||
FutureSet& operator=(const FutureSet&) = delete; | ||
FutureSet(FutureSet&&) = delete; | ||
FutureSet& operator=(FutureSet&&) = delete; | ||
|
||
/*! | ||
* \brief Adds one future to be monitored by this set. | ||
* | ||
* Note: the provided future will be left in "moved from" state. | ||
*/ | ||
void add(TaskFuture&& f); | ||
|
||
/*! | ||
* \brief Waits for completion of all added futures. | ||
* | ||
* If one of the future ends on exception, re-throws the first encountered exception. | ||
* Note that futures cannot be added while some thread is waiting for completion. | ||
* | ||
* Joining also resets the list of tasks to wait for. | ||
*/ | ||
void join(); | ||
|
||
private: | ||
std::mutex mutex_; | ||
std::vector<TaskFuture> futures_; | ||
}; | ||
|
||
} | ||
|
||
|
||
#endif //ANTARES_CONCURRENCY_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
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
Oops, something went wrong.