Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Task.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19//
20// ---------------------------------------------------------------
21// Tasking class header file
22//
23// Class Description:
24//
25// This file defines the task types for TaskManager and ThreadPool
26//
27// ---------------------------------------------------------------
28// Author: Jonathan Madsen (Feb 13th 2018)
29// ---------------------------------------------------------------
30
31#pragma once
32
33#include "Globals.hh"
34#include "TaskAllocator.hh"
35#include "VTask.hh"
36
37#include <cstdint>
38#include <functional>
39#include <stdexcept>
40
41namespace PTL
42{
43class VTaskGroup;
44class ThreadPool;
45
46//======================================================================================//
47
48/// \brief The task class is supplied to thread_pool.
49template <typename RetT, typename... Args>
50class PackagedTask : public VTask
51{
52public:
53 typedef PackagedTask<RetT, Args...> this_type;
54 typedef std::promise<RetT> promise_type;
55 typedef std::future<RetT> future_type;
56 typedef std::packaged_task<RetT(Args...)> packaged_task_type;
57 typedef RetT result_type;
58 typedef std::tuple<Args...> tuple_type;
59
60public:
61 // pass a free function pointer
62 template <typename FuncT>
63 PackagedTask(FuncT&& func, Args... args)
64 : VTask()
65 , m_ptask(std::forward<FuncT>(func))
66 , m_args(args...)
67 {}
68
69 template <typename FuncT>
70 PackagedTask(VTaskGroup* tg, FuncT&& func, Args... args)
71 : VTask(tg)
72 , m_ptask(std::forward<FuncT>(func))
73 , m_args(args...)
74 {}
75
76 template <typename FuncT>
77 PackagedTask(ThreadPool* _pool, FuncT&& func, Args... args)
78 : VTask(_pool)
79 , m_ptask(std::forward<FuncT>(func))
80 , m_args(args...)
81 {}
82
83 virtual ~PackagedTask() {}
84
85public:
86 // execution operator
87 virtual void operator()() override
88 {
89 mpl::apply(std::move(m_ptask), std::move(m_args));
90 }
91 future_type get_future() { return m_ptask.get_future(); }
92 virtual bool is_native_task() const override { return true; }
93
94private:
95 packaged_task_type m_ptask;
96 tuple_type m_args;
97};
98
99//======================================================================================//
100
101/// \brief The task class is supplied to thread_pool.
102template <typename RetT, typename... Args>
103class Task : public VTask
104{
105public:
106 typedef Task<RetT, Args...> this_type;
107 typedef std::promise<RetT> promise_type;
108 typedef std::future<RetT> future_type;
109 typedef std::packaged_task<RetT(Args...)> packaged_task_type;
110 typedef RetT result_type;
111 typedef std::tuple<Args...> tuple_type;
112
113public:
114 template <typename FuncT>
115 Task(FuncT&& func, Args... args)
116 : VTask()
117 , m_ptask(std::forward<FuncT>(func))
118 , m_args(args...)
119 {}
120
121 template <typename FuncT>
122 Task(VTaskGroup* tg, FuncT&& func, Args... args)
123 : VTask(tg)
124 , m_ptask(std::forward<FuncT>(func))
125 , m_args(args...)
126 {}
127
128 template <typename FuncT>
129 Task(ThreadPool* tp, FuncT&& func, Args... args)
130 : VTask(tp)
131 , m_ptask(std::forward<FuncT>(func))
132 , m_args(args...)
133 {}
134
135 virtual ~Task() {}
136
137public:
138 // execution operator
139 virtual void operator()() final
140 {
141 mpl::apply(std::move(m_ptask), std::move(m_args));
142 // decrements the task-group counter on active tasks
143 // when the counter is < 2, if the thread owning the task group is
144 // sleeping at the TaskGroup::wait(), it signals the thread to wake
145 // up and check if all tasks are finished, proceeding if this
146 // check returns as true
148 }
149
150 virtual bool is_native_task() const override { return true; }
151 future_type get_future() { return m_ptask.get_future(); }
152
153private:
154 packaged_task_type m_ptask;
155 tuple_type m_args;
156};
157
158//======================================================================================//
159
160/// \brief The task class is supplied to thread_pool.
161template <typename RetT>
162class Task<RetT, void> : public VTask
163{
164public:
166 typedef std::promise<RetT> promise_type;
167 typedef std::future<RetT> future_type;
168 typedef std::packaged_task<RetT()> packaged_task_type;
169 typedef RetT result_type;
170
171public:
172 template <typename FuncT>
173 Task(FuncT&& func)
174 : VTask()
175 , m_ptask(std::forward<FuncT>(func))
176 {}
177
178 template <typename FuncT>
179 Task(VTaskGroup* tg, FuncT&& func)
180 : VTask(tg)
181 , m_ptask(std::forward<FuncT>(func))
182 {}
183
184 template <typename FuncT>
185 Task(ThreadPool* tp, FuncT&& func)
186 : VTask(tp)
187 , m_ptask(std::forward<FuncT>(func))
188 {}
189
190 virtual ~Task() {}
191
192public:
193 // execution operator
194 virtual void operator()() final
195 {
196 m_ptask();
197 // decrements the task-group counter on active tasks
198 // when the counter is < 2, if the thread owning the task group is
199 // sleeping at the TaskGroup::wait(), it signals the thread to wake
200 // up and check if all tasks are finished, proceeding if this
201 // check returns as true
203 }
204
205 virtual bool is_native_task() const override { return true; }
206 future_type get_future() { return m_ptask.get_future(); }
207
208private:
209 packaged_task_type m_ptask;
210};
211
212//======================================================================================//
213
214/// \brief The task class is supplied to thread_pool.
215template <>
216class Task<void, void> : public VTask
217{
218public:
219 typedef void RetT;
221 typedef std::promise<RetT> promise_type;
222 typedef std::future<RetT> future_type;
223 typedef std::packaged_task<RetT()> packaged_task_type;
225
226public:
227 template <typename FuncT>
228 explicit Task(FuncT&& func)
229 : VTask()
230 , m_ptask(std::forward<FuncT>(func))
231 {}
232
233 template <typename FuncT>
234 Task(VTaskGroup* tg, FuncT&& func)
235 : VTask(tg)
236 , m_ptask(std::forward<FuncT>(func))
237 {}
238
239 template <typename FuncT>
240 Task(ThreadPool* tp, FuncT&& func)
241 : VTask(tp)
242 , m_ptask(std::forward<FuncT>(func))
243 {}
244
245 virtual ~Task() {}
246
247public:
248 // execution operator
249 virtual void operator()() final
250 {
251 m_ptask();
252 // decrements the task-group counter on active tasks
253 // when the counter is < 2, if the thread owning the task group is
254 // sleeping at the TaskGroup::wait(), it signals the thread to wake
255 // up and check if all tasks are finished, proceeding if this
256 // check returns as true
258 }
259
260 virtual bool is_native_task() const override { return true; }
261 future_type get_future() { return m_ptask.get_future(); }
262
263private:
264 packaged_task_type m_ptask;
265};
266
267//======================================================================================//
268
269} // namespace PTL
The task class is supplied to thread_pool.
Definition: Task.hh:51
virtual ~PackagedTask()
Definition: Task.hh:83
std::packaged_task< RetT(Args...)> packaged_task_type
Definition: Task.hh:56
RetT result_type
Definition: Task.hh:57
std::tuple< Args... > tuple_type
Definition: Task.hh:58
std::promise< RetT > promise_type
Definition: Task.hh:54
PackagedTask(FuncT &&func, Args... args)
Definition: Task.hh:63
PackagedTask(VTaskGroup *tg, FuncT &&func, Args... args)
Definition: Task.hh:70
virtual void operator()() override
Definition: Task.hh:87
virtual bool is_native_task() const override
Definition: Task.hh:92
future_type get_future()
Definition: Task.hh:91
std::future< RetT > future_type
Definition: Task.hh:55
PackagedTask(ThreadPool *_pool, FuncT &&func, Args... args)
Definition: Task.hh:77
PackagedTask< RetT, Args... > this_type
Definition: Task.hh:53
future_type get_future()
Definition: Task.hh:206
std::promise< RetT > promise_type
Definition: Task.hh:166
std::packaged_task< RetT()> packaged_task_type
Definition: Task.hh:168
Task< RetT > this_type
Definition: Task.hh:165
virtual bool is_native_task() const override
Definition: Task.hh:205
std::future< RetT > future_type
Definition: Task.hh:167
Task(VTaskGroup *tg, FuncT &&func)
Definition: Task.hh:179
Task(ThreadPool *tp, FuncT &&func)
Definition: Task.hh:185
Task(FuncT &&func)
Definition: Task.hh:173
virtual void operator()() final
Definition: Task.hh:194
Task(FuncT &&func)
Definition: Task.hh:228
future_type get_future()
Definition: Task.hh:261
std::future< RetT > future_type
Definition: Task.hh:222
std::packaged_task< RetT()> packaged_task_type
Definition: Task.hh:223
virtual void operator()() final
Definition: Task.hh:249
Task< void, void > this_type
Definition: Task.hh:220
std::promise< RetT > promise_type
Definition: Task.hh:221
Task(VTaskGroup *tg, FuncT &&func)
Definition: Task.hh:234
virtual bool is_native_task() const override
Definition: Task.hh:260
Task(ThreadPool *tp, FuncT &&func)
Definition: Task.hh:240
The task class is supplied to thread_pool.
Definition: Task.hh:104
Task(FuncT &&func, Args... args)
Definition: Task.hh:115
Task(ThreadPool *tp, FuncT &&func, Args... args)
Definition: Task.hh:129
std::tuple< Args... > tuple_type
Definition: Task.hh:111
std::promise< RetT > promise_type
Definition: Task.hh:107
std::future< RetT > future_type
Definition: Task.hh:108
RetT result_type
Definition: Task.hh:110
virtual void operator()() final
Definition: Task.hh:139
virtual bool is_native_task() const override
Definition: Task.hh:150
future_type get_future()
Definition: Task.hh:151
virtual ~Task()
Definition: Task.hh:135
std::packaged_task< RetT(Args...)> packaged_task_type
Definition: Task.hh:109
Task(VTaskGroup *tg, FuncT &&func, Args... args)
Definition: Task.hh:122
Task< RetT, Args... > this_type
Definition: Task.hh:106
VTask is the abstract class stored in thread_pool.
Definition: VTask.hh:55
void operator--()
Definition: VTask.cc:69
Definition: AutoLock.hh:254