source: rtems/cpukit/rtems/src/tasks.c @ 1b1be254

4.115
Last change on this file since 1b1be254 was 1b1be254, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 09:54:49

score: Thread life cycle re-implementation

The thread deletion is now supported on SMP.

This change fixes the following PRs:

PR1814: SMP race condition between stack free and dispatch

PR2035: psxcancel reveals NULL pointer access in _Thread_queue_Extract()

The POSIX cleanup handler are now called in the right context (should be
called in the context of the terminating thread).

http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_09.html

Add a user extension the reflects a thread termination event. This is
used to reclaim the Newlib reentrancy structure (may use file
operations), the POSIX cleanup handlers and the POSIX key destructors.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief RTEMS Task API Extensions
5 *  @ingroup ClassicTasks
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
21#include <rtems/system.h>
22#include <rtems/config.h>
23#include <rtems/rtems/asrimpl.h>
24#include <rtems/rtems/eventimpl.h>
25#include <rtems/rtems/signalimpl.h>
26#include <rtems/rtems/status.h>
27#include <rtems/rtems/support.h>
28#include <rtems/rtems/modes.h>
29#include <rtems/rtems/tasksimpl.h>
30#include <rtems/posix/keyimpl.h>
31#include <rtems/score/stack.h>
32#include <rtems/score/threadimpl.h>
33#include <rtems/score/userextimpl.h>
34#include <rtems/score/wkspace.h>
35#include <rtems/score/apiext.h>
36
37/*
38 *  _RTEMS_tasks_Create_extension
39 *
40 *  This routine is an extension routine that is invoked as part
41 *  of creating any type of task or thread in the system.  If the
42 *  task is created via another API, then this routine is invoked
43 *  and this API given the opportunity to initialize its extension
44 *  area.
45 */
46
47static bool _RTEMS_tasks_Create_extension(
48  Thread_Control *executing,
49  Thread_Control *created
50)
51{
52  RTEMS_API_Control *api;
53  int                i;
54  size_t             to_allocate;
55
56  /*
57   *  Notepads must be the last entry in the structure and they
58   *  can be left off if disabled in the configuration.
59   */
60  to_allocate = sizeof( RTEMS_API_Control );
61  if ( !rtems_configuration_get_notepads_enabled() )
62    to_allocate -= (RTEMS_NUMBER_NOTEPADS * sizeof(uint32_t));
63
64  api = _Workspace_Allocate( to_allocate );
65
66  if ( !api )
67    return false;
68
69  created->API_Extensions[ THREAD_API_RTEMS ] = api;
70
71  _Event_Initialize( &api->Event );
72  _Event_Initialize( &api->System_event );
73  _ASR_Initialize( &api->Signal );
74  _Thread_Action_initialize( &api->Signal_action, _Signal_Action_handler );
75  created->task_variables = NULL;
76
77  if ( rtems_configuration_get_notepads_enabled() ) {
78    for (i=0; i < RTEMS_NUMBER_NOTEPADS; i++)
79      api->Notepads[i] = 0;
80  }
81
82  return true;
83}
84
85/*
86 *  _RTEMS_tasks_Start_extension
87 *
88 *  This extension routine is invoked when a task is started for the
89 *  first time.
90 */
91
92static void _RTEMS_tasks_Start_extension(
93  Thread_Control *executing,
94  Thread_Control *started
95)
96{
97  RTEMS_API_Control *api;
98
99  api = started->API_Extensions[ THREAD_API_RTEMS ];
100
101  _Event_Initialize( &api->Event );
102  _Event_Initialize( &api->System_event );
103}
104
105/*
106 *  _RTEMS_tasks_Delete_extension
107 *
108 *  This extension routine is invoked when a task is deleted.
109 */
110
111static void _RTEMS_tasks_Delete_extension(
112  Thread_Control *executing,
113  Thread_Control *deleted
114)
115{
116  /*
117   *  Free API specific memory
118   */
119
120  (void) _Workspace_Free( deleted->API_Extensions[ THREAD_API_RTEMS ] );
121}
122
123static void _RTEMS_tasks_Terminate_extension(
124  Thread_Control *executing
125)
126{
127  rtems_task_variable_t *tvp, *next;
128
129  /*
130   *  Free per task variable memory
131   */
132
133  tvp = executing->task_variables;
134  executing->task_variables = NULL;
135  while (tvp) {
136    next = (rtems_task_variable_t *)tvp->next;
137    _RTEMS_Tasks_Invoke_task_variable_dtor( executing, tvp );
138    tvp = next;
139  }
140
141  /*
142   *  Run all the key destructors
143   */
144  _POSIX_Keys_Run_destructors( executing );
145}
146
147/*
148 *  _RTEMS_tasks_Switch_extension
149 *
150 *  This extension routine is invoked at each context switch.
151 */
152
153static void _RTEMS_tasks_Switch_extension(
154  Thread_Control *executing,
155  Thread_Control *heir
156)
157{
158  rtems_task_variable_t *tvp;
159
160  /*
161   *  Per Task Variables
162   */
163
164  tvp = executing->task_variables;
165  while (tvp) {
166    tvp->tval = *tvp->ptr;
167    *tvp->ptr = tvp->gval;
168    tvp = (rtems_task_variable_t *)tvp->next;
169  }
170
171  tvp = heir->task_variables;
172  while (tvp) {
173    tvp->gval = *tvp->ptr;
174    *tvp->ptr = tvp->tval;
175    tvp = (rtems_task_variable_t *)tvp->next;
176  }
177}
178
179API_extensions_Control _RTEMS_tasks_API_extensions = {
180  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
181    .predriver_hook = NULL,
182  #endif
183  .postdriver_hook = _RTEMS_tasks_Initialize_user_tasks
184};
185
186User_extensions_Control _RTEMS_tasks_User_extensions = {
187  { NULL, NULL },
188  { { NULL, NULL }, _RTEMS_tasks_Switch_extension },
189  { _RTEMS_tasks_Create_extension,            /* create */
190    _RTEMS_tasks_Start_extension,             /* start */
191    _RTEMS_tasks_Start_extension,             /* restart */
192    _RTEMS_tasks_Delete_extension,            /* delete */
193    _RTEMS_tasks_Switch_extension,            /* switch */
194    NULL,                                     /* begin */
195    NULL,                                     /* exitted */
196    NULL,                                     /* fatal */
197    _RTEMS_tasks_Terminate_extension          /* terminate */
198  }
199};
200
201void _RTEMS_tasks_Manager_initialization(void)
202{
203  _Objects_Initialize_information(
204    &_RTEMS_tasks_Information, /* object information table */
205    OBJECTS_CLASSIC_API,       /* object API */
206    OBJECTS_RTEMS_TASKS,       /* object class */
207    Configuration_RTEMS_API.maximum_tasks,
208                               /* maximum objects of this class */
209    sizeof( Thread_Control ),  /* size of this object's control block */
210    false,                     /* true if the name is a string */
211    RTEMS_MAXIMUM_NAME_LENGTH  /* maximum length of an object name */
212#if defined(RTEMS_MULTIPROCESSING)
213    ,
214    true,                      /* true if this is a global object class */
215    NULL                       /* Proxy extraction support callout */
216#endif
217  );
218
219  /*
220   *  Add all the extensions for this API
221   */
222
223  _User_extensions_Add_API_set( &_RTEMS_tasks_User_extensions );
224
225  _API_extensions_Add( &_RTEMS_tasks_API_extensions );
226
227  /*
228   *  Register the MP Process Packet routine.
229   */
230
231#if defined(RTEMS_MULTIPROCESSING)
232  _MPCI_Register_packet_processor(
233    MP_PACKET_TASKS,
234    _RTEMS_tasks_MP_Process_packet
235  );
236#endif
237
238}
239
240void _RTEMS_tasks_Initialize_user_tasks( void )
241{
242  if ( _RTEMS_tasks_Initialize_user_tasks_p )
243    (*_RTEMS_tasks_Initialize_user_tasks_p)();
244}
Note: See TracBrowser for help on using the repository browser.