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>
29 #define AO_NO_TASK_INDEX 0xff
31 __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
32 __data uint8_t ao_num_tasks;
33 __xdata struct ao_task *__data ao_cur_task;
36 static __data uint8_t ao_cur_task_index;
39 #ifdef ao_arch_task_globals
43 #define AO_CHECK_STACK 0
46 static uint8_t in_yield;
48 static inline void ao_check_stack(void) {
50 if (!in_yield && ao_cur_task && &q < &ao_cur_task->stack[0])
51 ao_panic(AO_PANIC_STACK);
54 #define ao_check_stack()
59 #define SLEEP_HASH_SIZE 17
61 static struct ao_list run_queue;
62 static struct ao_list alarm_queue;
63 static struct ao_list sleep_queue[SLEEP_HASH_SIZE];
66 ao_task_to_run_queue(struct ao_task *task)
68 ao_list_del(&task->queue);
69 ao_list_append(&task->queue, &run_queue);
72 static struct ao_list *
73 ao_task_sleep_queue(void *wchan)
75 return &sleep_queue[(uintptr_t) wchan % SLEEP_HASH_SIZE];
79 ao_task_to_sleep_queue(struct ao_task *task, void *wchan)
81 ao_list_del(&task->queue);
82 ao_list_append(&task->queue, ao_task_sleep_queue(wchan));
87 ao_task_validate_alarm_queue(void)
89 struct ao_task *alarm, *prev = NULL;
92 if (ao_list_is_empty(&alarm_queue))
94 ao_list_for_each_entry(alarm, &alarm_queue, struct ao_task, alarm_queue) {
96 if ((int16_t) (alarm->alarm - prev->alarm) < 0) {
102 for (i = 0; i < ao_num_tasks; i++) {
105 if (ao_list_is_empty(&alarm->alarm_queue))
108 if (!ao_list_is_empty(&alarm->alarm_queue))
112 if (ao_task_alarm_tick != ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm)
116 #define ao_task_validate_alarm_queue()
119 uint16_t ao_task_alarm_tick;
122 ao_task_to_alarm_queue(struct ao_task *task)
124 struct ao_task *alarm;
125 ao_list_for_each_entry(alarm, &alarm_queue, struct ao_task, alarm_queue) {
126 if ((int16_t) (alarm->alarm - task->alarm) >= 0) {
127 ao_list_insert(&task->alarm_queue, alarm->alarm_queue.prev);
128 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
129 ao_task_validate_alarm_queue();
133 ao_list_append(&task->alarm_queue, &alarm_queue);
134 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
135 ao_task_validate_alarm_queue();
139 ao_task_from_alarm_queue(struct ao_task *task)
141 ao_list_del(&task->alarm_queue);
142 if (ao_list_is_empty(&alarm_queue))
143 ao_task_alarm_tick = 0;
145 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
146 ao_task_validate_alarm_queue();
150 ao_task_init_queue(struct ao_task *task)
152 ao_list_init(&task->queue);
153 ao_list_init(&task->alarm_queue);
157 ao_task_exit_queue(struct ao_task *task)
159 ao_list_del(&task->queue);
160 ao_list_del(&task->alarm_queue);
164 ao_task_check_alarm(uint16_t tick)
166 struct ao_task *alarm, *next;
168 ao_list_for_each_entry_safe(alarm, next, &alarm_queue, struct ao_task, alarm_queue) {
169 if ((int16_t) (tick - alarm->alarm) < 0)
172 ao_task_from_alarm_queue(alarm);
173 ao_task_to_run_queue(alarm);
181 ao_list_init(&run_queue);
182 ao_list_init(&alarm_queue);
183 ao_task_alarm_tick = 0;
184 for (i = 0; i < SLEEP_HASH_SIZE; i++)
185 ao_list_init(&sleep_queue[i]);
190 ao_task_validate_queue(struct ao_task *task)
195 struct ao_list *queue;
197 flags = ao_arch_irqsave();
199 queue = ao_task_sleep_queue(task->wchan);
205 ao_list_for_each_entry(m, queue, struct ao_task, queue) {
211 ao_arch_irqrestore(flags);
216 ao_task_validate_alarm(struct ao_task *task)
222 flags = ao_arch_irqsave();
223 if (task->alarm == 0)
225 ao_list_for_each_entry(m, &alarm_queue, struct ao_task, alarm_queue) {
230 if ((int16_t) (m->alarm - task->alarm) > 0)
233 if ((int16_t) (task->alarm - m->alarm) > 0)
238 ao_arch_irqrestore(flags);
244 ao_task_validate(void)
247 struct ao_task *task;
250 for (i = 0; i < ao_num_tasks; i++) {
252 ret = ao_task_validate_queue(task);
255 printf ("sleeping task not on sleep queue %s %08x\n",
256 task->name, task->wchan);
258 printf ("running task not on run queue %s\n",
261 ret = ao_task_validate_alarm(task);
264 printf ("alarm task not on alarm queue %s %d\n",
265 task->name, task->alarm);
267 printf ("alarm queue has sooner entries after %s %d\n",
268 task->name, task->alarm);
270 printf ("alarm queue has later entries before %s %d\n",
271 task->name, task->alarm);
277 #endif /* HAS_TASK_QUEUE */
280 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
284 if (ao_num_tasks == AO_NUM_TASKS)
285 ao_panic(AO_PANIC_NO_TASK);
286 for (task_id = 1; task_id != 0; task_id++) {
287 for (t = 0; t < ao_num_tasks; t++)
288 if (ao_tasks[t]->task_id == task_id)
290 if (t == ao_num_tasks)
293 task->task_id = task_id;
297 * Construct a stack frame so that it will 'return'
298 * to the start of the task
300 ao_arch_init_stack(task, start);
303 ao_task_init_queue(task);
304 ao_task_to_run_queue(task);
306 ao_tasks[ao_num_tasks] = task;
311 __data uint8_t ao_task_minimize_latency;
313 /* Task switching function. This must not use any stack variables */
315 ao_yield(void) ao_arch_naked_define
320 if (ao_cur_task == NULL)
321 ao_cur_task = ao_tasks[ao_num_tasks-1];
323 if (ao_cur_task_index == AO_NO_TASK_INDEX)
324 ao_cur_task_index = ao_num_tasks-1;
328 #if HAS_SAMPLE_PROFILE
329 uint16_t tick = ao_sample_profile_timer_value();
330 uint16_t run = tick - ao_cur_task->start;
331 if (run > ao_cur_task->max_run)
332 ao_cur_task->max_run = run;
333 ++ao_cur_task->yields;
335 ao_arch_save_stack();
340 if (ao_task_minimize_latency)
341 ao_arch_release_interrupts();
344 ao_arch_block_interrupts();
349 /* Find a task to run. If there isn't any runnable task,
350 * this loop will run forever, which is just fine
353 /* If the current task is running, move it to the
354 * end of the queue to allow other tasks a chance
356 if (ao_cur_task->wchan == NULL)
357 ao_task_to_run_queue(ao_cur_task);
359 ao_arch_memory_barrier();
360 if (!ao_list_is_empty(&run_queue))
362 /* Wait for interrupts when there's nothing ready */
363 ao_arch_wait_interrupt();
365 ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue);
368 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
371 if (ao_cur_task_index == ao_num_tasks)
372 ao_cur_task_index = 0;
374 ao_cur_task = ao_tasks[ao_cur_task_index];
376 /* Check for ready task */
377 if (ao_cur_task->wchan == NULL)
380 /* Check if the alarm is set for a time which has passed */
381 if (ao_cur_task->alarm &&
382 (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
385 /* Wait for interrupts when there's nothing ready */
386 if (ao_cur_task_index == ao_last_task_index && !ao_task_minimize_latency)
387 ao_arch_wait_interrupt();
391 #if HAS_SAMPLE_PROFILE
392 ao_cur_task->start = ao_sample_profile_timer_value();
395 ao_mpu_stack_guard(ao_cur_task->stack);
400 ao_arch_restore_stack();
404 ao_sleep(__xdata void *wchan)
408 flags = ao_arch_irqsave();
410 ao_cur_task->wchan = wchan;
412 ao_task_to_sleep_queue(ao_cur_task, wchan);
413 ao_arch_irqrestore(flags);
416 if (ao_cur_task->wchan) {
417 ao_cur_task->wchan = NULL;
418 ao_cur_task->alarm = 0;
425 ao_wakeup(__xdata void *wchan) __reentrant
427 ao_validate_cur_stack();
429 struct ao_task *sleep, *next;
430 struct ao_list *sleep_queue;
433 if (ao_num_tasks == 0)
435 sleep_queue = ao_task_sleep_queue(wchan);
436 flags = ao_arch_irqsave();
437 ao_list_for_each_entry_safe(sleep, next, sleep_queue, struct ao_task, queue) {
438 if (sleep->wchan == wchan) {
440 ao_task_to_run_queue(sleep);
443 ao_arch_irqrestore(flags);
447 for (i = 0; i < ao_num_tasks; i++)
448 if (ao_tasks[i]->wchan == wchan)
449 ao_tasks[i]->wchan = NULL;
456 ao_sleep_for(__xdata void *wchan, uint16_t timeout)
462 /* Make sure we sleep *at least* delay ticks, which means adding
463 * one to account for the fact that we may be close to the next tick
465 flags = ao_arch_irqsave();
467 if (!(ao_cur_task->alarm = ao_time() + timeout + 1))
468 ao_cur_task->alarm = 1;
470 ao_task_to_alarm_queue(ao_cur_task);
471 ao_arch_irqrestore(flags);
474 ret = ao_sleep(wchan);
479 flags = ao_arch_irqsave();
481 ao_cur_task->alarm = 0;
483 ao_task_from_alarm_queue(ao_cur_task);
484 ao_arch_irqrestore(flags);
490 static __xdata uint8_t ao_forever;
493 ao_delay(uint16_t ticks)
495 ao_sleep_for(&ao_forever, ticks);
502 ao_arch_block_interrupts();
505 for (i = 0; i < ao_num_tasks; i++)
506 if (ao_tasks[i] == ao_cur_task)
508 ao_task_exit_queue(ao_cur_task);
510 i = ao_cur_task_index;
511 ao_cur_task_index = AO_NO_TASK_INDEX;
513 for (; i < ao_num_tasks; i++)
514 ao_tasks[i] = ao_tasks[i+1];
517 /* we'll never get back here */
525 __xdata struct ao_task *task;
527 for (i = 0; i < ao_num_tasks; i++) {
529 printf("%12s: wchan %04x\n",
533 #if HAS_TASK_QUEUE && DEBUG
540 ao_start_scheduler(void)
543 ao_cur_task_index = AO_NO_TASK_INDEX;
546 #if HAS_ARCH_START_SCHEDULER
547 ao_arch_start_scheduler();