1 | /* |
---|
2 | * RTEMS Task Manager |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-1999. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.OARcorp.com/rtems/license.html. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <rtems/system.h> |
---|
16 | #include <rtems/rtems/status.h> |
---|
17 | #include <rtems/rtems/support.h> |
---|
18 | #include <rtems/rtems/modes.h> |
---|
19 | #include <rtems/score/object.h> |
---|
20 | #include <rtems/score/stack.h> |
---|
21 | #include <rtems/score/states.h> |
---|
22 | #include <rtems/rtems/tasks.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/score/threadq.h> |
---|
25 | #include <rtems/score/tod.h> |
---|
26 | #include <rtems/score/userext.h> |
---|
27 | #include <rtems/score/wkspace.h> |
---|
28 | #include <rtems/score/apiext.h> |
---|
29 | #include <rtems/score/sysstate.h> |
---|
30 | |
---|
31 | /*PAGE |
---|
32 | * |
---|
33 | * _RTEMS_tasks_Create_extension |
---|
34 | * |
---|
35 | * This routine is an extension routine that is invoked as part |
---|
36 | * of creating any type of task or thread in the system. If the |
---|
37 | * task is created via another API, then this routine is invoked |
---|
38 | * and this API given the opportunity to initialize its extension |
---|
39 | * area. |
---|
40 | */ |
---|
41 | |
---|
42 | boolean _RTEMS_tasks_Create_extension( |
---|
43 | Thread_Control *executing, |
---|
44 | Thread_Control *created |
---|
45 | ) |
---|
46 | { |
---|
47 | RTEMS_API_Control *api; |
---|
48 | |
---|
49 | api = _Workspace_Allocate( sizeof( RTEMS_API_Control ) ); |
---|
50 | |
---|
51 | if ( !api ) |
---|
52 | return FALSE; |
---|
53 | |
---|
54 | created->API_Extensions[ THREAD_API_RTEMS ] = api; |
---|
55 | |
---|
56 | api->pending_events = EVENT_SETS_NONE_PENDING; |
---|
57 | _ASR_Initialize( &api->Signal ); |
---|
58 | created->task_variables = NULL; |
---|
59 | return TRUE; |
---|
60 | } |
---|
61 | |
---|
62 | /*PAGE |
---|
63 | * |
---|
64 | * _RTEMS_tasks_Start_extension |
---|
65 | * |
---|
66 | * This extension routine is invoked when a task is started for the |
---|
67 | * first time. |
---|
68 | */ |
---|
69 | |
---|
70 | User_extensions_routine _RTEMS_tasks_Start_extension( |
---|
71 | Thread_Control *executing, |
---|
72 | Thread_Control *started |
---|
73 | ) |
---|
74 | { |
---|
75 | RTEMS_API_Control *api; |
---|
76 | |
---|
77 | api = started->API_Extensions[ THREAD_API_RTEMS ]; |
---|
78 | |
---|
79 | api->pending_events = EVENT_SETS_NONE_PENDING; |
---|
80 | |
---|
81 | _ASR_Initialize( &api->Signal ); |
---|
82 | } |
---|
83 | |
---|
84 | /*PAGE |
---|
85 | * |
---|
86 | * _RTEMS_tasks_Delete_extension |
---|
87 | * |
---|
88 | * This extension routine is invoked when a task is deleted. |
---|
89 | */ |
---|
90 | |
---|
91 | User_extensions_routine _RTEMS_tasks_Delete_extension( |
---|
92 | Thread_Control *executing, |
---|
93 | Thread_Control *deleted |
---|
94 | ) |
---|
95 | { |
---|
96 | rtems_task_variable_t *tvp, *next; |
---|
97 | |
---|
98 | /* |
---|
99 | * Free per task variable memory |
---|
100 | */ |
---|
101 | |
---|
102 | tvp = deleted->task_variables; |
---|
103 | deleted->task_variables = NULL; |
---|
104 | while (tvp) { |
---|
105 | next = tvp->next; |
---|
106 | if (tvp->dtor) |
---|
107 | (*tvp->dtor)( tvp->ptr ); |
---|
108 | if (executing == deleted) |
---|
109 | *tvp->ptr = tvp->gval; |
---|
110 | _Workspace_Free( tvp ); |
---|
111 | tvp = next; |
---|
112 | } |
---|
113 | |
---|
114 | /* |
---|
115 | * Free API specific memory |
---|
116 | */ |
---|
117 | |
---|
118 | (void) _Workspace_Free( deleted->API_Extensions[ THREAD_API_RTEMS ] ); |
---|
119 | deleted->API_Extensions[ THREAD_API_RTEMS ] = NULL; |
---|
120 | } |
---|
121 | |
---|
122 | /*PAGE |
---|
123 | * |
---|
124 | * _RTEMS_tasks_Switch_extension |
---|
125 | * |
---|
126 | * This extension routine is invoked at each context switch. |
---|
127 | */ |
---|
128 | |
---|
129 | void _RTEMS_tasks_Switch_extension( |
---|
130 | Thread_Control *executing, |
---|
131 | Thread_Control *heir |
---|
132 | ) |
---|
133 | { |
---|
134 | rtems_task_variable_t *tvp; |
---|
135 | |
---|
136 | /* |
---|
137 | * Per Task Variables |
---|
138 | */ |
---|
139 | |
---|
140 | tvp = executing->task_variables; |
---|
141 | while (tvp) { |
---|
142 | tvp->tval = *tvp->ptr; |
---|
143 | *tvp->ptr = tvp->gval; |
---|
144 | tvp = tvp->next; |
---|
145 | } |
---|
146 | |
---|
147 | tvp = heir->task_variables; |
---|
148 | while (tvp) { |
---|
149 | tvp->gval = *tvp->ptr; |
---|
150 | *tvp->ptr = tvp->tval; |
---|
151 | tvp = tvp->next; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /*PAGE |
---|
156 | * |
---|
157 | * _RTEMS_tasks_Post_switch_extension |
---|
158 | * |
---|
159 | * This extension routine is invoked at each context switch. |
---|
160 | */ |
---|
161 | |
---|
162 | void _RTEMS_tasks_Post_switch_extension( |
---|
163 | Thread_Control *executing |
---|
164 | ) |
---|
165 | { |
---|
166 | ISR_Level level; |
---|
167 | RTEMS_API_Control *api; |
---|
168 | ASR_Information *asr; |
---|
169 | rtems_signal_set signal_set; |
---|
170 | Modes_Control prev_mode; |
---|
171 | |
---|
172 | api = executing->API_Extensions[ THREAD_API_RTEMS ]; |
---|
173 | |
---|
174 | /* |
---|
175 | * Signal Processing |
---|
176 | */ |
---|
177 | |
---|
178 | asr = &api->Signal; |
---|
179 | |
---|
180 | _ISR_Disable( level ); |
---|
181 | signal_set = asr->signals_posted; |
---|
182 | asr->signals_posted = 0; |
---|
183 | _ISR_Enable( level ); |
---|
184 | |
---|
185 | |
---|
186 | if ( !signal_set ) /* similar to _ASR_Are_signals_pending( asr ) */ |
---|
187 | return; |
---|
188 | |
---|
189 | asr->nest_level += 1; |
---|
190 | rtems_task_mode( asr->mode_set, RTEMS_ALL_MODE_MASKS, &prev_mode ); |
---|
191 | |
---|
192 | (*asr->handler)( signal_set ); |
---|
193 | |
---|
194 | asr->nest_level -= 1; |
---|
195 | rtems_task_mode( prev_mode, RTEMS_ALL_MODE_MASKS, &prev_mode ); |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | API_extensions_Control _RTEMS_tasks_API_extensions = { |
---|
200 | { NULL, NULL }, |
---|
201 | NULL, /* predriver */ |
---|
202 | _RTEMS_tasks_Initialize_user_tasks, /* postdriver */ |
---|
203 | _RTEMS_tasks_Post_switch_extension /* post switch */ |
---|
204 | }; |
---|
205 | |
---|
206 | User_extensions_Control _RTEMS_tasks_User_extensions = { |
---|
207 | { NULL, NULL }, |
---|
208 | { _RTEMS_tasks_Create_extension, /* create */ |
---|
209 | _RTEMS_tasks_Start_extension, /* start */ |
---|
210 | _RTEMS_tasks_Start_extension, /* restart */ |
---|
211 | _RTEMS_tasks_Delete_extension, /* delete */ |
---|
212 | _RTEMS_tasks_Switch_extension, /* switch */ |
---|
213 | NULL, /* begin */ |
---|
214 | NULL, /* exitted */ |
---|
215 | NULL /* fatal */ |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | /*PAGE |
---|
220 | * |
---|
221 | * _RTEMS_tasks_Manager_initialization |
---|
222 | * |
---|
223 | * This routine initializes all Task Manager related data structures. |
---|
224 | * |
---|
225 | * Input parameters: |
---|
226 | * maximum_tasks - number of tasks to initialize |
---|
227 | * |
---|
228 | * Output parameters: NONE |
---|
229 | */ |
---|
230 | |
---|
231 | void _RTEMS_tasks_Manager_initialization( |
---|
232 | unsigned32 maximum_tasks, |
---|
233 | unsigned32 number_of_initialization_tasks, |
---|
234 | rtems_initialization_tasks_table *user_tasks |
---|
235 | ) |
---|
236 | { |
---|
237 | |
---|
238 | _RTEMS_tasks_Number_of_initialization_tasks = number_of_initialization_tasks; |
---|
239 | _RTEMS_tasks_User_initialization_tasks = user_tasks; |
---|
240 | |
---|
241 | /* |
---|
242 | * There may not be any RTEMS initialization tasks configured. |
---|
243 | */ |
---|
244 | |
---|
245 | #if 0 |
---|
246 | if ( user_tasks == NULL || number_of_initialization_tasks == 0 ) |
---|
247 | _Internal_error_Occurred( INTERNAL_ERROR_RTEMS_API, TRUE, RTEMS_TOO_MANY ); |
---|
248 | #endif |
---|
249 | |
---|
250 | _Objects_Initialize_information( |
---|
251 | &_RTEMS_tasks_Information, |
---|
252 | OBJECTS_RTEMS_TASKS, |
---|
253 | TRUE, |
---|
254 | maximum_tasks, |
---|
255 | sizeof( Thread_Control ), |
---|
256 | FALSE, |
---|
257 | RTEMS_MAXIMUM_NAME_LENGTH, |
---|
258 | TRUE |
---|
259 | ); |
---|
260 | |
---|
261 | /* |
---|
262 | * Add all the extensions for this API |
---|
263 | */ |
---|
264 | |
---|
265 | _User_extensions_Add_API_set( &_RTEMS_tasks_User_extensions ); |
---|
266 | |
---|
267 | _API_extensions_Add( &_RTEMS_tasks_API_extensions ); |
---|
268 | |
---|
269 | /* |
---|
270 | * Register the MP Process Packet routine. |
---|
271 | */ |
---|
272 | |
---|
273 | #if defined(RTEMS_MULTIPROCESSING) |
---|
274 | _MPCI_Register_packet_processor( |
---|
275 | MP_PACKET_TASKS, |
---|
276 | _RTEMS_tasks_MP_Process_packet |
---|
277 | ); |
---|
278 | #endif |
---|
279 | |
---|
280 | } |
---|
281 | |
---|