ffeb7313ba423af2c55cd052c6728ef4d48eb896
[fw/altos] / src / kernel / ao_task.h
1 /*
2  * Copyright © 2012 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 #ifndef _AO_TASK_H_
20 #define _AO_TASK_H_
21 #if HAS_TASK_QUEUE
22 #include <ao_list.h>
23 #endif
24
25 #ifndef HAS_TASK_INFO
26 #define HAS_TASK_INFO 1
27 #endif
28
29 /* arm stacks must be 32-bit aligned */
30 #ifdef __arm__
31 #define AO_STACK_ALIGNMENT __attribute__ ((aligned(4)))
32 #endif
33 #ifdef SDCC
34 #define AO_STACK_ALIGNMENT
35 #endif
36 #ifdef __AVR__
37 #define AO_STACK_ALIGNMENT
38 #endif
39
40 /* An AltOS task */
41 struct ao_task {
42         void *wchan;            /* current wait channel (NULL if running) */
43         uint16_t alarm;                 /* abort ao_sleep time */
44         ao_arch_task_members            /* any architecture-specific fields */
45         uint8_t task_id;                /* unique id */
46         const char *name;               /* task name */
47 #ifdef NEWLIB
48         int __errno;                    /* storage for errno in newlib libc */
49 #endif
50 #if HAS_TASK_QUEUE
51         struct ao_list  queue;
52         struct ao_list  alarm_queue;
53 #endif
54         uint8_t stack[AO_STACK_SIZE] AO_STACK_ALIGNMENT;        /* saved stack */
55 #if HAS_SAMPLE_PROFILE
56         uint32_t ticks;
57         uint32_t yields;
58         uint16_t start;
59         uint16_t max_run;
60 #endif
61 };
62
63 #ifndef AO_NUM_TASKS
64 #define AO_NUM_TASKS            16      /* maximum number of tasks */
65 #endif
66
67 #define AO_NO_TASK              0       /* no task id */
68
69 extern struct ao_task * ao_tasks[AO_NUM_TASKS];
70 extern uint8_t ao_num_tasks;
71 extern struct ao_task *ao_cur_task;
72 extern uint8_t ao_task_minimize_latency;        /* Reduce IRQ latency */
73
74 #ifndef HAS_ARCH_VALIDATE_CUR_STACK
75 #define ao_validate_cur_stack()
76 #endif
77
78 /*
79  ao_task.c
80  */
81
82 /* Suspend the current task until wchan is awoken.
83  * returns:
84  *  0 on normal wake
85  *  1 on alarm
86  */
87 uint8_t
88 ao_sleep(void *wchan);
89
90 /* Suspend the current task until wchan is awoken or the timeout
91  * expires. returns:
92  *  0 on normal wake
93  *  1 on alarm
94  */
95 uint8_t
96 ao_sleep_for(void *wchan, uint16_t timeout);
97
98 /* Wake all tasks sleeping on wchan */
99 void
100 ao_wakeup(void *wchan);
101
102 #if 0
103 /* set an alarm to go off in 'delay' ticks */
104 void
105 ao_alarm(uint16_t delay);
106
107 /* Clear any pending alarm */
108 void
109 ao_clear_alarm(void);
110 #endif
111
112 /* Yield the processor to another task */
113 void
114 ao_yield(void) ao_arch_naked_declare;
115
116 /* Add a task to the run queue */
117 void
118 ao_add_task(struct ao_task * task, void (*start)(void), const char *name);
119
120 #if HAS_TASK_QUEUE
121 /* Called on timer interrupt to check alarms */
122 extern uint16_t ao_task_alarm_tick;
123 void
124 ao_task_check_alarm(uint16_t tick);
125 #endif
126
127 /* Terminate the current task */
128 void
129 ao_exit(void);
130
131 /* Dump task info to console */
132 void
133 ao_task_info(void);
134
135 /* Start the scheduler. This will not return */
136 void
137 ao_start_scheduler(void);
138
139 #if HAS_TASK_QUEUE
140 void
141 ao_task_init(void);
142 #else
143 #define ao_task_init()
144 #endif
145
146 #endif