summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/userext.h
blob: 087df8c7d27c8b5bc94516ad86e6d5991ff6e48a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
 * @file
 *
 * @ingroup ScoreUserExt
 *
 * @brief User Extension Handler API.
 */

/*
 *  COPYRIGHT (c) 1989-2009.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_USEREXT_H
#define _RTEMS_SCORE_USEREXT_H

#include <rtems/score/interr.h>
#include <rtems/score/chain.h>
#include <rtems/score/thread.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef void User_extensions_routine RTEMS_COMPILER_DEPRECATED_ATTRIBUTE;

/**
 * @defgroup ScoreUserExt User Extension Handler
 *
 * @ingroup Score
 *
 * @brief The User Extension Handler provides invocation of application
 * dependent routines at critical points in the life of each thread and the
 * system as a whole.
 *
 * @{
 */

/**
 * @brief Task create extension.
 *
 * It corresponds to _Thread_Initialize() (used by the rtems_task_create()
 * directive).  The first parameter points to the currently executing thread
 * which created the new thread.  The second parameter points to the created
 * thread.
 *
 * It is invoked after the new thread has been completely initialized, but
 * before it is placed on a ready chain.
 *
 * Thread dispatching may be disabled.  This depends on the context of the
 * _Thread_Initialize() call.  Thread dispatch is disabled during the creation
 * of the idle thread and the initialization threads.  It can be considered as
 * an invalid API usage, if the application calls _Thread_Initialize() with
 * disabled thread dispatching.  Disabled thread dispatching is different from
 * disabled preemption.
 *
 * It can be assumed that the executing thread locked the allocator mutex.
 * The only exception is the creation of the idle thread.  In this case the
 * allocator mutex is not locked.  Since the allocator mutex is non-recursive,
 * it is prohibited to call the normal memory allocation routines.  It is
 * possible to use internal rountines like _Workspace_Allocate() or
 * _Heap_Allocate() for heaps which are protected by the allocator mutex.
 *
 * @retval true The thread create extension was successful.
 * @retval false A thread create user extension will frequently attempt to
 * allocate resources.  If this allocation fails, then the extension should
 * return @a false and the entire thread create operation will fail.
 */
typedef bool ( *User_extensions_thread_create_extension )(
  Thread_Control *,
  Thread_Control *
);

/**
 * @brief Task delete extension.
 *
 * It corresponds to _Thread_Close() (used by the rtems_task_delete()
 * directive).  The first parameter points to the currently executing thread
 * which deleted the thread.  The second parameter points to the deleted
 * thread.
 *
 * It is invoked before all resources of the thread are deleted.
 *
 * Thread dispatching is enabled.  The executing thread locked the allocator
 * mutex.
 */
typedef void( *User_extensions_thread_delete_extension )(
  Thread_Control *,
  Thread_Control *
);

/**
 * @brief Task start extension.
 *
 * It corresponds to _Thread_Start() (used by the rtems_task_start()
 * directive).  The first parameter points to the currently executing thread
 * which started the thread.  The second parameter points to the started
 * thread.
 *
 * It is invoked after the environment of the thread has been loaded and the
 * thread has been made ready.
 *
 * Thread dispatching is disabled.  The executing thread is not the holder of
 * the allocator mutex.
 */
typedef void( *User_extensions_thread_start_extension )(
  Thread_Control *,
  Thread_Control *
);

/**
 * @brief Task restart extension.
 *
 * It corresponds to _Thread_Restart() (used by the rtems_task_restart()
 * directive).  The first parameter points to the currently executing thread
 * which restarted the thread.  The second parameter points to the restarted
 * thread.
 *
 * It is invoked after the environment of the thread has been loaded and the
 * thread has been made ready.
 *
 * Thread dispatching is disabled.  The executing thread is not the holder of
 * the allocator mutex.
 */
typedef void( *User_extensions_thread_restart_extension )(
  Thread_Control *,
  Thread_Control *
);

/**
 * @brief Task switch extension.
 *
 * It corresponds to _Thread_Dispatch().  The first parameter points to the
 * currently executing thread.  The second parameter points to the heir thread.
 *
 * It is invoked before the context switch from the executing to the heir
 * thread.
 *
 * Thread dispatching is disabled.  The state of the allocator mutex is
 * arbitrary.
 *
 * The context switches initiated through _Thread_Start_multitasking() and
 * _Thread_Stop_multitasking() are not covered by this extension.  The
 * executing thread may run with a minimal setup, for example with a freed task
 * stack.
 */
