Skip to content

Commit 41e774d

Browse files
committed
Add am_ao_get_own_prio()
1 parent 105af93 commit 41e774d

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

libs/ao/ao.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ void am_ao_state_ctor(const struct am_ao_state_cfg *cfg) {
286286
me->crit_exit = cfg->crit_exit;
287287
me->on_idle = cfg->on_idle;
288288

289+
me->running_ao_prio = AM_AO_PRIO_INVALID;
290+
289291
struct am_event_state_cfg cfg_event = {
290292
.crit_enter = cfg->crit_enter,
291293
.crit_exit = cfg->crit_exit,

libs/ao/ao.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ struct am_ao_state_cfg {
100100
#define AM_AO_NUM_MAX 64
101101
#endif
102102

103+
/** Invalid AO priority */
104+
#define AM_AO_PRIO_INVALID -1
103105
/** The minimum AO priority level. */
104106
#define AM_AO_PRIO_MIN 0
105107
/** The maximum AO priority level. */
@@ -496,6 +498,13 @@ void am_ao_wait_start_all(void);
496498
*/
497499
int am_ao_get_cnt(void);
498500

501+
/**
502+
* Get active object own priority level.
503+
*
504+
* @return the priority level
505+
*/
506+
int am_ao_get_own_prio(void);
507+
499508
#ifdef __cplusplus
500509
}
501510
#endif

libs/ao/cooperative/port.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ bool am_ao_run_all(void) {
4747
for (int i = 0; i < AM_COUNTOF(me->aos); ++i) {
4848
struct am_ao *ao = me->aos[i];
4949
if (ao && ao->hsm_init_pend) {
50+
me->running_ao_prio = ao->prio;
5051
am_hsm_init(&ao->hsm, ao->init_event);
52+
me->running_ao_prio = AM_AO_PRIO_INVALID;
5153
ao->hsm_init_pend = false;
5254
}
5355
}
@@ -88,7 +90,11 @@ bool am_ao_run_all(void) {
8890
}
8991
me->debug(ao, e);
9092
AM_ATOMIC_STORE_N(&ao->last_event, e->id);
93+
me->running_ao_prio = ao->prio;
94+
9195
am_hsm_dispatch(&ao->hsm, e);
96+
97+
me->running_ao_prio = AM_AO_PRIO_INVALID;
9298
AM_ATOMIC_STORE_N(&ao->last_event, AM_EVT_INVALID);
9399
am_event_free(&e);
94100

@@ -184,3 +190,9 @@ void am_ao_notify(const struct am_ao *ao) {
184190
}
185191

186192
void am_ao_wait_start_all(void) {}
193+
194+
int am_ao_get_own_prio(void) {
195+
const struct am_ao_state *me = &am_ao_state_;
196+
AM_ASSERT(AM_AO_PRIO_INVALID != me->running_ao_prio);
197+
return me->running_ao_prio;
198+
}

libs/ao/preemptive/port.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,16 @@ void am_ao_wait_start_all(void) {
170170
am_pal_mutex_lock(me->startup_mutex);
171171
am_pal_mutex_unlock(me->startup_mutex);
172172
}
173+
174+
int am_ao_get_own_prio(void) {
175+
int task_id = am_pal_task_get_own_id();
176+
AM_ASSERT(AM_PAL_TASK_ID_MAIN != task_id);
177+
struct am_ao_state *me = &am_ao_state_;
178+
for (int i = 0; i < AM_COUNTOF(me->aos); ++i) {
179+
struct am_ao *ao = me->aos[i];
180+
if (ao && ao->task_id == task_id) {
181+
return ao->prio;
182+
}
183+
}
184+
AM_ASSERT(0);
185+
}

libs/ao/state.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ struct am_ao_state {
6565
/** Exit critical section. */
6666
void (*crit_exit)(void);
6767

68+
/**
69+
* The priority of the currently running AO.
70+
* Only valid for cooperative AO builds.
71+
*/
72+
int running_ao_prio;
73+
6874
/**
6975
* there is a pending am_ao_task() call to be done from
7076
* am_ao_run_all() function

libs/ao/tests/minimal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static struct loopback_test {
5959
static int loopback_proc(struct loopback *me, const struct am_event *event) {
6060
switch (event->id) {
6161
case AM_EVT_MIN:
62+
AM_ASSERT(am_ao_get_own_prio() == (AM_AO_PRIO_MAX - 1));
6263
am_ao_post_fifo(&m_loopback_test.ao, event);
6364
return AM_HSM_HANDLED();
6465
default:
@@ -69,6 +70,7 @@ static int loopback_proc(struct loopback *me, const struct am_event *event) {
6970

7071
static int loopback_init(struct loopback *me, const struct am_event *event) {
7172
(void)event;
73+
AM_ASSERT(am_ao_get_own_prio() == (AM_AO_PRIO_MAX - 1));
7274
return AM_HSM_TRAN(loopback_proc);
7375
}
7476

@@ -77,6 +79,7 @@ static int loopback_test_proc(
7779
) {
7880
switch (event->id) {
7981
case AM_EVT_MIN:
82+
AM_ASSERT(am_ao_get_own_prio() == AM_AO_PRIO_MAX);
8083
++me->cnt;
8184
if (100 == me->cnt) {
8285
exit(0);
@@ -93,6 +96,7 @@ static int loopback_test_init(
9396
struct loopback_test *me, const struct am_event *event
9497
) {
9598
(void)event;
99+
AM_ASSERT(am_ao_get_own_prio() == AM_AO_PRIO_MAX);
96100
am_ao_post_fifo(&m_loopback.ao, &m_min_event);
97101
return AM_HSM_TRAN(loopback_test_proc);
98102
}

0 commit comments

Comments
 (0)