1 | /* |
---|
2 | * 16.1.2 Thread Creation, P1003.1c/Draft 10, p. 144 |
---|
3 | */ |
---|
4 | |
---|
5 | /* COPYRIGHT (c) 1989-2007. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <pthread.h> |
---|
20 | #include <errno.h> |
---|
21 | |
---|
22 | #include <rtems/system.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/posix/pthread.h> |
---|
25 | #include <rtems/posix/priority.h> |
---|
26 | #include <rtems/posix/time.h> |
---|
27 | |
---|
28 | int pthread_create( |
---|
29 | pthread_t *thread, |
---|
30 | const pthread_attr_t *attr, |
---|
31 | void *(*start_routine)( void * ), |
---|
32 | void *arg |
---|
33 | ) |
---|
34 | { |
---|
35 | const pthread_attr_t *the_attr; |
---|
36 | Priority_Control core_priority; |
---|
37 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
38 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
39 | boolean is_fp; |
---|
40 | boolean status; |
---|
41 | Thread_Control *the_thread; |
---|
42 | POSIX_API_Control *api; |
---|
43 | int schedpolicy = SCHED_RR; |
---|
44 | struct sched_param schedparam; |
---|
45 | Objects_Name name; |
---|
46 | |
---|
47 | the_attr = (attr) ? attr : &_POSIX_Threads_Default_attributes; |
---|
48 | |
---|
49 | if ( !the_attr->is_initialized ) |
---|
50 | return EINVAL; |
---|
51 | |
---|
52 | /* |
---|
53 | * Core Thread Initialize insures we get the minimum amount of |
---|
54 | * stack space if it is allowed to allocate it itself. |
---|
55 | */ |
---|
56 | |
---|
57 | if ( the_attr->stackaddr && !_Stack_Is_enough( the_attr->stacksize ) ) |
---|
58 | return EINVAL; |
---|
59 | |
---|
60 | #if 0 |
---|
61 | int cputime_clock_allowed; /* see time.h */ |
---|
62 | rtems_set_errno_and_return_minus_one( ENOSYS ); |
---|
63 | #endif |
---|
64 | |
---|
65 | /* |
---|
66 | * P1003.1c/Draft 10, p. 121. |
---|
67 | * |
---|
68 | * If inheritsched is set to PTHREAD_INHERIT_SCHED, then this thread |
---|
69 | * inherits scheduling attributes from the creating thread. If it is |
---|
70 | * PTHREAD_EXPLICIT_SCHED, then scheduling parameters come from the |
---|
71 | * attributes structure. |
---|
72 | */ |
---|
73 | |
---|
74 | switch ( the_attr->inheritsched ) { |
---|
75 | case PTHREAD_INHERIT_SCHED: |
---|
76 | api = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ]; |
---|
77 | schedpolicy = api->schedpolicy; |
---|
78 | schedparam = api->schedparam; |
---|
79 | break; |
---|
80 | |
---|
81 | case PTHREAD_EXPLICIT_SCHED: |
---|
82 | schedpolicy = the_attr->schedpolicy; |
---|
83 | schedparam = the_attr->schedparam; |
---|
84 | break; |
---|
85 | |
---|
86 | default: |
---|
87 | return EINVAL; |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | * Check the contentionscope since rtems only supports PROCESS wide |
---|
92 | * contention (i.e. no system wide contention). |
---|
93 | */ |
---|
94 | |
---|
95 | if ( the_attr->contentionscope != PTHREAD_SCOPE_PROCESS ) |
---|
96 | return ENOTSUP; |
---|
97 | |
---|
98 | /* |
---|
99 | * Interpret the scheduling parameters. |
---|
100 | */ |
---|
101 | |
---|
102 | if ( !_POSIX_Priority_Is_valid( schedparam.sched_priority ) ) |
---|
103 | return EINVAL; |
---|
104 | |
---|
105 | core_priority = _POSIX_Priority_To_core( schedparam.sched_priority ); |
---|
106 | |
---|
107 | /* |
---|
108 | * Set the core scheduling policy information. |
---|
109 | */ |
---|
110 | |
---|
111 | budget_callout = NULL; |
---|
112 | budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_NONE; |
---|
113 | |
---|
114 | switch ( schedpolicy ) { |
---|
115 | case SCHED_OTHER: |
---|
116 | budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE; |
---|
117 | break; |
---|
118 | |
---|
119 | case SCHED_FIFO: |
---|
120 | budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_NONE; |
---|
121 | break; |
---|
122 | |
---|
123 | case SCHED_RR: |
---|
124 | budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE; |
---|
125 | break; |
---|
126 | |
---|
127 | case SCHED_SPORADIC: |
---|
128 | budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_CALLOUT; |
---|
129 | budget_callout = _POSIX_Threads_Sporadic_budget_callout; |
---|
130 | |
---|
131 | if ( _Timespec_To_ticks( &schedparam.ss_replenish_period ) < |
---|
132 | _Timespec_To_ticks( &schedparam.ss_initial_budget ) ) |
---|
133 | return EINVAL; |
---|
134 | |
---|
135 | if ( !_POSIX_Priority_Is_valid( schedparam.ss_low_priority ) ) |
---|
136 | return EINVAL; |
---|
137 | |
---|
138 | break; |
---|
139 | |
---|
140 | default: |
---|
141 | return EINVAL; |
---|
142 | } |
---|
143 | |
---|
144 | /* |
---|
145 | * Currently all POSIX threads are floating point if the hardware |
---|
146 | * supports it. |
---|
147 | */ |
---|
148 | |
---|
149 | |
---|
150 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
151 | is_fp = TRUE; |
---|
152 | #else |
---|
153 | is_fp = FALSE; |
---|
154 | #endif |
---|
155 | |
---|
156 | /* |
---|
157 | * Disable dispatch for protection |
---|
158 | */ |
---|
159 | |
---|
160 | _Thread_Disable_dispatch(); |
---|
161 | |
---|
162 | /* |
---|
163 | * Allocate the thread control block. |
---|
164 | * |
---|
165 | * NOTE: Global threads are not currently supported. |
---|
166 | */ |
---|
167 | |
---|
168 | the_thread = _POSIX_Threads_Allocate(); |
---|
169 | |
---|
170 | if ( !the_thread ) { |
---|
171 | _Thread_Enable_dispatch(); |
---|
172 | return EAGAIN; |
---|
173 | } |
---|
174 | |
---|
175 | /* |
---|
176 | * Initialize the core thread for this task. |
---|
177 | */ |
---|
178 | |
---|
179 | name.name_p = NULL; /* posix threads don't have a name by default */ |
---|
180 | status = _Thread_Initialize( |
---|
181 | &_POSIX_Threads_Information, |
---|
182 | the_thread, |
---|
183 | the_attr->stackaddr, |
---|
184 | the_attr->stacksize, |
---|
185 | is_fp, |
---|
186 | core_priority, |
---|
187 | TRUE, /* preemptible */ |
---|
188 | budget_algorithm, |
---|
189 | budget_callout, |
---|
190 | 0, /* isr level */ |
---|
191 | name /* posix threads don't have a name */ |
---|
192 | ); |
---|
193 | |
---|
194 | if ( !status ) { |
---|
195 | _POSIX_Threads_Free( the_thread ); |
---|
196 | _Thread_Enable_dispatch(); |
---|
197 | return EAGAIN; |
---|
198 | } |
---|
199 | |
---|
200 | /* |
---|
201 | * finish initializing the per API structure |
---|
202 | */ |
---|
203 | |
---|
204 | |
---|
205 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
206 | |
---|
207 | api->Attributes = *the_attr; |
---|
208 | api->detachstate = the_attr->detachstate; |
---|
209 | api->schedpolicy = schedpolicy; |
---|
210 | api->schedparam = schedparam; |
---|
211 | |
---|
212 | /* |
---|
213 | * This insures we evaluate the process-wide signals pending when we |
---|
214 | * first run. |
---|
215 | * |
---|
216 | * NOTE: Since the thread starts with all unblocked, this is necessary. |
---|
217 | */ |
---|
218 | |
---|
219 | the_thread->do_post_task_switch_extension = TRUE; |
---|
220 | |
---|
221 | /* |
---|
222 | * POSIX threads are allocated and started in one operation. |
---|
223 | */ |
---|
224 | |
---|
225 | status = _Thread_Start( |
---|
226 | the_thread, |
---|
227 | THREAD_START_POINTER, |
---|
228 | start_routine, |
---|
229 | arg, |
---|
230 | 0 /* unused */ |
---|
231 | ); |
---|
232 | |
---|
233 | if ( schedpolicy == SCHED_SPORADIC ) { |
---|
234 | _Watchdog_Insert_ticks( |
---|
235 | &api->Sporadic_timer, |
---|
236 | _Timespec_To_ticks( &api->schedparam.ss_replenish_period ) |
---|
237 | ); |
---|
238 | } |
---|
239 | |
---|
240 | /* |
---|
241 | * _Thread_Start only fails if the thread was in the incorrect state |
---|
242 | * |
---|
243 | * NOTE: This can only happen if someone slips in and touches the |
---|
244 | * thread while we are creating it. |
---|
245 | */ |
---|
246 | |
---|
247 | if ( !status ) { |
---|
248 | _POSIX_Threads_Free( the_thread ); |
---|
249 | _Thread_Enable_dispatch(); |
---|
250 | return EINVAL; |
---|
251 | } |
---|
252 | |
---|
253 | /* |
---|
254 | * Return the id and indicate we successfully created the thread |
---|
255 | */ |
---|
256 | |
---|
257 | *thread = the_thread->Object.id; |
---|
258 | |
---|
259 | _Thread_Enable_dispatch(); |
---|
260 | |
---|
261 | return 0; |
---|
262 | } |
---|