altos: Add debug check for irq block when editing task queues
[fw/altos] / src / kernel / ao_task.c
index bafb49439d4441022be1b9159947f30a9f8d7bff..47352fc104afde937559c9ee2a31060447c62c95 100644 (file)
@@ -54,6 +54,12 @@ 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
@@ -65,6 +71,7 @@ static struct ao_list 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);
 }
@@ -78,6 +85,7 @@ ao_task_sleep_queue(void *wchan)
 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 +130,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 +147,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 +166,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 +176,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
@@ -355,7 +367,6 @@ 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))
@@ -425,6 +436,7 @@ ao_sleep(__xdata void *wchan)
 void
 ao_wakeup(__xdata void *wchan) __reentrant
 {
+       ao_validate_cur_stack();
 #if HAS_TASK_QUEUE
        struct ao_task  *sleep, *next;
        struct ao_list  *sleep_queue;
@@ -442,45 +454,49 @@ 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(__xdata 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;
@@ -488,9 +504,7 @@ static __xdata uint8_t ao_forever;
 void
 ao_delay(uint16_t ticks)
 {
-       ao_alarm(ticks);
-       ao_sleep(&ao_forever);
-       ao_clear_alarm();
+       ao_sleep_for(&ao_forever, ticks);
 }
 
 void