Skip to content

Commit 3527b61

Browse files
committed
Refactor timer APIs to use helper macros
Timer APIs no longer require memory operations now that timers are statically allocated with embedded list nodes. This change removes redundant allocation paths and uses the timer_from_node() helper to align timer access with the embedded list node design.
1 parent d4bf754 commit 3527b61

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

kernel/timer.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ int32_t mo_timer_create(void *(*callback)(void *arg),
275275
if (unlikely(timer_subsystem_init() != ERR_OK))
276276
return ERR_FAIL;
277277

278-
timer_t *t = malloc(sizeof(timer_t));
279-
if (unlikely(!t))
278+
/* Try to get a static timer from the pool */
279+
timer_t *t = get_timer();
280+
if (!t)
280281
return ERR_FAIL;
281282

282283
NOSCHED_ENTER();
@@ -290,13 +291,11 @@ int32_t mo_timer_create(void *(*callback)(void *arg),
290291
t->last_expected_fire_tick = 0;
291292
t->mode = TIMER_DISABLED;
292293
t->_reserved = 0;
294+
t->t_node.next = NULL;
295+
t->t_running_node.next = NULL;
293296

294297
/* Insert into sorted all_timers_list */
295-
if (unlikely(timer_insert_sorted_by_id(t) != ERR_OK)) {
296-
NOSCHED_LEAVE();
297-
free(t);
298-
return ERR_FAIL;
299-
}
298+
timer_insert_sorted_timer_list(t);
300299

301300
/* Add to cache */
302301
cache_timer(t->id, t);
@@ -318,11 +317,11 @@ int32_t mo_timer_destroy(uint16_t id)
318317
return ERR_FAIL;
319318
}
320319

321-
timer_t *t = (timer_t *) node->data;
320+
timer_t *t = timer_from_node(node);
322321

323322
/* Remove from active list if running */
324323
if (t->mode != TIMER_DISABLED)
325-
timer_remove_item_by_data(kcb->timer_list, t);
324+
timer_remove_from_running_list(kcb->timer_list, t);
326325

327326
/* Remove from cache */
328327
for (int i = 0; i < 4; i++) {
@@ -342,9 +341,7 @@ int32_t mo_timer_destroy(uint16_t id)
342341
all_timers_list->length--;
343342
}
344343

345-
free(t);
346-
return_timer_node(node);
347-
344+
return_timer(t);
348345
NOSCHED_LEAVE();
349346
return ERR_OK;
350347
}
@@ -366,18 +363,14 @@ int32_t mo_timer_start(uint16_t id, uint8_t mode)
366363

367364
/* Remove from active list if already running */
368365
if (t->mode != TIMER_DISABLED)
369-
timer_remove_item_by_data(kcb->timer_list, t);
366+
timer_remove_from_running_list(kcb->timer_list, t);
370367

371368
/* Configure and start timer */
372369
t->mode = mode;
373370
t->last_expected_fire_tick = mo_ticks() + MS_TO_TICKS(t->period_ms);
374371
t->deadline_ticks = t->last_expected_fire_tick;
375372

376-
if (unlikely(timer_sorted_insert(t) != ERR_OK)) {
377-
t->mode = TIMER_DISABLED;
378-
NOSCHED_LEAVE();
379-
return ERR_FAIL;
380-
}
373+
timer_sorted_insert_running_list(t);
381374

382375
NOSCHED_LEAVE();
383376
return ERR_OK;
@@ -396,7 +389,7 @@ int32_t mo_timer_cancel(uint16_t id)
396389
return ERR_FAIL;
397390
}
398391

399-
timer_remove_item_by_data(kcb->timer_list, t);
392+
timer_remove_from_running_list(kcb->timer_list, t);
400393
t->mode = TIMER_DISABLED;
401394

402395
NOSCHED_LEAVE();

0 commit comments

Comments
 (0)