source: rtems/cpukit/posix/src/pthreadcreate.c @ ccd5434

5
Last change on this file since ccd5434 was ccd5434, checked in by Sebastian Huber <sebastian.huber@…>, on 01/07/16 at 08:55:45

score: Introduce Thread_Entry_information

This avoids potential dead code in _Thread_Handler(). It gets rid of
the dangerous function pointer casts.

Update #2514.

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