source: rtems/cpukit/posix/src/pthreadcreate.c @ 37eb717

Last change on this file since 37eb717 was 37eb717, checked in by Sebastian Huber <sebastian.huber@…>, on Oct 17, 2017 at 9:46:54 AM

posix: Simplify _POSIX_Threads_Create_extension()

Move unblocked signals initialization to pthread_create().

Update #2514.

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