Switch from GPLv2 to GPLv2+
[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 /* An AltOS task */
30 struct ao_task {
31         __xdata void *wchan;            /* current wait channel (NULL if running) */
32         uint16_t alarm;                 /* abort ao_sleep time */
33         ao_arch_task_members            /* any architecture-specific fields */
34         uint8_t task_id;                /* unique id */
35         __code char *name;              /* task name */
36 #if HAS_TASK_QUEUE
37         struct ao_list  queue;
38         struct ao_list  alarm_queue;
39 #endif
40         uint8_t stack[AO_STACK_SIZE];   /* saved stack */
41 #if HAS_SAMPLE_PROFILE
42         uint32_t ticks;
43         uint32_t yields;
44         uint16_t start;
45         uint16_t max_run;
46 #endif
47 };
48
49 #ifndef AO_NUM_TASKS
50 #define AO_NUM_TASKS            16      /* maximum number of tasks */
51 #endif
52
53 #define AO_NO_TASK              0       /* no task id */
54
55 extern __xdata struct ao_task * __xdata ao_tasks[AO_NUM_TASKS];
56 extern __data uint8_t ao_num_tasks;
57 extern __xdata struct ao_task *__data ao_cur_task;
58 extern __data uint8_t ao_task_minimize_latency; /* Reduce IRQ latency */
59
60 #ifndef HAS_ARCH_VALIDATE_CUR_STACK
61 #define ao_validate_cur_stack()
62 #endif
63
64 /*
65  ao_task.c
66  */
67
68 /* Suspend the current task until wchan is awoken.
69  * returns:
70  *  0 on normal wake
71  *  1 on alarm
72  */
73 uint8_t
74 ao_sleep(__xdata void *wchan);
75
76 /* Suspend the current task until wchan is awoken or the timeout
77  * expires. returns:
78  *  0 on normal wake
79  *  1 on alarm
80  */
81 uint8_t
82 ao_sleep_for(__xdata void *wchan, uint16_t timeout);
83
84 /* Wake all tasks sleeping on wchan */
85 void
86 ao_wakeup(__xdata void *wchan) __reentrant;
87
88 #if 0
89 /* set an alarm to go off in 'delay' ticks */
90 void
91 ao_alarm(uint16_t delay);
92
93 /* Clear any pending alarm */
94 void
95 ao_clear_alarm(void);
96 #endif
97
98 /* Yield the processor to another task */
99 void
100 ao_yield(void) ao_arch_naked_declare;
101
102 /* Add a task to the run queue */
103 void
104 ao_add_task(__xdata struct ao_task * task, void (*start)(void), __code char *name) __reentrant;
105
106 #if HAS_TASK_QUEUE
107 /* Called on timer interrupt to check alarms */
108 extern uint16_t ao_task_alarm_tick;
109 void
110 ao_task_check_alarm(uint16_t tick);
111 #endif
112
113 /* Terminate the current task */
114 void
115 ao_exit(void);
116
117 /* Dump task info to console */
118 void
119 ao_task_info(void);
120
121 /* Start the scheduler. This will not return */
122 void
123 ao_start_scheduler(void);
124
125 #if HAS_TASK_QUEUE
126 void
127 ao_task_init(void);
128 #else
129 #define ao_task_init()
130 #endif
131
132 #endif