source: rtems/cpukit/posix/src/pthreadcreate.c @ 5cb175bb

4.115
Last change on this file since 5cb175bb was 5cb175bb, checked in by Joel Sherrill <joel.sherrill@…>, on 01/10/13 at 19:22:31

cpukit/posix: Doxygen group is POSIXAPI

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