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

5
Last change on this file since da826560 was da826560, checked in by Sebastian Huber <sebastian.huber@…>, on May 13, 2016 at 8:28:14 AM

posix: Rework thread cancellation

Add Thread_Life_state::THREAD_LIFE_CHANGE_DEFERRED and rework the POSIX
thread cancellation to use the thread life states.

Update #2555.
Update #2626.

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