Skip to content

Commit fc5728b

Browse files
committed
Add AM_PAL_TASK_ID_MAIN
1 parent b33b4af commit fc5728b

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

libs/ao/cooperative/port.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ static struct am_bit_u64 am_ready_aos_ = {0};
4141
bool am_ao_run_all(bool loop) {
4242
struct am_ao_state *me = &am_ao_state_;
4343
bool processed = false;
44+
int task_id = am_pal_task_own_id();
4445
do {
4546
me->crit_enter();
4647

4748
while (am_bit_u64_is_empty(&am_ready_aos_)) {
4849
me->crit_exit();
49-
me->on_idle();
50+
if (me->on_idle) {
51+
me->on_idle();
52+
} else {
53+
am_pal_task_wait(task_id);
54+
}
5055
me->crit_enter();
5156
}
5257
int msb = am_bit_u64_msb(&am_ready_aos_);
@@ -105,6 +110,7 @@ void am_ao_start(
105110

106111
ao->prio = prio;
107112
ao->name = name;
113+
ao->task_id = am_pal_task_own_id();
108114

109115
struct am_ao_state *me = &am_ao_state_;
110116
AM_ASSERT(NULL == me->ao[prio]);
@@ -123,4 +129,6 @@ void am_ao_notify(const struct am_ao *ao) {
123129
me->crit_enter();
124130
am_bit_u64_set(&am_ready_aos_, ao->prio);
125131
me->crit_exit();
132+
133+
am_pal_task_notify(ao->task_id);
126134
}

libs/pal/pal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
/** Invalid task ID */
4040
#define AM_PAL_TASK_ID_NONE 0
4141

42+
/** Main task ID */
43+
#define AM_PAL_TASK_ID_MAIN -1
44+
4245
/** Default tick domain */
4346
#define AM_PAL_TICK_DOMAIN_DEFAULT 0
4447

libs/pal/posix/pal.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct am_pal_task {
7070
void *arg;
7171
};
7272

73+
static struct am_pal_task task_main_ = {0};
7374
static struct am_pal_task task_arr_[AM_PAL_TASK_NUM_MAX] = {0};
7475

7576
struct am_pal_mutex {
@@ -114,6 +115,9 @@ void am_pal_crit_exit(void) {
114115

115116
int am_pal_task_own_id(void) {
116117
pthread_t thread = pthread_self();
118+
if (task_main_.thread == thread) {
119+
return AM_PAL_TASK_ID_MAIN;
120+
}
117121
for (int i = 0; i < AM_COUNTOF(task_arr_); ++i) {
118122
if (task_arr_[i].thread == thread) {
119123
return am_pal_id_from_index(i);
@@ -192,8 +196,13 @@ int am_pal_task_create(
192196
void am_pal_task_notify(int task_id) {
193197
AM_ASSERT(task_id != AM_PAL_TASK_ID_NONE);
194198

195-
int index = am_pal_index_from_id(task_id);
196-
struct am_pal_task *t = &task_arr_[index];
199+
struct am_pal_task *t = NULL;
200+
if (AM_PAL_TASK_ID_MAIN == task_id) {
201+
t = &task_main_;
202+
} else {
203+
int index = am_pal_index_from_id(task_id);
204+
t = &task_arr_[index];
205+
}
197206
AM_ASSERT(t);
198207
pthread_mutex_lock(&t->mutex);
199208
AM_ATOMIC_STORE_N(&t->notified, true);
@@ -205,8 +214,15 @@ void am_pal_task_wait(int task_id) {
205214
if (AM_PAL_TASK_ID_NONE == task_id) {
206215
task_id = am_pal_task_own_id();
207216
}
208-
int index = am_pal_index_from_id(task_id);
209-
struct am_pal_task *t = &task_arr_[index];
217+
AM_ASSERT(task_id != AM_PAL_TASK_ID_NONE);
218+
219+
struct am_pal_task *t = NULL;
220+
if (AM_PAL_TASK_ID_MAIN == task_id) {
221+
t = &task_main_;
222+
} else {
223+
int index = am_pal_index_from_id(task_id);
224+
t = &task_arr_[index];
225+
}
210226
AM_ASSERT(t);
211227
pthread_mutex_lock(&t->mutex);
212228
while (!AM_ATOMIC_LOAD_N(&t->notified)) {
@@ -347,7 +363,16 @@ int am_pal_printf(const char *fmt, ...) {
347363

348364
void am_pal_flush(void) { fflush(stdout); }
349365

350-
void am_pal_ctor(void) {}
366+
void am_pal_ctor(void) {
367+
struct am_pal_task *task = &task_main_;
368+
369+
task->thread = pthread_self();
370+
am_pal_mutex_init(&task->mutex);
371+
372+
int ret = pthread_cond_init(&task->cond, /*attr=*/NULL);
373+
AM_ASSERT(0 == ret);
374+
task->valid = true;
375+
}
351376

352377
void am_pal_dtor(void) {
353378
for (int i = 0; i < AM_COUNTOF(mutex_arr_); ++i) {

0 commit comments

Comments
 (0)