source: rtems/cpukit/posix/src/pthread.c @ 03b900d

5
Last change on this file since 03b900d was 03b900d, checked in by Sebastian Huber <sebastian.huber@…>, on 02/18/16 at 07:36:26

score: Replace watchdog handler implementation

Use a red-black tree instead of delta chains.

Close #2344.
Update #2554.
Update #2555.
Close #2606.

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