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