a stab at turning on rudimentary logging for telefiretwo
[fw/altos] / src / kernel / ao_task.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_task.h>
21 #if HAS_SAMPLE_PROFILE
22 #include <ao_sample_profile.h>
23 #endif
24 #if HAS_STACK_GUARD
25 #include <ao_mpu.h>
26 #endif
27
28 #define DEBUG   0
29
30 #define AO_NO_TASK_INDEX        0xff
31
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;
35
36 #if !HAS_TASK_QUEUE
37 static __data uint8_t ao_cur_task_index;
38 #endif
39
40 #ifdef ao_arch_task_globals
41 ao_arch_task_globals
42 #endif
43
44 #define AO_CHECK_STACK  0
45
46 #if AO_CHECK_STACK
47 static uint8_t  in_yield;
48
49 static inline void ao_check_stack(void) {
50         uint8_t q;
51         if (!in_yield && ao_cur_task && &q < &ao_cur_task->stack[0])
52                 ao_panic(AO_PANIC_STACK);
53 }
54 #else
55 #define ao_check_stack()
56 #endif
57
58 #if DEBUG
59 #define ao_task_irq_check()     ao_arch_irq_check()
60 #else
61 #define ao_task_irq_check()
62 #endif
63
64 #if HAS_TASK_QUEUE
65
66 #define SLEEP_HASH_SIZE 17
67
68 static struct ao_list   run_queue;
69 static struct ao_list   alarm_queue;
70 static struct ao_list   sleep_queue[SLEEP_HASH_SIZE];
71
72 static void
73 ao_task_to_run_queue(struct ao_task *task)
74 {
75         ao_task_irq_check();
76         ao_list_del(&task->queue);
77         ao_list_append(&task->queue, &run_queue);
78 }
79
80 static struct ao_list *
81 ao_task_sleep_queue(void *wchan)
82 {
83         return &sleep_queue[(uintptr_t) wchan % SLEEP_HASH_SIZE];
84 }
85
86 static void
87 ao_task_to_sleep_queue(struct ao_task *task, void *wchan)
88 {
89         ao_task_irq_check();
90         ao_list_del(&task->queue);
91         ao_list_append(&task->queue, ao_task_sleep_queue(wchan));
92 }
93
94 #if DEBUG
95 static void
96 ao_task_validate_alarm_queue(void)
97 {
98         struct ao_task  *alarm, *prev = NULL;
99         int             i;
100
101         if (ao_list_is_empty(&alarm_queue))
102                 return;
103         ao_list_for_each_entry(alarm, &alarm_queue, struct ao_task, alarm_queue) {
104                 if (prev) {
105                         if ((int16_t) (alarm->alarm - prev->alarm) < 0) {
106                                 ao_panic(1);
107                         }
108                 }
109                 prev = alarm;
110         }
111         for (i = 0; i < ao_num_tasks; i++) {
112                 alarm = ao_tasks[i];
113                 if (alarm->alarm) {
114                         if (ao_list_is_empty(&alarm->alarm_queue))
115                                 ao_panic(2);
116                 } else {
117                         if (!ao_list_is_empty(&alarm->alarm_queue))
118                                 ao_panic(3);
119                 }
120         }
121         if (ao_task_alarm_tick != ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm)
122                 ao_panic(4);
123 }
124 #else
125 #define ao_task_validate_alarm_queue()
126 #endif
127
128 uint16_t        ao_task_alarm_tick;
129
130 static void
131 ao_task_to_alarm_queue(struct ao_task *task)
132 {
133         struct ao_task  *alarm;
134         ao_task_irq_check();
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();
140                         return;
141                 }
142         }
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();
146 }
147
148 static void
149 ao_task_from_alarm_queue(struct ao_task *task)
150 {
151         ao_task_irq_check();
152         ao_list_del(&task->alarm_queue);
153         if (ao_list_is_empty(&alarm_queue))
154                 ao_task_alarm_tick = 0;
155         else
156                 ao_task_alarm_tick = ao_list_first_entry(&alarm_queue, struct ao_task, alarm_queue)->alarm;
157         ao_task_validate_alarm_queue();
158 }
159
160 static void
161 ao_task_init_queue(struct ao_task *task)
162 {
163         ao_list_init(&task->queue);
164         ao_list_init(&task->alarm_queue);
165 }
166
167 static void
168 ao_task_exit_queue(struct ao_task *task)
169 {
170         ao_task_irq_check();
171         ao_list_del(&task->queue);
172         ao_list_del(&task->alarm_queue);
173 }
174
175 void
176 ao_task_check_alarm(uint16_t tick)
177 {
178         struct ao_task  *alarm, *next;
179
180         ao_arch_critical(
181                 ao_list_for_each_entry_safe(alarm, next, &alarm_queue, struct ao_task, alarm_queue) {
182                         if ((int16_t) (tick - alarm->alarm) < 0)
183                                 break;
184                         alarm->alarm = 0;
185                         ao_task_from_alarm_queue(alarm);
186                         ao_task_to_run_queue(alarm);
187                 });
188 }
189
190 void
191 ao_task_init(void)
192 {
193         uint8_t i;
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]);
199 }
200
201 #if DEBUG
202 static uint8_t
203 ao_task_validate_queue(struct ao_task *task)
204 {
205         uint32_t flags;
206         struct ao_task *m;
207         uint8_t ret = 0;
208         struct ao_list *queue;
209
210         flags = ao_arch_irqsave();
211         if (task->wchan) {
212                 queue = ao_task_sleep_queue(task->wchan);
213                 ret |= 2;
214         } else {
215                 queue = &run_queue;
216                 ret |= 4;
217         }
218         ao_list_for_each_entry(m, queue, struct ao_task, queue) {
219                 if (m == task) {
220                         ret |= 1;
221                         break;
222                 }
223         }
224         ao_arch_irqrestore(flags);
225         return ret;
226 }
227
228 static uint8_t
229 ao_task_validate_alarm(struct ao_task *task)
230 {
231         uint32_t        flags;
232         struct ao_task  *m;
233         uint8_t         ret = 0;
234
235         flags = ao_arch_irqsave();
236         if (task->alarm == 0)
237                 return 0xff;
238         ao_list_for_each_entry(m, &alarm_queue, struct ao_task, alarm_queue) {
239                 if (m == task)
240                         ret |= 1;
241                 else {
242                         if (!(ret&1)) {
243                                 if ((int16_t) (m->alarm - task->alarm) > 0)
244                                         ret |= 2;
245                         } else {
246                                 if ((int16_t) (task->alarm - m->alarm) > 0)
247                                         ret |= 4;
248                         }
249                 }
250         }
251         ao_arch_irqrestore(flags);
252         return ret;
253 }
254
255
256 static void
257 ao_task_validate(void)
258 {
259         uint8_t         i;
260         struct ao_task  *task;
261         uint8_t         ret;
262
263         for (i = 0; i < ao_num_tasks; i++) {
264                 task = ao_tasks[i];
265                 ret = ao_task_validate_queue(task);
266                 if (!(ret & 1)) {
267                         if (ret & 2)
268                                 printf ("sleeping task not on sleep queue %s %08x\n",
269                                         task->name, task->wchan);
270                         else
271                                 printf ("running task not on run queue %s\n",
272                                         task->name);
273                 }
274                 ret = ao_task_validate_alarm(task);
275                 if (ret != 0xff) {
276                         if (!(ret & 1))
277                                 printf ("alarm task not on alarm queue %s %d\n",
278                                         task->name, task->alarm);
279                         if (ret & 2)
280                                 printf ("alarm queue has sooner entries after %s %d\n",
281                                         task->name, task->alarm);
282                         if (ret & 4)
283                                 printf ("alarm queue has later entries before %s %d\n",
284                                         task->name, task->alarm);
285                 }
286         }
287 }
288 #endif /* DEBUG */
289
290 #endif /* HAS_TASK_QUEUE */
291
292 void
293 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant
294 {
295         uint8_t task_id;
296         uint8_t t;
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)
302                                 break;
303                 if (t == ao_num_tasks)
304                         break;
305         }
306         task->task_id = task_id;
307         task->name = name;
308         task->wchan = NULL;
309         /*
310          * Construct a stack frame so that it will 'return'
311          * to the start of the task
312          */
313         ao_arch_init_stack(task, start);
314         ao_arch_critical(
315 #if HAS_TASK_QUEUE
316                 ao_task_init_queue(task);
317                 ao_task_to_run_queue(task);
318 #endif
319                 ao_tasks[ao_num_tasks] = task;
320                 ao_num_tasks++;
321                 );
322 }
323
324 __data uint8_t  ao_task_minimize_latency;
325
326 /* Task switching function. This must not use any stack variables */
327 void
328 ao_yield(void) ao_arch_naked_define
329 {
330         ao_arch_save_regs();
331
332 #if HAS_TASK_QUEUE
333         if (ao_cur_task == NULL)
334                 ao_cur_task = ao_tasks[ao_num_tasks-1];
335 #else
336         if (ao_cur_task_index == AO_NO_TASK_INDEX)
337                 ao_cur_task_index = ao_num_tasks-1;
338 #endif
339         else
340         {
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;
347 #endif
348                 ao_arch_save_stack();
349         }
350
351         ao_arch_isr_stack();
352 #if !HAS_TASK_QUEUE
353         if (ao_task_minimize_latency)
354                 ao_arch_release_interrupts();
355         else
356 #endif
357                 ao_arch_block_interrupts();
358
359 #if AO_CHECK_STACK
360         in_yield = 1;
361 #endif
362         /* Find a task to run. If there isn't any runnable task,
363          * this loop will run forever, which is just fine
364          */
365 #if HAS_TASK_QUEUE
366         /* If the current task is running, move it to the
367          * end of the queue to allow other tasks a chance
368          */
369         if (ao_cur_task->wchan == NULL)
370                 ao_task_to_run_queue(ao_cur_task);
371         for (;;) {
372                 ao_arch_memory_barrier();
373                 if (!ao_list_is_empty(&run_queue))
374                         break;
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();
379                 } else
380                         ao_arch_wait_interrupt();
381         }
382         ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue);
383 #else
384         {
385                 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
386                 for (;;) {
387                         ++ao_cur_task_index;
388                         if (ao_cur_task_index == ao_num_tasks)
389                                 ao_cur_task_index = 0;
390
391                         ao_cur_task = ao_tasks[ao_cur_task_index];
392
393                         /* Check for ready task */
394                         if (ao_cur_task->wchan == NULL)
395                                 break;
396
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)
400                                 break;
401
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();
405                 }
406         }
407 #endif
408 #if HAS_SAMPLE_PROFILE
409         ao_cur_task->start = ao_sample_profile_timer_value();
410 #endif
411 #if HAS_STACK_GUARD
412         ao_mpu_stack_guard(ao_cur_task->stack);
413 #endif
414 #if AO_CHECK_STACK
415         in_yield = 0;
416 #endif
417         ao_arch_restore_stack();
418 }
419
420 uint8_t
421 ao_sleep(__xdata void *wchan)
422 {
423 #if HAS_TASK_QUEUE
424         uint32_t flags;
425         flags = ao_arch_irqsave();
426 #endif
427         ao_cur_task->wchan = wchan;
428 #if HAS_TASK_QUEUE
429         ao_task_to_sleep_queue(ao_cur_task, wchan);
430         ao_arch_irqrestore(flags);
431 #endif
432         ao_yield();
433         if (ao_cur_task->wchan) {
434                 ao_cur_task->wchan = NULL;
435                 ao_cur_task->alarm = 0;
436                 return 1;
437         }
438         return 0;
439 }
440
441 void
442 ao_wakeup(__xdata void *wchan) __reentrant
443 {
444         ao_validate_cur_stack();
445 #if HAS_TASK_QUEUE
446         struct ao_task  *sleep, *next;
447         struct ao_list  *sleep_queue;
448         uint32_t        flags;
449
450         if (ao_num_tasks == 0)
451                 return;
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) {
456                         sleep->wchan = NULL;
457                         ao_task_to_run_queue(sleep);
458                 }
459         }
460         ao_arch_irqrestore(flags);
461 #else
462         {
463         uint8_t i;
464         for (i = 0; i < ao_num_tasks; i++)
465                 if (ao_tasks[i]->wchan == wchan)
466                         ao_tasks[i]->wchan = NULL;
467         }
468 #endif
469         ao_check_stack();
470 }
471
472 uint8_t
473 ao_sleep_for(__xdata void *wchan, uint16_t timeout)
474 {
475         uint8_t ret;
476         if (timeout) {
477 #if HAS_TASK_QUEUE
478                 uint32_t flags;
479                 flags = ao_arch_irqsave();
480 #endif
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
483                  */
484                 if (!(ao_cur_task->alarm = ao_time() + timeout + 1))
485                         ao_cur_task->alarm = 1;
486 #if HAS_TASK_QUEUE
487                 ao_task_to_alarm_queue(ao_cur_task);
488                 ao_arch_irqrestore(flags);
489 #endif
490         }
491         ret = ao_sleep(wchan);
492         if (timeout) {
493 #if HAS_TASK_QUEUE
494                 uint32_t flags;
495
496                 flags = ao_arch_irqsave();
497 #endif
498                 ao_cur_task->alarm = 0;
499 #if HAS_TASK_QUEUE
500                 ao_task_from_alarm_queue(ao_cur_task);
501                 ao_arch_irqrestore(flags);
502 #endif
503         }
504         return ret;
505 }
506
507 static __xdata uint8_t ao_forever;
508
509 void
510 ao_delay(uint16_t ticks)
511 {
512         if (!ticks)
513                 ticks = 1;
514         ao_sleep_for(&ao_forever, ticks);
515 }
516
517 void
518 ao_exit(void)
519 {
520         uint8_t i;
521         ao_arch_block_interrupts();
522         ao_num_tasks--;
523 #if HAS_TASK_QUEUE
524         for (i = 0; i < ao_num_tasks; i++)
525                 if (ao_tasks[i] == ao_cur_task)
526                         break;
527         ao_task_exit_queue(ao_cur_task);
528 #else
529         i = ao_cur_task_index;
530         ao_cur_task_index = AO_NO_TASK_INDEX;
531 #endif
532         for (; i < ao_num_tasks; i++)
533                 ao_tasks[i] = ao_tasks[i+1];
534         ao_cur_task = NULL;
535         ao_yield();
536         /* we'll never get back here */
537 }
538
539 #if HAS_TASK_INFO
540 void
541 ao_task_info(void)
542 {
543         uint8_t         i;
544         __xdata struct ao_task *task;
545         uint16_t        now = ao_time();
546
547         for (i = 0; i < ao_num_tasks; i++) {
548                 task = ao_tasks[i];
549                 printf("%2d: wchan %08x alarm %5d %s\n",
550                        task->task_id,
551                        (int) task->wchan,
552                        task->alarm ? (int16_t) (task->alarm - now) : 9999,
553                        task->name);
554         }
555 #if HAS_TASK_QUEUE && DEBUG
556         ao_task_validate();
557 #endif
558 }
559 #endif
560
561 void
562 ao_start_scheduler(void)
563 {
564 #if !HAS_TASK_QUEUE
565         ao_cur_task_index = AO_NO_TASK_INDEX;
566 #endif
567         ao_cur_task = NULL;
568 #if HAS_ARCH_START_SCHEDULER
569         ao_arch_start_scheduler();
570 #endif
571         ao_yield();
572 }