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;
169 ao_list_for_each_entry_safe(alarm, next, &alarm_queue, struct ao_task, alarm_queue) {
170 if ((int16_t) (tick - alarm->alarm) < 0)
173 ao_task_from_alarm_queue(alarm);
174 ao_task_to_run_queue(alarm);
182 ao_list_init(&run_queue);
183 ao_list_init(&alarm_queue);
184 ao_task_alarm_tick = 0;
185 for (i = 0; i < SLEEP_HASH_SIZE; i++)
186 ao_list_init(&sleep_queue[i]);
191 ao_task_validate_queue(struct ao_task *task)
196 struct ao_list *queue;
198 flags = ao_arch_irqsave();
200 queue = ao_task_sleep_queue(task->wchan);
206 ao_list_for_each_entry(m, queue, struct ao_task, queue) {
212 ao_arch_irqrestore(flags);
217 ao_task_validate_alarm(struct ao_task *task)
223 flags = ao_arch_irqsave();
224 if (task->alarm == 0)
226 ao_list_for_each_entry(m, &alarm_queue, struct ao_task, alarm_queue) {
231 if ((int16_t) (m->alarm - task->alarm) > 0)
234 if ((int16_t) (task->alarm - m->alarm) > 0)
239 ao_arch_irqrestore(flags);
245 ao_task_validate(void)
248 struct ao_task *task;
251 for (i = 0; i < ao_num_tasks; i++) {
253 ret = ao_task_validate_queue(task);
256 printf ("sleeping task not on sleep queue %s %08x\n",
257 task->name, task->wchan);
259 printf ("running task not on run queue %s\n",
262 ret = ao_task_validate_alarm(task);
265 printf ("alarm task not on alarm queue %s %d\n",
266 task->name, task->alarm);
268 printf ("alarm queue has sooner entries after %s %d\n",
269 task->name, task->alarm);
271 printf ("alarm queue has later entries before %s %d\n",
272 task->name, task->alarm);
278 #endif /* HAS_TASK_QUEUE */
281 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
285 if (ao_num_tasks == AO_NUM_TASKS)
286 ao_panic(AO_PANIC_NO_TASK);
287 for (task_id = 1; task_id != 0; task_id++) {
288 for (t = 0; t < ao_num_tasks; t++)
289 if (ao_tasks[t]->task_id == task_id)
291 if (t == ao_num_tasks)
294 task->task_id = task_id;
298 * Construct a stack frame so that it will 'return'
299 * to the start of the task
301 ao_arch_init_stack(task, start);
304 ao_task_init_queue(task);
305 ao_task_to_run_queue(task);
307 ao_tasks[ao_num_tasks] = task;
312 __data uint8_t ao_task_minimize_latency;
314 /* Task switching function. This must not use any stack variables */
316 ao_yield(void) ao_arch_naked_define
321 if (ao_cur_task == NULL)
322 ao_cur_task = ao_tasks[ao_num_tasks-1];
324 if (ao_cur_task_index == AO_NO_TASK_INDEX)
325 ao_cur_task_index = ao_num_tasks-1;
329 #if HAS_SAMPLE_PROFILE
330 uint16_t tick = ao_sample_profile_timer_value();
331 uint16_t run = tick - ao_cur_task->start;
332 if (run > ao_cur_task->max_run)
333 ao_cur_task->max_run = run;
334 ++ao_cur_task->yields;
336 ao_arch_save_stack();
341 if (ao_task_minimize_latency)
342 ao_arch_release_interrupts();
345 ao_arch_block_interrupts();
350 /* Find a task to run. If there isn't any runnable task,
351 * this loop will run forever, which is just fine
354 /* If the current task is running, move it to the
355 * end of the queue to allow other tasks a chance
357 if (ao_cur_task->wchan == NULL)
358 ao_task_to_run_queue(ao_cur_task);
360 ao_arch_memory_barrier();
361 if (!ao_list_is_empty(&run_queue))
363 /* Wait for interrupts when there's nothing ready */
364 ao_arch_wait_interrupt();
366 ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue);
369 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
372 if (ao_cur_task_index == ao_num_tasks)
373 ao_cur_task_index = 0;
375 ao_cur_task = ao_tasks[ao_cur_task_index];
377 /* Check for ready task */
378 if (ao_cur_task->wchan == NULL)
381 /* Check if the alarm is set for a time which has passed */
382 if (ao_cur_task->alarm &&
383 (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
386 /* Wait for interrupts when there's nothing ready */
387 if (ao_cur_task_index == ao_last_task_index && !ao_task_minimize_latency)
388 ao_arch_wait_interrupt();
392 #if HAS_SAMPLE_PROFILE
393 ao_cur_task->start = ao_sample_profile_timer_value();
396 ao_mpu_stack_guard(ao_cur_task->stack);
401 ao_arch_restore_stack();
405 ao_sleep(__xdata void *wchan)
409 flags = ao_arch_irqsave();
411 ao_cur_task->wchan = wchan;
413 ao_task_to_sleep_queue(ao_cur_task, wchan);
414 ao_arch_irqrestore(flags);
417 if (ao_cur_task->wchan) {
418 ao_cur_task->wchan = NULL;
419 ao_cur_task->alarm = 0;
426 ao_wakeup(__xdata void *wchan) __reentrant
428 ao_validate_cur_stack();
430 struct ao_task *sleep, *next;
431 struct ao_list *sleep_queue;
434 if (ao_num_tasks == 0)
436 sleep_queue = ao_task_sleep_queue(wchan);
437 flags = ao_arch_irqsave();
438 ao_list_for_each_entry_safe(sleep, next, sleep_queue, struct ao_task, queue) {
439 if (sleep->wchan == wchan) {
441 ao_task_to_run_queue(sleep);
444 ao_arch_irqrestore(flags);
448 for (i = 0; i < ao_num_tasks; i++)
449 if (ao_tasks[i]->wchan == wchan)
450 ao_tasks[i]->wchan = NULL;
457 ao_sleep_for(__xdata void *wchan, uint16_t timeout)
463 flags = ao_arch_irqsave();
465 /* Make sure we sleep *at least* delay ticks, which means adding
466 * one to account for the fact that we may be close to the next tick
468 if (!(ao_cur_task->alarm = ao_time() + timeout + 1))
469 ao_cur_task->alarm = 1;
471 ao_task_to_alarm_queue(ao_cur_task);
472 ao_arch_irqrestore(flags);
475 ret = ao_sleep(wchan);
480 flags = ao_arch_irqsave();
482 ao_cur_task->alarm = 0;
484 ao_task_from_alarm_queue(ao_cur_task);
485 ao_arch_irqrestore(flags);
491 static __xdata uint8_t ao_forever;
494 ao_delay(uint16_t ticks)
496 ao_sleep_for(&ao_forever, ticks);
503 ao_arch_block_interrupts();
506 for (i = 0; i < ao_num_tasks; i++)
507 if (ao_tasks[i] == ao_cur_task)
509 ao_task_exit_queue(ao_cur_task);
511 i = ao_cur_task_index;
512 ao_cur_task_index = AO_NO_TASK_INDEX;
514 for (; i < ao_num_tasks; i++)
515 ao_tasks[i] = ao_tasks[i+1];
518 /* we'll never get back here */
526 __xdata struct ao_task *task;
528 for (i = 0; i < ao_num_tasks; i++) {
530 printf("%12s: wchan %04x\n",
534 #if HAS_TASK_QUEUE && DEBUG
541 ao_start_scheduler(void)
544 ao_cur_task_index = AO_NO_TASK_INDEX;
547 #if HAS_ARCH_START_SCHEDULER
548 ao_arch_start_scheduler();