source: rtems/cpukit/posix/src/pthreadcreate.c @ 51e008dd

Last change on this file since 51e008dd was 51e008dd, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 23, 2016 at 9:49:22 AM

posix: Fix pthread_create()

Do not access data of the executing thread without proper locks.

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