telegps-v1.0: Provide one log and append to it
[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         ao_cur_task = NULL;
359         for (;;) {
360                 ao_arch_memory_barrier();
361                 if (!ao_list_is_empty(&run_queue))
362                         break;
363                 /* Wait for interrupts when there's nothing ready */
364                 ao_arch_wait_interrupt();
365         }
366         ao_cur_task = ao_list_first_entry(&run_queue, struct ao_task, queue);
367 #else
368         {
369                 __pdata uint8_t ao_last_task_index = ao_cur_task_index;
370                 for (;;) {
371                         ++ao_cur_task_index;
372                         if (ao_cur_task_index == ao_num_tasks)
373                                 ao_cur_task_index = 0;
374
375                         ao_cur_task = ao_tasks[ao_cur_task_index];
376
377                         /* Check for ready task */
378                         if (ao_cur_task->wchan == NULL)
379                                 break;
380
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)
384                                 break;
385
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();
389                 }
390         }
391 #endif
392 #if HAS_SAMPLE_PROFILE
393         ao_cur_task->start = ao_sample_profile_timer_value();
394 #endif
395 #if HAS_STACK_GUARD
396         ao_mpu_stack_guard(ao_cur_task->stack);
397 #endif
398 #if AO_CHECK_STACK
399         in_yield = 0;
400 #endif
401         ao_arch_restore_stack();
402 }
403
404 uint8_t
405 ao_sleep(__xdata void *wchan)
406 {
407 #if HAS_TASK_QUEUE
408         uint32_t flags;
409         flags = ao_arch_irqsave();
410 #endif
411         ao_cur_task->wchan = wchan;
412 #if HAS_TASK_QUEUE
413         ao_task_to_sleep_queue(ao_cur_task, wchan);
414         ao_arch_irqrestore(flags);
415 #endif
416         ao_yield();
417         if (ao_cur_task->wchan) {
418                 ao_cur_task->wchan = NULL;
419                 ao_cur_task->alarm = 0;
420                 return 1;
421         }
422         return 0;
423 }
424
425 void
426 ao_wakeup(__xdata void *wchan) __reentrant
427 {
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         uint8_t i;
446         for (i = 0; i < ao_num_tasks; i++)
447                 if (ao_tasks[i]->wchan == wchan)
448                         ao_tasks[i]->wchan = NULL;
449 #endif
450         ao_check_stack();
451 }
452
453 void
454 ao_alarm(uint16_t delay)
455 {
456 #if HAS_TASK_QUEUE
457         uint32_t flags;
458         /* Make sure we sleep *at least* delay ticks, which means adding
459          * one to account for the fact that we may be close to the next tick
460          */
461         flags = ao_arch_irqsave();
462 #endif
463         if (!(ao_cur_task->alarm = ao_time() + delay + 1))
464                 ao_cur_task->alarm = 1;
465 #if HAS_TASK_QUEUE
466         ao_task_to_alarm_queue(ao_cur_task);
467         ao_arch_irqrestore(flags);
468 #endif
469 }
470
471 void
472 ao_clear_alarm(void)
473 {
474 #if HAS_TASK_QUEUE
475         uint32_t flags;
476
477         flags = ao_arch_irqsave();
478 #endif
479         ao_cur_task->alarm = 0;
480 #if HAS_TASK_QUEUE
481         ao_task_from_alarm_queue(ao_cur_task);
482         ao_arch_irqrestore(flags);
483 #endif
484 }
485
486 static __xdata uint8_t ao_forever;
487
488 void
489 ao_delay(uint16_t ticks)
490 {
491         ao_alarm(ticks);
492         ao_sleep(&ao_forever);
493         ao_clear_alarm();
494 }
495
496 void
497 ao_exit(void)
498 {
499         uint8_t i;
500         ao_arch_block_interrupts();
501         ao_num_tasks--;
502 #if HAS_TASK_QUEUE
503         for (i = 0; i < ao_num_tasks; i++)
504                 if (ao_tasks[i] == ao_cur_task)
505                         break;
506         ao_task_exit_queue(ao_cur_task);
507 #else
508         i = ao_cur_task_index;
509         ao_cur_task_index = AO_NO_TASK_INDEX;
510 #endif
511         for (; i < ao_num_tasks; i++)
512                 ao_tasks[i] = ao_tasks[i+1];
513         ao_cur_task = NULL;
514         ao_yield();
515         /* we'll never get back here */
516 }
517
518 #if HAS_TASK_INFO
519 void
520 ao_task_info(void)
521 {
522         uint8_t         i;
523         __xdata struct ao_task *task;
524
525         for (i = 0; i < ao_num_tasks; i++) {
526                 task = ao_tasks[i];
527                 printf("%12s: wchan %04x\n",
528                        task->name,
529                        (int) task->wchan);
530         }
531 #if HAS_TASK_QUEUE && DEBUG
532         ao_task_validate();
533 #endif
534 }
535 #endif
536
537 void
538 ao_start_scheduler(void)
539 {
540 #if !HAS_TASK_QUEUE
541         ao_cur_task_index = AO_NO_TASK_INDEX;
542 #endif
543         ao_cur_task = NULL;
544 #if HAS_ARCH_START_SCHEDULER
545         ao_arch_start_scheduler();
546 #endif
547         ao_yield();
548 }