source: rtems/cpukit/posix/src/pthreadcreate.c @ 03b900d

5
Last change on this file since 03b900d was 03b900d, checked in by Sebastian Huber <sebastian.huber@…>, on 02/18/16 at 07:36:26

score: Replace watchdog handler implementation

Use a red-black tree instead of delta chains.

Close #2344.
Update #2554.
Update #2555.
Close #2606.

  • Property mode set to 100644
File size: 6.7 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_Level                           level;
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 defined(RTEMS_SMP) && __RTEMS_HAVE_SYS_CPUSET_H__
205   status = _Scheduler_Set_affinity(
206     the_thread,
207     the_attr->affinitysetsize,
208     the_attr->affinityset
209   );
210   if ( !status ) {
211     _POSIX_Threads_Free( the_thread );
212     _RTEMS_Unlock_allocator();
213     return EINVAL;
214   }
215#endif
216
217  /*
218   *  finish initializing the per API structure
219   */
220  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
221
222  _POSIX_Threads_Copy_attributes( &api->Attributes, the_attr );
223  api->detachstate = the_attr->detachstate;
224  api->schedpolicy = schedpolicy;
225  api->schedparam  = schedparam;
226
227  _Thread_Disable_dispatch();
228
229  /*
230   *  POSIX threads are allocated and started in one operation.
231   */
232  status = _Thread_Start( the_thread, &entry );
233
234  #if defined(RTEMS_DEBUG)
235    /*
236     *  _Thread_Start only fails if the thread was in the incorrect state
237     *
238     *  NOTE: This can only happen if someone slips in and touches the
239     *        thread while we are creating it.
240     */
241    if ( !status ) {
242      _Thread_Enable_dispatch();
243      _POSIX_Threads_Free( the_thread );
244      _Objects_Allocator_unlock();
245      return EINVAL;
246    }
247  #endif
248
249  if ( schedpolicy == SCHED_SPORADIC ) {
250    _ISR_Disable( level );
251    _Watchdog_Per_CPU_insert_relative(
252      &api->Sporadic_timer,
253      _Per_CPU_Get(),
254      _Timespec_To_ticks( &api->schedparam.sched_ss_repl_period )
255    );
256    _ISR_Enable( level );
257  }
258
259  _Thread_Enable_dispatch();
260
261  /*
262   *  Return the id and indicate we successfully created the thread
263   */
264  *thread = the_thread->Object.id;
265
266  _Objects_Allocator_unlock();
267  return 0;
268}
Note: See TracBrowser for help on using the repository browser.