X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fkernel%2Fao_task.c;h=15cd2a556b68a5176732eaf3329e9eacf2490c83;hb=4c5942fb082811f136322ec26de615cdb7e17580;hp=bafb49439d4441022be1b9159947f30a9f8d7bff;hpb=24167015705ae831692b95735968b04a876f935e;p=fw%2Faltos diff --git a/src/kernel/ao_task.c b/src/kernel/ao_task.c index bafb4943..15cd2a55 100644 --- a/src/kernel/ao_task.c +++ b/src/kernel/ao_task.c @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -23,17 +24,18 @@ #if HAS_STACK_GUARD #include #endif +#include #define DEBUG 0 #define AO_NO_TASK_INDEX 0xff -__xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS]; -__data uint8_t ao_num_tasks; -__xdata struct ao_task *__data ao_cur_task; +struct ao_task * ao_tasks[AO_NUM_TASKS]; +uint8_t ao_num_tasks; +struct ao_task *ao_cur_task; #if !HAS_TASK_QUEUE -static __data uint8_t ao_cur_task_index; +static uint8_t ao_cur_task_index; #endif #ifdef ao_arch_task_globals @@ -54,17 +56,24 @@ static inline void ao_check_stack(void) { #define ao_check_stack() #endif +#if DEBUG +#define ao_task_irq_check() ao_arch_irq_check() +#else +#define ao_task_irq_check() +#endif + #if HAS_TASK_QUEUE #define SLEEP_HASH_SIZE 17 static struct ao_list run_queue; static struct ao_list alarm_queue; -static struct ao_list sleep_queue[SLEEP_HASH_SIZE]; +static struct ao_list ao_sleep_queue[SLEEP_HASH_SIZE]; static void ao_task_to_run_queue(struct ao_task *task) { + ao_task_irq_check(); ao_list_del(&task->queue); ao_list_append(&task->queue, &run_queue); } @@ -72,12 +81,13 @@ ao_task_to_run_queue(struct ao_task *task) static struct ao_list * ao_task_sleep_queue(void *wchan) { - return &sleep_queue[(uintptr_t) wchan % SLEEP_HASH_SIZE]; + return &ao_sleep_queue[(uintptr_t) wchan % SLEEP_HASH_SIZE]; } static void ao_task_to_sleep_queue(struct ao_task *task, void *wchan) { + ao_task_irq_check(); ao_list_del(&task->queue); ao_list_append(&task->queue, ao_task_sleep_queue(wchan)); } @@ -122,6 +132,7 @@ static void ao_task_to_alarm_queue(struct ao_task *task) { struct ao_task *alarm; + ao_task_irq_check(); ao_list_for_each_entry(alarm, &alarm_queue, struct ao_task, alarm_queue) { if ((int16_t) (alarm->alarm - task->alarm) >= 0) { ao_list_insert(&task->alarm_queue, alarm->alarm_queue.prev); @@ -138,6 +149,7 @@ ao_task_to_alarm_queue(struct ao_task *task) static void ao_task_from_alarm_queue(struct ao_task *task) { + ao_task_irq_check(); ao_list_del(&task->alarm_queue); if (ao_list_is_empty(&alarm_queue)) ao_task_alarm_tick = 0; @@ -156,6 +168,7 @@ ao_task_init_queue(struct ao_task *task) static void ao_task_exit_queue(struct ao_task *task) { + ao_task_irq_check(); ao_list_del(&task->queue); ao_list_del(&task->alarm_queue); } @@ -165,13 +178,14 @@ ao_task_check_alarm(uint16_t tick) { struct ao_task *alarm, *next; - ao_list_for_each_entry_safe(alarm, next, &alarm_queue, struct ao_task, alarm_queue) { - if ((int16_t) (tick - alarm->alarm) < 0) - break; - alarm->alarm = 0; - ao_task_from_alarm_queue(alarm); - ao_task_to_run_queue(alarm); - } + ao_arch_critical( + ao_list_for_each_entry_safe(alarm, next, &alarm_queue, struct ao_task, alarm_queue) { + if ((int16_t) (tick - alarm->alarm) < 0) + break; + alarm->alarm = 0; + ao_task_from_alarm_queue(alarm); + ao_task_to_run_queue(alarm); + }); } void @@ -182,7 +196,7 @@ ao_task_init(void) ao_list_init(&alarm_queue); ao_task_alarm_tick = 0; for (i = 0; i < SLEEP_HASH_SIZE; i++) - ao_list_init(&sleep_queue[i]); + ao_list_init(&ao_sleep_queue[i]); } #if DEBUG @@ -276,8 +290,23 @@ ao_task_validate(void) #endif /* HAS_TASK_QUEUE */ +static inline void * +ao_stack_top(struct ao_task *task) +{ + uint8_t *top = &task->stack8[AO_STACK_SIZE]; + + /* Subtract off the TLS space, but keep the resulting + * stack 8-byte aligned + */ +#if USE_TLS + return top - ((_tls_size() + 7) & ~3); +#else + return top; +#endif +} + void -ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant +ao_add_task(struct ao_task * task, void (*task_func)(void), const char *name) { uint8_t task_id; uint8_t t; @@ -297,7 +326,11 @@ ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *nam * Construct a stack frame so that it will 'return' * to the start of the task */ - ao_arch_init_stack(task, start); + uint32_t *sp = ao_stack_top(task); +#if USE_TLS + _init_tls(sp); +#endif + ao_arch_init_stack(task, sp, task_func); ao_arch_critical( #if HAS_TASK_QUEUE ao_task_init_queue(task); @@ -308,7 +341,7 @@ ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *nam ); } -__data uint8_t ao_task_minimize_latency; +uint8_t ao_task_minimize_latency; /* Task switching function. This must not use any stack variables */ void @@ -355,18 +388,21 @@ ao_yield(void) ao_arch_naked_define */ if (ao_cur_task->wchan == NULL) ao_task_to_run_queue(ao_cur_task); - ao_cur_task = NULL; for (;;) { ao_arch_memory_barrier(); if (!ao_list_is_empty(&run_queue)) break; /* Wait for interrupts when there's nothing ready */ - ao_arch_wait_interrupt(); + if (ao_task_minimize_latency) { + ao_arch_release_interrupts(); + ao_arch_block_interrupts(); + } else + ao_arch_wait_interrupt(); } ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue); #else { - __pdata uint8_t ao_last_task_index = ao_cur_task_index; + uint8_t ao_last_task_index = ao_cur_task_index; for (;;) { ++ao_cur_task_index; if (ao_cur_task_index == ao_num_tasks) @@ -397,12 +433,15 @@ ao_yield(void) ao_arch_naked_define #endif #if AO_CHECK_STACK in_yield = 0; +#endif +#if USE_TLS + _set_tls(ao_stack_top(ao_cur_task)); #endif ao_arch_restore_stack(); } uint8_t -ao_sleep(__xdata void *wchan) +ao_sleep(void *wchan) { #if HAS_TASK_QUEUE uint32_t flags; @@ -423,8 +462,9 @@ ao_sleep(__xdata void *wchan) } void -ao_wakeup(__xdata void *wchan) __reentrant +ao_wakeup(void *wchan) { + ao_validate_cur_stack(); #if HAS_TASK_QUEUE struct ao_task *sleep, *next; struct ao_list *sleep_queue; @@ -442,55 +482,59 @@ ao_wakeup(__xdata void *wchan) __reentrant } ao_arch_irqrestore(flags); #else + { uint8_t i; for (i = 0; i < ao_num_tasks; i++) if (ao_tasks[i]->wchan == wchan) ao_tasks[i]->wchan = NULL; + } #endif ao_check_stack(); } -void -ao_alarm(uint16_t delay) +uint8_t +ao_sleep_for(void *wchan, uint16_t timeout) { + uint8_t ret; + if (timeout) { #if HAS_TASK_QUEUE - uint32_t flags; - /* Make sure we sleep *at least* delay ticks, which means adding - * one to account for the fact that we may be close to the next tick - */ - flags = ao_arch_irqsave(); + uint32_t flags; + flags = ao_arch_irqsave(); #endif - if (!(ao_cur_task->alarm = ao_time() + delay + 1)) - ao_cur_task->alarm = 1; + /* Make sure we sleep *at least* delay ticks, which means adding + * one to account for the fact that we may be close to the next tick + */ + if (!(ao_cur_task->alarm = ao_time() + timeout + 1)) + ao_cur_task->alarm = 1; #if HAS_TASK_QUEUE - ao_task_to_alarm_queue(ao_cur_task); - ao_arch_irqrestore(flags); + ao_task_to_alarm_queue(ao_cur_task); + ao_arch_irqrestore(flags); #endif -} - -void -ao_clear_alarm(void) -{ + } + ret = ao_sleep(wchan); + if (timeout) { #if HAS_TASK_QUEUE - uint32_t flags; + uint32_t flags; - flags = ao_arch_irqsave(); + flags = ao_arch_irqsave(); #endif - ao_cur_task->alarm = 0; + ao_cur_task->alarm = 0; #if HAS_TASK_QUEUE - ao_task_from_alarm_queue(ao_cur_task); - ao_arch_irqrestore(flags); + ao_task_from_alarm_queue(ao_cur_task); + ao_arch_irqrestore(flags); #endif + } + return ret; } -static __xdata uint8_t ao_forever; +static uint8_t ao_forever; void ao_delay(uint16_t ticks) { - ao_alarm(ticks); - ao_sleep(&ao_forever); - ao_clear_alarm(); + if (!ticks) + ticks = 1; + ao_sleep_for(&ao_forever, ticks); } void @@ -520,13 +564,16 @@ void ao_task_info(void) { uint8_t i; - __xdata struct ao_task *task; + struct ao_task *task; + uint16_t now = ao_time(); for (i = 0; i < ao_num_tasks; i++) { task = ao_tasks[i]; - printf("%12s: wchan %04x\n", - task->name, - (int) task->wchan); + printf("%2d: wchan %08x alarm %5d %s\n", + task->task_id, + (int) task->wchan, + task->alarm ? (int16_t) (task->alarm - now) : 9999, + task->name); } #if HAS_TASK_QUEUE && DEBUG ao_task_validate(); @@ -545,4 +592,5 @@ ao_start_scheduler(void) ao_arch_start_scheduler(); #endif ao_yield(); + __builtin_unreachable(); }