[8165b48f] | 1 | /* |
---|
[3bacb250] | 2 | * COPYRIGHT (c) 1989-2010. |
---|
[412dbff6] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | * |
---|
[eb5a7e07] | 9 | * $Id$ |
---|
[5e9b32b] | 10 | */ |
---|
| 11 | |
---|
[f42b726] | 12 | #if HAVE_CONFIG_H |
---|
| 13 | #include "config.h" |
---|
| 14 | #endif |
---|
| 15 | |
---|
[5e9b32b] | 16 | #include <errno.h> |
---|
| 17 | #include <pthread.h> |
---|
| 18 | #include <limits.h> |
---|
| 19 | |
---|
[f4719d5a] | 20 | #include <rtems/system.h> |
---|
[309e2f6] | 21 | #include <rtems/config.h> |
---|
[5a18e04] | 22 | #include <rtems/score/apiext.h> |
---|
[5e9b32b] | 23 | #include <rtems/score/stack.h> |
---|
| 24 | #include <rtems/score/thread.h> |
---|
[5a18e04] | 25 | #include <rtems/score/userext.h> |
---|
| 26 | #include <rtems/score/wkspace.h> |
---|
[2df1974] | 27 | #include <rtems/posix/cancel.h> |
---|
[5e9b32b] | 28 | #include <rtems/posix/pthread.h> |
---|
[ece8c9a] | 29 | #include <rtems/posix/priority.h> |
---|
[98ed15e] | 30 | #include <rtems/posix/psignal.h> |
---|
[5a18e04] | 31 | #include <rtems/posix/config.h> |
---|
[895efd9] | 32 | #include <rtems/posix/key.h> |
---|
[624133c] | 33 | #include <rtems/posix/time.h> |
---|
[c16bcc0] | 34 | #include <rtems/score/timespec.h> |
---|
[5e9b32b] | 35 | |
---|
[a1f0ca28] | 36 | /* |
---|
[5e9b32b] | 37 | * The default pthreads attributes structure. |
---|
[65c421f] | 38 | * |
---|
[874297f3] | 39 | * NOTE: Be careful .. if the default attribute set changes, |
---|
[65c421f] | 40 | * _POSIX_Threads_Initialize_user_threads will need to be examined. |
---|
[5e9b32b] | 41 | */ |
---|
| 42 | const pthread_attr_t _POSIX_Threads_Default_attributes = { |
---|
[b1dbfd7] | 43 | true, /* is_initialized */ |
---|
[0b710dbb] | 44 | NULL, /* stackaddr */ |
---|
[ecf0f4c] | 45 | 0, /* stacksize -- will be adjusted to minimum */ |
---|
[0b710dbb] | 46 | PTHREAD_SCOPE_PROCESS, /* contentionscope */ |
---|
[65c421f] | 47 | PTHREAD_INHERIT_SCHED, /* inheritsched */ |
---|
[0b710dbb] | 48 | SCHED_FIFO, /* schedpolicy */ |
---|
| 49 | { /* schedparam */ |
---|
| 50 | 2, /* sched_priority */ |
---|
[85e0a22] | 51 | #if defined(_POSIX_SPORADIC_SERVER) || \ |
---|
| 52 | defined(_POSIX_THREAD_SPORADIC_SERVER) |
---|
| 53 | 0, /* sched_ss_low_priority */ |
---|
| 54 | { 0L, 0 }, /* sched_ss_repl_period */ |
---|
[29aed671] | 55 | { 0L, 0 }, /* sched_ss_init_budget */ |
---|
| 56 | 0 /* sched_ss_max_repl */ |
---|
[85e0a22] | 57 | #endif |
---|
[5e9b32b] | 58 | }, |
---|
[21dd3ab8] | 59 | #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE |
---|
| 60 | 0, /* guardsize */ |
---|
| 61 | #endif |
---|
[85e0a22] | 62 | #if defined(_POSIX_THREAD_CPUTIME) |
---|
| 63 | 1, /* cputime_clock_allowed */ |
---|
| 64 | #endif |
---|
[0b710dbb] | 65 | PTHREAD_CREATE_JOINABLE, /* detachstate */ |
---|
[5e9b32b] | 66 | }; |
---|
| 67 | |
---|
[a1f0ca28] | 68 | /* |
---|
[65a5c1b] | 69 | * _POSIX_Threads_Sporadic_budget_TSR |
---|
| 70 | */ |
---|
| 71 | void _POSIX_Threads_Sporadic_budget_TSR( |
---|
[92f4671] | 72 | Objects_Id id __attribute__((unused)), |
---|
[65a5c1b] | 73 | void *argument |
---|
| 74 | ) |
---|
| 75 | { |
---|
[39cefdd] | 76 | uint32_t ticks; |
---|
| 77 | uint32_t new_priority; |
---|
[65a5c1b] | 78 | Thread_Control *the_thread; |
---|
| 79 | POSIX_API_Control *api; |
---|
| 80 | |
---|
| 81 | the_thread = argument; |
---|
| 82 | |
---|
| 83 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
| 84 | |
---|
[770db69] | 85 | /* ticks is guaranteed to be at least one */ |
---|
[3bacb250] | 86 | ticks = _Timespec_To_ticks( &api->schedparam.sched_ss_init_budget ); |
---|
[65a5c1b] | 87 | |
---|
[2014063] | 88 | the_thread->cpu_time_budget = ticks; |
---|
[874297f3] | 89 | |
---|
[3bacb250] | 90 | new_priority = _POSIX_Priority_To_core( api->schedparam.sched_priority ); |
---|
[2014063] | 91 | the_thread->real_priority = new_priority; |
---|
| 92 | |
---|
[a1f0ca28] | 93 | /* |
---|
| 94 | * If holding a resource, then do not change it. |
---|
| 95 | */ |
---|
| 96 | #if 0 |
---|
| 97 | printk( "TSR %d %d %d\n", the_thread->resource_count, |
---|
| 98 | the_thread->current_priority, new_priority ); |
---|
| 99 | #endif |
---|
| 100 | if ( the_thread->resource_count == 0 ) { |
---|
| 101 | /* |
---|
| 102 | * If this would make them less important, then do not change it. |
---|
| 103 | */ |
---|
| 104 | if ( the_thread->current_priority > new_priority ) { |
---|
| 105 | _Thread_Change_priority( the_thread, new_priority, true ); |
---|
| 106 | #if 0 |
---|
| 107 | printk( "raise priority\n" ); |
---|
| 108 | #endif |
---|
| 109 | } |
---|
| 110 | } |
---|
[2014063] | 111 | |
---|
[770db69] | 112 | /* ticks is guaranteed to be at least one */ |
---|
[3bacb250] | 113 | ticks = _Timespec_To_ticks( &api->schedparam.sched_ss_repl_period ); |
---|
[2014063] | 114 | |
---|
| 115 | _Watchdog_Insert_ticks( &api->Sporadic_timer, ticks ); |
---|
[65a5c1b] | 116 | } |
---|
| 117 | |
---|
[a1f0ca28] | 118 | /* |
---|
[65a5c1b] | 119 | * _POSIX_Threads_Sporadic_budget_callout |
---|
| 120 | */ |
---|
| 121 | void _POSIX_Threads_Sporadic_budget_callout( |
---|
| 122 | Thread_Control *the_thread |
---|
| 123 | ) |
---|
| 124 | { |
---|
[2014063] | 125 | POSIX_API_Control *api; |
---|
[39cefdd] | 126 | uint32_t new_priority; |
---|
[65a5c1b] | 127 | |
---|
[2014063] | 128 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
[65a5c1b] | 129 | |
---|
[2014063] | 130 | /* |
---|
| 131 | * This will prevent the thread from consuming its entire "budget" |
---|
| 132 | * while at low priority. |
---|
| 133 | */ |
---|
| 134 | the_thread->cpu_time_budget = 0xFFFFFFFF; /* XXX should be based on MAX_U32 */ |
---|
| 135 | |
---|
[3bacb250] | 136 | new_priority = _POSIX_Priority_To_core(api->schedparam.sched_ss_low_priority); |
---|
[2014063] | 137 | the_thread->real_priority = new_priority; |
---|
| 138 | |
---|
[a1f0ca28] | 139 | /* |
---|
| 140 | * If holding a resource, then do not change it. |
---|
| 141 | */ |
---|
| 142 | #if 0 |
---|
| 143 | printk( "callout %d %d %d\n", the_thread->resource_count, |
---|
| 144 | the_thread->current_priority, new_priority ); |
---|
| 145 | #endif |
---|
| 146 | if ( the_thread->resource_count == 0 ) { |
---|
| 147 | /* |
---|
| 148 | * Make sure we are actually lowering it. If they have lowered it |
---|
[3bacb250] | 149 | * to logically lower than sched_ss_low_priority, then we do not want to |
---|
[a1f0ca28] | 150 | * change it. |
---|
| 151 | */ |
---|
| 152 | if ( the_thread->current_priority < new_priority ) { |
---|
| 153 | _Thread_Change_priority( the_thread, new_priority, true ); |
---|
| 154 | #if 0 |
---|
| 155 | printk( "lower priority\n" ); |
---|
| 156 | #endif |
---|
| 157 | } |
---|
| 158 | } |
---|
[65a5c1b] | 159 | } |
---|
| 160 | |
---|
[a1f0ca28] | 161 | /* |
---|
[5a18e04] | 162 | * _POSIX_Threads_Create_extension |
---|
| 163 | * |
---|
[a1f0ca28] | 164 | * This method is invoked for each thread created. |
---|
[5a18e04] | 165 | */ |
---|
[874297f3] | 166 | |
---|
[29aed671] | 167 | static bool _POSIX_Threads_Create_extension( |
---|
[92f4671] | 168 | Thread_Control *executing __attribute__((unused)), |
---|
[5a18e04] | 169 | Thread_Control *created |
---|
| 170 | ) |
---|
| 171 | { |
---|
| 172 | POSIX_API_Control *api; |
---|
[65c421f] | 173 | POSIX_API_Control *executing_api; |
---|
[874297f3] | 174 | |
---|
[5a18e04] | 175 | api = _Workspace_Allocate( sizeof( POSIX_API_Control ) ); |
---|
[874297f3] | 176 | |
---|
[5a18e04] | 177 | if ( !api ) |
---|
[f8437c8] | 178 | return false; |
---|
[874297f3] | 179 | |
---|
[5a18e04] | 180 | created->API_Extensions[ THREAD_API_POSIX ] = api; |
---|
[874297f3] | 181 | |
---|
[b85649c4] | 182 | /* XXX check all fields are touched */ |
---|
[895efd9] | 183 | api->Attributes = _POSIX_Threads_Default_attributes; |
---|
| 184 | api->detachstate = _POSIX_Threads_Default_attributes.detachstate; |
---|
| 185 | api->schedpolicy = _POSIX_Threads_Default_attributes.schedpolicy; |
---|
| 186 | api->schedparam = _POSIX_Threads_Default_attributes.schedparam; |
---|
[874297f3] | 187 | api->schedparam.sched_priority = |
---|
[895efd9] | 188 | _POSIX_Priority_From_core( created->current_priority ); |
---|
[874297f3] | 189 | |
---|
| 190 | /* |
---|
[b6c868e] | 191 | * POSIX 1003.1 1996, 18.2.2.2 |
---|
| 192 | */ |
---|
| 193 | api->cancelation_requested = 0; |
---|
| 194 | api->cancelability_state = PTHREAD_CANCEL_ENABLE; |
---|
| 195 | api->cancelability_type = PTHREAD_CANCEL_DEFERRED; |
---|
| 196 | _Chain_Initialize_empty (&api->Cancellation_Handlers); |
---|
| 197 | |
---|
[b85649c4] | 198 | /* |
---|
| 199 | * If the thread is not a posix thread, then all posix signals are blocked |
---|
| 200 | * by default. |
---|
[2212a2ad] | 201 | * |
---|
| 202 | * The check for class == 1 is debug. Should never really happen. |
---|
[b85649c4] | 203 | */ |
---|
| 204 | |
---|
| 205 | /* XXX use signal constants */ |
---|
| 206 | api->signals_pending = 0; |
---|
[2212a2ad] | 207 | if ( _Objects_Get_API( created->Object.id ) == OBJECTS_POSIX_API |
---|
| 208 | #if defined(RTEMS_DEBUG) |
---|
[a1f0ca28] | 209 | && _Objects_Get_class( created->Object.id ) == 1 |
---|
[2212a2ad] | 210 | #endif |
---|
| 211 | ) { |
---|
[65c421f] | 212 | executing_api = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ]; |
---|
[7349e6d0] | 213 | api->signals_blocked = executing_api->signals_blocked; |
---|
| 214 | } else { |
---|
[b85649c4] | 215 | api->signals_blocked = 0xffffffff; |
---|
[7349e6d0] | 216 | } |
---|
[b85649c4] | 217 | |
---|
[895efd9] | 218 | _Thread_queue_Initialize( |
---|
| 219 | &api->Join_List, |
---|
| 220 | THREAD_QUEUE_DISCIPLINE_FIFO, |
---|
[f05af6b7] | 221 | STATES_WAITING_FOR_JOIN_AT_EXIT | STATES_INTERRUPTIBLE_BY_SIGNAL, |
---|
[895efd9] | 222 | 0 |
---|
| 223 | ); |
---|
[874297f3] | 224 | |
---|
[895efd9] | 225 | _Watchdog_Initialize( |
---|
| 226 | &api->Sporadic_timer, |
---|
| 227 | _POSIX_Threads_Sporadic_budget_TSR, |
---|
| 228 | created->Object.id, |
---|
| 229 | created |
---|
| 230 | ); |
---|
[5a18e04] | 231 | |
---|
[f8437c8] | 232 | return true; |
---|
[5a18e04] | 233 | } |
---|
[895efd9] | 234 | |
---|
[a1f0ca28] | 235 | /* |
---|
[5a18e04] | 236 | * _POSIX_Threads_Delete_extension |
---|
[a1f0ca28] | 237 | * |
---|
| 238 | * This method is invoked for each thread deleted. |
---|
[5a18e04] | 239 | */ |
---|
[29aed671] | 240 | static void _POSIX_Threads_Delete_extension( |
---|
[92f4671] | 241 | Thread_Control *executing __attribute__((unused)), |
---|
[5a18e04] | 242 | Thread_Control *deleted |
---|
| 243 | ) |
---|
| 244 | { |
---|
[895efd9] | 245 | Thread_Control *the_thread; |
---|
| 246 | POSIX_API_Control *api; |
---|
| 247 | void **value_ptr; |
---|
| 248 | |
---|
[a61f1a46] | 249 | api = deleted->API_Extensions[ THREAD_API_POSIX ]; |
---|
[874297f3] | 250 | |
---|
[f845e96e] | 251 | /* |
---|
| 252 | * Run the POSIX cancellation handlers |
---|
| 253 | */ |
---|
[345fc11] | 254 | _POSIX_Threads_cancel_run( deleted ); |
---|
[895efd9] | 255 | |
---|
[345fc11] | 256 | /* |
---|
| 257 | * Run all the key destructors |
---|
| 258 | */ |
---|
[895efd9] | 259 | _POSIX_Keys_Run_destructors( deleted ); |
---|
| 260 | |
---|
| 261 | /* |
---|
| 262 | * Wakeup all the tasks which joined with this one |
---|
| 263 | */ |
---|
| 264 | value_ptr = (void **) deleted->Wait.return_argument; |
---|
| 265 | |
---|
| 266 | while ( (the_thread = _Thread_queue_Dequeue( &api->Join_List )) ) |
---|
| 267 | *(void **)the_thread->Wait.return_argument = value_ptr; |
---|
[874297f3] | 268 | |
---|
[895efd9] | 269 | if ( api->schedpolicy == SCHED_SPORADIC ) |
---|
| 270 | (void) _Watchdog_Remove( &api->Sporadic_timer ); |
---|
[a61f1a46] | 271 | |
---|
| 272 | deleted->API_Extensions[ THREAD_API_POSIX ] = NULL; |
---|
| 273 | |
---|
[a0323a9f] | 274 | _Workspace_Free( api ); |
---|
[5a18e04] | 275 | } |
---|
| 276 | |
---|
[260b0c2] | 277 | /* |
---|
| 278 | * _POSIX_Threads_Exitted_extension |
---|
[a1f0ca28] | 279 | * |
---|
| 280 | * This method is invoked each time a thread exits. |
---|
[260b0c2] | 281 | */ |
---|
[29aed671] | 282 | static void _POSIX_Threads_Exitted_extension( |
---|
[260b0c2] | 283 | Thread_Control *executing |
---|
| 284 | ) |
---|
| 285 | { |
---|
[092fe28e] | 286 | /* |
---|
| 287 | * If the executing thread was not created with the POSIX API, then this |
---|
| 288 | * API do not get to define its exit behavior. |
---|
| 289 | */ |
---|
| 290 | if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API ) |
---|
| 291 | pthread_exit( executing->Wait.return_argument ); |
---|
[260b0c2] | 292 | } |
---|
| 293 | |
---|
[a1f0ca28] | 294 | /* |
---|
[dd32d883] | 295 | * _POSIX_Threads_Initialize_user_threads |
---|
[5a18e04] | 296 | * |
---|
| 297 | * This routine creates and starts all configured user |
---|
[76f1677] | 298 | * initialization threads. |
---|
[5a18e04] | 299 | */ |
---|
[29aed671] | 300 | static void _POSIX_Threads_Initialize_user_threads( void ) |
---|
[5a18e04] | 301 | { |
---|
[0ab34c90] | 302 | if ( _POSIX_Threads_Initialize_user_threads_p ) |
---|
| 303 | (*_POSIX_Threads_Initialize_user_threads_p)(); |
---|
[5a18e04] | 304 | } |
---|
| 305 | |
---|
[a1f0ca28] | 306 | /* |
---|
[22b4b2e] | 307 | * API Extension control structures |
---|
| 308 | */ |
---|
[5a18e04] | 309 | API_extensions_Control _POSIX_Threads_API_extensions = { |
---|
| 310 | { NULL, NULL }, |
---|
[27b961a] | 311 | #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API) |
---|
| 312 | NULL, /* predriver */ |
---|
| 313 | #endif |
---|
[dd32d883] | 314 | _POSIX_Threads_Initialize_user_threads, /* postdriver */ |
---|
[98ed15e] | 315 | _POSIX_signals_Post_switch_extension, /* post switch */ |
---|
[5a18e04] | 316 | }; |
---|
[874297f3] | 317 | |
---|
[5a18e04] | 318 | User_extensions_Control _POSIX_Threads_User_extensions = { |
---|
| 319 | { NULL, NULL }, |
---|
[aa08739a] | 320 | { { NULL, NULL }, NULL }, |
---|
[5a18e04] | 321 | { _POSIX_Threads_Create_extension, /* create */ |
---|
| 322 | NULL, /* start */ |
---|
| 323 | NULL, /* restart */ |
---|
| 324 | _POSIX_Threads_Delete_extension, /* delete */ |
---|
| 325 | NULL, /* switch */ |
---|
| 326 | NULL, /* begin */ |
---|
[260b0c2] | 327 | _POSIX_Threads_Exitted_extension, /* exitted */ |
---|
[5a18e04] | 328 | NULL /* fatal */ |
---|
| 329 | } |
---|
| 330 | }; |
---|
[874297f3] | 331 | |
---|
[a1f0ca28] | 332 | /* |
---|
[5e9b32b] | 333 | * _POSIX_Threads_Manager_initialization |
---|
| 334 | * |
---|
| 335 | * This routine initializes all threads manager related data structures. |
---|
| 336 | */ |
---|
[309e2f6] | 337 | void _POSIX_Threads_Manager_initialization(void) |
---|
[5e9b32b] | 338 | { |
---|
| 339 | _Objects_Initialize_information( |
---|
[3c465878] | 340 | &_POSIX_Threads_Information, /* object information table */ |
---|
| 341 | OBJECTS_POSIX_API, /* object API */ |
---|
| 342 | OBJECTS_POSIX_THREADS, /* object class */ |
---|
[309e2f6] | 343 | Configuration_POSIX_API.maximum_threads, |
---|
| 344 | /* maximum objects of this class */ |
---|
[e811d68] | 345 | sizeof( Thread_Control ), |
---|
[3c465878] | 346 | /* size of this object's control block */ |
---|
[b1dbfd7] | 347 | true, /* true if names for this object are strings */ |
---|
[309e2f6] | 348 | _POSIX_PATH_MAX /* maximum length of each object's name */ |
---|
[3c465878] | 349 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 350 | , |
---|
[b1dbfd7] | 351 | false, /* true if this is a global object class */ |
---|
[3c465878] | 352 | NULL /* Proxy extraction support callout */ |
---|
| 353 | #endif |
---|
[5e9b32b] | 354 | ); |
---|
[5a18e04] | 355 | |
---|
[613cff6] | 356 | /* |
---|
| 357 | * Add all the extensions for this API |
---|
| 358 | */ |
---|
| 359 | _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions ); |
---|
[f845e96e] | 360 | |
---|
[613cff6] | 361 | _API_extensions_Add( &_POSIX_Threads_API_extensions ); |
---|
[f845e96e] | 362 | |
---|
[613cff6] | 363 | /* |
---|
| 364 | * If we supported MP, then here we would ... |
---|
| 365 | * Register the MP Process Packet routine. |
---|
| 366 | */ |
---|
[5e9b32b] | 367 | } |
---|