altos: Fix a bunch of time variables to be AO_TICK_TYPE
[fw/altos] / src / kernel / ao_task.c
index dc5c19132caeb121a21b5bbe0698da713a68c9d8..38aefbec1ea3d91bd3bf2d4a4e0bc5664f8b5d95 100644 (file)
@@ -24,6 +24,7 @@
 #if HAS_STACK_GUARD
 #include <ao_mpu.h>
 #endif
+#include <picotls.h>
 
 #define DEBUG  0
 
@@ -67,7 +68,7 @@ static inline void ao_check_stack(void) {
 
 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)
@@ -80,7 +81,7 @@ 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
@@ -125,7 +126,7 @@ ao_task_validate_alarm_queue(void)
 #define ao_task_validate_alarm_queue()
 #endif
 
-uint16_t       ao_task_alarm_tick;
+AO_TICK_TYPE   ao_task_alarm_tick;
 
 static void
 ao_task_to_alarm_queue(struct ao_task *task)
@@ -173,7 +174,7 @@ ao_task_exit_queue(struct ao_task *task)
 }
 
 void
-ao_task_check_alarm(uint16_t tick)
+ao_task_check_alarm(AO_TICK_TYPE tick)
 {
        struct ao_task  *alarm, *next;
 
@@ -195,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
@@ -289,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(struct ao_task * task, void (*start)(void), const char *name) 
+ao_add_task(struct ao_task * task, void (*task_func)(void), const char *name) 
 {
        uint8_t task_id;
        uint8_t t;
@@ -310,7 +326,11 @@ ao_add_task(struct ao_task * task, void (*start)(void), const char *name)
         * 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);
@@ -339,8 +359,8 @@ ao_yield(void) ao_arch_naked_define
        else
        {
 #if HAS_SAMPLE_PROFILE
-               uint16_t        tick = ao_sample_profile_timer_value();
-               uint16_t        run = tick - ao_cur_task->start;
+               AO_TICK_TYPE    tick = ao_sample_profile_timer_value();
+               AO_TICK_TYPE    run = tick - ao_cur_task->start;
                if (run > ao_cur_task->max_run)
                        ao_cur_task->max_run = run;
                ++ao_cur_task->yields;
@@ -413,6 +433,9 @@ 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();
 }
@@ -470,7 +493,7 @@ ao_wakeup(void *wchan)
 }
 
 uint8_t
-ao_sleep_for(void *wchan, uint16_t timeout)
+ao_sleep_for(void *wchan, AO_TICK_TYPE timeout)
 {
        uint8_t ret;
        if (timeout) {
@@ -507,7 +530,7 @@ ao_sleep_for(void *wchan, uint16_t timeout)
 static uint8_t ao_forever;
 
 void
-ao_delay(uint16_t ticks)
+ao_delay(AO_TICK_TYPE ticks)
 {
        if (!ticks)
                ticks = 1;
@@ -542,7 +565,7 @@ ao_task_info(void)
 {
        uint8_t         i;
        struct ao_task *task;
-       uint16_t        now = ao_time();
+       AO_TICK_TYPE    now = ao_time();
 
        for (i = 0; i < ao_num_tasks; i++) {
                task = ao_tasks[i];