Skip to content

Commit 72b051f

Browse files
committed
Rework am_ao_run_all()
1 parent fc5728b commit 72b051f

File tree

6 files changed

+67
-54
lines changed

6 files changed

+67
-54
lines changed

apps/examples/dpp/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
*/
3030

3131
#include <stddef.h>
32+
#include <stdint.h>
3233

3334
#include "common/alignment.h"
3435
#include "common/compiler.h"
3536
#include "common/constants.h"
3637
#include "common/macros.h"
3738
#include "event/event.h"
39+
#include "timer/timer.h"
3840
#include "pal/pal.h"
3941
#include "ao/ao.h"
4042

@@ -145,7 +147,13 @@ int main(void) {
145147
);
146148
}
147149

148-
am_ao_run_all(/*loop=*/1);
150+
uint32_t now_ticks = am_pal_time_get_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
151+
for (;;) {
152+
while (am_ao_run_all()) {}
153+
am_pal_sleep_till_ticks(AM_PAL_TICK_DOMAIN_DEFAULT, now_ticks + 1);
154+
now_ticks += 1;
155+
am_timer_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
156+
}
149157

150158
return 0;
151159
}

apps/examples/ringbuf/main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
#include <stddef.h>
3232
#include <stdint.h>
3333

34+
#include "common/compiler.h"
3435
#include "common/macros.h"
36+
#include "timer/timer.h"
3537
#include "ringbuf/ringbuf.h"
3638
#include "ao/ao.h"
3739
#include "pal/pal.h"
@@ -45,7 +47,7 @@ int g_ringbuf_data_len = AM_COUNTOF(g_ringbuf_data);
4547
static const struct am_event *m_queue_ringbuf_reader[2];
4648
static const struct am_event *m_queue_ringbuf_writer[2];
4749

