Tasks may move in task structure as a result of ao_exit
[fw/altos] / src / ao_task.c
index 14aa84c841d698d3a285de7370c80de369c7b56f..72c9d7d61f67216a71436ce9ffd7c4d0989c5bef 100644 (file)
@@ -28,10 +28,19 @@ void
 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
 {
        uint8_t __xdata *stack;
+       uint8_t task_id;
+       uint8_t t;
        if (ao_num_tasks == AO_NUM_TASKS)
                ao_panic(AO_PANIC_NO_TASK);
+       for (task_id = 1; task_id != 0; task_id++) {
+               for (t = 0; t < ao_num_tasks; t++)
+                       if (ao_tasks[t]->task_id == task_id)
+                               break;
+               if (t == ao_num_tasks)
+                       break;
+       }
        ao_tasks[ao_num_tasks++] = task;
-       task->task_id = ao_num_tasks;
+       task->task_id = task_id;
        task->name = name;
        /*
         * Construct a stack frame so that it will 'return'
@@ -217,7 +226,10 @@ ao_wakeup(__xdata void *wchan)
 void
 ao_alarm(uint16_t delay)
 {
-       if (!(ao_cur_task->alarm = ao_time() + delay))
+       /* 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() + delay + 1))
                ao_cur_task->alarm = 1;
 }
 
@@ -228,7 +240,7 @@ ao_wake_task(__xdata struct ao_task *task)
 }
 
 void
-ao_exit(void)
+ao_exit(void) __critical
 {
        uint8_t i;
        ao_num_tasks--;