GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_runnables.cpp
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 200 226 88.5%
Functions: 43 44 97.7%
Branches: 63 125 50.4%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
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/runnables.hpp"
30
31 namespace gate
32 {
33
34
35
36 18 IRunnableBuilder::~IRunnableBuilder() noexcept
37 {
38 18 }
39
40 9 result_t IRunnableBuilder::runnable_run(void* this_ptr)
41 {
42 9 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (dispatcher == NULL)
44 {
45 ✗ return GATE_RESULT_NULLPOINTER;
46 }
47 9 IRunnableBuilder* obj = getInterface<IRunnableBuilder>(dispatcher);
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (NULL == obj)
49 {
50 ✗ return GATE_RESULT_INCORRECTTYPE;
51 }
52 9 IRunnable& runnable = *obj;
53
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 ExceptionInfo xcptInfo = Catch::exception(runnable);
54 9 return xcptInfo.result_code;
55 }
56
57 8 IRunnableBuilder::VtblRunnable::VtblRunnable()
58 {
59 8 obj_vtbl_t* this_ptr = reinterpret_cast<obj_vtbl_t*>(this);
60 8 this_ptr->run = &IRunnableBuilder::runnable_run;
61 8 }
62
63 IRunnableBuilder::VtblRunnable IRunnableBuilder::cpp_vtbl_runnable;
64
65 /*
66 IRunnableBuilder::IRunnableBuilder(obj_vtbl_t const* vtbl_ptr, char const* name)
67 : object_builder_t(vtbl_ptr, name)
68 {
69 }
70 */
71
72 9 IRunnableBuilder::IRunnableBuilder()
73
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 : object_builder_t(&cpp_vtbl_runnable, GATE_INTERFACE_NAME_RUNNABLE)
74 {
75 9 }
76
77
78
79 28 Runnable::~Runnable() noexcept
80 {
81 28 }
82 1 void Runnable::run()
83 {
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (this->empty())
85 {
86 ✗ GATEXX_RAISE_ERROR(results::NullPointer);
87 }
88 1 gate_result_t result = gate_runnable_run(this->c_impl());
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
90 1 }
91
92 9 Runnable::Runnable(gate_runnable_t* ptr_runnable)
93 9 : object_impl_t(ptr_runnable)
94 {
95 9 }
96
97 5 Runnable::Runnable(Runnable const& src)
98 5 : object_impl_t(src)
99 {
100 5 }
101
102 1 Runnable& Runnable::operator=(Runnable const& src)
103 {
104 1 object_impl_t::operator=(src);
105 1 return *this;
106 }
107
108
109
110 12 Task::~Task() noexcept
111 {
112 12 }
113 3 void Task::run()
114 {
115 3 result_t result = gate_task_run(this->c_impl());
116
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 GATEXX_CHECK_EXCEPTION(result);
117 2 }
118
119 3 void Task::cancel()
120 {
121 3 result_t result = gate_task_cancel(this->c_impl());
122
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 GATEXX_CHECK_EXCEPTION(result);
123 1 }
124
125 3 Task::Task(gate_task_t* ptr_task)
126 3 : object_impl_t(ptr_task)
127 {
128 3 }
129 3 Task::Task(Task const& src)
130 3 : object_impl_t(src)
131 {
132
133 3 }
134 1 Task& Task::operator=(Task const& src)
135 {
136 1 object_impl_t::operator=(src);
137 1 return *this;
138 }
139
140 3 Task::StatusEnum Task::status()
141 {
142 3 return static_cast<Task::StatusEnum>(gate_task_get_status(this->c_impl()));
143 }
144
145 5 bool_t Task::completed()
146 {
147 5 return gate_task_completed(this->c_impl());
148 }
149
150 1 void const* Task::getResult()
151 {
152 1 void const* ptrTaskData = NULL;
153 1 result_t taskResult = results::Failed;
154
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 result_t result = gate_task_get_result(this->c_impl(), &taskResult, &ptrTaskData);
155
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
156
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(taskResult);
157 1 return ptrTaskData;
158 }
159
160 2 void Task::wait()
161 {
162 2 result_t result = gate_task_wait(this->c_impl());
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
164 2 }
165
166 1 bool Task::waitFor(uint32_t timeoutMs)
167 {
168 1 result_t result = gate_task_timed_wait(this->c_impl(), timeoutMs);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(result))
170 {
171 ✗ if (result == GATE_RESULT_TIMEOUT)
172 {
173 ✗ return false;
174 }
175 else
176 {
177 ✗ GATEXX_RAISE_EXCEPTION(result, NULL, 0);
178 }
179 }
180 1 return true;
181 }
182
183
184
185
186 12 ITaskBuilder::~ITaskBuilder() noexcept
187 {
188
189 12 }
190
191 3 result_t ITaskBuilder::task_run(void* this_ptr)
192 {
193 3 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
194 3 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
195
196
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 ExceptionInfo xcptInfo = Catch::exception(*obj);
197 3 return xcptInfo.result_code;
198 }
199 3 result_t ITaskBuilder::task_cancel(void* this_ptr)
200 {
201 3 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
202 3 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
203
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 ExceptionInfo xcptInfo;
204
5/8
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
5 GATEXX_TRY_CATCHINFO(xcptInfo, {
205 obj->cancel();
206 });
207 3 return xcptInfo.result_code;
208 }
209 3 int ITaskBuilder::task_get_status(void* this_ptr)
210 {
211 3 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
212 3 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
213 3 return static_cast<int>(obj->status());
214 }
215 5 gate_bool_t ITaskBuilder::task_completed(void* this_ptr)
216 {
217 5 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
218 5 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
219 5 return static_cast<int>(obj->completed());
220
221 }
222 1 gate_result_t ITaskBuilder::task_get_result(void* this_ptr, gate_result_t* ptr_task_result, void const** ptr_task_data)
223 {
224 1 result_t ret = results::Ok;
225 1 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
226 1 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
227
228 1 result_t taskResult = results::Failed;
229 1 void const* taskData = NULL;
230
231 1 Task::StatusEnum status = obj->status();
232
1/5
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 switch (status)
233 {
234 ✗ case Task::Status_Prepared:
235 case Task::Status_Running:
236 {
237 ✗ taskResult = results::InvalidState;
238 ✗ break;
239 }
240 1 case Task::Status_Completed:
241 {
242
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ExceptionInfo xcptInfo;
243
2/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
1 GATEXX_TRY_CATCHINFO(xcptInfo, {
244 taskData = obj->getResult();
245 })
246 1 taskResult = xcptInfo.result_code;
247 1 ret = results::Ok;
248 1 break;
249 }
250 ✗ case Task::Status_Canceled:
251 {
252 ✗ taskResult = results::Canceled;
253 ✗ ret = results::Ok;
254 ✗ break;
255 }
256 ✗ case Task::Status_Failed:
257 {
258 ✗ taskResult = results::Failed;
259 ✗ ret = results::Ok;
260 ✗ break;
261 }
262 }
263
264
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_task_result)
265 {
266 1 *ptr_task_result = taskResult;
267 }
268
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_task_data)
269 {
270 1 *ptr_task_data = taskData;
271 }
272
273 1 return ret;
274 }
275 2 gate_result_t ITaskBuilder::task_wait(void* this_ptr)
276 {
277 2 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
278 2 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
279
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 ExceptionInfo xcptInfo;
280
2/8
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
2 GATEXX_TRY_CATCHINFO(xcptInfo, {
281 obj->wait();
282 });
283 2 return xcptInfo.result_code;
284 }
285 1 gate_result_t ITaskBuilder::task_timed_wait(void* this_ptr, gate_uint32_t timeout_ms)
286 {
287 1 cpp_object* dispatcher = static_cast<cpp_object*>(this_ptr);
288 1 ITaskBuilder* obj = getInterface<ITaskBuilder>(dispatcher);
289
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ExceptionInfo xcptInfo;
290
3/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
1 GATEXX_TRY_CATCHINFO(xcptInfo, {
291 bool_t const signaled = obj->waitFor(timeout_ms);
292 if (!signaled)
293 {
294 return results::Timeout;
295 }
296 });
297 1 return xcptInfo.result_code;
298 }
299
300 8 ITaskBuilder::VtblTask::VtblTask()
301 {
302 8 obj_vtbl_t* this_ptr = reinterpret_cast<obj_vtbl_t*>(this);
303 8 this_ptr->run = &ITaskBuilder::task_run;
304 8 this_ptr->cancel = ITaskBuilder::task_cancel;
305 8 this_ptr->get_status = ITaskBuilder::task_get_status;
306 8 this_ptr->completed = ITaskBuilder::task_completed;
307 8 this_ptr->get_result = ITaskBuilder::task_get_result;
308 8 this_ptr->wait = ITaskBuilder::task_wait;
309 8 this_ptr->timed_wait = ITaskBuilder::task_timed_wait;
310 8 }
311
312 ITaskBuilder::VtblTask ITaskBuilder::cpp_vtbl_task;
313
314 ✗ ITaskBuilder::ITaskBuilder(obj_vtbl_t const* vtbl_ptr, char const* name)
315 ✗ : object_builder_t(vtbl_ptr, name)
316 {
317 ✗ }
318 6 ITaskBuilder::ITaskBuilder()
319
1/2
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
6 : object_builder_t(&cpp_vtbl_task, GATE_INTERFACE_NAME_TASK)
320 {
321 6 }
322
323
324
325
326 6 ITaskBase::ITaskBase()
327 : statusvalue(static_cast<int32_t>(Task::Status_Prepared)),
328 completionsignal(false),
329 taskResult(results::Ok),
330
1/2
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
6 taskData(NULL)
331 {
332 6 }
333
334 12 ITaskBase::~ITaskBase() noexcept
335 {
336 12 }
337
338 3 void ITaskBase::runImpl()
339 {
340 3 }
341 1 void ITaskBase::cancelImpl()
342 {
343 1 }
344
345 18 static bool_t updateTaskStatus(AtomicInt& status, Task::StatusEnum fromStatus, Task::StatusEnum toStatus)
346 {
347 18 int32_t oldStatus = status.changeIf(static_cast<int32_t>(fromStatus), static_cast<int32_t>(toStatus));
348 18 return oldStatus == static_cast<int32_t>(fromStatus);
349 }
350
351 6 void ITaskBase::run()
352 {
353
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
6 if (!updateTaskStatus(this->statusvalue, Task::Status_Prepared, Task::Status_Running))
354 {
355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 GATEXX_RAISE_EXCEPTION(results::InvalidState, "Cannot start task", 0);
356 }
357
358 5 Task::StatusEnum nextStatus = Task::Status_Completed;
359 5 result_t nextTaskResult = results::Ok;
360
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 ExceptionInfo xcptInfo;
361
5/8
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
6 GATEXX_TRY_CATCHINFO(xcptInfo, {
362 this->runImpl();
363 });
364
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
5 if (xcptInfo.failed())
365 {
366 1 nextStatus = Task::Status_Failed;
367 1 nextTaskResult = xcptInfo.result_code;
368 }
369
370
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
5 if (updateTaskStatus(this->statusvalue, Task::Status_Running, nextStatus))
371 {
372 4 this->taskResult = nextTaskResult;
373
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 this->completionsignal.set();
374 }
375 5 }
376
377 1 void ITaskBase::setTaskData(void const* data)
378 {
379 1 this->taskData = data;
380 1 }
381
382
383 4 void ITaskBase::cancel()
384 {
385
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
4 if (updateTaskStatus(this->statusvalue, Task::Status_Prepared, Task::Status_Canceled))
386 {
387 1 this->completionsignal.set();
388 }
389
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 else if (updateTaskStatus(this->statusvalue, Task::Status_Running, Task::Status_Canceled))
390 {
391
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 GATEXX_TRY_IGNORE({
392 this->cancelImpl();
393 });
394 1 this->completionsignal.set();
395 }
396 else
397 {
398 2 GATEXX_RAISE_EXCEPTION(results::InvalidState, "Cannot cancel task", 0);
399 }
400 2 }
401 4 Task::StatusEnum ITaskBase::status()
402 {
403 4 return static_cast<Task::StatusEnum>(this->statusvalue.get());
404 }
405 5 bool_t ITaskBase::completed()
406 {
407 5 Task::StatusEnum state = static_cast<Task::StatusEnum>(this->statusvalue.get());
408
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 switch (state)
409 {
410 3 case Task::Status_Completed:
411 case Task::Status_Canceled:
412 case Task::Status_Failed:
413 {
414 3 return true;
415 }
416 2 default:
417 {
418 2 break;
419 }
420 }
421 2 return false;
422 }
423 5 void const* ITaskBase::getResult()
424 {
425 5 Task::StatusEnum state = static_cast<Task::StatusEnum>(this->statusvalue.get());
426
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
5 switch (state)
427 {
428 2 case Task::Status_Completed:
429 {
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(this->taskResult))
431 {
432 ✗ GATEXX_RAISE_EXCEPTION(this->taskResult, "Task execution failed", 0);
433 }
434 else
435 {
436 2 return this->taskData;
437 }
438 ✗ break;
439 }
440 1 case Task::Status_Canceled:
441 {
442 1 GATEXX_RAISE_EXCEPTION(results::Canceled, "Task canceled", 0);
443 ✗ break;
444 }
445 1 case Task::Status_Failed:
446 {
447 1 GATEXX_RAISE_EXCEPTION(this->taskResult, "Task execution failed", 0);
448 ✗ break;
449 }
450 1 default:
451 {
452 1 GATEXX_RAISE_EXCEPTION(results::InvalidState, "Task not completed", 0);
453 ✗ break;
454 }
455 }
456 ✗ return NULL;
457 }
458 2 void ITaskBase::wait()
459 {
460 2 this->completionsignal.wait();
461 2 }
462 1 bool ITaskBase::waitFor(uint32_t timeoutMs)
463 {
464 1 return this->completionsignal.waitFor(timeoutMs);
465 }
466
467 } // end of namespace gate
468