source: rtems/cpukit/posix/src/pthreadcreate.c @ 016e1e2

4.104.114.95
Last change on this file since 016e1e2 was e889a857, checked in by Joel Sherrill <joel.sherrill@…>, on 02/01/08 at 00:44:15

2008-01-31 Joel Sherrill <joel.sherrill@…>

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