Arcane
4.1.12.0
User documentation
Loading...
Searching...
No Matches
Task.h
1
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2
//-----------------------------------------------------------------------------
3
// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4
// See the top-level COPYRIGHT file for details.
5
// SPDX-License-Identifier: Apache-2.0
6
//-----------------------------------------------------------------------------
7
/*---------------------------------------------------------------------------*/
8
/* Task.h (C) 2000-2025 */
9
/* */
10
/* Classes managing concurrent tasks. */
11
/*---------------------------------------------------------------------------*/
12
#ifndef ARCCORE_BASE_TASK_H
13
#define ARCCORE_BASE_TASK_H
14
/*---------------------------------------------------------------------------*/
15
/*---------------------------------------------------------------------------*/
16
17
#include "arccore/base/RangeFunctor.h"
18
#include "arccore/base/FatalErrorException.h"
19
#include "arccore/base/ForLoopTraceInfo.h"
20
#include "arccore/base/ParallelLoopOptions.h"
21
#include "arccore/base/ForLoopRunInfo.h"
22
23
#include "arccore/concurrency/ConcurrencyGlobal.h"
24
25
/*---------------------------------------------------------------------------*/
26
/*---------------------------------------------------------------------------*/
27
28
namespace
Arcane
29
{
30
31
/*---------------------------------------------------------------------------*/
32
/*---------------------------------------------------------------------------*/
33
34
/*
35
* TODO:
36
* - Check memory leaks
37
* - CLEARLY INDICATE THAT A TASK MUST NOT BE USED AFTER THE WAIT!!!
38
* - Look into exception mechanism.
39
* - Overload For and Foreach without specifying the block_size
40
*/
41
42
/*---------------------------------------------------------------------------*/
43
/*---------------------------------------------------------------------------*/
44
45
/*!
46
* \brief Execution context of a task.
47
* \ingroup Concurrency
48
*/
49
class
ARCCORE_CONCURRENCY_EXPORT TaskContext
50
{
51
public
:
52
53
explicit
TaskContext(
ITask
* atask)
54
: m_task(atask)
55
{}
56
57
public
:
58
59
//! Current task.
60
ITask
*
task
()
const
{
return
m_task; }
61
62
private
:
63
64
ITask
* m_task;
65
};
66
67
/*---------------------------------------------------------------------------*/
68
/*---------------------------------------------------------------------------*/
69
70
/*!
71
* \internal
72
* \brief Interface for a task functor.
73
* \ingroup Concurrency
74
*/
75
class
ARCCORE_CONCURRENCY_EXPORT ITaskFunctor
76
{
77
public
:
78
79
virtual
~ITaskFunctor() =
default
;
80
81
protected
:
82
83
ITaskFunctor(
const
ITaskFunctor&) =
default
;
84
ITaskFunctor() =
default
;
85
86
public
:
87
88
//! Executes the associated method
89
virtual
void
executeFunctor
(
const
TaskContext
& tc) = 0;
90
virtual
ITaskFunctor* clone(
void
* buffer,
Integer
size) = 0;
91
};
92
93
/*---------------------------------------------------------------------------*/
94
/*---------------------------------------------------------------------------*/
95
96
/*!
97
* \internal
98
* \brief Functor without arguments for a task.
99
* \ingroup Concurrency
100
*/
101
template
<
typename
InstanceType>
102
class
TaskFunctor
103
:
public
ITaskFunctor
104
{
105
public
:
106
107
typedef
void (InstanceType::*FunctorType)();
108
109
public
:
110
111
TaskFunctor(InstanceType* instance, FunctorType func)
112
: m_instance(instance)
113
, m_function(func)
114
{
115
}
116
TaskFunctor(
const
TaskFunctor& rhs) =
default
;
117
TaskFunctor& operator=(
const
TaskFunctor& rhs) =
delete
;
118
119
public
:
120
121
//! Executes the associated method
122
void
executeFunctor
(
const
TaskContext
&
/*tc*/
)
override
123
{
124
(m_instance->*m_function)();
125
}
126
ITaskFunctor
* clone(
void
* buffer,
Integer
size)
override
127
{
128
if
(
sizeof
(*
this
) > (
size_t
)size)
129
ARCCORE_FATAL
(
"INTERNAL: task functor buffer is too small"
);
130
return
new
(buffer)
TaskFunctor<InstanceType>
(*
this
);
131
}
132
133
private
:
134
135
InstanceType* m_instance;
136
FunctorType m_function;
137
};
138
139
/*---------------------------------------------------------------------------*/
140
/*---------------------------------------------------------------------------*/
141
142
/*!
143
* \internal
144
* \brief Functor for a task taking a TaskContext as an argument.
145
* \ingroup Concurrency
146
*/
147
template
<
typename
InstanceType>
148
class
TaskFunctorWithContext
149
:
public
ITaskFunctor
150
{
151
public
:
152
153
typedef
void (InstanceType::*FunctorType)(
const
TaskContext
& tc);
154
155
public
:
156
157
TaskFunctorWithContext(InstanceType* instance, FunctorType func)
158
: ITaskFunctor()
159
, m_instance(instance)
160
, m_function(func)
161
{
162
}
163
164
public
:
165
166
//! Executes the associated method
167
void
executeFunctor
(
const
TaskContext
& tc)
override
168
{
169
(m_instance->*m_function)(tc);
170
}
171
ITaskFunctor
* clone(
void
* buffer,
Integer
size)
override
172
{
173
if
(
sizeof
(*
this
) > (
size_t
)size)
174
ARCCORE_FATAL
(
"INTERNAL: task functor buffer is too small"
);
175
return
new
(buffer)
TaskFunctorWithContext<InstanceType>
(*
this
);
176
}
177
178
private
:
179
180
InstanceType* m_instance =
nullptr
;
181
FunctorType m_function;
182
};
183
184
/*---------------------------------------------------------------------------*/
185
/*---------------------------------------------------------------------------*/
186
187
/*!
188
* \ingroup Concurrency
189
* \brief Interface for a concurrent task.
190
*
191
* Tasks are created via TaskFactory.
192
*/
193
class
ARCCORE_CONCURRENCY_EXPORT
ITask
194
{
195
friend
class
TaskFactory;
196
197
public
:
198
199
virtual
~ITask
() =
default
;
200
201
public
:
202
203
/*!
204
* \brief Launches the task and blocks until it finishes.
205
*
206
* After calling this function, the task is destroyed and must not
207
* be used again.
208
*/
209
virtual
void
launchAndWait
() = 0;
210
/*!
211
* \brief Launches the child tasks \a tasks and blocks
212
* until they finish.
213
*/
214
virtual
void
launchAndWait
(
ConstArrayView<ITask*>
tasks) = 0;
215
216
protected
:
217
218
virtual
ITask
* _createChildTask(
ITaskFunctor
* functor) = 0;
219
};
220
221
/*---------------------------------------------------------------------------*/
222
/*---------------------------------------------------------------------------*/
223
224
}
// End namespace Arcane
225
226
/*---------------------------------------------------------------------------*/
227
/*---------------------------------------------------------------------------*/
228
229
#endif
ARCCORE_FATAL
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Definition
ArccoreGlobal.h:549
Arcane::ConstArrayView
Constant view of an array of type T.
Definition
arccore/src/base/arccore/base/ArrayView.h:550
Arcane::ITaskFunctor
Definition
Task.h:76
Arcane::ITaskFunctor::executeFunctor
virtual void executeFunctor(const TaskContext &tc)=0
Executes the associated method.
Arcane::ITask
Interface for a concurrent task.
Definition
Task.h:194
Arcane::ITask::launchAndWait
virtual void launchAndWait()=0
Launches the task and blocks until it finishes.
Arcane::ITask::launchAndWait
virtual void launchAndWait(ConstArrayView< ITask * > tasks)=0
Launches the child tasks tasks and blocks until they finish.
Arcane::TaskContext
Execution context of a task.
Definition
Task.h:50
Arcane::TaskContext::task
ITask * task() const
Current task.
Definition
Task.h:60
Arcane::TaskFunctorWithContext
Definition
Task.h:150
Arcane::TaskFunctorWithContext::executeFunctor
void executeFunctor(const TaskContext &tc) override
Executes the associated method.
Definition
Task.h:167
Arcane::TaskFunctor
Definition
Task.h:104
Arcane::TaskFunctor::executeFunctor
void executeFunctor(const TaskContext &) override
Executes the associated method.
Definition
Task.h:122
Arcane
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition
AbstractCaseDocumentVisitor.cc:21
Arcane::Integer
Int32 Integer
Type representing an integer.
Definition
ArccoreGlobal.h:293
arccore
concurrency
Task.h
Generated on
for Arcane by
1.16.1