source: rtems/cpukit/posix/src/pthread.c @ 2ad250e

4.115
Last change on this file since 2ad250e was 2ad250e, checked in by Sebastian Huber <sebastian.huber@…>, on 08/06/13 at 12:46:24

posix: Create key implementation header

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

  • 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-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/threadimpl.h>
30#include <rtems/score/threadqimpl.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/keyimpl.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  /** initialize thread's key vaule node chain */
240  _Chain_Initialize_empty( &api->Key_Chain );
241
242  return true;
243}
244
245/*
246 *  _POSIX_Threads_Delete_extension
247 *
248 *  This method is invoked for each thread deleted.
249 */
250static void _POSIX_Threads_Delete_extension(
251  Thread_Control *executing __attribute__((unused)),
252  Thread_Control *deleted
253)
254{
255  Thread_Control     *the_thread;
256  POSIX_API_Control  *api;
257  void              **value_ptr;
258
259  api = deleted->API_Extensions[ THREAD_API_POSIX ];
260
261  /*
262   *  Run the POSIX cancellation handlers
263   */
264  _POSIX_Threads_cancel_run( deleted );
265
266  /*
267   *  Run all the key destructors
268   */
269  _POSIX_Keys_Run_destructors( deleted );
270
271  /*
272   *  Wakeup all the tasks which joined with this one
273   */
274  value_ptr = (void **) deleted->Wait.return_argument;
275
276  while ( (the_thread = _Thread_queue_Dequeue( &api->Join_List )) )
277      *(void **)the_thread->Wait.return_argument = value_ptr;
278
279  if ( api->schedpolicy == SCHED_SPORADIC )
280    (void) _Watchdog_Remove( &api->Sporadic_timer );
281
282  deleted->API_Extensions[ THREAD_API_POSIX ] = NULL;
283
284  _Workspace_Free( api );
285}
286
287/*
288 *  _POSIX_Threads_Exitted_extension
289 *
290 *  This method is invoked each time a thread exits.
291 */
292static void _POSIX_Threads_Exitted_extension(
293  Thread_Control *executing
294)
295{
296  /*
297   *  If the executing thread was not created with the POSIX API, then this
298   *  API do not get to define its exit behavior.
299   */
300  if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API )
301    pthread_exit( executing->Wait.return_argument );
302}
303
304/*
305 *  _POSIX_Threads_Initialize_user_threads
306 *
307 *  This routine creates and starts all configured user
308 *  initialization threads.
309 */
310static void _POSIX_Threads_Initialize_user_threads( void )
311{
312  if ( _POSIX_Threads_Initialize_user_threads_p )
313    (*_POSIX_Threads_Initialize_user_threads_p)();
314}
315
316/*
317 *  API Extension control structures
318 */
319API_extensions_Control _POSIX_Threads_API_extensions = {
320  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
321    .predriver_hook = NULL,
322  #endif
323  .postdriver_hook = _POSIX_Threads_Initialize_user_threads
324};
325
326User_extensions_Control _POSIX_Threads_User_extensions = {
327  { NULL, NULL },
328  { { NULL, NULL }, NULL },
329  { _POSIX_Threads_Create_extension,          /* create */
330    NULL,                                     /* start */
331    NULL,                                     /* restart */
332    _POSIX_Threads_Delete_extension,          /* delete */
333    NULL,                                     /* switch */
334    NULL,                                     /* begin */
335    _POSIX_Threads_Exitted_extension,         /* exitted */
336    NULL                                      /* fatal */
337  }
338};
339
340/*
341 *  _POSIX_Threads_Manager_initialization
342 *
343 *  This routine initializes all threads manager related data structures.
344 */
345void _POSIX_Threads_Manager_initialization(void)
346{
347  _Objects_Initialize_information(
348    &_POSIX_Threads_Information, /* object information table */
349    OBJECTS_POSIX_API,           /* object API */
350    OBJECTS_POSIX_THREADS,       /* object class */
351    Configuration_POSIX_API.maximum_threads,
352                                 /* maximum objects of this class */
353    sizeof( Thread_Control ),
354                                 /* size of this object's control block */
355    true,                        /* true if names for this object are strings */
356    _POSIX_PATH_MAX              /* maximum length of each object's name */
357#if defined(RTEMS_MULTIPROCESSING)
358    ,
359    false,                       /* true if this is a global object class */
360    NULL                         /* Proxy extraction support callout */
361#endif
362  );
363
364  /*
365   *  Add all the extensions for this API
366   */
367  _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions );
368
369  _API_extensions_Add( &_POSIX_Threads_API_extensions );
370
371  /*
372   *  If we supported MP, then here we would ...
373   *       Register the MP Process Packet routine.
374   */
375}
Note: See TracBrowser for help on using the repository browser.