source: rtems/cpukit/posix/src/pthreadcreate.c @ 1d572eba

5
Last change on this file since 1d572eba was 1d572eba, checked in by Sebastian Huber <sebastian.huber@…>, on 10/27/17 at 07:03:55

posix: Fix pthread_create() with user stack

In case the user provides a stack with address and size, then do not
alter the stack size.

Close #3211.

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