source: rtems/cpukit/posix/src/pthreadcreate.c @ 33829ce

5
Last change on this file since 33829ce was 33829ce, checked in by Sebastian Huber <sebastian.huber@…>, on 05/13/16 at 12:07:23

score: Avoid Giant lock for _Thread_Start()

Update #2555.

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