source: rtems/cpukit/posix/src/pthreadcreate.c @ 77ff5599

Last change on this file since 77ff5599 was 77ff5599, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 10, 2016 at 6:48:54 AM

score: Introduce map priority scheduler operation

Introduce map/unmap priority scheduler operations to map thread priority
values from/to the user domain to/from the scheduler domain. Use the
map priority operation to validate the thread priority. The EDF
schedulers use this new operation to distinguish between normal
priorities and priorities obtain through a job release.

Update #2173.
Update #2556.

  • Property mode set to 100644
File size: 7.3 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/schedulerimpl.h>
35
36static inline size_t _POSIX_Threads_Ensure_minimum_stack (
37  size_t size
38)
39{
40  if ( size >= PTHREAD_MINIMUM_STACK_SIZE )
41    return size;
42  return PTHREAD_MINIMUM_STACK_SIZE;
43}
44
45
46int pthread_create(
47  pthread_t              *thread,
48  const pthread_attr_t   *attr,
49  void                 *(*start_routine)( void * ),
50  void                   *arg
51)
52{
53  Thread_Entry_information entry = {
54    .adaptor = _Thread_Entry_adaptor_pointer,
55    .Kinds = {
56      .Pointer = {
57        .entry = start_routine,
58        .argument = arg
59      }
60    }
61  };
62  const pthread_attr_t               *the_attr;
63  int                                 low_prio;
64  int                                 high_prio;
65  bool                                valid;
66  Priority_Control                    core_low_prio;
67  Priority_Control                    core_high_prio;
68  Thread_CPU_budget_algorithms        budget_algorithm;
69  Thread_CPU_budget_algorithm_callout budget_callout;
70  bool                                is_fp;
71  bool                                status;
72  Thread_Control                     *the_thread;
73  Thread_Control                     *executing;
74  const Scheduler_Control            *scheduler;
75  POSIX_API_Control                  *api;
76  int                                 schedpolicy = SCHED_RR;
77  struct sched_param                  schedparam;
78  Objects_Name                        name;
79  int                                 error;
80  ISR_lock_Context                    lock_context;
81
82  if ( !start_routine )
83    return EFAULT;
84
85  the_attr = (attr) ? attr : &_POSIX_Threads_Default_attributes;
86
87  if ( !the_attr->is_initialized )
88    return EINVAL;
89
90  /*
91   *  Core Thread Initialize ensures we get the minimum amount of
92   *  stack space if it is allowed to allocate it itself.
93   *
94   *  NOTE: If the user provides the stack we will let it drop below
95   *        twice the minimum.
96   */
97  if ( the_attr->stackaddr && !_Stack_Is_enough(the_attr->stacksize) )
98    return EINVAL;
99
100  #if 0
101    int  cputime_clock_allowed;  /* see time.h */
102    rtems_set_errno_and_return_minus_one( ENOSYS );
103  #endif
104
105  executing = _Thread_Get_executing();
106  scheduler = _Scheduler_Get_own( 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      api = executing->API_Extensions[ THREAD_API_POSIX ];
119      schedpolicy = api->Attributes.schedpolicy;
120      schedparam  = api->Attributes.schedparam;
121      break;
122
123    case PTHREAD_EXPLICIT_SCHED:
124      schedpolicy = the_attr->schedpolicy;
125      schedparam  = the_attr->schedparam;
126      break;
127
128    default:
129      return EINVAL;
130  }
131
132  /*
133   *  Check the contentionscope since rtems only supports PROCESS wide
134   *  contention (i.e. no system wide contention).
135   */
136  if ( the_attr->contentionscope != PTHREAD_SCOPE_PROCESS )
137    return ENOTSUP;
138
139  error = _POSIX_Thread_Translate_sched_param(
140    schedpolicy,
141    &schedparam,
142    &budget_algorithm,
143    &budget_callout
144  );
145  if ( error != 0 ) {
146    return error;
147  }
148
149  if ( schedpolicy == SCHED_SPORADIC ) {
150    low_prio = schedparam.sched_ss_low_priority;
151    high_prio = schedparam.sched_priority;
152  } else {
153    low_prio = schedparam.sched_priority;
154    high_prio = low_prio;
155  }
156
157  scheduler = _Scheduler_Get_own( executing );
158
159  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid );
160  if ( !valid ) {
161    return EINVAL;
162  }
163
164  core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio, &valid );
165  if ( !valid ) {
166    return EINVAL;
167  }
168
169#if defined(RTEMS_SMP)
170#if __RTEMS_HAVE_SYS_CPUSET_H__
171  status = _CPU_set_Is_valid( the_attr->affinityset, the_attr->affinitysetsize );
172  if ( !status )
173    return EINVAL;
174#endif
175#endif
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_high_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#if defined(RTEMS_SMP) && __RTEMS_HAVE_SYS_CPUSET_H__
225  _ISR_lock_ISR_disable( &lock_context );
226   status = _Scheduler_Set_affinity(
227     the_thread,
228     the_attr->affinitysetsize,
229     the_attr->affinityset
230   );
231  _ISR_lock_ISR_enable( &lock_context );
232   if ( !status ) {
233     _POSIX_Threads_Free( the_thread );
234     _RTEMS_Unlock_allocator();
235     return EINVAL;
236   }
237#endif
238
239  /*
240   *  finish initializing the per API structure
241   */
242  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
243
244  _POSIX_Threads_Copy_attributes( &api->Attributes, the_attr );
245  api->Sporadic.low_priority = core_low_prio;
246  api->Sporadic.high_priority = core_high_prio;
247
248  if ( schedpolicy == SCHED_SPORADIC ) {
249    _ISR_lock_ISR_disable( &lock_context );
250    _POSIX_Threads_Sporadic_timer_insert( the_thread, api );
251    _ISR_lock_ISR_enable( &lock_context );
252  }
253
254  /*
255   *  POSIX threads are allocated and started in one operation.
256   */
257  _ISR_lock_ISR_disable( &lock_context );
258  status = _Thread_Start( the_thread, &entry, &lock_context );
259
260  #if defined(RTEMS_DEBUG)
261    /*
262     *  _Thread_Start only fails if the thread was in the incorrect state
263     *
264     *  NOTE: This can only happen if someone slips in and touches the
265     *        thread while we are creating it.
266     */
267    if ( !status ) {
268      _POSIX_Threads_Free( the_thread );
269      _Objects_Allocator_unlock();
270      return EINVAL;
271    }
272  #endif
273
274  /*
275   *  Return the id and indicate we successfully created the thread
276   */
277  *thread = the_thread->Object.id;
278
279  _Objects_Allocator_unlock();
280  return 0;
281}
Note: See TracBrowser for help on using the repository browser.