[eb08acf] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Private Support Information for POSIX Threads |
---|
| 5 | * @ingroup POSIX_PTHREADS Private Threads |
---|
| 6 | */ |
---|
| 7 | |
---|
[8165b48f] | 8 | /* |
---|
[e6c87f7] | 9 | * COPYRIGHT (c) 1989-2014. |
---|
[412dbff6] | 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 |
---|
[c499856] | 14 | * http://www.rtems.org/license/LICENSE. |
---|
[5e9b32b] | 15 | */ |
---|
| 16 | |
---|
[f42b726] | 17 | #if HAVE_CONFIG_H |
---|
| 18 | #include "config.h" |
---|
| 19 | #endif |
---|
[185e46f] | 20 | #include <stdio.h> |
---|
[f42b726] | 21 | |
---|
[5e9b32b] | 22 | #include <errno.h> |
---|
| 23 | #include <pthread.h> |
---|
| 24 | #include <limits.h> |
---|
[185e46f] | 25 | #include <assert.h> |
---|
[5e9b32b] | 26 | |
---|
[f4719d5a] | 27 | #include <rtems/system.h> |
---|
[309e2f6] | 28 | #include <rtems/config.h> |
---|
[ef1a985f] | 29 | #include <rtems/sysinit.h> |
---|
[5e9b32b] | 30 | #include <rtems/score/stack.h> |
---|
[5618c37a] | 31 | #include <rtems/score/threadimpl.h> |
---|
[a112364] | 32 | #include <rtems/score/threadqimpl.h> |
---|
[3be0c9a] | 33 | #include <rtems/score/userextimpl.h> |
---|
[4b48ece0] | 34 | #include <rtems/score/watchdogimpl.h> |
---|
[5a18e04] | 35 | #include <rtems/score/wkspace.h> |
---|
[0c5317d] | 36 | #include <rtems/posix/pthreadimpl.h> |
---|
[97552c98] | 37 | #include <rtems/posix/priorityimpl.h> |
---|
[f9340ed7] | 38 | #include <rtems/posix/psignalimpl.h> |
---|
[5a18e04] | 39 | #include <rtems/posix/config.h> |
---|
[2ad250e] | 40 | #include <rtems/posix/keyimpl.h> |
---|
[c16bcc0] | 41 | #include <rtems/score/timespec.h> |
---|
[7205189] | 42 | #include <rtems/score/cpusetimpl.h> |
---|
| 43 | #include <rtems/score/assert.h> |
---|
[5e9b32b] | 44 | |
---|
[ef1a985f] | 45 | Thread_Information _POSIX_Threads_Information; |
---|
| 46 | |
---|
[a1f0ca28] | 47 | /* |
---|
[5e9b32b] | 48 | * The default pthreads attributes structure. |
---|
[65c421f] | 49 | * |
---|
[874297f3] | 50 | * NOTE: Be careful .. if the default attribute set changes, |
---|
[65c421f] | 51 | * _POSIX_Threads_Initialize_user_threads will need to be examined. |
---|
[5e9b32b] | 52 | */ |
---|
[185e46f] | 53 | pthread_attr_t _POSIX_Threads_Default_attributes = { |
---|
| 54 | .is_initialized = true, /* is_initialized */ |
---|
| 55 | .stackaddr = NULL, /* stackaddr */ |
---|
| 56 | .stacksize = 0, /* stacksize -- will be adjusted to minimum */ |
---|
| 57 | .contentionscope = PTHREAD_SCOPE_PROCESS, /* contentionscope */ |
---|
| 58 | .inheritsched = PTHREAD_INHERIT_SCHED, /* inheritsched */ |
---|
| 59 | .schedpolicy = SCHED_FIFO, /* schedpolicy */ |
---|
| 60 | .schedparam = |
---|
[0b710dbb] | 61 | { /* schedparam */ |
---|
| 62 | 2, /* sched_priority */ |
---|
[85e0a22] | 63 | #if defined(_POSIX_SPORADIC_SERVER) || \ |
---|
| 64 | defined(_POSIX_THREAD_SPORADIC_SERVER) |
---|
| 65 | 0, /* sched_ss_low_priority */ |
---|
| 66 | { 0L, 0 }, /* sched_ss_repl_period */ |
---|
[29aed671] | 67 | { 0L, 0 }, /* sched_ss_init_budget */ |
---|
| 68 | 0 /* sched_ss_max_repl */ |
---|
[85e0a22] | 69 | #endif |
---|
[5e9b32b] | 70 | }, |
---|
[185e46f] | 71 | |
---|
[21dd3ab8] | 72 | #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE |
---|
[185e46f] | 73 | .guardsize = 0, /* guardsize */ |
---|
[21dd3ab8] | 74 | #endif |
---|
[85e0a22] | 75 | #if defined(_POSIX_THREAD_CPUTIME) |
---|
[185e46f] | 76 | .cputime_clock_allowed = 1, /* cputime_clock_allowed */ |
---|
| 77 | #endif |
---|
| 78 | .detachstate = PTHREAD_CREATE_JOINABLE, /* detachstate */ |
---|
| 79 | #if defined(__RTEMS_HAVE_SYS_CPUSET_H__) |
---|
| 80 | .affinitysetsize = 0, |
---|
| 81 | .affinityset = NULL, |
---|
| 82 | .affinitysetpreallocated = {{0x0}} |
---|
[85e0a22] | 83 | #endif |
---|
[5e9b32b] | 84 | }; |
---|
| 85 | |
---|
[900d337f] | 86 | static bool _POSIX_Threads_Sporadic_budget_TSR_filter( |
---|
| 87 | Thread_Control *the_thread, |
---|
| 88 | Priority_Control *new_priority, |
---|
| 89 | void *arg |
---|
| 90 | ) |
---|
| 91 | { |
---|
| 92 | the_thread->real_priority = *new_priority; |
---|
| 93 | |
---|
| 94 | /* |
---|
| 95 | * If holding a resource, then do not change it. |
---|
| 96 | * |
---|
| 97 | * If this would make them less important, then do not change it. |
---|
| 98 | */ |
---|
| 99 | return !_Thread_Owns_resources( the_thread ) && |
---|
| 100 | _Thread_Priority_less_than( the_thread->current_priority, *new_priority ); |
---|
| 101 | } |
---|
| 102 | |
---|
[a1f0ca28] | 103 | /* |
---|
[65a5c1b] | 104 | * _POSIX_Threads_Sporadic_budget_TSR |
---|
| 105 | */ |
---|
[03b900d] | 106 | void _POSIX_Threads_Sporadic_budget_TSR( Watchdog_Control *watchdog ) |
---|
[65a5c1b] | 107 | { |
---|
| 108 | POSIX_API_Control *api; |
---|
[03b900d] | 109 | Thread_Control *the_thread; |
---|
[9555341] | 110 | ISR_lock_Context lock_context; |
---|
| 111 | Priority_Control new_priority; |
---|
[65a5c1b] | 112 | |
---|
[03b900d] | 113 | api = RTEMS_CONTAINER_OF( watchdog, POSIX_API_Control, Sporadic_timer ); |
---|
| 114 | the_thread = api->thread; |
---|
[65a5c1b] | 115 | |
---|
[8bc6bf28] | 116 | _Thread_State_acquire( the_thread, &lock_context ); |
---|
[65a5c1b] | 117 | |
---|
[9555341] | 118 | the_thread->cpu_time_budget = |
---|
| 119 | _Timespec_To_ticks( &api->schedparam.sched_ss_init_budget ); |
---|
| 120 | |
---|
| 121 | _Watchdog_Per_CPU_remove_relative( &api->Sporadic_timer ); |
---|
| 122 | _Watchdog_Per_CPU_insert_relative( |
---|
| 123 | &api->Sporadic_timer, |
---|
| 124 | _Per_CPU_Get(), |
---|
| 125 | _Timespec_To_ticks( &api->schedparam.sched_ss_repl_period ) |
---|
| 126 | ); |
---|
| 127 | |
---|
| 128 | new_priority = _POSIX_Priority_To_core( api->schedparam.sched_priority ); |
---|
| 129 | |
---|
[8bc6bf28] | 130 | _Thread_State_release( the_thread, &lock_context ); |
---|
[874297f3] | 131 | |
---|
[900d337f] | 132 | _Thread_Change_priority( |
---|
| 133 | the_thread, |
---|
[9555341] | 134 | new_priority, |
---|
[900d337f] | 135 | NULL, |
---|
| 136 | _POSIX_Threads_Sporadic_budget_TSR_filter, |
---|
| 137 | true |
---|
| 138 | ); |
---|
[65a5c1b] | 139 | } |
---|
| 140 | |
---|
[900d337f] | 141 | static bool _POSIX_Threads_Sporadic_budget_callout_filter( |
---|
| 142 | Thread_Control *the_thread, |
---|
| 143 | Priority_Control *new_priority, |
---|
| 144 | void *arg |
---|
| 145 | ) |
---|
| 146 | { |
---|
| 147 | the_thread->real_priority = *new_priority; |
---|
| 148 | |
---|
| 149 | /* |
---|
| 150 | * If holding a resource, then do not change it. |
---|
| 151 | * |
---|
| 152 | * Make sure we are actually lowering it. If they have lowered it |
---|
| 153 | * to logically lower than sched_ss_low_priority, then we do not want to |
---|
| 154 | * change it. |
---|
| 155 | */ |
---|
| 156 | return !_Thread_Owns_resources( the_thread ) && |
---|
| 157 | _Thread_Priority_less_than( *new_priority, the_thread->current_priority ); |
---|
| 158 | } |
---|
| 159 | |
---|
[a1f0ca28] | 160 | /* |
---|
[65a5c1b] | 161 | * _POSIX_Threads_Sporadic_budget_callout |
---|
| 162 | */ |
---|
| 163 | void _POSIX_Threads_Sporadic_budget_callout( |
---|
| 164 | Thread_Control *the_thread |
---|
| 165 | ) |
---|
| 166 | { |
---|
[2014063] | 167 | POSIX_API_Control *api; |
---|
[65a5c1b] | 168 | |
---|
[2014063] | 169 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
[65a5c1b] | 170 | |
---|
[2014063] | 171 | /* |
---|
| 172 | * This will prevent the thread from consuming its entire "budget" |
---|
| 173 | * while at low priority. |
---|
| 174 | */ |
---|
[8ae999c3] | 175 | the_thread->cpu_time_budget = UINT32_MAX; |
---|
[2014063] | 176 | |
---|
[900d337f] | 177 | _Thread_Change_priority( |
---|
| 178 | the_thread, |
---|
| 179 | _POSIX_Priority_To_core( api->schedparam.sched_ss_low_priority ), |
---|
| 180 | NULL, |
---|
| 181 | _POSIX_Threads_Sporadic_budget_callout_filter, |
---|
| 182 | true |
---|
| 183 | ); |
---|
[65a5c1b] | 184 | } |
---|
| 185 | |
---|
[a1f0ca28] | 186 | /* |
---|
[5a18e04] | 187 | * _POSIX_Threads_Create_extension |
---|
| 188 | * |
---|
[a1f0ca28] | 189 | * This method is invoked for each thread created. |
---|
[5a18e04] | 190 | */ |
---|
[874297f3] | 191 | |
---|
[29aed671] | 192 | static bool _POSIX_Threads_Create_extension( |
---|
[f97536d] | 193 | Thread_Control *executing RTEMS_UNUSED, |
---|
[5a18e04] | 194 | Thread_Control *created |
---|
| 195 | ) |
---|
| 196 | { |
---|
| 197 | POSIX_API_Control *api; |
---|
[65c421f] | 198 | POSIX_API_Control *executing_api; |
---|
[874297f3] | 199 | |
---|
[69aa3349] | 200 | api = created->API_Extensions[ THREAD_API_POSIX ]; |
---|
[874297f3] | 201 | |
---|
[b85649c4] | 202 | /* XXX check all fields are touched */ |
---|
[03b900d] | 203 | api->thread = created; |
---|
[185e46f] | 204 | _POSIX_Threads_Initialize_attributes( &api->Attributes ); |
---|
[895efd9] | 205 | api->schedpolicy = _POSIX_Threads_Default_attributes.schedpolicy; |
---|
| 206 | api->schedparam = _POSIX_Threads_Default_attributes.schedparam; |
---|
[874297f3] | 207 | api->schedparam.sched_priority = |
---|
[895efd9] | 208 | _POSIX_Priority_From_core( created->current_priority ); |
---|
[874297f3] | 209 | |
---|
[b85649c4] | 210 | /* |
---|
| 211 | * If the thread is not a posix thread, then all posix signals are blocked |
---|
| 212 | * by default. |
---|
[2212a2ad] | 213 | * |
---|
| 214 | * The check for class == 1 is debug. Should never really happen. |
---|
[b85649c4] | 215 | */ |
---|
[54c3fbd] | 216 | RTEMS_STATIC_ASSERT( SIGNAL_EMPTY_MASK == 0, signals_pending ); |
---|
[2212a2ad] | 217 | if ( _Objects_Get_API( created->Object.id ) == OBJECTS_POSIX_API |
---|
| 218 | #if defined(RTEMS_DEBUG) |
---|
[a1f0ca28] | 219 | && _Objects_Get_class( created->Object.id ) == 1 |
---|
[2212a2ad] | 220 | #endif |
---|
| 221 | ) { |
---|
[fb7199d] | 222 | executing_api = _Thread_Get_executing()->API_Extensions[ THREAD_API_POSIX ]; |
---|
[173d1f8] | 223 | api->signals_unblocked = executing_api->signals_unblocked; |
---|
[7349e6d0] | 224 | } |
---|
[b85649c4] | 225 | |
---|
[03b900d] | 226 | _Watchdog_Preinitialize( &api->Sporadic_timer, _Per_CPU_Get_by_index( 0 ) ); |
---|
[895efd9] | 227 | _Watchdog_Initialize( |
---|
| 228 | &api->Sporadic_timer, |
---|
[03b900d] | 229 | _POSIX_Threads_Sporadic_budget_TSR |
---|
[895efd9] | 230 | ); |
---|
[5a18e04] | 231 | |
---|
[f8437c8] | 232 | return true; |
---|
[5a18e04] | 233 | } |
---|
[895efd9] | 234 | |
---|
[54550e04] | 235 | static void _POSIX_Threads_Terminate_extension( Thread_Control *executing ) |
---|
[9555341] | 236 | { |
---|
| 237 | POSIX_API_Control *api; |
---|
[54550e04] | 238 | ISR_lock_Context lock_context; |
---|
[895efd9] | 239 | |
---|
[1b1be254] | 240 | api = executing->API_Extensions[ THREAD_API_POSIX ]; |
---|
[874297f3] | 241 | |
---|
[8bc6bf28] | 242 | _Thread_State_acquire( executing, &lock_context ); |
---|
[02c4c441] | 243 | |
---|
[9555341] | 244 | if ( api->schedpolicy == SCHED_SPORADIC ) { |
---|
| 245 | _Watchdog_Per_CPU_remove_relative( &api->Sporadic_timer ); |
---|
| 246 | } |
---|
| 247 | |
---|
[8bc6bf28] | 248 | _Thread_State_release( executing, &lock_context ); |
---|
[5a18e04] | 249 | } |
---|
| 250 | |
---|
[260b0c2] | 251 | /* |
---|
| 252 | * _POSIX_Threads_Exitted_extension |
---|
[a1f0ca28] | 253 | * |
---|
| 254 | * This method is invoked each time a thread exits. |
---|
[260b0c2] | 255 | */ |
---|
[29aed671] | 256 | static void _POSIX_Threads_Exitted_extension( |
---|
[260b0c2] | 257 | Thread_Control *executing |
---|
| 258 | ) |
---|
| 259 | { |
---|
[092fe28e] | 260 | /* |
---|
| 261 | * If the executing thread was not created with the POSIX API, then this |
---|
| 262 | * API do not get to define its exit behavior. |
---|
| 263 | */ |
---|
| 264 | if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API ) |
---|
| 265 | pthread_exit( executing->Wait.return_argument ); |
---|
[260b0c2] | 266 | } |
---|
| 267 | |
---|
[5a18e04] | 268 | User_extensions_Control _POSIX_Threads_User_extensions = { |
---|
[54550e04] | 269 | .Callouts = { |
---|
| 270 | .thread_create = _POSIX_Threads_Create_extension, |
---|
| 271 | .thread_exitted = _POSIX_Threads_Exitted_extension, |
---|
| 272 | .thread_terminate = _POSIX_Threads_Terminate_extension |
---|
[5a18e04] | 273 | } |
---|
| 274 | }; |
---|
[874297f3] | 275 | |
---|
[a1f0ca28] | 276 | /* |
---|
[5e9b32b] | 277 | * _POSIX_Threads_Manager_initialization |
---|
| 278 | * |
---|
| 279 | * This routine initializes all threads manager related data structures. |
---|
| 280 | */ |
---|
[ef1a985f] | 281 | static void _POSIX_Threads_Manager_initialization(void) |
---|
[5e9b32b] | 282 | { |
---|
[a0a073d1] | 283 | #if defined(RTEMS_SMP) && defined(__RTEMS_HAVE_SYS_CPUSET_H__) |
---|
[7205189] | 284 | const CPU_set_Control *affinity; |
---|
[185e46f] | 285 | pthread_attr_t *attr; |
---|
| 286 | |
---|
| 287 | /* Initialize default attribute. */ |
---|
| 288 | attr = &_POSIX_Threads_Default_attributes; |
---|
| 289 | |
---|
[7205189] | 290 | /* Initialize the affinity to be the default cpu set for the system */ |
---|
| 291 | affinity = _CPU_set_Default(); |
---|
| 292 | _Assert( affinity->setsize == sizeof( attr->affinitysetpreallocated ) ); |
---|
[185e46f] | 293 | attr->affinityset = &attr->affinitysetpreallocated; |
---|
[7205189] | 294 | attr->affinitysetsize = affinity->setsize; |
---|
| 295 | CPU_COPY( attr->affinityset, affinity->set ); |
---|
[185e46f] | 296 | #endif |
---|
| 297 | |
---|
[d7665823] | 298 | _Thread_Initialize_information( |
---|
[3c465878] | 299 | &_POSIX_Threads_Information, /* object information table */ |
---|
| 300 | OBJECTS_POSIX_API, /* object API */ |
---|
| 301 | OBJECTS_POSIX_THREADS, /* object class */ |
---|
[309e2f6] | 302 | Configuration_POSIX_API.maximum_threads, |
---|
| 303 | /* maximum objects of this class */ |
---|
[b1dbfd7] | 304 | true, /* true if names for this object are strings */ |
---|
[309e2f6] | 305 | _POSIX_PATH_MAX /* maximum length of each object's name */ |
---|
[5e9b32b] | 306 | ); |
---|
[5a18e04] | 307 | |
---|
[613cff6] | 308 | /* |
---|
| 309 | * Add all the extensions for this API |
---|
| 310 | */ |
---|
| 311 | _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions ); |
---|
[f845e96e] | 312 | |
---|
[613cff6] | 313 | /* |
---|
| 314 | * If we supported MP, then here we would ... |
---|
| 315 | * Register the MP Process Packet routine. |
---|
| 316 | */ |
---|
[5e9b32b] | 317 | } |
---|
[ef1a985f] | 318 | |
---|
| 319 | RTEMS_SYSINIT_ITEM( |
---|
| 320 | _POSIX_Threads_Manager_initialization, |
---|
| 321 | RTEMS_SYSINIT_POSIX_THREADS, |
---|
| 322 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
| 323 | ); |
---|