source: rtems/cpukit/posix/src/pthread.c @ 917884c

5
Last change on this file since 917884c was 917884c, checked in by Sebastian Huber <sebastian.huber@…>, on 06/15/16 at 08:39:09

posix: Fix poradic server initial CPU budget

Update #2738.

  • Property mode set to 100644
File size: 9.0 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/cpusetimpl.h>
41#include <rtems/score/assert.h>
42
43Thread_Information _POSIX_Threads_Information;
44
45/*
46 *  The default pthreads attributes structure.
47 *
48 *  NOTE: Be careful .. if the default attribute set changes,
49 *        _POSIX_Threads_Initialize_user_threads will need to be examined.
50 */
51pthread_attr_t _POSIX_Threads_Default_attributes = {
52  .is_initialized  = true,                       /* is_initialized */
53  .stackaddr       = NULL,                       /* stackaddr */
54  .stacksize       = 0,                          /* stacksize -- will be adjusted to minimum */
55  .contentionscope = PTHREAD_SCOPE_PROCESS,      /* contentionscope */
56  .inheritsched    = PTHREAD_INHERIT_SCHED,      /* inheritsched */
57  .schedpolicy     = SCHED_FIFO,                 /* schedpolicy */
58  .schedparam      =
59  {                           /* schedparam */
60    2,                        /* sched_priority */
61    #if defined(_POSIX_SPORADIC_SERVER) || \
62        defined(_POSIX_THREAD_SPORADIC_SERVER)
63      0,                        /* sched_ss_low_priority */
64      { 0L, 0 },                /* sched_ss_repl_period */
65      { 0L, 0 },                /* sched_ss_init_budget */
66      0                         /* sched_ss_max_repl */
67    #endif
68  },
69
70  #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
71    .guardsize = 0,                            /* guardsize */
72  #endif
73  #if defined(_POSIX_THREAD_CPUTIME)
74    .cputime_clock_allowed = 1,                        /* cputime_clock_allowed */
75  #endif
76  .detachstate             = PTHREAD_CREATE_JOINABLE,    /* detachstate */
77  #if defined(__RTEMS_HAVE_SYS_CPUSET_H__)
78    .affinitysetsize         = 0,
79    .affinityset             = NULL,
80    .affinitysetpreallocated = {{0x0}}
81  #endif
82};
83
84static bool _POSIX_Threads_Sporadic_budget_TSR_filter(
85  Thread_Control   *the_thread,
86  Priority_Control *new_priority,
87  void             *arg
88)
89{
90  the_thread->real_priority = *new_priority;
91
92  /*
93   * If holding a resource, then do not change it.
94   *
95   * If this would make them less important, then do not change it.
96   */
97  return !_Thread_Owns_resources( the_thread ) &&
98    _Thread_Priority_less_than( the_thread->current_priority, *new_priority );
99}
100
101/*
102 *  _POSIX_Threads_Sporadic_budget_TSR
103 */
104void _POSIX_Threads_Sporadic_budget_TSR( Watchdog_Control *watchdog )
105{
106  POSIX_API_Control  *api;
107  Thread_Control     *the_thread;
108  ISR_lock_Context    lock_context;
109  Priority_Control    new_priority;
110
111  api = RTEMS_CONTAINER_OF( watchdog, POSIX_API_Control, Sporadic_timer );
112  the_thread = api->thread;
113
114  _Thread_State_acquire( the_thread, &lock_context );
115
116  _Watchdog_Per_CPU_remove_relative( &api->Sporadic_timer );
117  _POSIX_Threads_Sporadic_timer_insert( the_thread, api );
118
119  new_priority = _POSIX_Priority_To_core( api->schedparam.sched_priority );
120
121  _Thread_State_release( the_thread, &lock_context );
122
123  _Thread_Change_priority(
124    the_thread,
125    new_priority,
126    NULL,
127    _POSIX_Threads_Sporadic_budget_TSR_filter,
128    true
129  );
130}
131
132static bool _POSIX_Threads_Sporadic_budget_callout_filter(
133  Thread_Control   *the_thread,
134  Priority_Control *new_priority,
135  void             *arg
136)
137{
138  the_thread->real_priority = *new_priority;
139
140  /*
141   * If holding a resource, then do not change it.
142   *
143   * Make sure we are actually lowering it. If they have lowered it
144   * to logically lower than sched_ss_low_priority, then we do not want to
145   * change it.
146   */
147  return !_Thread_Owns_resources( the_thread ) &&
148    _Thread_Priority_less_than( *new_priority, the_thread->current_priority );
149}
150
151/*
152 *  _POSIX_Threads_Sporadic_budget_callout
153 */
154void _POSIX_Threads_Sporadic_budget_callout(
155  Thread_Control *the_thread
156)
157{
158  POSIX_API_Control *api;
159
160  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
161
162  /*
163   *  This will prevent the thread from consuming its entire "budget"
164   *  while at low priority.
165   */
166  the_thread->cpu_time_budget = UINT32_MAX;
167
168  _Thread_Change_priority(
169    the_thread,
170    _POSIX_Priority_To_core( api->schedparam.sched_ss_low_priority ),
171    NULL,
172    _POSIX_Threads_Sporadic_budget_callout_filter,
173    true
174  );
175}
176
177/*
178 *  _POSIX_Threads_Create_extension
179 *
180 *  This method is invoked for each thread created.
181 */
182
183static bool _POSIX_Threads_Create_extension(
184  Thread_Control *executing RTEMS_UNUSED,
185  Thread_Control *created
186)
187{
188  POSIX_API_Control *api;
189  POSIX_API_Control *executing_api;
190
191  api = created->API_Extensions[ THREAD_API_POSIX ];
192
193  /* XXX check all fields are touched */
194  api->thread = created;
195  _POSIX_Threads_Initialize_attributes( &api->Attributes );
196  api->schedpolicy = _POSIX_Threads_Default_attributes.schedpolicy;
197  api->schedparam  = _POSIX_Threads_Default_attributes.schedparam;
198  api->schedparam.sched_priority =
199     _POSIX_Priority_From_core( created->current_priority );
200
201  /*
202   *  If the thread is not a posix thread, then all posix signals are blocked
203   *  by default.
204   *
205   *  The check for class == 1 is debug.  Should never really happen.
206   */
207  RTEMS_STATIC_ASSERT( SIGNAL_EMPTY_MASK == 0, signals_pending );
208  if ( _Objects_Get_API( created->Object.id ) == OBJECTS_POSIX_API
209       #if defined(RTEMS_DEBUG)
210         && _Objects_Get_class( created->Object.id ) == 1
211       #endif
212  ) {
213    executing_api = _Thread_Get_executing()->API_Extensions[ THREAD_API_POSIX ];
214    api->signals_unblocked = executing_api->signals_unblocked;
215  }
216
217  _Watchdog_Preinitialize( &api->Sporadic_timer, _Per_CPU_Get_by_index( 0 ) );
218  _Watchdog_Initialize(
219    &api->Sporadic_timer,
220    _POSIX_Threads_Sporadic_budget_TSR
221  );
222
223  return true;
224}
225
226static void _POSIX_Threads_Terminate_extension( Thread_Control *executing )
227{
228  POSIX_API_Control *api;
229  ISR_lock_Context   lock_context;
230
231  api = executing->API_Extensions[ THREAD_API_POSIX ];
232
233  _Thread_State_acquire( executing, &lock_context );
234
235  if ( api->schedpolicy == SCHED_SPORADIC ) {
236    _Watchdog_Per_CPU_remove_relative( &api->Sporadic_timer );
237  }
238
239  _Thread_State_release( executing, &lock_context );
240}
241
242/*
243 *  _POSIX_Threads_Exitted_extension
244 *
245 *  This method is invoked each time a thread exits.
246 */
247static void _POSIX_Threads_Exitted_extension(
248  Thread_Control *executing
249)
250{
251  /*
252   *  If the executing thread was not created with the POSIX API, then this
253   *  API do not get to define its exit behavior.
254   */
255  if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API )
256    pthread_exit( executing->Wait.return_argument );
257}
258
259User_extensions_Control _POSIX_Threads_User_extensions = {
260  .Callouts = {
261    .thread_create    = _POSIX_Threads_Create_extension,
262    .thread_exitted   = _POSIX_Threads_Exitted_extension,
263    .thread_terminate = _POSIX_Threads_Terminate_extension
264  }
265};
266
267/*
268 *  _POSIX_Threads_Manager_initialization
269 *
270 *  This routine initializes all threads manager related data structures.
271 */
272static void _POSIX_Threads_Manager_initialization(void)
273{
274  #if defined(RTEMS_SMP) && defined(__RTEMS_HAVE_SYS_CPUSET_H__)
275    const CPU_set_Control *affinity;
276    pthread_attr_t *attr;
277
278    /* Initialize default attribute. */
279    attr = &_POSIX_Threads_Default_attributes;
280
281    /*  Initialize the affinity to be the default cpu set for the system */
282    affinity = _CPU_set_Default();
283    _Assert( affinity->setsize == sizeof( attr->affinitysetpreallocated ) );
284    attr->affinityset             = &attr->affinitysetpreallocated;
285    attr->affinitysetsize         = affinity->setsize;
286    CPU_COPY( attr->affinityset, affinity->set );
287  #endif
288
289  _Thread_Initialize_information(
290    &_POSIX_Threads_Information, /* object information table */
291    OBJECTS_POSIX_API,           /* object API */
292    OBJECTS_POSIX_THREADS,       /* object class */
293    Configuration_POSIX_API.maximum_threads,
294                                 /* maximum objects of this class */
295    true,                        /* true if names for this object are strings */
296    _POSIX_PATH_MAX              /* maximum length of each object's name */
297  );
298
299  /*
300   *  Add all the extensions for this API
301   */
302  _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions );
303
304  /*
305   *  If we supported MP, then here we would ...
306   *       Register the MP Process Packet routine.
307   */
308}
309
310RTEMS_SYSINIT_ITEM(
311  _POSIX_Threads_Manager_initialization,
312  RTEMS_SYSINIT_POSIX_THREADS,
313  RTEMS_SYSINIT_ORDER_MIDDLE
314);
Note: See TracBrowser for help on using the repository browser.