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/cpusetimpl.h> |
---|
31 | #include <rtems/score/threadimpl.h> |
---|
32 | #include <rtems/score/apimutex.h> |
---|
33 | #include <rtems/score/stackimpl.h> |
---|
34 | #include <rtems/score/watchdogimpl.h> |
---|
35 | #include <rtems/score/schedulerimpl.h> |
---|
36 | |
---|
37 | |
---|
38 | static inline size_t _POSIX_Threads_Ensure_minimum_stack ( |
---|
39 | size_t size |
---|
40 | ) |
---|
41 | { |
---|
42 | if ( size >= PTHREAD_MINIMUM_STACK_SIZE ) |
---|
43 | return size; |
---|
44 | return PTHREAD_MINIMUM_STACK_SIZE; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | int pthread_create( |
---|
49 | pthread_t *thread, |
---|
50 | const pthread_attr_t *attr, |
---|
51 | void *(*start_routine)( void * ), |
---|
52 | void *arg |
---|
53 | ) |
---|
54 | { |
---|
55 | const pthread_attr_t *the_attr; |
---|
56 | Priority_Control core_priority; |
---|
57 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
58 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
59 | bool is_fp; |
---|
60 | bool status; |
---|
61 | Thread_Control *the_thread; |
---|
62 | Thread_Control *executing; |
---|
63 | POSIX_API_Control *api; |
---|
64 | int schedpolicy = SCHED_RR; |
---|
65 | struct sched_param schedparam; |
---|
66 | Objects_Name name; |
---|
67 | int rc; |
---|
68 | |
---|
69 | if ( !start_routine ) |
---|
70 | return EFAULT; |
---|
71 | |
---|
72 | the_attr = (attr) ? attr : &_POSIX_Threads_Default_attributes; |
---|
73 | |
---|
74 | if ( !the_attr->is_initialized ) |
---|
75 | return EINVAL; |
---|
76 | |
---|
77 | /* |
---|
78 | * Core Thread Initialize ensures we get the minimum amount of |
---|
79 | * stack space if it is allowed to allocate it itself. |
---|
80 | * |
---|
81 | * NOTE: If the user provides the stack we will let it drop below |
---|
82 | * twice the minimum. |
---|
83 | */ |
---|
84 | if ( the_attr->stackaddr && !_Stack_Is_enough(the_attr->stacksize) ) |
---|
85 | return EINVAL; |
---|
86 | |
---|
87 | #if 0 |
---|
88 | int cputime_clock_allowed; /* see time.h */ |
---|
89 | rtems_set_errno_and_return_minus_one( ENOSYS ); |
---|
90 | #endif |
---|
91 | |
---|
92 | executing = _Thread_Get_executing(); |
---|
93 | |
---|
94 | /* |
---|
95 | * P1003.1c/Draft 10, p. 121. |
---|
96 | * |
---|
97 | * If inheritsched is set to PTHREAD_INHERIT_SCHED, then this thread |
---|
98 | * inherits scheduling attributes from the creating thread. If it is |
---|
99 | * PTHREAD_EXPLICIT_SCHED, then scheduling parameters come from the |
---|
100 | * attributes structure. |
---|
101 | */ |
---|
102 | switch ( the_attr->inheritsched ) { |
---|
103 | case PTHREAD_INHERIT_SCHED: |
---|
104 | api = executing->API_Extensions[ THREAD_API_POSIX ]; |
---|
105 | schedpolicy = api->schedpolicy; |
---|
106 | schedparam = api->schedparam; |
---|
107 | break; |
---|
108 | |
---|
109 | case PTHREAD_EXPLICIT_SCHED: |
---|
110 | schedpolicy = the_attr->schedpolicy; |
---|
111 | schedparam = the_attr->schedparam; |
---|
112 | break; |
---|
113 | |
---|
114 | default: |
---|
115 | return EINVAL; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * Check the contentionscope since rtems only supports PROCESS wide |
---|
120 | * contention (i.e. no system wide contention). |
---|
121 | */ |
---|
122 | if ( the_attr->contentionscope != PTHREAD_SCOPE_PROCESS ) |
---|
123 | return ENOTSUP; |
---|
124 | |
---|
125 | /* |
---|
126 | * Interpret the scheduling parameters. |
---|
127 | */ |
---|
128 | if ( !_POSIX_Priority_Is_valid( schedparam.sched_priority ) ) |
---|
129 | return EINVAL; |
---|
130 | |
---|
131 | core_priority = _POSIX_Priority_To_core( schedparam.sched_priority ); |
---|
132 | |
---|
133 | /* |
---|
134 | * Set the core scheduling policy information. |
---|
135 | */ |
---|
136 | rc = _POSIX_Thread_Translate_sched_param( |
---|
137 | schedpolicy, |
---|
138 | &schedparam, |
---|
139 | &budget_algorithm, |
---|
140 | &budget_callout |
---|
141 | ); |
---|
142 | if ( rc ) |
---|
143 | return rc; |
---|
144 | |
---|
145 | #if defined(RTEMS_SMP) |
---|
146 | #if __RTEMS_HAVE_SYS_CPUSET_H__ |
---|
147 | status = _CPU_set_Is_valid( the_attr->affinityset, the_attr->affinitysetsize ); |
---|
148 | if ( !status ) |
---|
149 | return EINVAL; |
---|
150 | #endif |
---|
151 | #endif |
---|
152 | |
---|
153 | /* |
---|
154 | * Currently all POSIX threads are floating point if the hardware |
---|
155 | * supports it. |
---|
156 | */ |
---|
157 | is_fp = true; |
---|
158 | |
---|
159 | /* |
---|
160 | * Allocate the thread control block. |
---|
161 | * |
---|
162 | * NOTE: Global threads are not currently supported. |
---|
163 | */ |
---|
164 | the_thread = _POSIX_Threads_Allocate(); |
---|
165 | if ( !the_thread ) { |
---|
166 | _Objects_Allocator_unlock(); |
---|
167 | return EAGAIN; |
---|
168 | } |
---|
169 | |
---|
170 | /* |
---|
171 | * Initialize the core thread for this task. |
---|
172 | */ |
---|
173 | name.name_p = NULL; /* posix threads don't have a name by default */ |
---|
174 | status = _Thread_Initialize( |
---|
175 | &_POSIX_Threads_Information, |
---|
176 | the_thread, |
---|
177 | _Scheduler_Get( executing ), |
---|
178 | the_attr->stackaddr, |
---|
179 | _POSIX_Threads_Ensure_minimum_stack(the_attr->stacksize), |
---|
180 | is_fp, |
---|
181 | core_priority, |
---|
182 | true, /* preemptible */ |
---|
183 | budget_algorithm, |
---|
184 | budget_callout, |
---|
185 | 0, /* isr level */ |
---|
186 | name /* posix threads don't have a name */ |
---|
187 | ); |
---|
188 | if ( !status ) { |
---|
189 | _POSIX_Threads_Free( the_thread ); |
---|
190 | _Objects_Allocator_unlock(); |
---|
191 | return EAGAIN; |
---|
192 | } |
---|
193 | |
---|
194 | #if defined(RTEMS_SMP) && __RTEMS_HAVE_SYS_CPUSET_H__ |
---|
195 | status = _Scheduler_Set_affinity( |
---|
196 | the_thread, |
---|
197 | the_attr->affinitysetsize, |
---|
198 | the_attr->affinityset |
---|
199 | ); |
---|
200 | if ( !status ) { |
---|
201 | _POSIX_Threads_Free( the_thread ); |
---|
202 | _RTEMS_Unlock_allocator(); |
---|
203 | return EINVAL; |
---|
204 | } |
---|
205 | #endif |
---|
206 | |
---|
207 | /* |
---|
208 | * finish initializing the per API structure |
---|
209 | */ |
---|
210 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
211 | |
---|
212 | _POSIX_Threads_Copy_attributes( &api->Attributes, the_attr ); |
---|
213 | api->detachstate = the_attr->detachstate; |
---|
214 | api->schedpolicy = schedpolicy; |
---|
215 | api->schedparam = schedparam; |
---|
216 | |
---|
217 | _Thread_Disable_dispatch(); |
---|
218 | |
---|
219 | /* |
---|
220 | * POSIX threads are allocated and started in one operation. |
---|
221 | */ |
---|
222 | status = _Thread_Start( |
---|
223 | the_thread, |
---|
224 | THREAD_START_POINTER, |
---|
225 | start_routine, |
---|
226 | arg, |
---|
227 | 0, /* unused */ |
---|
228 | NULL |
---|
229 | ); |
---|
230 | |
---|
231 | #if defined(RTEMS_DEBUG) |
---|
232 | /* |
---|
233 | * _Thread_Start only fails if the thread was in the incorrect state |
---|
234 | * |
---|
235 | * NOTE: This can only happen if someone slips in and touches the |
---|
236 | * thread while we are creating it. |
---|
237 | */ |
---|
238 | if ( !status ) { |
---|
239 | _Thread_Enable_dispatch(); |
---|
240 | _POSIX_Threads_Free( the_thread ); |
---|
241 | _Objects_Allocator_unlock(); |
---|
242 | return EINVAL; |
---|
243 | } |
---|
244 | #endif |
---|
245 | |
---|
246 | if ( schedpolicy == SCHED_SPORADIC ) { |
---|
247 | _Watchdog_Insert_ticks( |
---|
248 | &api->Sporadic_timer, |
---|
249 | _Timespec_To_ticks( &api->schedparam.sched_ss_repl_period ) |
---|
250 | ); |
---|
251 | } |
---|
252 | |
---|
253 | _Thread_Enable_dispatch(); |
---|
254 | |
---|
255 | /* |
---|
256 | * Return the id and indicate we successfully created the thread |
---|
257 | */ |
---|
258 | *thread = the_thread->Object.id; |
---|
259 | |
---|
260 | _Objects_Allocator_unlock(); |
---|
261 | return 0; |
---|
262 | } |
---|