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

4.115
Last change on this file since be1b8a7 was 4b48ece0, checked in by Sebastian Huber <sebastian.huber@…>, on 07/22/13 at 08:21:03

score: Create watchdog implementation header

Move implementation specific parts of watchdog.h and watchdog.inl into
new header file watchdogimpl.h. The watchdog.h contains now only the
application visible API.

  • Property mode set to 100644
File size: 10.4 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-2010.
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.com/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <errno.h>
22#include <pthread.h>
23#include <limits.h>
24
25#include <rtems/system.h>
26#include <rtems/config.h>
27#include <rtems/score/apiext.h>
28#include <rtems/score/stack.h>
29#include <rtems/score/thread.h>
30#include <rtems/score/threadq.h>
31#include <rtems/score/userextimpl.h>
32#include <rtems/score/watchdogimpl.h>
33#include <rtems/score/wkspace.h>
34#include <rtems/posix/cancel.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/key.h>
40#include <rtems/posix/time.h>
41#include <rtems/score/timespec.h>
42
43/*
44 *  The default pthreads attributes structure.
45 *
46 *  NOTE: Be careful .. if the default attribute set changes,
47 *        _POSIX_Threads_Initialize_user_threads will need to be examined.
48 */
49const pthread_attr_t _POSIX_Threads_Default_attributes = {
50  true,                       /* is_initialized */
51  NULL,                       /* stackaddr */
52  0,                          /* stacksize -- will be adjusted to minimum */
53  PTHREAD_SCOPE_PROCESS,      /* contentionscope */
54  PTHREAD_INHERIT_SCHED,      /* inheritsched */
55  SCHED_FIFO,                 /* schedpolicy */
56  {                           /* schedparam */
57    2,                        /* sched_priority */
58    #if defined(_POSIX_SPORADIC_SERVER) || \
59        defined(_POSIX_THREAD_SPORADIC_SERVER)
60      0,                        /* sched_ss_low_priority */
61      { 0L, 0 },                /* sched_ss_repl_period */
62      { 0L, 0 },                /* sched_ss_init_budget */
63      0                         /* sched_ss_max_repl */
64    #endif
65  },
66  #if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
67    0,                        /* guardsize */
68  #endif
69  #if defined(_POSIX_THREAD_CPUTIME)
70    1,                        /* cputime_clock_allowed */
71  #endif
72  PTHREAD_CREATE_JOINABLE,    /* detachstate */
73};
74
75/*
76 *  _POSIX_Threads_Sporadic_budget_TSR
77 */
78void _POSIX_Threads_Sporadic_budget_TSR(
79  Objects_Id      id __attribute__((unused)),
80  void           *argument
81)
82{
83  uint32_t            ticks;
84  uint32_t            new_priority;
85  Thread_Control     *the_thread;
86  POSIX_API_Control  *api;
87
88  the_thread = argument;
89
90  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
91
92  /* ticks is guaranteed to be at least one */
93  ticks = _Timespec_To_ticks( &api->schedparam.sched_ss_init_budget );
94
95  the_thread->cpu_time_budget = ticks;
96
97  new_priority = _POSIX_Priority_To_core( api->schedparam.sched_priority );
98  the_thread->real_priority = new_priority;
99
100  /*
101   *  If holding a resource, then do not change it.
102   */
103  #if 0
104    printk( "TSR %d %d %d\n", the_thread->resource_count,
105        the_thread->current_priority, new_priority );
106  #endif
107  if ( the_thread->resource_count == 0 ) {
108    /*
109     *  If this would make them less important, then do not change it.
110     */
111    if ( the_thread->current_priority > new_priority ) {
112      _Thread_Change_priority( the_thread, new_priority, true );
113      #if 0
114        printk( "raise priority\n" );
115      #endif
116    }
117  }
118
119  /* ticks is guaranteed to be at least one */
120  ticks = _Timespec_To_ticks( &api->schedparam.sched_ss_repl_period );
121
122  _Watchdog_Insert_ticks( &api->Sporadic_timer, ticks );
123}
124
125/*
126 *  _POSIX_Threads_Sporadic_budget_callout
127 */
128void _POSIX_Threads_Sporadic_budget_callout(
129  Thread_Control *the_thread
130)
131{
132  POSIX_API_Control *api;
133  uint32_t           new_priority;
134
135  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
136
137  /*
138   *  This will prevent the thread from consuming its entire "budget"
139   *  while at low priority.
140   */
141  the_thread->cpu_time_budget = 0xFFFFFFFF; /* XXX should be based on MAX_U32 */
142
143  new_priority = _POSIX_Priority_To_core(api->schedparam.sched_ss_low_priority);
144  the_thread->real_priority = new_priority;
145
146  /*
147   *  If holding a resource, then do not change it.
148   */
149  #if 0
150    printk( "callout %d %d %d\n", the_thread->resource_count,
151        the_thread->current_priority, new_priority );
152  #endif
153  if ( the_thread->resource_count == 0 ) {
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    if ( the_thread->current_priority < new_priority ) {
160      _Thread_Change_priority( the_thread, new_priority, true );
161      #if 0
162        printk( "lower priority\n" );
163      #endif
164    }
165  }
166}
167
168/*
169 *  _POSIX_Threads_Create_extension
170 *
171 *  This method is invoked for each thread created.
172 */
173
174static bool _POSIX_Threads_Create_extension(
175  Thread_Control *executing __attribute__((unused)),
176  Thread_Control *created
177)
178{
179  POSIX_API_Control *api;
180  POSIX_API_Control *executing_api;
181
182  api = _Workspace_Allocate( sizeof( POSIX_API_Control ) );
183
184  if ( !api )
185    return false;
186
187  created->API_Extensions[ THREAD_API_POSIX ] = api;
188
189  /* XXX check all fields are touched */
190  api->Attributes  = _POSIX_Threads_Default_attributes;
191  api->detachstate = _POSIX_Threads_Default_attributes.detachstate;
192  api->schedpolicy = _POSIX_Threads_Default_attributes.schedpolicy;
193  api->schedparam  = _POSIX_Threads_Default_attributes.schedparam;
194  api->schedparam.sched_priority =
195     _POSIX_Priority_From_core( created->current_priority );
196
197  /*
198   *  POSIX 1003.1 1996, 18.2.2.2
199   */
200  api->cancelation_requested = 0;
201  api->cancelability_state = PTHREAD_CANCEL_ENABLE;
202  api->cancelability_type = PTHREAD_CANCEL_DEFERRED;
203  _Chain_Initialize_empty (&api->Cancellation_Handlers);
204
205  /*
206   *  If the thread is not a posix thread, then all posix signals are blocked
207   *  by default.
208   *
209   *  The check for class == 1 is debug.  Should never really happen.
210   */
211
212  /* XXX use signal constants */
213  api->signals_pending = 0;
214  if ( _Objects_Get_API( created->Object.id ) == OBJECTS_POSIX_API
215       #if defined(RTEMS_DEBUG)
216         && _Objects_Get_class( created->Object.id ) == 1
217       #endif
218  ) {
219    executing_api = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
220    api->signals_blocked = executing_api->signals_blocked;
221  } else {
222    api->signals_blocked = 0xffffffff;
223  }
224
225  _Thread_queue_Initialize(
226    &api->Join_List,
227    THREAD_QUEUE_DISCIPLINE_FIFO,
228    STATES_WAITING_FOR_JOIN_AT_EXIT | STATES_INTERRUPTIBLE_BY_SIGNAL,
229    0
230  );
231
232  _Watchdog_Initialize(
233    &api->Sporadic_timer,
234    _POSIX_Threads_Sporadic_budget_TSR,
235    created->Object.id,
236    created
237  );
238
239  return true;
240}
241
242/*
243 *  _POSIX_Threads_Delete_extension
244 *
245 *  This method is invoked for each thread deleted.
246 */
247static void _POSIX_Threads_Delete_extension(
248  Thread_Control *executing __attribute__((unused)),
249  Thread_Control *deleted
250)
251{
252  Thread_Control     *the_thread;
253  POSIX_API_Control  *api;
254  void              **value_ptr;
255
256  api = deleted->API_Extensions[ THREAD_API_POSIX ];
257
258  /*
259   *  Run the POSIX cancellation handlers
260   */
261  _POSIX_Threads_cancel_run( deleted );
262
263  /*
264   *  Run all the key destructors
265   */
266  _POSIX_Keys_Run_destructors( deleted );
267
268  /*
269   *  Wakeup all the tasks which joined with this one
270   */
271  value_ptr = (void **) deleted->Wait.return_argument;
272
273  while ( (the_thread = _Thread_queue_Dequeue( &api->Join_List )) )
274      *(void **)the_thread->Wait.return_argument = value_ptr;
275
276  if ( api->schedpolicy == SCHED_SPORADIC )
277    (void) _Watchdog_Remove( &api->Sporadic_timer );
278
279  deleted->API_Extensions[ THREAD_API_POSIX ] = NULL;
280
281  _Workspace_Free( api );
282}
283
284/*
285 *  _POSIX_Threads_Exitted_extension
286 *
287 *  This method is invoked each time a thread exits.
288 */
289static void _POSIX_Threads_Exitted_extension(
290  Thread_Control *executing
291)
292{
293  /*
294   *  If the executing thread was not created with the POSIX API, then this
295   *  API do not get to define its exit behavior.
296   */
297  if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API )
298    pthread_exit( executing->Wait.return_argument );
299}
300
301/*
302 *  _POSIX_Threads_Initialize_user_threads
303 *
304 *  This routine creates and starts all configured user
305 *  initialization threads.
306 */
307static void _POSIX_Threads_Initialize_user_threads( void )
308{
309  if ( _POSIX_Threads_Initialize_user_threads_p )
310    (*_POSIX_Threads_Initialize_user_threads_p)();
311}
312
313/*
314 *  API Extension control structures
315 */
316API_extensions_Control _POSIX_Threads_API_extensions = {
317  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
318    .predriver_hook = NULL,
319  #endif
320  .postdriver_hook = _POSIX_Threads_Initialize_user_threads
321};
322
323User_extensions_Control _POSIX_Threads_User_extensions = {
324  { NULL, NULL },
325  { { NULL, NULL }, NULL },
326  { _POSIX_Threads_Create_extension,          /* create */
327    NULL,                                     /* start */
328    NULL,                                     /* restart */
329    _POSIX_Threads_Delete_extension,          /* delete */
330    NULL,                                     /* switch */
331    NULL,                                     /* begin */
332    _POSIX_Threads_Exitted_extension,         /* exitted */
333    NULL                                      /* fatal */
334  }
335};
336
337/*
338 *  _POSIX_Threads_Manager_initialization
339 *
340 *  This routine initializes all threads manager related data structures.
341 */
342void _POSIX_Threads_Manager_initialization(void)
343{
344  _Objects_Initialize_information(
345    &_POSIX_Threads_Information, /* object information table */
346    OBJECTS_POSIX_API,           /* object API */
347    OBJECTS_POSIX_THREADS,       /* object class */
348    Configuration_POSIX_API.maximum_threads,
349                                 /* maximum objects of this class */
350    sizeof( Thread_Control ),
351                                 /* size of this object's control block */
352    true,                        /* true if names for this object are strings */
353    _POSIX_PATH_MAX              /* maximum length of each object's name */
354#if defined(RTEMS_MULTIPROCESSING)
355    ,
356    false,                       /* true if this is a global object class */
357    NULL                         /* Proxy extraction support callout */
358#endif
359  );
360
361  /*
362   *  Add all the extensions for this API
363   */
364  _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions );
365
366  _API_extensions_Add( &_POSIX_Threads_API_extensions );
367
368  /*
369   *  If we supported MP, then here we would ...
370   *       Register the MP Process Packet routine.
371   */
372}
Note: See TracBrowser for help on using the repository browser.