Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
concurrency.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#ifndef ANTARES_CONCURRENCY_H
22#define ANTARES_CONCURRENCY_H
23
24#include <future>
25
26#include "yuni/job/queue/service.h"
27
28namespace Antares::Concurrency
29{
30
31using Task = std::function<void()>;
32using TaskFuture = std::future<void>;
33
40[[nodiscard]] TaskFuture AddTask(Yuni::Job::QueueService& threadPool,
41 const Task& task,
42 Yuni::Job::Priority priority = Yuni::Job::priorityDefault);
43
52template<class T>
53[[nodiscard]] TaskFuture AddTask(Yuni::Job::QueueService& threadPool,
54 const std::shared_ptr<T>& task,
55 Yuni::Job::Priority priority = Yuni::Job::priorityDefault);
56
61{
62public:
63 FutureSet() = default;
64 ~FutureSet() = default;
65
66 FutureSet(const FutureSet&) = delete;
67 FutureSet& operator=(const FutureSet&) = delete;
68 FutureSet(FutureSet&&) = delete;
69 FutureSet& operator=(FutureSet&&) = delete;
70
76 void add(TaskFuture&& f);
77
86 void join();
87
88private:
89 std::mutex mutex_;
90 std::vector<TaskFuture> futures_;
91};
92
93namespace Detail
94{ // implementation details
95
102template<class T>
104{
105public:
106 explicit CopyableCallable(const std::shared_ptr<T>& functionObject):
107 functionObject_(functionObject)
108 {
109 }
110
111 void operator()()
112 {
113 (*functionObject_)();
114 }
115
116private:
117 std::shared_ptr<T> functionObject_;
118};
119
120} // namespace Detail
121
122template<class T>
123TaskFuture AddTask(Yuni::Job::QueueService& threadPool,
124 const std::shared_ptr<T>& task,
125 Yuni::Job::Priority priority)
126{
127 Task wrappedTask = Detail::CopyableCallable<T>(task);
128 return AddTask(threadPool, wrappedTask, priority);
129}
130
131} // namespace Antares::Concurrency
132
133#endif // ANTARES_CONCURRENCY_H
Utility class to gather futures to wait for.
Definition concurrency.h:61
void add(TaskFuture &&f)
Adds one future to be monitored by this set.
Definition concurrency.cpp:71
void join()
Waits for completion of all added futures.
Definition concurrency.cpp:77