[65f6d3c] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Function Starts a New Thread in The Calling Process |
---|
[5cb175bb] | 5 | * @ingroup POSIXAPI |
---|
[65f6d3c] | 6 | */ |
---|
| 7 | |
---|
[03598b1] | 8 | /* |
---|
| 9 | * 16.1.2 Thread Creation, P1003.1c/Draft 10, p. 144 |
---|
[412dbff6] | 10 | */ |
---|
| 11 | |
---|
[68c156a] | 12 | /* |
---|
| 13 | * COPYRIGHT (c) 1989-2014. |
---|
[03598b1] | 14 | * On-Line Applications Research Corporation (OAR). |
---|
| 15 | * |
---|
| 16 | * The license and distribution terms for this file may be |
---|
| 17 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 18 | * http://www.rtems.org/license/LICENSE. |
---|
[03598b1] | 19 | */ |
---|
| 20 | |
---|
[f42b726] | 21 | #if HAVE_CONFIG_H |
---|
| 22 | #include "config.h" |
---|
| 23 | #endif |
---|
| 24 | |
---|
[03598b1] | 25 | #include <pthread.h> |
---|
| 26 | #include <errno.h> |
---|
| 27 | |
---|
[97552c98] | 28 | #include <rtems/posix/priorityimpl.h> |
---|
[5618c37a] | 29 | #include <rtems/posix/pthreadimpl.h> |
---|
[185e46f] | 30 | #include <rtems/score/cpusetimpl.h> |
---|
[5618c37a] | 31 | #include <rtems/score/threadimpl.h> |
---|
[5088d97] | 32 | #include <rtems/score/apimutex.h> |
---|
[218286bc] | 33 | #include <rtems/score/stackimpl.h> |
---|
[e045fb6c] | 34 | #include <rtems/score/schedulerimpl.h> |
---|
| 35 | |
---|
[ecf0f4c] | 36 | static inline size_t _POSIX_Threads_Ensure_minimum_stack ( |
---|
| 37 | size_t size |
---|
| 38 | ) |
---|
| 39 | { |
---|
| 40 | if ( size >= PTHREAD_MINIMUM_STACK_SIZE ) |
---|
| 41 | return size; |
---|
| 42 | return PTHREAD_MINIMUM_STACK_SIZE; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
[03598b1] | 46 | int pthread_create( |
---|
| 47 | pthread_t *thread, |
---|
| 48 | const pthread_attr_t *attr, |
---|
| 49 | void *(*start_routine)( void * ), |
---|
| 50 | void *arg |
---|
| 51 | ) |
---|
| 52 | { |
---|
[ccd5434] | 53 | Thread_Entry_information entry = { |
---|
| 54 | .adaptor = _Thread_Entry_adaptor_pointer, |
---|
| 55 | .Kinds = { |
---|
| 56 | .Pointer = { |
---|
| 57 | .entry = start_routine, |
---|
| 58 | .argument = arg |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | }; |
---|
[03598b1] | 62 | const pthread_attr_t *the_attr; |
---|
[eec08ef] | 63 | int low_prio; |
---|
| 64 | int high_prio; |
---|
[1a4eac50] | 65 | bool valid; |
---|
[eec08ef] | 66 | Priority_Control core_low_prio; |
---|
| 67 | Priority_Control core_high_prio; |
---|
[03598b1] | 68 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
| 69 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
[f8437c8] | 70 | bool is_fp; |
---|
| 71 | bool status; |
---|
[03598b1] | 72 | Thread_Control *the_thread; |
---|
[c5831a3f] | 73 | Thread_Control *executing; |
---|
[5a32c48] | 74 | const Scheduler_Control *scheduler; |
---|
[03598b1] | 75 | POSIX_API_Control *api; |
---|
| 76 | int schedpolicy = SCHED_RR; |
---|
| 77 | struct sched_param schedparam; |
---|
[ce19f1fa] | 78 | Objects_Name name; |
---|
[eec08ef] | 79 | int error; |
---|
[e135271] | 80 | ISR_lock_Context lock_context; |
---|
[03598b1] | 81 | |
---|
[e889a857] | 82 | if ( !start_routine ) |
---|
| 83 | return EFAULT; |
---|
| 84 | |
---|
[03598b1] | 85 | the_attr = (attr) ? attr : &_POSIX_Threads_Default_attributes; |
---|
| 86 | |
---|
| 87 | if ( !the_attr->is_initialized ) |
---|
| 88 | return EINVAL; |
---|
| 89 | |
---|
| 90 | /* |
---|
[ecf0f4c] | 91 | * Core Thread Initialize ensures we get the minimum amount of |
---|
[03598b1] | 92 | * stack space if it is allowed to allocate it itself. |
---|
[ecf0f4c] | 93 | * |
---|
| 94 | * NOTE: If the user provides the stack we will let it drop below |
---|
| 95 | * twice the minimum. |
---|
[03598b1] | 96 | */ |
---|
[ecf0f4c] | 97 | if ( the_attr->stackaddr && !_Stack_Is_enough(the_attr->stacksize) ) |
---|
[03598b1] | 98 | return EINVAL; |
---|
| 99 | |
---|
[2212a2ad] | 100 | #if 0 |
---|
| 101 | int cputime_clock_allowed; /* see time.h */ |
---|
| 102 | rtems_set_errno_and_return_minus_one( ENOSYS ); |
---|
| 103 | #endif |
---|
[03598b1] | 104 | |
---|
[c5831a3f] | 105 | executing = _Thread_Get_executing(); |
---|
[77ff5599] | 106 | scheduler = _Scheduler_Get_own( executing ); |
---|
[c5831a3f] | 107 | |
---|
[03598b1] | 108 | /* |
---|
| 109 | * P1003.1c/Draft 10, p. 121. |
---|
| 110 | * |
---|
| 111 | * If inheritsched is set to PTHREAD_INHERIT_SCHED, then this thread |
---|
| 112 | * inherits scheduling attributes from the creating thread. If it is |
---|
[874297f3] | 113 | * PTHREAD_EXPLICIT_SCHED, then scheduling parameters come from the |
---|
[03598b1] | 114 | * attributes structure. |
---|
| 115 | */ |
---|
| 116 | switch ( the_attr->inheritsched ) { |
---|
| 117 | case PTHREAD_INHERIT_SCHED: |
---|
[c5831a3f] | 118 | api = executing->API_Extensions[ THREAD_API_POSIX ]; |
---|
[4dc5450] | 119 | schedpolicy = api->Attributes.schedpolicy; |
---|
[6bab009] | 120 | schedparam = api->Attributes.schedparam; |
---|
[874297f3] | 121 | break; |
---|
[03598b1] | 122 | |
---|
| 123 | case PTHREAD_EXPLICIT_SCHED: |
---|
| 124 | schedpolicy = the_attr->schedpolicy; |
---|
| 125 | schedparam = the_attr->schedparam; |
---|
[874297f3] | 126 | break; |
---|
[03598b1] | 127 | |
---|
| 128 | default: |
---|
| 129 | return EINVAL; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /* |
---|
[874297f3] | 133 | * Check the contentionscope since rtems only supports PROCESS wide |
---|
[03598b1] | 134 | * contention (i.e. no system wide contention). |
---|
| 135 | */ |
---|
| 136 | if ( the_attr->contentionscope != PTHREAD_SCOPE_PROCESS ) |
---|
| 137 | return ENOTSUP; |
---|
| 138 | |
---|
[eec08ef] | 139 | error = _POSIX_Thread_Translate_sched_param( |
---|
[2212a2ad] | 140 | schedpolicy, |
---|
| 141 | &schedparam, |
---|
| 142 | &budget_algorithm, |
---|
| 143 | &budget_callout |
---|
| 144 | ); |
---|
[eec08ef] | 145 | if ( error != 0 ) { |
---|
| 146 | return error; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | if ( schedpolicy == SCHED_SPORADIC ) { |
---|
| 150 | low_prio = schedparam.sched_ss_low_priority; |
---|
| 151 | high_prio = schedparam.sched_priority; |
---|
| 152 | } else { |
---|
| 153 | low_prio = schedparam.sched_priority; |
---|
| 154 | high_prio = low_prio; |
---|
| 155 | } |
---|
| 156 | |
---|
[5a32c48] | 157 | scheduler = _Scheduler_Get_own( executing ); |
---|
| 158 | |
---|
[1a4eac50] | 159 | core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid ); |
---|
| 160 | if ( !valid ) { |
---|
[eec08ef] | 161 | return EINVAL; |
---|
| 162 | } |
---|
| 163 | |
---|
[1a4eac50] | 164 | core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio, &valid ); |
---|
| 165 | if ( !valid ) { |
---|
[eec08ef] | 166 | return EINVAL; |
---|
| 167 | } |
---|
| 168 | |
---|
[185e46f] | 169 | #if defined(RTEMS_SMP) |
---|
| 170 | #if __RTEMS_HAVE_SYS_CPUSET_H__ |
---|
[e045fb6c] | 171 | status = _CPU_set_Is_valid( the_attr->affinityset, the_attr->affinitysetsize ); |
---|
[68c156a] | 172 | if ( !status ) |
---|
[185e46f] | 173 | return EINVAL; |
---|
| 174 | #endif |
---|
| 175 | #endif |
---|
| 176 | |
---|
[03598b1] | 177 | /* |
---|
[874297f3] | 178 | * Currently all POSIX threads are floating point if the hardware |
---|
[03598b1] | 179 | * supports it. |
---|
| 180 | */ |
---|
[335e5ca] | 181 | is_fp = true; |
---|
[03598b1] | 182 | |
---|
| 183 | /* |
---|
| 184 | * Allocate the thread control block. |
---|
| 185 | * |
---|
| 186 | * NOTE: Global threads are not currently supported. |
---|
| 187 | */ |
---|
| 188 | the_thread = _POSIX_Threads_Allocate(); |
---|
| 189 | if ( !the_thread ) { |
---|
[23fec9f0] | 190 | _Objects_Allocator_unlock(); |
---|
[03598b1] | 191 | return EAGAIN; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /* |
---|
| 195 | * Initialize the core thread for this task. |
---|
| 196 | */ |
---|
[ce19f1fa] | 197 | name.name_p = NULL; /* posix threads don't have a name by default */ |
---|
[03598b1] | 198 | status = _Thread_Initialize( |
---|
| 199 | &_POSIX_Threads_Information, |
---|
| 200 | the_thread, |
---|
[77ff5599] | 201 | scheduler, |
---|
[03598b1] | 202 | the_attr->stackaddr, |
---|
[ecf0f4c] | 203 | _POSIX_Threads_Ensure_minimum_stack(the_attr->stacksize), |
---|
[03598b1] | 204 | is_fp, |
---|
[eec08ef] | 205 | core_high_prio, |
---|
[b1dbfd7] | 206 | true, /* preemptible */ |
---|
[03598b1] | 207 | budget_algorithm, |
---|
| 208 | budget_callout, |
---|
| 209 | 0, /* isr level */ |
---|
[ce19f1fa] | 210 | name /* posix threads don't have a name */ |
---|
[03598b1] | 211 | ); |
---|
| 212 | if ( !status ) { |
---|
| 213 | _POSIX_Threads_Free( the_thread ); |
---|
[23fec9f0] | 214 | _Objects_Allocator_unlock(); |
---|
[03598b1] | 215 | return EAGAIN; |
---|
| 216 | } |
---|
| 217 | |
---|
[54550e04] | 218 | if ( the_attr->detachstate == PTHREAD_CREATE_DETACHED ) { |
---|
| 219 | the_thread->Life.state |= THREAD_LIFE_DETACHED; |
---|
| 220 | } |
---|
| 221 | |
---|
[da826560] | 222 | the_thread->Life.state |= THREAD_LIFE_CHANGE_DEFERRED; |
---|
| 223 | |
---|
[68c156a] | 224 | #if defined(RTEMS_SMP) && __RTEMS_HAVE_SYS_CPUSET_H__ |
---|
[e135271] | 225 | _ISR_lock_ISR_disable( &lock_context ); |
---|
[68c156a] | 226 | status = _Scheduler_Set_affinity( |
---|
| 227 | the_thread, |
---|
| 228 | the_attr->affinitysetsize, |
---|
| 229 | the_attr->affinityset |
---|
[e045fb6c] | 230 | ); |
---|
[e135271] | 231 | _ISR_lock_ISR_enable( &lock_context ); |
---|
[68c156a] | 232 | if ( !status ) { |
---|
| 233 | _POSIX_Threads_Free( the_thread ); |
---|
| 234 | _RTEMS_Unlock_allocator(); |
---|
| 235 | return EINVAL; |
---|
| 236 | } |
---|
[185e46f] | 237 | #endif |
---|
| 238 | |
---|
[03598b1] | 239 | /* |
---|
| 240 | * finish initializing the per API structure |
---|
| 241 | */ |
---|
| 242 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
| 243 | |
---|
[185e46f] | 244 | _POSIX_Threads_Copy_attributes( &api->Attributes, the_attr ); |
---|
[eec08ef] | 245 | api->Sporadic.low_priority = core_low_prio; |
---|
| 246 | api->Sporadic.high_priority = core_high_prio; |
---|
[03598b1] | 247 | |
---|
[917884c] | 248 | if ( schedpolicy == SCHED_SPORADIC ) { |
---|
| 249 | _ISR_lock_ISR_disable( &lock_context ); |
---|
| 250 | _POSIX_Threads_Sporadic_timer_insert( the_thread, api ); |
---|
| 251 | _ISR_lock_ISR_enable( &lock_context ); |
---|
| 252 | } |
---|
| 253 | |
---|
[03598b1] | 254 | /* |
---|
| 255 | * POSIX threads are allocated and started in one operation. |
---|
| 256 | */ |
---|
[33829ce] | 257 | _ISR_lock_ISR_disable( &lock_context ); |
---|
| 258 | status = _Thread_Start( the_thread, &entry, &lock_context ); |
---|
[03598b1] | 259 | |
---|
[2212a2ad] | 260 | #if defined(RTEMS_DEBUG) |
---|
| 261 | /* |
---|
| 262 | * _Thread_Start only fails if the thread was in the incorrect state |
---|
[1de949a8] | 263 | * |
---|
[2212a2ad] | 264 | * NOTE: This can only happen if someone slips in and touches the |
---|
| 265 | * thread while we are creating it. |
---|
| 266 | */ |
---|
| 267 | if ( !status ) { |
---|
| 268 | _POSIX_Threads_Free( the_thread ); |
---|
[23fec9f0] | 269 | _Objects_Allocator_unlock(); |
---|
[2212a2ad] | 270 | return EINVAL; |
---|
| 271 | } |
---|
| 272 | #endif |
---|
| 273 | |
---|
[03598b1] | 274 | /* |
---|
| 275 | * Return the id and indicate we successfully created the thread |
---|
| 276 | */ |
---|
| 277 | *thread = the_thread->Object.id; |
---|
| 278 | |
---|
[23fec9f0] | 279 | _Objects_Allocator_unlock(); |
---|
[5088d97] | 280 | return 0; |
---|
[03598b1] | 281 | } |
---|