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

5
Last change on this file since a3ad4af was a3ad4af, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/17 at 09:22:21

posix: Validate affinity sets by the scheduler

Update #2514.

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