2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #if HAS_SAMPLE_PROFILE
21 #include <ao_sample_profile.h>
24 #define AO_NO_TASK_INDEX 0xff
26 __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
27 __data uint8_t ao_num_tasks;
28 __data uint8_t ao_cur_task_index;
29 __xdata struct ao_task *__data ao_cur_task;
31 #ifdef ao_arch_task_globals
35 #define AO_CHECK_STACK 0
38 static uint8_t in_yield;
40 static inline void ao_check_stack(void) {
42 if (!in_yield && ao_cur_task && &q < &ao_cur_task->stack[0])
43 ao_panic(AO_PANIC_STACK);
46 #define ao_check_stack()
50 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
54 if (ao_num_tasks == AO_NUM_TASKS)
55 ao_panic(AO_PANIC_NO_TASK);
56 for (task_id = 1; task_id != 0; task_id++) {
57 for (t = 0; t < ao_num_tasks; t++)
58 if (ao_tasks[t]->task_id == task_id)
60 if (t == ao_num_tasks)
63 ao_tasks[ao_num_tasks++] = task;
64 task->task_id = task_id;
68 * Construct a stack frame so that it will 'return'
69 * to the start of the task
71 ao_arch_init_stack(task, start);
74 __xdata uint8_t ao_idle;
76 /* Task switching function. This must not use any stack variables */
78 ao_yield(void) ao_arch_naked_define
82 if (ao_cur_task_index == AO_NO_TASK_INDEX)
83 ao_cur_task_index = ao_num_tasks-1;
86 #if HAS_SAMPLE_PROFILE
87 uint16_t tick = ao_sample_profile_timer_value();
88 uint16_t run = tick - ao_cur_task->start;
89 if (run > ao_cur_task->max_run)
90 ao_cur_task->max_run = run;
91 ++ao_cur_task->yields;
101 /* Find a task to run. If there isn't any runnable task,
102 * this loop will run forever, which is just fine
105 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
108 if (ao_cur_task_index == ao_num_tasks)
109 ao_cur_task_index = 0;
111 ao_cur_task = ao_tasks[ao_cur_task_index];
113 /* Check for ready task */
114 if (ao_cur_task->wchan == NULL)
117 /* Check if the alarm is set for a time which has passed */
118 if (ao_cur_task->alarm &&
119 (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
122 /* Enter lower power mode when there isn't anything to do */
123 if (ao_cur_task_index == ao_last_task_index)
126 #if HAS_SAMPLE_PROFILE
127 ao_cur_task->start = ao_sample_profile_timer_value();
134 ao_arch_restore_stack();
138 ao_sleep(__xdata void *wchan)
140 ao_cur_task->wchan = wchan;
142 if (ao_cur_task->wchan) {
143 ao_cur_task->wchan = NULL;
144 ao_cur_task->alarm = 0;
151 ao_wakeup(__xdata void *wchan)
156 for (i = 0; i < ao_num_tasks; i++)
157 if (ao_tasks[i]->wchan == wchan)
158 ao_tasks[i]->wchan = NULL;
162 ao_alarm(uint16_t delay)
164 /* Make sure we sleep *at least* delay ticks, which means adding
165 * one to account for the fact that we may be close to the next tick
167 if (!(ao_cur_task->alarm = ao_time() + delay + 1))
168 ao_cur_task->alarm = 1;
174 ao_cur_task->alarm = 0;
177 static __xdata uint8_t ao_forever;
180 ao_delay(uint16_t ticks)
183 ao_sleep(&ao_forever);
193 for (i = ao_cur_task_index; i < ao_num_tasks; i++)
194 ao_tasks[i] = ao_tasks[i+1];
195 ao_cur_task_index = AO_NO_TASK_INDEX;
198 /* we'll never get back here */
205 __xdata struct ao_task *task;
207 for (i = 0; i < ao_num_tasks; i++) {
209 printf("%12s: wchan %04x\n",
216 ao_start_scheduler(void)
218 ao_cur_task_index = AO_NO_TASK_INDEX;