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

4.115
Last change on this file since f5d6c8b was f5d6c8b, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/15 at 14:25:52

score: Delete Thread_queue_Control::timeout_status

Use a parameter for _Thread_queue_Enqueue() instead to reduce memory
usage.

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