source: rtems/cpukit/posix/src/pthreadcreate.c @ 92f4671

4.104.115
Last change on this file since 92f4671 was f8437c8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/04/08 at 15:23:12

Convert to "bool".

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