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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #if HAS_SAMPLE_PROFILE
22 #include <ao_sample_profile.h>
30 #define AO_NO_TASK_INDEX 0xff
32 __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
33 __data uint8_t ao_num_tasks;
34 __xdata struct ao_task *__data ao_cur_task;
37 static __data uint8_t ao_cur_task_index;
40 #ifdef ao_arch_task_globals
44 #define AO_CHECK_STACK 0
47 static uint8_t in_yield;
49 static inline void ao_check_stack(void) {
51 if (!in_yield && ao_cur_task && &q < &ao_cur_task->stack[0])
52 ao_panic(AO_PANIC_STACK);
55 #define ao_check_stack()
59 #define ao_task_irq_check() ao_arch_irq_check()
61 #define ao_task_irq_check()
66 #define SLEEP_HASH_SIZE 17
68 static struct ao_list run_queue;
69 static struct ao_list alarm_queue;
70 static struct ao_list sleep_queue[SLEEP_HASH_SIZE];
73 ao_task_to_run_queue(struct ao_task *task)
76 ao_list_del(&task->queue);
77 ao_list_append(&task->queue, &run_queue);
80 static struct ao_list *
81 ao_task_sleep_queue(void *wchan)
83 return &sleep_queue[(uintptr_t) wchan % SLEEP_HASH_SIZE];
87 ao_task_to_sleep_queue(struct ao_task *task, void *wchan)
90 ao_list_del(&task->queue);
91 ao_list_append(&task->queue, ao_task_sleep_queue(wchan));
96 ao_task_validate_alarm_queue(void)
98 struct ao_task *alarm, *prev = NULL;
101 if (ao_list_is_empty(&alarm_queue))
103 ao_list_for_each_entry(alarm, &alarm_queue, struct ao_task, alarm_queue) {
105 if ((int16_t) (alarm->alarm - prev->alarm) < 0) {
111 for (i = 0; i < ao_num_tasks; i++) {
114 if (ao_list_is_empty(&alarm->alarm_queue))
117 if (!ao_list_is_empty(&alarm->alarm_queue))
121 if (ao_task_alarm_tick != ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm)
125 #define ao_task_validate_alarm_queue()
128 uint16_t ao_task_alarm_tick;
131 ao_task_to_alarm_queue(struct ao_task *task)
133 struct ao_task *alarm;
135 ao_list_for_each_entry(alarm, &alarm_queue, struct ao_task, alarm_queue) {
136 if ((int16_t) (alarm->alarm - task->alarm) >= 0) {
137 ao_list_insert(&task->alarm_queue, alarm->alarm_queue.prev);
138 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
139 ao_task_validate_alarm_queue();
143 ao_list_append(&task->alarm_queue, &alarm_queue);
144 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
145 ao_task_validate_alarm_queue();
149 ao_task_from_alarm_queue(struct ao_task *task)
152 ao_list_del(&task->alarm_queue);
153 if (ao_list_is_empty(&alarm_queue))
154 ao_task_alarm_tick = 0;
156 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
157 ao_task_validate_alarm_queue();
161 ao_task_init_queue(struct ao_task *task)
163 ao_list_init(&task->queue);
164 ao_list_init(&task->alarm_queue);
168 ao_task_exit_queue(struct ao_task *task)
171 ao_list_del(&task->queue);
172 ao_list_del(&task->alarm_queue);
176 ao_task_check_alarm(uint16_t tick)
178 struct ao_task *alarm, *next;
181 ao_list_for_each_entry_safe(alarm, next, &alarm_queue, struct ao_task, alarm_queue) {
182 if ((int16_t) (tick - alarm->alarm) < 0)
185 ao_task_from_alarm_queue(alarm);
186 ao_task_to_run_queue(alarm);
194 ao_list_init(&run_queue);
195 ao_list_init(&alarm_queue);
196 ao_task_alarm_tick = 0;
197 for (i = 0; i < SLEEP_HASH_SIZE; i++)
198 ao_list_init(&sleep_queue[i]);
203 ao_task_validate_queue(struct ao_task *task)
208 struct ao_list *queue;
210 flags = ao_arch_irqsave();
212 queue = ao_task_sleep_queue(task->wchan);
218 ao_list_for_each_entry(m, queue, struct ao_task, queue) {
224 ao_arch_irqrestore(flags);
229 ao_task_validate_alarm(struct ao_task *task)
235 flags = ao_arch_irqsave();
236 if (task->alarm == 0)
238 ao_list_for_each_entry(m, &alarm_queue, struct ao_task, alarm_queue) {
243 if ((int16_t) (m->alarm - task->alarm) > 0)
246 if ((int16_t) (task->alarm - m->alarm) > 0)
251 ao_arch_irqrestore(flags);
257 ao_task_validate(void)
260 struct ao_task *task;
263 for (i = 0; i < ao_num_tasks; i++) {
265 ret = ao_task_validate_queue(task);
268 printf ("sleeping task not on sleep queue %s %08x\n",
269 task->name, task->wchan);
271 printf ("running task not on run queue %s\n",
274 ret = ao_task_validate_alarm(task);
277 printf ("alarm task not on alarm queue %s %d\n",
278 task->name, task->alarm);
280 printf ("alarm queue has sooner entries after %s %d\n",
281 task->name, task->alarm);
283 printf ("alarm queue has later entries before %s %d\n",
284 task->name, task->alarm);
290 #endif /* HAS_TASK_QUEUE */
293 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
297 if (ao_num_tasks == AO_NUM_TASKS)
298 ao_panic(AO_PANIC_NO_TASK);
299 for (task_id = 1; task_id != 0; task_id++) {
300 for (t = 0; t < ao_num_tasks; t++)
301 if (ao_tasks[t]->task_id == task_id)
303 if (t == ao_num_tasks)
306 task->task_id = task_id;
310 * Construct a stack frame so that it will 'return'
311 * to the start of the task
313 ao_arch_init_stack(task, start);
316 ao_task_init_queue(task);
317 ao_task_to_run_queue(task);
319 ao_tasks[ao_num_tasks] = task;
324 __data uint8_t ao_task_minimize_latency;
326 /* Task switching function. This must not use any stack variables */
328 ao_yield(void) ao_arch_naked_define
333 if (ao_cur_task == NULL)
334 ao_cur_task = ao_tasks[ao_num_tasks-1];
336 if (ao_cur_task_index == AO_NO_TASK_INDEX)
337 ao_cur_task_index = ao_num_tasks-1;
341 #if HAS_SAMPLE_PROFILE
342 uint16_t tick = ao_sample_profile_timer_value();
343 uint16_t run = tick - ao_cur_task->start;
344 if (run > ao_cur_task->max_run)
345 ao_cur_task->max_run = run;
346 ++ao_cur_task->yields;
348 ao_arch_save_stack();
353 if (ao_task_minimize_latency)
354 ao_arch_release_interrupts();
357 ao_arch_block_interrupts();
362 /* Find a task to run. If there isn't any runnable task,
363 * this loop will run forever, which is just fine
366 /* If the current task is running, move it to the
367 * end of the queue to allow other tasks a chance
369 if (ao_cur_task->wchan == NULL)
370 ao_task_to_run_queue(ao_cur_task);
372 ao_arch_memory_barrier();
373 if (!ao_list_is_empty(&run_queue))
375 /* Wait for interrupts when there's nothing ready */
376 if (ao_task_minimize_latency) {
377 ao_arch_release_interrupts();
378 ao_arch_block_interrupts();
380 ao_arch_wait_interrupt();
382 ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue);
385 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
388 if (ao_cur_task_index == ao_num_tasks)
389 ao_cur_task_index = 0;
391 ao_cur_task = ao_tasks[ao_cur_task_index];
393 /* Check for ready task */
394 if (ao_cur_task->wchan == NULL)
397 /* Check if the alarm is set for a time which has passed */
398 if (ao_cur_task->alarm &&
399 (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
402 /* Wait for interrupts when there's nothing ready */
403 if (ao_cur_task_index == ao_last_task_index && !ao_task_minimize_latency)
404 ao_arch_wait_interrupt();
408 #if HAS_SAMPLE_PROFILE
409 ao_cur_task->start = ao_sample_profile_timer_value();
412 ao_mpu_stack_guard(ao_cur_task->stack);
417 ao_arch_restore_stack();
421 ao_sleep(__xdata void *wchan)
425 flags = ao_arch_irqsave();
427 ao_cur_task->wchan = wchan;
429 ao_task_to_sleep_queue(ao_cur_task, wchan);
430 ao_arch_irqrestore(flags);
433 if (ao_cur_task->wchan) {
434 ao_cur_task->wchan = NULL;
435 ao_cur_task->alarm = 0;
442 ao_wakeup(__xdata void *wchan) __reentrant
444 ao_validate_cur_stack();
446 struct ao_task *sleep, *next;
447 struct ao_list *sleep_queue;
450 if (ao_num_tasks == 0)
452 sleep_queue = ao_task_sleep_queue(wchan);
453 flags = ao_arch_irqsave();
454 ao_list_for_each_entry_safe(sleep, next, sleep_queue, struct ao_task, queue) {
455 if (sleep->wchan == wchan) {
457 ao_task_to_run_queue(sleep);
460 ao_arch_irqrestore(flags);
464 for (i = 0; i < ao_num_tasks; i++)
465 if (ao_tasks[i]->wchan == wchan)
466 ao_tasks[i]->wchan = NULL;
473 ao_sleep_for(__xdata void *wchan, uint16_t timeout)
479 flags = ao_arch_irqsave();
481 /* Make sure we sleep *at least* delay ticks, which means adding
482 * one to account for the fact that we may be close to the next tick
484 if (!(ao_cur_task->alarm = ao_time() + timeout + 1))
485 ao_cur_task->alarm = 1;
487 ao_task_to_alarm_queue(ao_cur_task);
488 ao_arch_irqrestore(flags);
491 ret = ao_sleep(wchan);
496 flags = ao_arch_irqsave();
498 ao_cur_task->alarm = 0;
500 ao_task_from_alarm_queue(ao_cur_task);
501 ao_arch_irqrestore(flags);
507 static __xdata uint8_t ao_forever;
510 ao_delay(uint16_t ticks)
514 ao_sleep_for(&ao_forever, ticks);
521 ao_arch_block_interrupts();
524 for (i = 0; i < ao_num_tasks; i++)
525 if (ao_tasks[i] == ao_cur_task)
527 ao_task_exit_queue(ao_cur_task);
529 i = ao_cur_task_index;
530 ao_cur_task_index = AO_NO_TASK_INDEX;
532 for (; i < ao_num_tasks; i++)
533 ao_tasks[i] = ao_tasks[i+1];
536 /* we'll never get back here */
544 __xdata struct ao_task *task;
545 uint16_t now = ao_time();
547 for (i = 0; i < ao_num_tasks; i++) {
549 printf("%2d: wchan %08x alarm %5d %s\n",
552 task->alarm ? (int16_t) (task->alarm - now) : 9999,
555 #if HAS_TASK_QUEUE && DEBUG
562 ao_start_scheduler(void)
565 ao_cur_task_index = AO_NO_TASK_INDEX;
568 #if HAS_ARCH_START_SCHEDULER
569 ao_arch_start_scheduler();