source: rtems/cpukit/posix/src/pthreadcreate.c @ 23fec9f0

4.115
Last change on this file since 23fec9f0 was 23fec9f0, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/14 at 13:16:12

score: PR2152: Use allocator mutex for objects

Use allocator mutex for objects allocate/free. This prevents that the
thread dispatch latency depends on the workspace/heap fragmentation.

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