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