Skip to content

Commit 64ad1d6

Browse files
committed
Add critical section handling to timer module
1 parent adb31f4 commit 64ad1d6

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

libs/meson.build

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ inc = [include_directories('.')]
2626
src = []
2727

2828
subdir('common')
29-
if unit_test
30-
subdir('pal' / 'stubs')
31-
endif
29+
subdir('pal' / 'stubs')
3230
subdir('blk')
3331
subdir('crc')
3432
subdir('dlist')

libs/timer/timer.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ struct timer {
4949

5050
static struct timer m_timer;
5151

52+
static void am_timer_crit_enter(void) {}
53+
static void am_timer_crit_exit(void) {}
54+
5255
void am_timer_ctor(const struct am_timer_cfg *cfg) {
5356
AM_ASSERT(cfg);
5457
AM_ASSERT(cfg->post || cfg->publish);
@@ -58,6 +61,10 @@ void am_timer_ctor(const struct am_timer_cfg *cfg) {
5861
am_dlist_init(&m_timer.domains[i]);
5962
}
6063
m_timer.cfg = *cfg;
64+
if (!m_timer.cfg.crit_enter || !m_timer.cfg.crit_exit) {
65+
m_timer.cfg.crit_enter = am_timer_crit_enter;
66+
m_timer.cfg.crit_exit = am_timer_crit_exit;
67+
}
6168
}
6269

6370
void am_timer_event_ctor(struct am_event_timer *event, int id, int domain) {
@@ -93,14 +100,21 @@ void am_timer_arm(
93100
event->shot_in_ticks = ticks;
94101
event->interval_ticks = interval;
95102
event->event.pubsub_time = owner ? false : true;
103+
104+
me->cfg.crit_enter();
96105
am_dlist_push_back(&me->domains[event->event.tick_domain], &event->item);
106+
me->cfg.crit_exit();
97107
}
98108

99109
bool am_timer_disarm(struct am_event_timer *event) {
100110
AM_ASSERT(event);
101111
AM_ASSERT(AM_EVENT_HAS_USER_ID(event));
102112

113+
struct timer *me = &m_timer;
114+
115+
me->cfg.crit_enter();
103116
bool was_armed = am_dlist_pop(&event->item);
117+
me->cfg.crit_exit();
104118
event->shot_in_ticks = event->interval_ticks = 0;
105119

106120
return was_armed;
@@ -118,6 +132,8 @@ void am_timer_tick(int domain) {
118132
AM_ASSERT(domain < AM_COUNTOF(me->domains));
119133

120134
struct am_dlist_iterator it;
135+
136+
me->cfg.crit_enter();
121137
am_dlist_iterator_init(
122138
&me->domains[domain], &it, /*direction=*/AM_DLIST_FORWARD
123139
);
@@ -149,6 +165,7 @@ void am_timer_tick(int domain) {
149165
me->cfg.post(t->owner, &t->event);
150166
}
151167
}
168+
me->cfg.crit_exit();
152169
}
153170

154171
struct am_event_timer *am_timer_event_allocate(int id, int size, int domain) {

libs/timer/timer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ struct am_timer_cfg {
5959
* @return the updated event
6060
*/
6161
struct am_event_timer *(*update)(struct am_event_timer *event);
62+
/** Enter critical section */
63+
void (*crit_enter)(void);
64+
/** Exit critical section */
65+
void (*crit_exit)(void);
6266
};
6367

6468
/** Timer event. */

0 commit comments

Comments
 (0)