e8a092aaf660fcf6bbec48706e14d2f7936e297b
[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                 ao_arch_wait_interrupt();
377         }
378         ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue);
379 #else
380         {
381                 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
382                 for (;;) {
383                         ++ao_cur_task_index;
384                         if (ao_cur_task_index == ao_num_tasks)
385                                 ao_cur_task_index = 0;
386
387                         ao_cur_task = ao_tasks[ao_cur_task_index];
388
389                         /* Check for ready task */
390                         if (ao_cur_task->wchan == NULL)
391                                 break;
392
393                         /* Check if the alarm is set for a time which has passed */
394                         if (ao_cur_task->alarm &&
395                             (int16_t) (ao_time() - ao_cur_task->alarm) >= 0)
396                                 break;
397
398                         /* Wait for interrupts when there's nothing ready */
399                         if (ao_cur_task_index == ao_last_task_index && !ao_task_minimize_latency)
400                                 ao_arch_wait_interrupt();
401                 }
402         }
403 #endif
404 #if HAS_SAMPLE_PROFILE
405         ao_cur_task->start = ao_sample_profile_timer_value();
406 #endif
407 #if HAS_STACK_GUARD
408         ao_mpu_stack_guard(ao_cur_task->stack);
409 #endif
410 #if AO_CHECK_STACK
411         in_yield = 0;
412 #endif
413         ao_arch_restore_stack();
414 }
415
416 uint8_t
417 ao_sleep(__xdata void *wchan)
418 {
419 #if HAS_TASK_QUEUE
420         uint32_t flags;
421         flags = ao_arch_irqsave();
422 #endif
423         ao_cur_task->wchan = wchan;
424 #if HAS_TASK_QUEUE
425         ao_task_to_sleep_queue(ao_cur_task, wchan);
426         ao_arch_irqrestore(flags);
427 #endif
428         ao_yield();
429         if (ao_cur_task->wchan) {
430                 ao_cur_task->wchan = NULL;
431                 ao_cur_task->alarm = 0;
432                 return 1;
433         }
434         return 0;
435 }
436
437 void
438 ao_wakeup(__xdata void *wchan) __reentrant
439 {
440         ao_validate_cur_stack();
441 #if HAS_TASK_QUEUE
442         struct ao_task  *sleep, *next;
443         struct ao_list  *sleep_queue;
444         uint32_t        flags;
445
446         if (ao_num_tasks == 0)
447                 return;
448         sleep_queue = ao_task_sleep_queue(wchan);
449         flags = ao_arch_irqsave();
450         ao_list_for_each_entry_safe(sleep, next, sleep_queue, struct ao_task, queue) {
451                 if (sleep->wchan == wchan) {
452                         sleep->wchan = NULL;
453                         ao_task_to_run_queue(sleep);
454                 }
455         }
456         ao_arch_irqrestore(flags);
457 #else
458         {
459         uint8_t i;
460         for (i = 0; i < ao_num_tasks; i++)
461                 if (ao_tasks[i]->wchan == wchan)
462                         ao_tasks[i]->wchan = NULL;
463         }
464 #endif
465         ao_check_stack();
466 }
467
468 uint8_t
469 ao_sleep_for(__xdata void *wchan, uint16_t timeout)
470 {
471         uint8_t ret;
472         if (timeout) {
473 #if HAS_TASK_QUEUE
474                 uint32_t flags;
475                 flags = ao_arch_irqsave();
476 #endif
477                 /* Make sure we sleep *at least* delay ticks, which means adding
478                  * one to account for the fact that we may be close to the next tick
479                  */
480                 if (!(ao_cur_task->alarm = ao_time() + timeout + 1))
481                         ao_cur_task->alarm = 1;
482 #if HAS_TASK_QUEUE
483                 ao_task_to_alarm_queue(ao_cur_task);
484                 ao_arch_irqrestore(flags);
485 #endif
486         }
487         ret = ao_sleep(wchan);
488         if (timeout) {
489 #if HAS_TASK_QUEUE
490                 uint32_t flags;
491
492                 flags = ao_arch_irqsave();
493 #endif
494                 ao_cur_task->alarm = 0;
495 #if HAS_TASK_QUEUE
496                 ao_task_from_alarm_queue(ao_cur_task);
497                 ao_arch_irqrestore(flags);
498 #endif
499         }
500         return ret;
501 }
502
503 static __xdata uint8_t ao_forever;
504
505 void
506 ao_delay(uint16_t ticks)
507 {
508         if (!ticks)
509                 ticks = 1;
510         ao_sleep_for(&ao_forever, ticks);
511 }
512
513 void
514 ao_exit(void)
515 {
516         uint8_t i;
517         ao_arch_block_interrupts();
518         ao_num_tasks--;
519 #if HAS_TASK_QUEUE
520         for (i = 0; i < ao_num_tasks; i++)
521                 if (ao_tasks[i] == ao_cur_task)
522                         break;
523         ao_task_exit_queue(ao_cur_task);
524 #else
525         i = ao_cur_task_index;
526         ao_cur_task_index = AO_NO_TASK_INDEX;
527 #endif
528         for (; i < ao_num_tasks; i++)
529                 ao_tasks[i] = ao_tasks[i+1];
530         ao_cur_task = NULL;
531         ao_yield();
532         /* we'll never get back here */
533 }
534
535 #if HAS_TASK_INFO
536 void
537 ao_task_info(void)
538 {
539         uint8_t         i;
540         __xdata struct ao_task *task;
541         uint16_t        now = ao_time();
542
543         for (i = 0; i < ao_num_tasks; i++) {
544                 task = ao_tasks[i];
545                 printf("%2d: wchan %08x alarm %5d %s\n",
546                        task->task_id,
547                        (int) task->wchan,
548                        task->alarm ? (int16_t) (task->alarm - now) : 9999,
549                        task->name);
550         }
551 #if HAS_TASK_QUEUE && DEBUG
552         ao_task_validate();
553 #endif
554 }
555 #endif
556
557 void
558 ao_start_scheduler(void)
559 {
560 #if !HAS_TASK_QUEUE
561         ao_cur_task_index = AO_NO_TASK_INDEX;
562 #endif
563         ao_cur_task = NULL;
564 #if HAS_ARCH_START_SCHEDULER
565         ao_arch_start_scheduler();
566 #endif
567         ao_yield();
568 }