Ocean
Loading...
Searching...
No Matches
BackgroundTask.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_TRACKING_SLAM_BACKGROUND_TASK_H
9#define META_OCEAN_TRACKING_SLAM_BACKGROUND_TASK_H
10
12
13#include <condition_variable>
14#include <functional>
15#include <thread>
16
17namespace Ocean
18{
19
20namespace Tracking
21{
22
23namespace SLAM
24{
25
26/**
27 * This class implements a task which runs in the background while the caller is able to wait for the task to be finished.
28 * The class provides a handshake pattern where wait() and execute() must be called in alternating sequence.<br>
29 * This ensures proper synchronization between the main thread and the background thread.<br>
30 * Usage pattern:
31 * <pre>
32 * task.setTask(...);
33 *
34 * while (processing)
35 * {
36 * task.wait(); // wait for previous execution to finish (first call returns immediately)
37 *
38 * // ... do main thread work ...
39 *
40 * task.execute(); // start background execution
41 * }
42 *
43 * task.release();
44 * </pre>
45 * @ingroup trackingslam
46 */
47class OCEAN_TRACKING_SLAM_EXPORT BackgroundTask
48{
49 public:
50
51 /**
52 * Definition of individual wait result states.
53 */
54 enum WaitResult : uint32_t
55 {
56 /// Invalid wait result.
57 WR_INVALID = 0u,
58 /// The task was released before completion.
60 /// The task was processed successfully.
61 WR_PROCESSED
62 };
63
64 /**
65 * Definition of a task function to be executed in the background.
66 */
67 using Task = std::function<void()>;
68
69 public:
70
71 /**
72 * Creates a new background task object without a task.
73 * @see setTask()
74 */
75 BackgroundTask() = default;
76
77 /**
78 * Creates a new background task object with a given task.
79 * @param task The task to be executed in the background, must be valid
80 * @see setTask()
81 */
82 explicit BackgroundTask(Task&& task);
83
84 /**
85 * Destructs the background task object.
86 * The destructor automatically calls release() to ensure the background thread is stopped.
87 * @see release()
88 */
90
91 public:
92
93 /**
94 * Sets the task to be executed in the background.
95 * This function creates and starts the background thread which will wait for execute() calls.
96 * @param task The task to be executed in the background, must be valid
97 * @return True, if succeeded; False, if a task is already set
98 */
99 bool setTask(Task&& task);
100
101 /**
102 * Executes the task in the background.
103 * This function signals the background thread to execute the task.
104 * The function returns immediately without waiting for the task to complete.
105 * wait() must be called before this function (except for the first call after setTask()).
106 * @return True, if the task execution was initiated successfully; False, if the task is already executing or the object was released
107 * @see wait()
108 */
109 bool execute();
110
111 /**
112 * Waits until the previous task execution has been processed.
113 * This function must be called before execute() to ensure the previous execution has completed.
114 * On the first call after setTask(), this function returns immediately with WR_PROCESSED.
115 * @return WR_PROCESSED if the task completed successfully, WR_RELEASED if the task was released, or WR_INVALID in case of an error
116 * @see execute()
117 */
119
120 /**
121 * Explicitly releases the background task and stops the background thread.
122 * This function blocks until the background thread has fully terminated.
123 * After calling this function, the object cannot be used anymore unless setTask() is called again.
124 * The destructor automatically calls this function.
125 */
126 void release();
127
128 protected:
129
130 /**
131 * Disabled copy constructor.
132 * @param backgroundTask The background task object to copy
133 */
134 BackgroundTask(const BackgroundTask& backgroundTask) = delete;
135
136 /**
137 * Disabled move constructor.
138 * @param backgroundTask The background task object to move
139 */
140 BackgroundTask(BackgroundTask&& backgroundTask) = delete;
141
142 /**
143 * The internal thread run function.
144 * This function runs in the background thread and waits for execute() signals to run the task.
145 * @param stopToken The stop token allowing to check whether the thread should be stopped
146 */
147 void threadRun(std::stop_token stopToken);
148
149 /**
150 * Disabled copy assignment operator.
151 * @param backgroundTask The background task object to copy
152 * @return Reference to this object
153 */
154 BackgroundTask& operator=(const BackgroundTask& backgroundTask) = delete;
155
156 /**
157 * Disabled move assignment operator.
158 * @param backgroundTask The background task object to move
159 * @return Reference to this object
160 */
161 BackgroundTask& operator=(BackgroundTask&& backgroundTask) = delete;
162
163 protected:
164
165 /// The background task to execute.
167
168 /// The background thread.
169 std::jthread thread_;
170
171 /// True, if the task should be executed; False, otherwise.
172 bool taskExecute_ = false;
173
174 /// The condition variable to signal task execution.
175 std::condition_variable taskExecuteCondition_;
176
177 /// True, if the task has been processed; False, otherwise.
178 bool taskProcessed_ = true;
179
180 /// The condition variable to signal task completion.
181 std::condition_variable taskProcessedCondition_;
182
183 /// The mutex protecting the internal state.
184 std::mutex mutex_;
185
186 /// True, if the background task has been released or not yet initialized; False, if active.
187 bool released_ = true;
188};
189
190}
191
192}
193
194}
195
196#endif // META_OCEAN_TRACKING_SLAM_BACKGROUND_TASK_H
This class implements a task which runs in the background while the caller is able to wait for the ta...
Definition BackgroundTask.h:48
std::condition_variable taskExecuteCondition_
The condition variable to signal task execution.
Definition BackgroundTask.h:175
BackgroundTask(const BackgroundTask &backgroundTask)=delete
Disabled copy constructor.
WaitResult wait()
Waits until the previous task execution has been processed.
bool execute()
Executes the task in the background.
BackgroundTask(BackgroundTask &&backgroundTask)=delete
Disabled move constructor.
BackgroundTask & operator=(BackgroundTask &&backgroundTask)=delete
Disabled move assignment operator.
Task task_
The background task to execute.
Definition BackgroundTask.h:166
~BackgroundTask()
Destructs the background task object.
std::jthread thread_
The background thread.
Definition BackgroundTask.h:169
void release()
Explicitly releases the background task and stops the background thread.
void threadRun(std::stop_token stopToken)
The internal thread run function.
bool setTask(Task &&task)
Sets the task to be executed in the background.
BackgroundTask()=default
Creates a new background task object without a task.
WaitResult
Definition of individual wait result states.
Definition BackgroundTask.h:55
@ WR_RELEASED
The task was released before completion.
Definition BackgroundTask.h:59
std::function< void()> Task
Definition of a task function to be executed in the background.
Definition BackgroundTask.h:67
BackgroundTask & operator=(const BackgroundTask &backgroundTask)=delete
Disabled copy assignment operator.
BackgroundTask(Task &&task)
Creates a new background task object with a given task.
std::mutex mutex_
The mutex protecting the internal state.
Definition BackgroundTask.h:184
std::condition_variable taskProcessedCondition_
The condition variable to signal task completion.
Definition BackgroundTask.h:181
The namespace covering the entire Ocean framework.
Definition Accessor.h:15