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