GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tech/microservices/timer_service.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 111 123 90.2%
Functions: 11 11 100.0%
Branches: 28 42 66.7%

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 #include "gate/tech/microservices/timer_service.h"
29 #include "gate/synchronization.h"
30 #include "gate/threading.h"
31 #include "gate/results.h"
32 #include "gate/times.h"
33
34 typedef struct timerservice_config_interval_class
35 {
36 gate_struct_t struct_base;
37
38 gate_int32_t seconds;
39 gate_int32_t alignment;
40 gate_string_t msg_id;
41 gate_string_t msg_content;
42
43 } timerservice_config_interval_t;
44
45
46 #define TIMERSERVICE_ROOT_NAME "timer_service"
47
48 #define TIMERSERVICE_CONFIG_INTERVAL_NAME GATE_STRUCT_ROOT_NAME GATE_STRUCT_SEPARATOR TIMERSERVICE_ROOT_NAME GATE_STRUCT_SEPARATOR "config_interval"
49
50 static gate_struct_item_t const timerservice_config_interval_members[] =
51 {
52 GATE_STRUCT_ITEM(timerservice_config_interval_t, GATE_TYPE_I32, seconds),
53 GATE_STRUCT_ITEM(timerservice_config_interval_t, GATE_TYPE_I32, alignment),
54 GATE_STRUCT_ITEM(timerservice_config_interval_t, GATE_TYPE_STRING, msg_id),
55 GATE_STRUCT_ITEM(timerservice_config_interval_t, GATE_TYPE_STRING, msg_content)
56 };
57
58 static gate_struct_descriptor_t const timerservice_config_interval_descriptor =
59 {
60 TIMERSERVICE_CONFIG_INTERVAL_NAME,
61 timerservice_config_interval_members,
62 sizeof(timerservice_config_interval_members) / sizeof(timerservice_config_interval_members[0]),
63 sizeof(timerservice_config_interval_t)
64 };
65
66 2 static gate_result_t GATE_CALL timerservice_config_interval_ctor(void* target, void const* src)
67 {
68 gate_result_t result;
69
70 2 gate_mem_clear(target, sizeof(timerservice_config_interval_t));
71
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (src)
72 {
73 1 result = gate_struct_copy((gate_struct_t*)target, (gate_struct_t const*)src);
74 }
75 else
76 {
77 1 result = gate_struct_init((gate_struct_t*)target, &timerservice_config_interval_descriptor);
78 }
79 2 return result;
80 }
81
82
83 typedef struct timerservice_config_class
84 {
85 gate_struct_t struct_base;
86
87 gate_arraylist_t intervals;
88 } timerservice_config_t;
89
90 #define TIMERSERVICE_CONFIG_NAME GATE_STRUCT_ROOT_NAME GATE_STRUCT_SEPARATOR TIMERSERVICE_ROOT_NAME GATE_STRUCT_SEPARATOR "config"
91
92 static gate_struct_item_t const timerservice_config_members[] =
93 {
94 GATE_STRUCT_ITEM(timerservice_config_t, GATE_TYPE_ARRAYLIST_STRUCT, intervals)
95 };
96
97 static gate_struct_descriptor_t const timerservice_config_descriptor =
98 {
99 TIMERSERVICE_CONFIG_NAME,
100 timerservice_config_members,
101 sizeof(timerservice_config_members) / sizeof(timerservice_config_members[0]),
102 sizeof(timerservice_config_t)
103 };
104
105 1 static gate_result_t timerservice_config_ctor(void* target, void const* src)
106 {
107 gate_result_t result;
108 1 timerservice_config_t const* ptr_src = (timerservice_config_t const*)src;
109 1 timerservice_config_t* ptr_target = (timerservice_config_t*)target;
110
111 1 gate_mem_clear(target, sizeof(timerservice_config_t));
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (src)
113 {
114 ✗ result = gate_struct_copy(&ptr_target->struct_base, &ptr_src->struct_base);
115 }
116 else
117 {
118 1 result = gate_struct_init(&ptr_target->struct_base, &timerservice_config_descriptor);
119
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
120 {
121 1 ptr_target->intervals = gate_arraylist_create(sizeof(timerservice_config_interval_t), NULL, 0,
122 &timerservice_config_interval_ctor, &gate_struct_destructor);
123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (gate_arraylist_is_null(ptr_target->intervals))
124 {
125 ✗ gate_struct_destructor(&ptr_target->struct_base);
126 ✗ result = GATE_RESULT_OUTOFMEMORY;
127 }
128 }
129 }
130 1 return result;
131 }
132
133
134 typedef struct timer_service_data_class
135 {
136 gate_thread_t timer_thread;
137 gate_thread_id_t timer_thread_id;
138 gate_syncevent_t timer_event;
139 gate_atomic_flag_t running;
140 timerservice_config_t config;
141 } gate_timer_service_data_t;
142
143
144 typedef struct timer_service_task
145 {
146 gate_timestamp_t trigger_time;
147 gate_string_t msg_id;
148 gate_string_t msg_content;
149 } timer_service_task_t;
150
151 703 static void timer_service_task_create(timer_service_task_t* task, gate_timestamp_t timestamp, gate_string_t const* id, gate_string_t const* content)
152 {
153 703 gate_mem_clear(task, sizeof(timer_service_task_t));
154 703 task->trigger_time = timestamp;
155 703 gate_string_clone(&task->msg_id, id);
156 703 gate_string_clone(&task->msg_content, content);
157 703 }
158 703 static void timer_service_task_destroy(timer_service_task_t* task)
159 {
160 703 gate_string_release(&task->msg_id);
161 703 gate_string_release(&task->msg_content);
162 703 }
163
164 703 static void timer_service_tasks_destroy(timer_service_task_t* task, gate_size_t count)
165 {
166
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 703 times.
1406 while (count-- != 0)
167 {
168 703 timer_service_task_destroy(task++);
169 }
170 703 }
171
172 703 static gate_size_t get_next_tasks(timerservice_config_t const* ptr_cfg, gate_timestamp_t now,
173 timer_service_task_t* tasks, gate_size_t tasks_max)
174 {
175 703 gate_size_t tasks_used = 0;
176 703 gate_size_t len = gate_arraylist_length(ptr_cfg->intervals);
177 gate_size_t ndx;
178 timerservice_config_interval_t const* ptr_interval;
179 gate_timestamp_t us_interval;
180 gate_timestamp_t us_alignment;
181 timer_service_task_t* ptr_task;
182
183
3/4
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 703 times.
✓ Branch 2 taken 703 times.
✗ Branch 3 not taken.
1406 for (ndx = 0; (ndx != len) && (tasks_used < tasks_max); ++ndx)
184 {
185 703 ptr_interval = gate_arraylist_get(ptr_cfg->intervals, ndx);
186
1/2
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
703 if (ptr_interval)
187 {
188 703 us_interval = (gate_timestamp_t)ptr_interval->seconds * GATE_TYPE_CONST_LONGLONG(1000000);
189 703 us_alignment = (gate_timestamp_t)ptr_interval->alignment * GATE_TYPE_CONST_LONGLONG(1000000);
190
1/2
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
703 if (us_alignment <= 0)
191 {
192 703 us_alignment = 1;
193 }
194
195 703 ptr_task = &tasks[tasks_used];
196 703 timer_service_task_create(ptr_task, now + us_interval, &ptr_interval->msg_id, &ptr_interval->msg_content);
197 703 ptr_task->trigger_time -= ptr_task->trigger_time % us_alignment;
198 703 ++tasks_used;
199 }
200 }
201 703 return tasks_used;
202 }
203
204 1 static gate_result_t timer_worker_thread(void* ptr)
205 {
206 1 gate_result_t ret = GATE_RESULT_OK;
207 1 gate_microservice_base_t* self = (gate_microservice_base_t*)ptr;
208 1 gate_timer_service_data_t* data = (gate_timer_service_data_t*)self->data_ptr;
209 static gate_string_t const msg_error_wait = GATE_STRING_INIT_STATIC("Failed to wait for synchronization event");
210 static gate_string_t const msg_exit = GATE_STRING_INIT_STATIC("Exiting timer worker thread");
211 static gate_string_t const msg_shutdown = GATE_STRING_INIT_STATIC("Shutdown signal received");
212 /*static gate_string_t const msg_trigger = GATE_STRING_INIT_STATIC("Time trigger");*/
213
214 1 gate_time_t now = GATE_INIT_EMPTY;
215 timer_service_task_t next_tasks[64];
216 1 gate_size_t next_tasks_count = 0;
217 gate_size_t ndx;
218 1 gate_bool_t trigger_executed = false;
219
220 1 gate_time_now(&now);
221 1 next_tasks_count = get_next_tasks(&data->config, now.timestamp, next_tasks, sizeof(next_tasks) / sizeof(next_tasks[0]));
222
223
2/2
✓ Branch 1 taken 242552 times.
✓ Branch 2 taken 1 times.
242553 while (gate_atomic_flag_set(&data->running) == true)
224 {
225 242552 ret = gate_syncevent_timed_wait(&data->timer_event, 500);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242552 times.
242552 if (ret == GATE_RESULT_OK)
227 {
228 /* shutdown event detected */
229 ✗ gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_DEBUG, ret, 0, &self->address, &msg_shutdown);
230 ✗ break;
231 }
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242552 times.
242552 else if (ret != GATE_RESULT_TIMEOUT)
233 {
234 /* internal error detected */
235 ✗ gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_ERROR, ret, 0, &self->address, &msg_error_wait);
236 ✗ break;
237 }
238
239 242552 gate_time_now(&now);
240 242552 trigger_executed = false;
241
2/2
✓ Branch 0 taken 242552 times.
✓ Branch 1 taken 242552 times.
485104 for (ndx = 0; ndx != next_tasks_count; ++ndx)
242 {
243
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 241850 times.
242552 if (now.timestamp >= next_tasks[ndx].trigger_time)
244 {
245 702 gate_microhost_publish_message(self->host, &self->address, NULL, &next_tasks[ndx].msg_id, &next_tasks[ndx].msg_content);
246 702 trigger_executed = true;
247 }
248 }
249
250
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 241850 times.
242552 if (trigger_executed)
251 {
252 702 timer_service_tasks_destroy(next_tasks, next_tasks_count);
253 702 next_tasks_count = get_next_tasks(&data->config, now.timestamp + GATE_TYPE_CONST_LONGLONG(1000),
254 next_tasks, sizeof(next_tasks) / sizeof(next_tasks[0]));
255 }
256
257 }
258
259 1 timer_service_tasks_destroy(next_tasks, next_tasks_count);
260
261 1 gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_DEBUG, ret, 0, &self->address, &msg_exit);
262 1 return GATE_RESULT_OK;
263 }
264
265
266 1 static void timer_on_release(gate_microservice_base_t* self)
267 {
268 1 gate_timer_service_data_t* data = (gate_timer_service_data_t*)self->data_ptr;
269 1 gate_struct_release(&data->config.struct_base);
270 1 }
271 1 static gate_result_t timer_on_start(gate_microservice_base_t* self)
272 {
273 gate_result_t ret;
274 1 gate_timer_service_data_t* data = (gate_timer_service_data_t*)self->data_ptr;
275
276 do
277 {
278 /* export parameters from property storage to native config struct */
279 1 gate_property_export(&self->parameters, GATE_TYPE_STRUCT, &data->config.struct_base);
280
281 1 ret = gate_syncevent_create(&data->timer_event, true);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
283
284 1 gate_atomic_flag_set(&data->running);
285 1 ret = gate_thread_start_code(&timer_worker_thread, self, &data->timer_thread, &data->timer_thread_id);
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
287 {
288 ✗ gate_syncevent_destroy(&data->timer_thread);
289 ✗ break;
290 }
291 } while (0);
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
294 {
295 ✗ gate_atomic_flag_clear(&data->running);
296 }
297
298 1 return ret;
299
300 }
301 1 static gate_result_t timer_on_stop(gate_microservice_base_t* self)
302 {
303 gate_result_t ret;
304 1 gate_result_t thread_result = GATE_RESULT_OK;
305 1 gate_timer_service_data_t* data = (gate_timer_service_data_t*)self->data_ptr;
306 static gate_string_t error_msg_join = GATE_STRING_INIT_STATIC("Failed to join timer thread");
307 static gate_string_t error_msg_sync = GATE_STRING_INIT_STATIC("Failed to destroy synchronization object");
308
309 1 gate_atomic_flag_clear(&data->running);
310
311 1 ret = gate_thread_join(&data->timer_thread, &thread_result);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
313 {
314 ✗ gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_WARNING, ret, 1,
315 &self->address, &error_msg_join);
316 }
317 1 ret = gate_syncevent_destroy(&data->timer_event);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
319 {
320 ✗ gate_microhost_log(self->host, GATE_MICROSERVICE_LOGTYPE_WARNING, ret, 1,
321 &self->address, &error_msg_sync);
322 }
323
324 1 gate_struct_release(&data->config.struct_base);
325
326 1 gate_mem_clear(data, sizeof(gate_timer_service_data_t));
327 1 return GATE_RESULT_OK;
328 }
329
330 /*
331 static gate_result_t timer_on_message_received(gate_microservice_base_t* self,
332 gate_string_t const* source, gate_string_t const* destination,
333 gate_string_t const* msg_id, gate_string_t const* message)
334 {
335 GATE_UNUSED_ARG(self);
336 GATE_UNUSED_ARG(source);
337 GATE_UNUSED_ARG(destination);
338 GATE_UNUSED_ARG(msg_id);
339 GATE_UNUSED_ARG(message);
340
341 return GATE_RESULT_NOTSUPPORTED;
342 }
343 static gate_result_t timer_on_object_received(gate_microservice_base_t* self,
344 gate_string_t const* source, gate_string_t const* destination,
345 gate_string_t const* obj_id, gate_object_t* obj)
346 {
347 GATE_UNUSED_ARG(self);
348 GATE_UNUSED_ARG(source);
349 GATE_UNUSED_ARG(destination);
350 GATE_UNUSED_ARG(obj_id);
351 GATE_UNUSED_ARG(obj);
352 return GATE_RESULT_NOTSUPPORTED;
353 }
354 static gate_result_t timer_on_invoke(gate_microservice_base_t* self, gate_string_t const* method,
355 gate_struct_t const* request, gate_struct_t* response)
356 {
357 GATE_UNUSED_ARG(self);
358 GATE_UNUSED_ARG(method);
359 GATE_UNUSED_ARG(request);
360 GATE_UNUSED_ARG(response);
361 return GATE_RESULT_NOTSUPPORTED;
362 }
363 */
364
365 1 gate_microservice_t* gate_microservice_timer_create(void)
366 {
367 1 gate_microservice_base_t* timer_service = gate_microservice_base_create(sizeof(gate_timer_service_data_t), NULL);
368 1 gate_timer_service_data_t* timer_data = NULL;
369
370
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (timer_service)
371 {
372 1 timer_service->on_start = &timer_on_start;
373 1 timer_service->on_stop = &timer_on_stop;
374 1 timer_service->on_release = &timer_on_release;
375 1 timer_service->on_message_received = NULL;
376 1 timer_service->on_object_received = NULL;
377 1 timer_service->on_invoke = NULL;
378
379 1 timer_data = (gate_timer_service_data_t*)timer_service->data_ptr;
380 1 timerservice_config_ctor(&timer_data->config, NULL);
381
382 1 gate_property_create_object(&timer_service->parameters);
383 1 gate_property_import(&timer_service->parameters, GATE_TYPE_STRUCT, &timer_data->config.struct_base);
384 1 timer_service->ptr_parameters = &timer_data->config.struct_base;
385 }
386 1 return (gate_microservice_t*)timer_service;
387 }
388
389