typedef void( *User_extensions_thread_switch_extension )(
  Thread_Control *,
  Thread_Control *
);

/**
 * @brief Task begin extension.
 *
 * It corresponds to _Thread_Handler().  The first parameter points to the
 * currently executing thread which begins now execution.
 *
 * Thread dispatching is disabled.  The executing thread is not the holder of
 * the allocator mutex.
 */
typedef void( *User_extensions_thread_begin_extension )(
  Thread_Control *
);

/**
 * @brief Task exitted extension.
 *
 * It corresponds to _Thread_Handler().  The first parameter points to the
 * currently executing thread which exitted before.
 *
 * Thread dispatching is disabled.  The state of the allocator mutex is
 * arbitrary.
 */
typedef void( *User_extensions_thread_exitted_extension )(
  Thread_Control *
);

/**
 * @brief Fatal error extension.
 *
 * It corresponds to _Internal_error_Occurred() (used by the
 * rtems_fatal_error_occurred() directive).  The first parameter contains the
 * error source.  The second parameter indicates if it was an internal error.
 * The third parameter contains the error code.
 *
 * This extension should not call any RTEMS directives.
 */
typedef void( *User_extensions_fatal_extension )(
  Internal_errors_Source,
  bool,
  Internal_errors_t
);

/**
 * @brief User extension table.
 */
typedef struct {
  User_extensions_thread_create_extension  thread_create;
  User_extensions_thread_start_extension   thread_start;
  User_extensions_thread_restart_extension thread_restart;
  User_extensions_thread_delete_extension  thread_delete;
  User_extensions_thread_switch_extension  thread_switch;
  User_extensions_thread_begin_extension   thread_begin;
  User_extensions_thread_exitted_extension thread_exitted;
  User_extensions_fatal_extension          fatal;
}   User_extensions_Table;

/**
 * @brief Manages the switch callouts.
 *
 * They are managed separately from other extensions for performance reasons.
 */
typedef struct {
  Chain_Node                              Node;
  User_extensions_thread_switch_extension thread_switch;
}   User_extensions_Switch_control;

/**
 * @brief Manages each user extension set.
 *
 * The switch control is part of the extensions control even if not used due to
 * the extension not having a switch handler.
 */
typedef struct {
  Chain_Node                     Node;
  User_extensions_Switch_control Switch;
  User_extensions_Table          Callouts;
}   User_extensions_Control;

/**
 * @brief List of active extensions.
 */
SCORE_EXTERN Chain_Control _User_extensions_List;

/**
 * @brief List of active task switch extensions.
 */
SCORE_EXTERN Chain_Control _User_extensions_Switches_list;

/**
 * @name Extension Maintainance
 *
 * @{
 */

void _User_extensions_Handler_initialization( void );

void _User_extensions_Add_set(
  User_extensions_Control *extension
);

RTEMS_INLINE_ROUTINE void _User_extensions_Add_API_set(
  User_extensions_Control *extension
)
{
  _User_extensions_Add_set( extension );
}

RTEMS_INLINE_ROUTINE void _User_extensions_Add_set_with_table(
  User_extensions_Control     *extension,
  const User_extensions_Table *extension_table
)
{
  extension->Callouts = *extension_table;

  _User_extensions_Add_set( extension );
}

void _User_extensions_Remove_set(
  User_extensions_Control *extension
);

/** @} */

/**
 * @name Extension Callout Dispatcher
 *
 * @{
 */

bool _User_extensions_Thread_create(
  Thread_Control *created
);

void _User_extensions_Thread_delete(
  Thread_Control *deleted
);

void _User_extensions_Thread_start(
  Thread_Control *started
);

void _User_extensions_Thread_restart(
  Thread_Control *restarted
);

void _User_extensions_Thread_begin(
  Thread_Control *executing
);

void _User_extensions_Thread_switch(
  Thread_Control *executing,
  Thread_Control *heir
);

void _User_extensions_Thread_exitted(
  Thread_Control *executing
);

void _User_extensions_Fatal(
  Internal_errors_Source source,
  bool                   is_internal,
  Internal_errors_t      error
);

/** @} */

/** @} */

#ifdef __cplusplus
}
#endif

#endif
/* end of include file */