source: rtems/cpukit/posix/src/pthread.c @ 092fe28e

4.104.114.84.95
Last change on this file since 092fe28e was 092fe28e, checked in by Joel Sherrill <joel.sherrill@…>, on 09/27/04 at 22:44:19

2004-09-27 Joel Sherrill <joel@…>

PR 294/rtems

  • posix/src/pthread.c: POSIX thread exit handler now confirms that it created the executing thread before implicitly exitting it.
  • Property mode set to 100644
File size: 10.2 KB
Line 
1/*
2 *  $Id$
3 */
4
5#if HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9#include <assert.h>
10#include <errno.h>
11#include <pthread.h>
12#include <limits.h>
13
14#include <rtems/system.h>
15#include <rtems/score/apiext.h>
16#include <rtems/score/stack.h>
17#include <rtems/score/thread.h>
18#include <rtems/score/userext.h>
19#include <rtems/score/wkspace.h>
20#include <rtems/posix/cancel.h>
21#include <rtems/posix/pthread.h>
22#include <rtems/posix/priority.h>
23#include <rtems/posix/psignal.h>
24#include <rtems/posix/config.h>
25#include <rtems/posix/key.h>
26#include <rtems/posix/time.h>
27
28/*PAGE
29 *
30 *  The default pthreads attributes structure.
31 *
32 *  NOTE: Be careful .. if the default attribute set changes,
33 *        _POSIX_Threads_Initialize_user_threads will need to be examined.
34 *
35 */
36
37const pthread_attr_t _POSIX_Threads_Default_attributes = {
38  TRUE,                       /* is_initialized */
39  NULL,                       /* stackaddr */
40  PTHREAD_MINIMUM_STACK_SIZE, /* stacksize */
41  PTHREAD_SCOPE_PROCESS,      /* contentionscope */
42  PTHREAD_INHERIT_SCHED,      /* inheritsched */
43  SCHED_FIFO,                 /* schedpolicy */
44  {                           /* schedparam */
45    2,                        /* sched_priority */
46    0,                        /* ss_low_priority */
47    { 0L, 0 },                /* ss_replenish_period */
48    { 0L, 0 }                 /* ss_initial_budget */
49  },
50  PTHREAD_CREATE_JOINABLE,    /* detachstate */
51  1                           /* cputime_clock_allowed */
52};
53
54/*PAGE
55 *
56 *  _POSIX_Threads_Sporadic_budget_TSR
57 */
58
59void _POSIX_Threads_Sporadic_budget_TSR(
60  Objects_Id      id,
61  void           *argument
62)
63{
64  uint32_t            ticks;
65  uint32_t            new_priority;
66  Thread_Control     *the_thread;
67  POSIX_API_Control  *api;
68
69  the_thread = argument;
70
71  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
72
73  ticks = _POSIX_Timespec_to_interval( &api->schedparam.ss_initial_budget );
74
75  if ( !ticks )
76    ticks = 1;
77
78  the_thread->cpu_time_budget = ticks;
79
80  new_priority = _POSIX_Priority_To_core( api->ss_high_priority );
81  the_thread->real_priority = new_priority;
82
83  if ( the_thread->resource_count == 0 ||
84       the_thread->current_priority > new_priority )
85    _Thread_Change_priority( the_thread, new_priority, TRUE );
86
87  ticks = _POSIX_Timespec_to_interval( &api->schedparam.ss_replenish_period );
88
89  if ( !ticks )
90    ticks = 1;
91
92  _Watchdog_Insert_ticks( &api->Sporadic_timer, ticks );
93}
94
95/*PAGE
96 *
97 *  _POSIX_Threads_Sporadic_budget_callout
98 */
99
100void _POSIX_Threads_Sporadic_budget_callout(
101  Thread_Control *the_thread
102)
103{
104  POSIX_API_Control *api;
105  uint32_t           new_priority;
106
107  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
108
109  /*
110   *  This will prevent the thread from consuming its entire "budget"
111   *  while at low priority.
112   */
113
114
115  the_thread->cpu_time_budget = 0xFFFFFFFF; /* XXX should be based on MAX_U32 */
116
117  new_priority = _POSIX_Priority_To_core( api->schedparam.ss_low_priority );
118  the_thread->real_priority = new_priority;
119
120 if ( the_thread->resource_count == 0 ||
121      the_thread->current_priority > new_priority )
122    _Thread_Change_priority( the_thread, new_priority, TRUE );
123}
124
125/*PAGE
126 *
127 *  _POSIX_Threads_Create_extension
128 *
129 *  XXX
130 */
131
132boolean _POSIX_Threads_Create_extension(
133  Thread_Control *executing,
134  Thread_Control *created
135)
136{
137  POSIX_API_Control *api;
138  POSIX_API_Control *executing_api;
139
140  api = _Workspace_Allocate( sizeof( POSIX_API_Control ) );
141
142  if ( !api )
143    return FALSE;
144
145  created->API_Extensions[ THREAD_API_POSIX ] = api;
146
147  /* XXX check all fields are touched */
148  api->Attributes  = _POSIX_Threads_Default_attributes;
149  api->detachstate = _POSIX_Threads_Default_attributes.detachstate;
150  api->schedpolicy = _POSIX_Threads_Default_attributes.schedpolicy;
151  api->schedparam  = _POSIX_Threads_Default_attributes.schedparam;
152  api->schedparam.sched_priority =
153     _POSIX_Priority_From_core( created->current_priority );
154
155  /*
156   *  POSIX 1003.1 1996, 18.2.2.2
157   */
158  api->cancelation_requested = 0;
159  api->cancelability_state = PTHREAD_CANCEL_ENABLE;
160  api->cancelability_type = PTHREAD_CANCEL_DEFERRED;
161  _Chain_Initialize_empty (&api->Cancellation_Handlers);
162
163  /*
164   *  If the thread is not a posix thread, then all posix signals are blocked
165   *  by default.
166   */
167
168  /* XXX use signal constants */
169  api->signals_pending = 0;
170  if ( _Objects_Get_API( created->Object.id ) == OBJECTS_POSIX_API &&
171       _Objects_Get_class( created->Object.id ) == 1 ) {
172    executing_api = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
173    api->signals_blocked = api->signals_blocked;
174  } else
175    api->signals_blocked = 0xffffffff;
176
177  _Thread_queue_Initialize(
178    &api->Join_List,
179    THREAD_QUEUE_DISCIPLINE_FIFO,
180    STATES_WAITING_FOR_JOIN_AT_EXIT,
181    0
182  );
183
184  _Watchdog_Initialize(
185    &api->Sporadic_timer,
186    _POSIX_Threads_Sporadic_budget_TSR,
187    created->Object.id,
188    created
189  );
190
191  return TRUE;
192}
193
194/*PAGE
195 *
196 *  _POSIX_Threads_Delete_extension
197 */
198
199User_extensions_routine _POSIX_Threads_Delete_extension(
200  Thread_Control *executing,
201  Thread_Control *deleted
202)
203{
204  Thread_Control     *the_thread;
205  POSIX_API_Control  *api;
206  void              **value_ptr;
207
208  api = deleted->API_Extensions[ THREAD_API_POSIX ];
209
210  /*
211   *  Run the POSIX cancellation handlers
212   */
213
214  _POSIX_Keys_Run_destructors( deleted );
215
216  /*
217   *  Wakeup all the tasks which joined with this one
218   */
219
220  value_ptr = (void **) deleted->Wait.return_argument;
221
222  while ( (the_thread = _Thread_queue_Dequeue( &api->Join_List )) )
223      *(void **)the_thread->Wait.return_argument = value_ptr;
224
225  if ( api->schedpolicy == SCHED_SPORADIC )
226    (void) _Watchdog_Remove( &api->Sporadic_timer );
227
228  deleted->API_Extensions[ THREAD_API_POSIX ] = NULL;
229
230  (void) _Workspace_Free( api );
231}
232
233/*
234 *
235 *  _POSIX_Threads_Exitted_extension
236 */
237
238User_extensions_routine _POSIX_Threads_Exitted_extension(
239  Thread_Control *executing
240)
241{
242  /*
243   *  If the executing thread was not created with the POSIX API, then this
244   *  API do not get to define its exit behavior.
245   */
246  if ( _Objects_Get_API( executing->Object.id ) == OBJECTS_POSIX_API )
247    pthread_exit( executing->Wait.return_argument );
248}
249
250/*PAGE
251 *
252 *  _POSIX_Threads_Initialize_user_threads
253 *
254 *  This routine creates and starts all configured user
255 *  initialzation threads.
256 *
257 *  Input parameters: NONE
258 *
259 *  Output parameters:  NONE
260 */
261
262void _POSIX_Threads_Initialize_user_threads( void )
263{
264  int                                 status;
265  uint32_t                            index;
266  uint32_t                            maximum;
267  posix_initialization_threads_table *user_threads;
268  pthread_t                           thread_id;
269  pthread_attr_t                      attr;
270
271  user_threads = _POSIX_Threads_User_initialization_threads;
272  maximum      = _POSIX_Threads_Number_of_initialization_threads;
273
274  if ( !user_threads || maximum == 0 )
275    return;
276
277  /*
278   *  Be careful .. if the default attribute set changes, this may need to.
279   *
280   *  Setting the attributes explicitly is critical, since we don't want
281   *  to inherit the idle tasks attributes.
282   */
283
284  for ( index=0 ; index < maximum ; index++ ) {
285    status = pthread_attr_init( &attr );
286    assert( !status );
287
288    status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
289    assert( !status );
290
291    status = pthread_attr_setstacksize( &attr, user_threads[ index ].stack_size);
292    assert( !status );
293
294    status = pthread_create(
295      &thread_id,
296      &attr,
297      user_threads[ index ].thread_entry,
298      NULL
299    );
300    assert( !status );
301  }
302}
303
304/*PAGE
305 *
306 *  API Extension control structures
307 */
308
309API_extensions_Control _POSIX_Threads_API_extensions = {
310  { NULL, NULL },
311  NULL,                                     /* predriver */
312  _POSIX_Threads_Initialize_user_threads,   /* postdriver */
313  _POSIX_signals_Post_switch_extension,     /* post switch */
314};
315
316User_extensions_Control _POSIX_Threads_User_extensions = {
317  { NULL, NULL },
318  { { NULL, NULL }, NULL },
319  { _POSIX_Threads_Create_extension,          /* create */
320    NULL,                                     /* start */
321    NULL,                                     /* restart */
322    _POSIX_Threads_Delete_extension,          /* delete */
323    NULL,                                     /* switch */
324    NULL,                                     /* begin */
325    _POSIX_Threads_Exitted_extension,         /* exitted */
326    NULL                                      /* fatal */
327  }
328};
329
330/*PAGE
331 *
332 *  _POSIX_Threads_Manager_initialization
333 *
334 *  This routine initializes all threads manager related data structures.
335 *
336 *  Input parameters:
337 *    maximum_pthreads - maximum configured pthreads
338 *
339 *  Output parameters:  NONE
340 */
341
342void _POSIX_Threads_Manager_initialization(
343  uint32_t                            maximum_pthreads,
344  uint32_t                            number_of_initialization_threads,
345  posix_initialization_threads_table *user_threads
346
347)
348{
349  _POSIX_Threads_Number_of_initialization_threads =
350                                           number_of_initialization_threads;
351  _POSIX_Threads_User_initialization_threads = user_threads;
352
353  /*
354   *  There may not be any POSIX initialization threads configured.
355   */
356
357#if 0
358  if ( user_threads == NULL || number_of_initialization_threads == 0 )
359    _Internal_error_Occurred( INTERNAL_ERROR_POSIX_API, TRUE, EINVAL );
360#endif
361
362  _Objects_Initialize_information(
363    &_POSIX_Threads_Information, /* object information table */
364    OBJECTS_POSIX_API,           /* object API */
365    OBJECTS_POSIX_THREADS,       /* object class */
366    maximum_pthreads,            /* maximum objects of this class */
367    sizeof( Thread_Control ),
368                                 /* size of this object's control block */
369    FALSE,                       /* TRUE if names for this object are strings */
370    0                            /* maximum length of each object's name */
371#if defined(RTEMS_MULTIPROCESSING)
372    ,
373    FALSE,                       /* TRUE if this is a global object class */
374    NULL                         /* Proxy extraction support callout */
375#endif
376  );
377
378  /*
379   *  Add all the extensions for this API
380   */
381
382  _User_extensions_Add_API_set( &_POSIX_Threads_User_extensions );
383
384  _API_extensions_Add( &_POSIX_Threads_API_extensions );
385
386
387  /*
388   *  If we supported MP, then here we would ...
389   *       Register the MP Process Packet routine.
390   */
391
392}
Note: See TracBrowser for help on using the repository browser.