1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Initialize Thread |
---|
5 | * |
---|
6 | * @ingroup ScoreThread |
---|
7 | */ |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2014. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/threadimpl.h> |
---|
22 | #include <rtems/score/resourceimpl.h> |
---|
23 | #include <rtems/score/schedulerimpl.h> |
---|
24 | #include <rtems/score/stackimpl.h> |
---|
25 | #include <rtems/score/tls.h> |
---|
26 | #include <rtems/score/userextimpl.h> |
---|
27 | #include <rtems/score/watchdogimpl.h> |
---|
28 | #include <rtems/score/wkspace.h> |
---|
29 | #include <rtems/score/cpusetimpl.h> |
---|
30 | #include <rtems/config.h> |
---|
31 | |
---|
32 | bool _Thread_Initialize( |
---|
33 | Thread_Information *information, |
---|
34 | Thread_Control *the_thread, |
---|
35 | const Scheduler_Control *scheduler, |
---|
36 | void *stack_area, |
---|
37 | size_t stack_size, |
---|
38 | bool is_fp, |
---|
39 | Priority_Control priority, |
---|
40 | bool is_preemptible, |
---|
41 | Thread_CPU_budget_algorithms budget_algorithm, |
---|
42 | Thread_CPU_budget_algorithm_callout budget_callout, |
---|
43 | uint32_t isr_level, |
---|
44 | Objects_Name name |
---|
45 | ) |
---|
46 | { |
---|
47 | uintptr_t tls_size = _TLS_Get_size(); |
---|
48 | size_t actual_stack_size = 0; |
---|
49 | void *stack = NULL; |
---|
50 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
51 | void *fp_area = NULL; |
---|
52 | #endif |
---|
53 | bool extension_status; |
---|
54 | size_t i; |
---|
55 | Scheduler_Node *scheduler_node; |
---|
56 | bool scheduler_node_initialized = false; |
---|
57 | Per_CPU_Control *cpu = _Per_CPU_Get_by_index( 0 ); |
---|
58 | |
---|
59 | #if defined( RTEMS_SMP ) |
---|
60 | if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) { |
---|
61 | return false; |
---|
62 | } |
---|
63 | #endif |
---|
64 | |
---|
65 | memset( |
---|
66 | &the_thread->current_state, |
---|
67 | 0, |
---|
68 | information->Objects.size - offsetof( Thread_Control, current_state ) |
---|
69 | ); |
---|
70 | |
---|
71 | for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) { |
---|
72 | const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ]; |
---|
73 | |
---|
74 | *(void **) ( (char *) the_thread + add_on->destination_offset ) = |
---|
75 | (char *) the_thread + add_on->source_offset; |
---|
76 | } |
---|
77 | |
---|
78 | /* |
---|
79 | * Allocate and Initialize the stack for this thread. |
---|
80 | */ |
---|
81 | #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API) |
---|
82 | actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size ); |
---|
83 | if ( !actual_stack_size || actual_stack_size < stack_size ) |
---|
84 | return false; /* stack allocation failed */ |
---|
85 | |
---|
86 | stack = the_thread->Start.stack; |
---|
87 | #else |
---|
88 | if ( !stack_area ) { |
---|
89 | actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size ); |
---|
90 | if ( !actual_stack_size || actual_stack_size < stack_size ) |
---|
91 | return false; /* stack allocation failed */ |
---|
92 | |
---|
93 | stack = the_thread->Start.stack; |
---|
94 | the_thread->Start.core_allocated_stack = true; |
---|
95 | } else { |
---|
96 | stack = stack_area; |
---|
97 | actual_stack_size = stack_size; |
---|
98 | the_thread->Start.core_allocated_stack = false; |
---|
99 | } |
---|
100 | #endif |
---|
101 | |
---|
102 | _Stack_Initialize( |
---|
103 | &the_thread->Start.Initial_stack, |
---|
104 | stack, |
---|
105 | actual_stack_size |
---|
106 | ); |
---|
107 | |
---|
108 | /* Thread-local storage (TLS) area allocation */ |
---|
109 | if ( tls_size > 0 ) { |
---|
110 | uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment ); |
---|
111 | uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align ); |
---|
112 | |
---|
113 | the_thread->Start.tls_area = |
---|
114 | _Workspace_Allocate_aligned( tls_alloc, tls_align ); |
---|
115 | |
---|
116 | if ( the_thread->Start.tls_area == NULL ) { |
---|
117 | goto failed; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /* |
---|
122 | * Allocate the floating point area for this thread |
---|
123 | */ |
---|
124 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
125 | if ( is_fp ) { |
---|
126 | fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE ); |
---|
127 | if ( !fp_area ) |
---|
128 | goto failed; |
---|
129 | fp_area = _Context_Fp_start( fp_area, 0 ); |
---|
130 | } |
---|
131 | the_thread->fp_context = fp_area; |
---|
132 | the_thread->Start.fp_context = fp_area; |
---|
133 | #endif |
---|
134 | |
---|
135 | /* |
---|
136 | * Get thread queue heads |
---|
137 | */ |
---|
138 | the_thread->Wait.spare_heads = _Freechain_Get( |
---|
139 | &information->Free_thread_queue_heads, |
---|
140 | _Workspace_Allocate, |
---|
141 | _Objects_Extend_size( &information->Objects ), |
---|
142 | THREAD_QUEUE_HEADS_SIZE( _Scheduler_Count ) |
---|
143 | ); |
---|
144 | if ( the_thread->Wait.spare_heads == NULL ) { |
---|
145 | goto failed; |
---|
146 | } |
---|
147 | _Thread_queue_Heads_initialize( the_thread->Wait.spare_heads ); |
---|
148 | |
---|
149 | /* |
---|
150 | * General initialization |
---|
151 | */ |
---|
152 | |
---|
153 | the_thread->is_fp = is_fp; |
---|
154 | the_thread->Start.isr_level = isr_level; |
---|
155 | the_thread->Start.is_preemptible = is_preemptible; |
---|
156 | the_thread->Start.budget_algorithm = budget_algorithm; |
---|
157 | the_thread->Start.budget_callout = budget_callout; |
---|
158 | |
---|
159 | _Thread_Timer_initialize( &the_thread->Timer, cpu ); |
---|
160 | |
---|
161 | switch ( budget_algorithm ) { |
---|
162 | case THREAD_CPU_BUDGET_ALGORITHM_NONE: |
---|
163 | case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE: |
---|
164 | break; |
---|
165 | #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE) |
---|
166 | case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE: |
---|
167 | the_thread->cpu_time_budget = |
---|
168 | rtems_configuration_get_ticks_per_timeslice(); |
---|
169 | break; |
---|
170 | #endif |
---|
171 | #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT) |
---|
172 | case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT: |
---|
173 | break; |
---|
174 | #endif |
---|
175 | } |
---|
176 | |
---|
177 | scheduler_node = the_thread->Scheduler.node; |
---|
178 | |
---|
179 | #if defined(RTEMS_SMP) |
---|
180 | RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state ); |
---|
181 | the_thread->Scheduler.own_control = scheduler; |
---|
182 | the_thread->Scheduler.control = scheduler; |
---|
183 | the_thread->Scheduler.own_node = scheduler_node; |
---|
184 | _Resource_Node_initialize( &the_thread->Resource_node ); |
---|
185 | _ISR_lock_Initialize( |
---|
186 | &the_thread->Wait.Lock.Default, |
---|
187 | "Thread Wait Default Lock" |
---|
188 | ); |
---|
189 | _Thread_queue_Gate_open( &the_thread->Wait.Lock.Tranquilizer ); |
---|
190 | _RBTree_Initialize_node( &the_thread->Wait.Link.Registry_node ); |
---|
191 | _SMP_lock_Stats_initialize( &the_thread->Potpourri_stats, "Thread Potpourri" ); |
---|
192 | #endif |
---|
193 | |
---|
194 | /* Initialize the CPU for the non-SMP schedulers */ |
---|
195 | _Thread_Set_CPU( the_thread, cpu ); |
---|
196 | |
---|
197 | _Thread_queue_Initialize( &the_thread->Join_queue ); |
---|
198 | |
---|
199 | the_thread->current_state = STATES_DORMANT; |
---|
200 | the_thread->Wait.operations = &_Thread_queue_Operations_default; |
---|
201 | the_thread->current_priority = priority; |
---|
202 | the_thread->real_priority = priority; |
---|
203 | the_thread->Start.initial_priority = priority; |
---|
204 | |
---|
205 | RTEMS_STATIC_ASSERT( THREAD_WAIT_FLAGS_INITIAL == 0, Wait_flags ); |
---|
206 | |
---|
207 | _Scheduler_Node_initialize( scheduler, scheduler_node, the_thread, priority ); |
---|
208 | scheduler_node_initialized = true; |
---|
209 | |
---|
210 | /* POSIX Keys */ |
---|
211 | _RBTree_Initialize_empty( &the_thread->Keys.Key_value_pairs ); |
---|
212 | _ISR_lock_Initialize( &the_thread->Keys.Lock, "POSIX Key Value Pairs" ); |
---|
213 | |
---|
214 | _Thread_Action_control_initialize( &the_thread->Post_switch_actions ); |
---|
215 | |
---|
216 | /* |
---|
217 | * Open the object |
---|
218 | */ |
---|
219 | _Objects_Open( &information->Objects, &the_thread->Object, name ); |
---|
220 | |
---|
221 | /* |
---|
222 | * We assume the Allocator Mutex is locked and dispatching is |
---|
223 | * enabled when we get here. We want to be able to run the |
---|
224 | * user extensions with dispatching enabled. The Allocator |
---|
225 | * Mutex provides sufficient protection to let the user extensions |
---|
226 | * run safely. |
---|
227 | */ |
---|
228 | extension_status = _User_extensions_Thread_create( the_thread ); |
---|
229 | if ( extension_status ) |
---|
230 | return true; |
---|
231 | |
---|
232 | failed: |
---|
233 | |
---|
234 | if ( scheduler_node_initialized ) { |
---|
235 | _Scheduler_Node_destroy( scheduler, scheduler_node ); |
---|
236 | } |
---|
237 | |
---|
238 | _Workspace_Free( the_thread->Start.tls_area ); |
---|
239 | |
---|
240 | _Freechain_Put( |
---|
241 | &information->Free_thread_queue_heads, |
---|
242 | the_thread->Wait.spare_heads |
---|
243 | ); |
---|
244 | |
---|
245 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
246 | _Workspace_Free( fp_area ); |
---|
247 | #endif |
---|
248 | |
---|
249 | _Thread_Stack_Free( the_thread ); |
---|
250 | return false; |
---|
251 | } |
---|