[05df0a8] | 1 | /* |
---|
| 2 | * Thread Handler |
---|
| 3 | * |
---|
| 4 | * |
---|
[5088d97] | 5 | * COPYRIGHT (c) 1989-2008. |
---|
[05df0a8] | 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 |
---|
[dd687d97] | 10 | * http://www.rtems.com/license/LICENSE. |
---|
[05df0a8] | 11 | * |
---|
| 12 | * $Id$ |
---|
| 13 | */ |
---|
| 14 | |
---|
[a8eed23] | 15 | #if HAVE_CONFIG_H |
---|
| 16 | #include "config.h" |
---|
| 17 | #endif |
---|
| 18 | |
---|
[05df0a8] | 19 | #include <rtems/system.h> |
---|
| 20 | #include <rtems/score/apiext.h> |
---|
| 21 | #include <rtems/score/context.h> |
---|
| 22 | #include <rtems/score/interr.h> |
---|
| 23 | #include <rtems/score/isr.h> |
---|
| 24 | #include <rtems/score/object.h> |
---|
| 25 | #include <rtems/score/priority.h> |
---|
| 26 | #include <rtems/score/states.h> |
---|
| 27 | #include <rtems/score/sysstate.h> |
---|
| 28 | #include <rtems/score/thread.h> |
---|
| 29 | #include <rtems/score/threadq.h> |
---|
| 30 | #include <rtems/score/userext.h> |
---|
[bdffb59] | 31 | #include <rtems/score/watchdog.h> |
---|
[05df0a8] | 32 | #include <rtems/score/wkspace.h> |
---|
| 33 | |
---|
| 34 | /*PAGE |
---|
| 35 | * |
---|
| 36 | * _Thread_Initialize |
---|
| 37 | * |
---|
[0cb7cb9] | 38 | * This routine initializes the specified the thread. It allocates |
---|
| 39 | * all memory associated with this thread. It completes by adding |
---|
| 40 | * the thread to the local object table so operations on this |
---|
| 41 | * thread id are allowed. |
---|
[05df0a8] | 42 | */ |
---|
| 43 | |
---|
| 44 | boolean _Thread_Initialize( |
---|
| 45 | Objects_Information *information, |
---|
| 46 | Thread_Control *the_thread, |
---|
| 47 | void *stack_area, |
---|
[728a0bd3] | 48 | size_t stack_size, |
---|
[05df0a8] | 49 | boolean is_fp, |
---|
| 50 | Priority_Control priority, |
---|
| 51 | boolean is_preemptible, |
---|
| 52 | Thread_CPU_budget_algorithms budget_algorithm, |
---|
| 53 | Thread_CPU_budget_algorithm_callout budget_callout, |
---|
[3127180] | 54 | uint32_t isr_level, |
---|
[05df0a8] | 55 | Objects_Name name |
---|
| 56 | ) |
---|
| 57 | { |
---|
[728a0bd3] | 58 | size_t actual_stack_size = 0; |
---|
[05df0a8] | 59 | void *stack = NULL; |
---|
[ac5c8c7] | 60 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
[05df0a8] | 61 | void *fp_area; |
---|
[ac5c8c7] | 62 | #endif |
---|
[05df0a8] | 63 | void *extensions_area; |
---|
[5088d97] | 64 | boolean extension_status; |
---|
[05df0a8] | 65 | |
---|
[81b329a] | 66 | #if __RTEMS_ADA__ |
---|
[05df0a8] | 67 | /* |
---|
| 68 | * Initialize the Ada self pointer |
---|
| 69 | */ |
---|
| 70 | |
---|
| 71 | the_thread->rtems_ada_self = NULL; |
---|
[81b329a] | 72 | #endif |
---|
[05df0a8] | 73 | |
---|
| 74 | /* |
---|
| 75 | * Allocate and Initialize the stack for this thread. |
---|
| 76 | */ |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | if ( !stack_area ) { |
---|
[ecf0f4c] | 80 | |
---|
| 81 | actual_stack_size = _Stack_Ensure_minimum( stack_size ); |
---|
[05df0a8] | 82 | |
---|
| 83 | actual_stack_size = _Thread_Stack_Allocate( the_thread, actual_stack_size ); |
---|
[05279b84] | 84 | |
---|
[8a4a349e] | 85 | if ( !actual_stack_size || actual_stack_size < stack_size ) |
---|
[05df0a8] | 86 | return FALSE; /* stack allocation failed */ |
---|
| 87 | |
---|
| 88 | stack = the_thread->Start.stack; |
---|
| 89 | the_thread->Start.core_allocated_stack = TRUE; |
---|
| 90 | } else { |
---|
| 91 | stack = stack_area; |
---|
| 92 | actual_stack_size = stack_size; |
---|
| 93 | the_thread->Start.core_allocated_stack = FALSE; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | _Stack_Initialize( |
---|
| 97 | &the_thread->Start.Initial_stack, |
---|
| 98 | stack, |
---|
| 99 | actual_stack_size |
---|
| 100 | ); |
---|
| 101 | |
---|
| 102 | /* |
---|
| 103 | * Allocate the floating point area for this thread |
---|
| 104 | */ |
---|
[05279b84] | 105 | |
---|
[ca7858bb] | 106 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
[05df0a8] | 107 | if ( is_fp ) { |
---|
| 108 | |
---|
| 109 | fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE ); |
---|
| 110 | if ( !fp_area ) { |
---|
| 111 | _Thread_Stack_Free( the_thread ); |
---|
| 112 | return FALSE; |
---|
| 113 | } |
---|
| 114 | fp_area = _Context_Fp_start( fp_area, 0 ); |
---|
| 115 | |
---|
| 116 | } else |
---|
| 117 | fp_area = NULL; |
---|
| 118 | |
---|
| 119 | the_thread->fp_context = fp_area; |
---|
| 120 | the_thread->Start.fp_context = fp_area; |
---|
[ca7858bb] | 121 | #endif |
---|
[05df0a8] | 122 | |
---|
[bdffb59] | 123 | /* |
---|
| 124 | * Initialize the thread timer |
---|
| 125 | */ |
---|
| 126 | _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL ); |
---|
| 127 | |
---|
[66a9239a] | 128 | #ifdef __RTEMS_STRICT_ORDER_MUTEX__ |
---|
[fd84982] | 129 | /*Initialize the head of chain of mutex */ |
---|
| 130 | _Chain_Initialize_empty(&the_thread->lock_mutex); |
---|
| 131 | #endif |
---|
| 132 | |
---|
[0df8293e] | 133 | /* |
---|
| 134 | * Clear the libc reent hook. |
---|
| 135 | */ |
---|
[05279b84] | 136 | |
---|
[0df8293e] | 137 | the_thread->libc_reent = NULL; |
---|
[05279b84] | 138 | |
---|
[05df0a8] | 139 | /* |
---|
| 140 | * Allocate the extensions area for this thread |
---|
| 141 | */ |
---|
| 142 | |
---|
| 143 | if ( _Thread_Maximum_extensions ) { |
---|
| 144 | extensions_area = _Workspace_Allocate( |
---|
| 145 | (_Thread_Maximum_extensions + 1) * sizeof( void * ) |
---|
| 146 | ); |
---|
| 147 | |
---|
| 148 | if ( !extensions_area ) { |
---|
[81f6e8cc] | 149 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
[05df0a8] | 150 | if ( fp_area ) |
---|
| 151 | (void) _Workspace_Free( fp_area ); |
---|
[81f6e8cc] | 152 | #endif |
---|
[05df0a8] | 153 | |
---|
| 154 | _Thread_Stack_Free( the_thread ); |
---|
| 155 | |
---|
| 156 | return FALSE; |
---|
| 157 | } |
---|
[a0ed4ed] | 158 | } else |
---|
[05df0a8] | 159 | extensions_area = NULL; |
---|
[05279b84] | 160 | |
---|
[05df0a8] | 161 | the_thread->extensions = (void **) extensions_area; |
---|
| 162 | |
---|
[f918e74] | 163 | /* |
---|
| 164 | * Clear the extensions area so extension users can determine |
---|
| 165 | * if they are linked to the thread. An extension user may |
---|
| 166 | * create the extension long after tasks have been created |
---|
| 167 | * so they cannot rely on the thread create user extension |
---|
| 168 | * call. |
---|
| 169 | */ |
---|
| 170 | |
---|
| 171 | if ( the_thread->extensions ) { |
---|
[aa4a3f1] | 172 | uint32_t i; |
---|
[f918e74] | 173 | for ( i = 0; i < (_Thread_Maximum_extensions + 1); i++ ) |
---|
| 174 | the_thread->extensions[i] = NULL; |
---|
| 175 | } |
---|
| 176 | |
---|
[05df0a8] | 177 | /* |
---|
| 178 | * General initialization |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | the_thread->Start.is_preemptible = is_preemptible; |
---|
| 182 | the_thread->Start.budget_algorithm = budget_algorithm; |
---|
| 183 | the_thread->Start.budget_callout = budget_callout; |
---|
[f94e76ba] | 184 | |
---|
| 185 | switch ( budget_algorithm ) { |
---|
| 186 | case THREAD_CPU_BUDGET_ALGORITHM_NONE: |
---|
| 187 | case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE: |
---|
| 188 | break; |
---|
| 189 | case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE: |
---|
| 190 | the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
| 191 | break; |
---|
| 192 | case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT: |
---|
| 193 | break; |
---|
| 194 | } |
---|
| 195 | |
---|
[c3330a8] | 196 | the_thread->Start.isr_level = isr_level; |
---|
| 197 | |
---|
| 198 | the_thread->current_state = STATES_DORMANT; |
---|
| 199 | the_thread->Wait.queue = NULL; |
---|
| 200 | the_thread->resource_count = 0; |
---|
| 201 | the_thread->suspend_count = 0; |
---|
| 202 | the_thread->real_priority = priority; |
---|
| 203 | the_thread->Start.initial_priority = priority; |
---|
| 204 | _Thread_Set_priority( the_thread, priority ); |
---|
[05df0a8] | 205 | |
---|
[c3330a8] | 206 | /* |
---|
| 207 | * Initialize the CPU usage statistics |
---|
| 208 | */ |
---|
[05279b84] | 209 | |
---|
[c3330a8] | 210 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
| 211 | the_thread->cpu_time_used.tv_sec = 0; |
---|
| 212 | the_thread->cpu_time_used.tv_nsec = 0; |
---|
| 213 | #else |
---|
[5fa5185] | 214 | the_thread->cpu_time_used = 0; |
---|
[c3330a8] | 215 | #endif |
---|
[05df0a8] | 216 | |
---|
| 217 | /* |
---|
| 218 | * Open the object |
---|
| 219 | */ |
---|
| 220 | |
---|
| 221 | _Objects_Open( information, &the_thread->Object, name ); |
---|
| 222 | |
---|
| 223 | /* |
---|
[5088d97] | 224 | * We assume the Allocator Mutex is locked and dispatching is |
---|
| 225 | * enabled when we get here. We want to be able to run the |
---|
| 226 | * user extensions with dispatching enabled. The Allocator |
---|
| 227 | * Mutex provides sufficient protection to let the user extensions |
---|
| 228 | * run safely. |
---|
[05df0a8] | 229 | */ |
---|
[5088d97] | 230 | extension_status = _User_extensions_Thread_create( the_thread ); |
---|
[05df0a8] | 231 | |
---|
[5088d97] | 232 | if ( !extension_status ) { |
---|
[05df0a8] | 233 | |
---|
| 234 | if ( extensions_area ) |
---|
| 235 | (void) _Workspace_Free( extensions_area ); |
---|
| 236 | |
---|
[81f6e8cc] | 237 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
[05df0a8] | 238 | if ( fp_area ) |
---|
| 239 | (void) _Workspace_Free( fp_area ); |
---|
[81f6e8cc] | 240 | #endif |
---|
[05df0a8] | 241 | |
---|
| 242 | _Thread_Stack_Free( the_thread ); |
---|
| 243 | |
---|
| 244 | return FALSE; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | return TRUE; |
---|
[05279b84] | 248 | |
---|
[05df0a8] | 249 | } |
---|