source: rtems/cpukit/posix/src/pthread.c @ 127c20eb

5
Last change on this file since 127c20eb was 5a32c48, checked in by Sebastian Huber <sebastian.huber@…>, on 06/14/16 at 13:57:54

posix: Make POSIX API aware of scheduler instances

  • Property mode set to 100644
File size: 8.6 KB
Line 
1/**
2 * @file
3 *
4 * @brief Private Support Information for POSIX Threads
5 * @ingroup POSIX_PTHREADS Private Threads
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2014.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20#include <stdio.h>
21
22#include <errno.h>
23#include <pthread.h>
24#include <limits.h>
25#include <assert.h>
26
27#include <rtems/system.h>
28#include <rtems/config.h>
29#include <rtems/sysinit.h>
30#include <rtems/score/stack.h>
31#include <rtems/score/threadimpl.h>
32#include <rtems/score/threadqimpl.h>
33#include <rtems/score/userextimpl.h>
34#include <rtems/score/wkspace.h>
35#include <rtems/posix/pthreadimpl.h>
36#include <rtems/posix/priorityimpl.h>
37#include <rtems/posix/psignalimpl.h>
38#include <rtems/posix/config.h>
39#include <rtems/posix/keyimpl.h>
40#include <rtems/score/assert.h>
41#include <rtems/score/cpusetimpl.h>
42#include <rtems/score/schedulerimpl.h>
43
44Thread_Information _POSIX_Threads_Information;
45
46/*
47 *  The default pthreads attributes structure.
48 *
49 *  NOTE: Be careful .. if the default attribute set changes,
50 *        _POSIX_Threads_Initialize_user_threads will need to be examined.
51 */
52pthread_attr_t _POSIX_Threads_Default_attributes = {
53  .is_initialized  = true,                       /* is_initialized */
54  .stackaddr       = NULL,                       /* stackaddr */
55  .stacksize       = 0,                          /* stacksize -- will be adjusted to minimum */
56  .contentionscope = PTHREAD_SCOPE_PROCESS,      /* contentionscope */
57  .inheritsched    = PTHREAD_INHERIT_SCHED,      /* inheritsched */
58  .schedpolicy     = SCHED_FIFO,                 /* schedpolicy */
59  .schedparam      =
60  {                           /* schedparam */
61    2,                        /* sched_priority */
62    #if defined(_POSIX_SPORADIC_SERVER) || \
63        defined(_POSIX_THREAD_SPORADIC_SERVER)
64      0,                        /* sched_ss_low_priority */
65      { 0L, 0 },                /* sched_ss_repl_period */
66      { 0L, 0 },                /* sched_ss_init_budget */
67      0                         /* sched_ss_max_repl */
68    #endif
69  },
70
71  #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
72    .guardsize = 0,                            /* guardsize */
73  #endif
74  #if defined(_POSIX_THREAD_CPUTIME)
75    .cputime_clock_allowed = 1,                        /* cputime_clock_allowed */
76  #endif
77  .detachstate             = PTHREAD_CREATE_JOINABLE,    /* detachstate */
78  #if defined(__RTEMS_HAVE_SYS_CPUSET_H__)
79    .affinitysetsize         = 0,
80    .affinityset             = NULL,
81    .affinitysetpreallocated = {{0x0}}
82  #endif
83};
84
85static bool _POSIX_Threads_Sporadic_timer_filter(
86  Thread_Control   *the_thread,
87  Priority_Control *new_priority_p,
88  void             *arg
89)
90{
91  POSIX_API_Control *api;
92  Priority_Control   current_priority;
93  Priority_Control   new_priority;
94
95  api = arg;
96
97  new_priority = api->Sporadic.high_priority;
98  *new_priority_p = new_priority;
99
100  current_priority = the_thread->current_priority;
101  the_thread->real_priority = new_priority;
102
103  _Watchdog_Per_CPU_remove_relative( &api->Sporadic.Timer );
104  _POSIX_Threads_Sporadic_timer_insert( the_thread, api );
105
106  return _Thread_Priority_less_than( current_priority, new_priority )
107    || !_Thread_Owns_resources( the_thread );
108}
109
110static void _POSIX_Threads_Sporadic_timer( Watchdog_Control *watchdog )
111{
112  POSIX_API_Control *api;
113  Thread_Control    *the_thread;
114
115  api = RTEMS_CONTAINER_OF( watchdog, POSIX_API_Control, Sporadic.Timer );
116  the_thread = api->thread;
117
118  _Thread_Change_priority(
119    the_thread,
120    0,
121    api,
122    _POSIX_Threads_Sporadic_timer_filter,
123    true
124  );
125}
126
127static bool _POSIX_Threads_Sporadic_budget_callout_filter(
128  Thread_Control   *the_thread,
129  Priority_Control *new_priority_p,
130  void             *arg
131)
132{
133  POSIX_API_Control *api;
134  Priority_Control   current_priority;
135  Priority_Control   new_priority;
136
137  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
138
139  /*
140   *  This will prevent the thread from consuming its entire "budget"
141   *  while at low priority.
142   */
143  the_thread->cpu_time_budget = UINT32_MAX;
144
145  new_priority = api->Sporadic.low_priority;
146  *new_priority_p = new_priority;
147
148  current_priority = the_thread->current_priority;
149  the_thread->real_priority = new_priority;
150
151  return _Thread_Priority_less_than( current_priority, new_priority )
152    || !_Thread_Owns_resources( the_thread );
153}
154
155void _POSIX_Threads_Sporadic_budget_callout( Thread_Control *the_thread )
156{
157  _Thread_Change_priority(
158    the_thread,
159    0,
160    NULL,
161    _POSIX_Threads_Sporadic_budget_callout_filter,
162    true
163  );
164}
165
166/*
167 *  _POSIX_Threads_Create_extension
168 *
169 *  This method is invoked for each thread created.
170 */
171
172static bool _POSIX_Threads_Create_extension(
173  Thread_Control *executing RTEMS_UNUSED,
174  Thread_Control *created
175)
176{
177  POSIX_API_Control *api;
178  POSIX_API_Control *executing_api;
179
180  api = created->API_Extensions[ THREAD_API_POSIX ];
181
182  /* XXX check all fields are touched */
183  api->thread = created;
184  _POSIX_Threads_Initialize_attributes( &api->Attributes );
185  api->Attributes.schedparam.sched_priority = _POSIX_Priority_From_core(
186    _Scheduler_Get_own( created ),
187    created->current_priority
188  );
189
190  /*
191   *  If the thread is not a posix thread, then all posix signals are blocked
192   *  by default.
193   *
194   *  The check for class == 1 is debug.  Should never really happen.
195   */
196  RTEMS_STATIC_ASSERT( SIGNAL_EMPTY_MASK == 0, signals_pending );
197  if ( _Objects_Get_API( created->Object.id ) == OBJECTS_POSIX_API
198       #if defined(RTEMS_DEBUG)
199         && _Objects_Get_class( created->Object.id ) == 1
200       #endif
201  ) {
202    executing_api = _Thread_Get_executing()->API_Extensions[ THREAD_API_POSIX ];
203    api->signals_unblocked = executing_api->signals_unblocked;
204  }
205
206  _Watchdog_Preinitialize( &api->Sporadic.Timer, _Per_CPU_Get_by_index( 0 ) );
207  _Watchdog_Initialize( &api->Sporadic.Timer, _POSIX_Threads_Sporadic_timer );
208
209  return true;
210}
211
212static void _POSIX_Threads_Terminate_extension( Thread_Control *executing )
213{
214  POSIX_API_Control *api;
215  ISR_lock_Context   lock_context;
216
217  api = executing->API_Extensions[ THREAD_API_POSIX ];
218
219  _Thread_State_acquire( executing, &lock_context );
220
221  if ( api->Attributes.schedpolicy == SCHED_SPORADIC ) {
222    _Watchdog_Per_CPU_remove_relative( &api->Sporadic.Timer );
223  }
224
225  _Thread_State_release( executing, &lock_context );
226}
227
228/*
229 *  _POSIX_Threads_Exitted_extension
230 *
231 *  This method is invoked each time a thread exits.
232 */
233static void _POSIX_Threads_Exitted_extension(
234  Thread_Control *executing
235)
236{
237  /*
238   *  If the executing thread was not created with the POSIX API, then this
239   *  API do not get to define its exit behavior.
240   */
241  if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API )
242    pthread_exit( executing->Wait.return_argument );
243}
244
245User_extensions_Control _POSIX_Threads_User_extensions = {
246  .Callouts = {
247    .thread_create    = _POSIX_Threads_Create_extension,
248    .thread_exitted   = _POSIX_Threads_Exitted_extension,
249    .thread_terminate = _POSIX_Threads_Terminate_extension
250  }
251};
252
253/*
254 *  _POSIX_Threads_Manager_initialization
255 *
256 *  This routine initializes all threads manager related data structures.
257 */
258static void _POSIX_Threads_Manager_initialization(void)
259{
260  #if defined(RTEMS_SMP) && defined(__RTEMS_HAVE_SYS_CPUSET_H__)
261    const CPU_set_Control *affinity;
262    pthread_attr_t *attr;
263
264    /* Initialize default attribute. */
265    attr = &_POSIX_Threads_Default_attributes;
266
267    /*  Initialize the affinity to be the default cpu set for the system */
268    affinity = _CPU_set_Default();
269    _Assert( affinity->setsize == sizeof( attr->affinitysetpreallocated ) );
270    attr->affinityset             = &attr->affinitysetpreallocated;
271    attr->affinitysetsize         = affinity->setsize;
272    CPU_COPY( attr->affinityset, affinity->set );
273  #endif
274
275  _Thread_Initialize_information(
276    &_POSIX_Threads_Information, /* object information table */
277    OBJECTS_POSIX_API,           /* object API */
278    OBJECTS_POSIX_THREADS,       /* object class */
279    Configuration_POSIX_API.maximum_threads,
280                                 /* maximum objects of this class */
281    true,                        /* true if names for this object are strings */
282    _POSIX_PATH_MAX              /* maximum length of each object's name */
283  );
284
285  /*
286   *  Add all the extensions for this API
287   */
288  _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions );
289
290  /*
291   *  If we supported MP, then here we would ...
292   *       Register the MP Process Packet routine.
293   */
294}
295
296RTEMS_SYSINIT_ITEM(
297  _POSIX_Threads_Manager_initialization,
298  RTEMS_SYSINIT_POSIX_THREADS,
299  RTEMS_SYSINIT_ORDER_MIDDLE
300);
Note: See TracBrowser for help on using the repository browser.