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