Skip to content

Commit 73c2c90

Browse files
committed
Prune on_idle AO callback
1 parent 72b051f commit 73c2c90

File tree

7 files changed

+60
-52
lines changed

7 files changed

+60
-52
lines changed

apps/examples/dpp/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ int main(void) {
149149

150150
uint32_t now_ticks = am_pal_time_get_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
151151
for (;;) {
152-
while (am_ao_run_all()) {}
152+
while (am_ao_run_all()) {
153+
}
153154
am_pal_sleep_till_ticks(AM_PAL_TICK_DOMAIN_DEFAULT, now_ticks + 1);
154155
now_ticks += 1;
155156
am_timer_tick(AM_PAL_TICK_DOMAIN_DEFAULT);

apps/examples/meson.build

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ e = executable(
5959
libtimer_dep,
6060
libbit_dep])
6161

62-
ringbuf_example = executable(
63-
'ringbuf-example',
62+
ringbuf_preemptive_example = executable(
63+
'ringbuf_preemptive_example',
6464
[
6565
'ringbuf' / 'main.c',
6666
'ringbuf' / 'reader.c',
@@ -73,6 +73,22 @@ ringbuf_example = executable(
7373
libassert_dep
7474
])
7575

76+
ringbuf_cooperative_example = executable(
77+
'ringbuf_cooperative_example',
78+
[
79+
'ringbuf' / 'main.c',
80+
'ringbuf' / 'reader.c',
81+
'ringbuf' / 'writer.c'
82+
],
83+
dependencies: [
84+
libao_cooperative_dep,
85+
libpal_dep,
86+
libringbuf_dep,
87+
libassert_dep,
88+
libbit_dep
89+
])
90+
7691
if unit_test
77-
test('threaded', ringbuf_example, suite: 'ringbuf')
92+
test('threaded_preemptive', ringbuf_preemptive_example, suite: 'ringbuf')
93+
test('threaded_cooperative', ringbuf_cooperative_example, suite: 'ringbuf')
7894
endif

libs/ao/ao.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ void am_ao_stop(struct am_ao *ao) {
245245
me->ao[ao->prio] = NULL;
246246
}
247247

248-
static void am_ao_on_idle_stub(void) {}
249-
250248
static void am_ao_debug_stub(const struct am_ao *ao, const struct am_event *e) {
251249
(void)ao;
252250
(void)e;
@@ -263,10 +261,6 @@ void am_ao_state_ctor(const struct am_ao_state_cfg *cfg) {
263261
me->startup_mutex = am_pal_mutex_create();
264262
am_pal_mutex_lock(me->startup_mutex);
265263

266-
me->on_idle = cfg->on_idle;
267-
if (!me->on_idle) {
268-
me->on_idle = am_ao_on_idle_stub;
269-
}
270264
me->debug = cfg->debug;
271265
if (!me->debug) {
272266
me->debug = am_ao_debug_stub;

libs/ao/ao.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ struct am_ao {
6262

6363
/** AO state configuration. */
6464
struct am_ao_state_cfg {
65-
/** User callback on idle state, when no AO is running. */
66-
void (*on_idle)(void);
6765
/** Debug callback. */
6866
void (*debug)(const struct am_ao *ao, const struct am_event *e);
6967

@@ -220,13 +218,6 @@ void am_ao_start(
220218
void am_ao_stop(struct am_ao *ao);
221219

222220
struct am_ao_cfg {
223-
/**
224-
* User callback for idle condition actions (e.g. sleep mode)
225-
* The interrupts are disabled when the callback is called.
226-
* The function must unlock interrupts internally, ideally
227-
* atomically with a transition to a sleep mode.
228-
*/
229-
void (*on_idle)(void);
230221
/** Debug callback. */
231222
void (*debug)(const struct am_ao *ao, const struct am_event *e);
232223
/** Enter critical section. */

libs/ao/cooperative/port.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,33 @@ bool am_ao_run_all(void) {
4242
struct am_ao_state *me = &am_ao_state_;
4343
me->crit_enter();
4444

45-
while (am_bit_u64_is_empty(&am_ready_aos_)) {
46-
me->crit_exit();
47-
if (me->on_idle) {
48-
me->on_idle();
49-
} else {
50-
int task_id = am_pal_task_own_id();
51-
am_pal_task_wait(task_id);
52-
}
53-
me->crit_enter();
54-
}
55-
int msb = am_bit_u64_msb(&am_ready_aos_);
56-
57-
me->crit_exit();
45+
while (!am_bit_u64_is_empty(&am_ready_aos_)) {
46+
int msb = am_bit_u64_msb(&am_ready_aos_);
5847

59-
struct am_ao *ao = me->ao[msb];
60-
AM_ASSERT(ao);
48+
me->crit_exit();
6149

62-
const struct am_event *e = am_event_pop_front(&ao->event_queue);
63-
if (!e) {
64-
me->crit_enter();
65-
if (am_queue_is_empty(&ao->event_queue)) {
66-
am_bit_u64_clear(&am_ready_aos_, ao->prio);
50+
struct am_ao *ao = me->ao[msb];
51+
AM_ASSERT(ao);
52+
AM_ASSERT(ao->prio == msb);
53+
54+
const struct am_event *e = am_event_pop_front(&ao->event_queue);
55+
if (!e) {
56+
me->crit_enter();
57+
if (am_queue_is_empty(&ao->event_queue)) {
58+
am_bit_u64_clear(&am_ready_aos_, ao->prio);
59+
}
60+
me->crit_exit();
61+
continue;
6762
}
68-
me->crit_exit();
69-
return false;
70-
}
71-
me->debug(ao, e);
72-
AM_ATOMIC_STORE_N(&ao->last_event, e->id);
73-
am_hsm_dispatch(&ao->hsm, e);
74-
AM_ATOMIC_STORE_N(&ao->last_event, AM_EVT_INVALID);
75-
am_event_free(&e);
63+
me->debug(ao, e);
64+
AM_ATOMIC_STORE_N(&ao->last_event, e->id);
65+
am_hsm_dispatch(&ao->hsm, e);
66+
AM_ATOMIC_STORE_N(&ao->last_event, AM_EVT_INVALID);
67+
am_event_free(&e);
7668

77-
return true;
69+
return true;
70+
}
71+
return false;
7872
}
7973

8074
void am_ao_start(

libs/ao/meson.build

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,22 @@ libao_preemptive_dep = declare_dependency(
6060

6161
if unit_test
6262
e = executable(
63-
'minimal',
63+
'minimal_preemptive',
6464
[
6565
'tests' / 'minimal.c'
6666
],
6767
dependencies: [libao_preemptive_dep, libassert_dep, libpal_dep],
6868
include_directories: [include_directories('tests')])
69-
test('minimal', e, suite: 'ao')
69+
test('minimal_preemptive', e, suite: 'ao')
70+
71+
e = executable(
72+
'minimal_cooperative',
73+
[
74+
'tests' / 'minimal.c'
75+
],
76+
dependencies: [
77+
libao_cooperative_dep, libassert_dep, libpal_dep, libbit_dep
78+
],
79+
include_directories: [include_directories('tests')])
80+
test('minimal_cooperative', e, suite: 'ao')
7081
endif

libs/ao/tests/minimal.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ int main(void) {
127127
/*nqueue=*/AM_COUNTOF(m_queue_loopback_test),
128128
/*stack=*/NULL,
129129
/*stack_size=*/0,
130-
/*name=*/"loopback",
130+
/*name=*/"loopback_test",
131131
/*init_event=*/NULL
132132
);
133133

134134
uint32_t now_ticks = am_pal_time_get_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
135135
for (;;) {
136-
while (am_ao_run_all()) {}
136+
while (am_ao_run_all()) {
137+
}
137138
am_pal_sleep_till_ticks(AM_PAL_TICK_DOMAIN_DEFAULT, now_ticks + 1);
138139
now_ticks += 1;
139140
am_timer_tick(AM_PAL_TICK_DOMAIN_DEFAULT);

0 commit comments

Comments
 (0)