1 | /* |
---|
2 | * Thread Handler |
---|
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 found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <rtems/system.h> |
---|
16 | #include <rtems/score/apiext.h> |
---|
17 | #include <rtems/score/context.h> |
---|
18 | #include <rtems/score/interr.h> |
---|
19 | #include <rtems/score/isr.h> |
---|
20 | #include <rtems/score/object.h> |
---|
21 | #include <rtems/score/priority.h> |
---|
22 | #include <rtems/score/states.h> |
---|
23 | #include <rtems/score/sysstate.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | #include <rtems/score/threadq.h> |
---|
26 | #include <rtems/score/userext.h> |
---|
27 | #include <rtems/score/wkspace.h> |
---|
28 | |
---|
29 | /*PAGE |
---|
30 | * |
---|
31 | * _Thread_Initialize |
---|
32 | * |
---|
33 | * This routine initializes the specified the thread. It allocates |
---|
34 | * all memory associated with this thread. It completes by adding |
---|
35 | * the thread to the local object table so operations on this |
---|
36 | * thread id are allowed. |
---|
37 | */ |
---|
38 | |
---|
39 | boolean _Thread_Initialize( |
---|
40 | Objects_Information *information, |
---|
41 | Thread_Control *the_thread, |
---|
42 | void *stack_area, |
---|
43 | uint32_t stack_size, |
---|
44 | boolean is_fp, |
---|
45 | Priority_Control priority, |
---|
46 | boolean is_preemptible, |
---|
47 | Thread_CPU_budget_algorithms budget_algorithm, |
---|
48 | Thread_CPU_budget_algorithm_callout budget_callout, |
---|
49 | uint32_t isr_level, |
---|
50 | Objects_Name name |
---|
51 | ) |
---|
52 | { |
---|
53 | uint32_t actual_stack_size = 0; |
---|
54 | void *stack = NULL; |
---|
55 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
56 | void *fp_area; |
---|
57 | #endif |
---|
58 | void *extensions_area; |
---|
59 | |
---|
60 | /* |
---|
61 | * Initialize the Ada self pointer |
---|
62 | */ |
---|
63 | |
---|
64 | the_thread->rtems_ada_self = NULL; |
---|
65 | |
---|
66 | /* |
---|
67 | * Allocate and Initialize the stack for this thread. |
---|
68 | */ |
---|
69 | |
---|
70 | |
---|
71 | if ( !stack_area ) { |
---|
72 | if ( !_Stack_Is_enough( stack_size ) ) |
---|
73 | actual_stack_size = STACK_MINIMUM_SIZE; |
---|
74 | else |
---|
75 | actual_stack_size = stack_size; |
---|
76 | |
---|
77 | actual_stack_size = _Thread_Stack_Allocate( the_thread, actual_stack_size ); |
---|
78 | |
---|
79 | if ( !actual_stack_size ) |
---|
80 | return FALSE; /* stack allocation failed */ |
---|
81 | |
---|
82 | stack = the_thread->Start.stack; |
---|
83 | the_thread->Start.core_allocated_stack = TRUE; |
---|
84 | } else { |
---|
85 | stack = stack_area; |
---|
86 | actual_stack_size = stack_size; |
---|
87 | the_thread->Start.core_allocated_stack = FALSE; |
---|
88 | } |
---|
89 | |
---|
90 | _Stack_Initialize( |
---|
91 | &the_thread->Start.Initial_stack, |
---|
92 | stack, |
---|
93 | actual_stack_size |
---|
94 | ); |
---|
95 | |
---|
96 | /* |
---|
97 | * Allocate the floating point area for this thread |
---|
98 | */ |
---|
99 | |
---|
100 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
101 | if ( is_fp ) { |
---|
102 | |
---|
103 | fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE ); |
---|
104 | if ( !fp_area ) { |
---|
105 | _Thread_Stack_Free( the_thread ); |
---|
106 | return FALSE; |
---|
107 | } |
---|
108 | fp_area = _Context_Fp_start( fp_area, 0 ); |
---|
109 | |
---|
110 | } else |
---|
111 | fp_area = NULL; |
---|
112 | |
---|
113 | the_thread->fp_context = fp_area; |
---|
114 | the_thread->Start.fp_context = fp_area; |
---|
115 | #endif |
---|
116 | |
---|
117 | /* |
---|
118 | * Clear the libc reent hook. |
---|
119 | */ |
---|
120 | |
---|
121 | the_thread->libc_reent = NULL; |
---|
122 | |
---|
123 | /* |
---|
124 | * Allocate the extensions area for this thread |
---|
125 | */ |
---|
126 | |
---|
127 | if ( _Thread_Maximum_extensions ) { |
---|
128 | extensions_area = _Workspace_Allocate( |
---|
129 | (_Thread_Maximum_extensions + 1) * sizeof( void * ) |
---|
130 | ); |
---|
131 | |
---|
132 | if ( !extensions_area ) { |
---|
133 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
134 | if ( fp_area ) |
---|
135 | (void) _Workspace_Free( fp_area ); |
---|
136 | #endif |
---|
137 | |
---|
138 | _Thread_Stack_Free( the_thread ); |
---|
139 | |
---|
140 | return FALSE; |
---|
141 | } |
---|
142 | } else |
---|
143 | extensions_area = NULL; |
---|
144 | |
---|
145 | the_thread->extensions = (void **) extensions_area; |
---|
146 | |
---|
147 | /* |
---|
148 | * Clear the extensions area so extension users can determine |
---|
149 | * if they are linked to the thread. An extension user may |
---|
150 | * create the extension long after tasks have been created |
---|
151 | * so they cannot rely on the thread create user extension |
---|
152 | * call. |
---|
153 | */ |
---|
154 | |
---|
155 | if ( the_thread->extensions ) { |
---|
156 | int i; |
---|
157 | for ( i = 0; i < (_Thread_Maximum_extensions + 1); i++ ) |
---|
158 | the_thread->extensions[i] = NULL; |
---|
159 | } |
---|
160 | |
---|
161 | /* |
---|
162 | * General initialization |
---|
163 | */ |
---|
164 | |
---|
165 | the_thread->Start.is_preemptible = is_preemptible; |
---|
166 | the_thread->Start.budget_algorithm = budget_algorithm; |
---|
167 | the_thread->Start.budget_callout = budget_callout; |
---|
168 | |
---|
169 | switch ( budget_algorithm ) { |
---|
170 | case THREAD_CPU_BUDGET_ALGORITHM_NONE: |
---|
171 | case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE: |
---|
172 | break; |
---|
173 | case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE: |
---|
174 | the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
175 | break; |
---|
176 | case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT: |
---|
177 | break; |
---|
178 | } |
---|
179 | |
---|
180 | the_thread->Start.isr_level = isr_level; |
---|
181 | |
---|
182 | the_thread->current_state = STATES_DORMANT; |
---|
183 | the_thread->resource_count = 0; |
---|
184 | the_thread->suspend_count = 0; |
---|
185 | the_thread->real_priority = priority; |
---|
186 | the_thread->Start.initial_priority = priority; |
---|
187 | the_thread->ticks_executed = 0; |
---|
188 | |
---|
189 | _Thread_Set_priority( the_thread, priority ); |
---|
190 | |
---|
191 | /* |
---|
192 | * Open the object |
---|
193 | */ |
---|
194 | |
---|
195 | _Objects_Open( information, &the_thread->Object, name ); |
---|
196 | |
---|
197 | /* |
---|
198 | * Invoke create extensions |
---|
199 | */ |
---|
200 | |
---|
201 | if ( !_User_extensions_Thread_create( the_thread ) ) { |
---|
202 | |
---|
203 | if ( extensions_area ) |
---|
204 | (void) _Workspace_Free( extensions_area ); |
---|
205 | |
---|
206 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
207 | if ( fp_area ) |
---|
208 | (void) _Workspace_Free( fp_area ); |
---|
209 | #endif |
---|
210 | |
---|
211 | _Thread_Stack_Free( the_thread ); |
---|
212 | |
---|
213 | return FALSE; |
---|
214 | } |
---|
215 | |
---|
216 | return TRUE; |
---|
217 | |
---|
218 | } |
---|