GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tech/cxx_tasks.cpp
Date: 2026-09-21 06:26:40
Exec Total Coverage
Lines: 18 117 15.4%
Functions: 8 29 27.6%
Branches: 6 110 5.5%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2026, Stefan Meislinger |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/tech/tasks.hpp"
30 #include "gate/queues.hpp"
31
32 namespace gate
33 {
34 namespace tech
35 {
36
37 4 ITaskFactory::~ITaskFactory() noexcept
38 {
39 4 }
40
41
42 2 TaskFactoryBase::TaskFactoryBase(String const& taskname, String const& descr, Property const& defaultParams)
43
3/6
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
2 : name(taskname.clone()), description(descr.clone()), defaultParameters(defaultParams)
44 {
45 2 }
46 4 TaskFactoryBase::~TaskFactoryBase() noexcept
47 {
48 4 }
49
50 1 String TaskFactoryBase::getName() const
51 {
52 1 return this->name;
53 }
54 1 Property const& TaskFactoryBase::getDefaultParameters() const
55 {
56 1 return this->defaultParameters;
57 }
58 1 String TaskFactoryBase::getDescription(Property const& parameters)
59 {
60 1 return this->description;
61 }
62
63
64
65 ManagedTask::Statistics::Statistics()
66 : started(0), completed(0), failed(0)
67 {
68 }
69
70 ManagedTask::ManagedTask(id_t taskid, Ref<ThreadPool> workers, TaskFactory fact, Property const& params, time::Milliseconds autoRepeat)
71 : id(taskid), threadpool(workers), factory(fact), parameters(params), repeatInterval(autoRepeat), task(NULL)
72 {
73 }
74
75 ManagedTask::~ManagedTask() noexcept
76 {
77 }
78
79 ManagedTask::id_t ManagedTask::getId()
80 {
81 return this->id;
82 }
83
84 Time ManagedTask::getLastStarted()
85 {
86 MutexLock lock(this->mutex);
87 return this->stats.lastStartTime;
88 }
89
90 TimeCounter ManagedTask::getLastStartedTimestamp()
91 {
92 MutexLock lock(this->mutex);
93 return this->stats.lastStartTimestamp;
94 }
95
96 Time ManagedTask::getNextPlannedStart()
97 {
98 MutexLock lock(this->mutex);
99 if (!this->stats.lastStartTime)
100 {
101 return this->stats.lastStartTime;
102 }
103 else
104 {
105 return this->stats.lastStartTime + this->repeatInterval;
106 }
107 }
108
109 TimeCounter ManagedTask::getNextPlannedStartTimestamp()
110 {
111 MutexLock lock(this->mutex);
112 if (!this->stats.lastStartTimestamp)
113 {
114 return this->stats.lastStartTimestamp;
115 }
116 else
117 {
118 return this->stats.lastStartTimestamp + this->repeatInterval;
119 }
120 }
121
122 Task::StatusEnum ManagedTask::getStatus()
123 {
124 MutexLock lock(this->mutex);
125 if (this->task.empty())
126 {
127 return Task::Status_Prepared;
128 }
129 else
130 {
131 return this->task.status();
132 }
133 }
134
135 void ManagedTask::run()
136 {
137 TimeCounter startTime = TimeCounter::now();
138 Task runTask((NULL));
139 {
140 MutexLock lock(this->mutex);
141 if (this->task.empty() && !this->stats.lastStartTimestamp)
142 {
143 // First time the managed task is executed and no internal task present -> create one
144 this->task = this->factory->createTask(this->parameters);
145 }
146 runTask = this->task;
147 this->stats.started++;
148 this->stats.lastStartTime = Time::now();
149 this->stats.lastStartTimestamp = startTime;
150 }
151
152 Task::StatusEnum status = Task::Status_Failed;
153
154 time::Milliseconds executionTime;
155 if (!runTask.empty())
156 {
157 ExceptionInfo xcptInfo;
158 GATEXX_TRY_CATCHINFO(xcptInfo, {
159 runTask.run();
160 status = runTask.status();
161 });
162 if (xcptInfo.failed())
163 {
164 status = Task::Status_Failed;
165 }
166 TimeCounter endTime = TimeCounter::now();
167
168 time::Microseconds duration = endTime - startTime;
169 executionTime = time::duration_cast<time::Milliseconds>(duration);
170 }
171
172 {
173 MutexLock lock(this->mutex);
174 if (status == Task::Status_Failed)
175 {
176 this->stats.failed++;
177 }
178 else
179 {
180 this->stats.completed++;
181 }
182 this->stats.lastExecutionTime = executionTime;
183 this->stats.totalExecutionTime += executionTime;
184
185 if (this->repeatInterval.value() == 0)
186 {
187 // release task object
188 this->task = Task(NULL);
189 }
190 }
191 }
192
193 class GATE_API_LOCAL RunnableManagedTask : public IRunnableBuilder
194 {
195 private:
196 Ref<ManagedTask> task;
197 public:
198 explicit RunnableManagedTask(Ref<ManagedTask> const& mtask)
199 : task(mtask)
200 {
201 }
202
203 ~RunnableManagedTask() noexcept
204 {
205 }
206
207 virtual void run()
208 {
209 if (!this->task.empty())
210 {
211 this->task->run();
212 }
213 }
214 };
215
216 static Runnable createRunnableManagedTask(Ref<ManagedTask> const& task)
217 {
218 RunnableManagedTask* ptr = new RunnableManagedTask(task);
219 Runnable ret(ptr->c_impl());
220 return ret;
221 }
222
223
224
225 1 TaskManager::TaskManager()
226
3/6
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
1 : workers(Ref<ThreadPool>::fromPtr(new ThreadPool()))
227 {
228 1 }
229 1 TaskManager::~TaskManager() noexcept
230 {
231
232 1 }
233 void TaskManager::addTaskFactory(Ref<ITaskFactory> const& factory)
234 {
235 //TODO
236 GATEXX_RAISE_ERROR(results::NotImplemented);
237 }
238 Property TaskManager::getTaskDefaultParameters(String const& taskName)
239 {
240 Property ret;
241 //TODO
242 GATEXX_RAISE_ERROR(results::NotImplemented);
243 return ret;
244 }
245
246 TaskManager::id_t TaskManager::addNewTask(String const& name, Property const& parameters, time::Milliseconds autoRepeat)
247 {
248 id_t ret = 0;
249 //TODO
250 GATEXX_RAISE_ERROR(results::NotImplemented);
251 return ret;
252 }
253 Array<TaskManager::id_t> TaskManager::getTasks()
254 {
255 Array<TaskManager::id_t> ret;
256 //TODO
257 GATEXX_RAISE_ERROR(results::NotImplemented);
258 return ret;
259 }
260 void TaskManager::removeTask(id_t taskId)
261 {
262 //TODO
263 GATEXX_RAISE_ERROR(results::NotImplemented);
264 }
265 TaskManager::Info TaskManager::getTaskInfo(id_t taskId)
266 {
267 Info ret;
268 //TODO
269 GATEXX_RAISE_ERROR(results::NotImplemented);
270 return ret;
271 }
272
273 void TaskManager::startTask(id_t id)
274 {
275 //TODO
276 GATEXX_RAISE_ERROR(results::NotImplemented);
277 }
278
279 } // end of namespace tech
280 } // end of namespace gate
281