source: rtems/cpukit/rtems/src/tasks.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

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