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