48-
static void test_ringbuf_threading(void) {
50+
AM_NORETURN static void test_ringbuf_threading(void) {
4951
uint8_t buf[32];
5052

5153
am_ringbuf_ctor(&g_ringbuf, buf, AM_COUNTOF(buf));
@@ -80,7 +82,14 @@ static void test_ringbuf_threading(void) {
8082
/*init_event=*/NULL
8183
);
8284

83-
am_ao_run_all(/*loop=*/1);
85+
uint32_t now_ticks = am_pal_time_get_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
86+
for (;;) {
87+
while (am_ao_run_all()) {
88+
}
89+
am_pal_sleep_till_ticks(AM_PAL_TICK_DOMAIN_DEFAULT, now_ticks + 1);
90+
now_ticks += 1;
91+
am_timer_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
92+
}
8493
}
8594

8695
int main(void) {

libs/ao/ao.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,10 @@ void am_ao_init_subscribe_list(struct am_ao_subscribe_list *sub, int nsub);
282282
/**
283283
* Run all active objects.
284284
*
285-
* @param loop run active objects in loop (true: do not return)
286-
*
287285
* @retval true processed at least one event
288286
* @retval false processed no events
289287
*/
290-
bool am_ao_run_all(bool loop);
288+
bool am_ao_run_all(void);
291289

292290
/**
293291
* Check if AO event queue is empty.

libs/ao/cooperative/port.c

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,43 @@
3838

3939
static struct am_bit_u64 am_ready_aos_ = {0};
4040

41-
bool am_ao_run_all(bool loop) {
41+
bool am_ao_run_all(void) {
4242
struct am_ao_state *me = &am_ao_state_;
43-
bool processed = false;
44-
int task_id = am_pal_task_own_id();
45-
do {
46-
me->crit_enter();
43+
me->crit_enter();
4744

48-
while (am_bit_u64_is_empty(&am_ready_aos_)) {
49-
me->crit_exit();
50-
if (me->on_idle) {
51-
me->on_idle();
52-
} else {
53-
am_pal_task_wait(task_id);
54-
}
55-
me->crit_enter();
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);
5652
}
57-
int msb = am_bit_u64_msb(&am_ready_aos_);
53+
me->crit_enter();
54+
}
55+
int msb = am_bit_u64_msb(&am_ready_aos_);
5856

59-
me->crit_exit();
57+
me->crit_exit();
6058

61-
struct am_ao *ao = me->ao[msb];
62-
AM_ASSERT(ao);
63-
64-
const struct am_event *e = am_event_pop_front(&ao->event_queue);
65-
if (!e) {
66-
me->crit_enter();
67-
if (am_queue_is_empty(&ao->event_queue)) {
68-
am_bit_u64_clear(&am_ready_aos_, ao->prio);
69-
}
70-
me->crit_exit();
71-
continue;
59+
struct am_ao *ao = me->ao[msb];
60+
AM_ASSERT(ao);
61+
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);
7267
}
73-
me->debug(ao, e);
74-
AM_ATOMIC_STORE_N(&ao->last_event, e->id);
75-
am_hsm_dispatch(&ao->hsm, e);
76-
AM_ATOMIC_STORE_N(&ao->last_event, AM_EVT_INVALID);
77-
am_event_free(&e);
78-
processed = true;
79-
} while (loop && AM_UNLIKELY(!AM_ATOMIC_LOAD_N(&me->ao_state_dtor_called)));
80-
81-
return processed;
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);
76+
77+
return true;
8278
}
8379

8480
void am_ao_start(

libs/ao/preemptive/port.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424

2525
#include <stdbool.h>
2626
#include <stddef.h>
27-
#include <stdint.h>
2827

2928
#include "blk/blk.h"
3029
#include "common/compiler.h"
3130
#include "common/macros.h"
3231
#include "hsm/hsm.h"
3332
#include "queue/queue.h"
3433
#include "event/event.h"
35-
#include "timer/timer.h"
3634
#include "pal/pal.h"
3735
#include "ao/ao.h"
3836
#include "state.h"
@@ -66,19 +64,15 @@ static void am_ao_task(void *param) {
6664
}
6765
}
6866

69-
bool am_ao_run_all(bool loop) {
67+
bool am_ao_run_all(void) {
68+
static bool started = false;
69+
if (started) {
70+
return false;
71+
}
7072
const struct am_ao_state *me = &am_ao_state_;
7173
/* start all AOs */
7274
am_pal_mutex_unlock(me->startup_mutex);
73-
uint32_t now_ticks =
74-
am_pal_time_get_tick(/*domain=*/AM_PAL_TICK_DOMAIN_DEFAULT);
75-
while (loop && AM_UNLIKELY(!AM_ATOMIC_LOAD_N(&me->ao_state_dtor_called))) {
76-
am_pal_sleep_till_ticks(
77-
/*domain=*/AM_PAL_TICK_DOMAIN_DEFAULT, now_ticks + 1
78-
);
79-
now_ticks += 1;
80-
am_timer_tick(/*domain=*/AM_PAL_TICK_DOMAIN_DEFAULT);
81-
}
75+
started = true;
8276
return false;
8377
}
8478

libs/ao/tests/minimal.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232

3333
#include <stddef.h>
3434
#include <stdlib.h>
35+
#include <stdint.h>
3536

3637
#include "common/macros.h"
3738
#include "event/event.h"
39+
#include "timer/timer.h"
3840
#include "hsm/hsm.h"
3941
#include "pal/pal.h"
4042
#include "ao/ao.h"
@@ -129,7 +131,13 @@ int main(void) {
129131
/*init_event=*/NULL
130132
);
131133

132-
am_ao_run_all(/*loop=*/1);
134+
uint32_t now_ticks = am_pal_time_get_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
135+
for (;;) {
136+
while (am_ao_run_all()) {}
137+
am_pal_sleep_till_ticks(AM_PAL_TICK_DOMAIN_DEFAULT, now_ticks + 1);
138+
now_ticks += 1;
139+
am_timer_tick(AM_PAL_TICK_DOMAIN_DEFAULT);
140+
}
133141

134142
return 0;
135143
}

0 commit comments

Comments
 (0)