source: rtems-docs/c-user/user_extensions.rst @ 3384994

5
Last change on this file since 3384994 was 3384994, checked in by Chris Johns <chrisj@…>, on 11/13/17 at 02:25:18

Clean up sphinx warnings.

  • Fix minor formatting issues.
  • Fix reference the gloassary TLS using ':term:'.
  • Make sure nothing is between an anchor and the heading where ':ref:' references the anchor. This meant moving all the recently added '.. index::' entries.

Update #3232.
Update #3229.

  • Property mode set to 100644
File size: 18.5 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. COMMENT: COPYRIGHT (c) 1988-2008.
4.. COMMENT: On-Line Applications Research Corporation (OAR).
5.. COMMENT: All rights reserved.
6
7.. index:: user extensions
8
9.. _User Extensions Manager:
10
11User Extensions Manager
12***********************
13
14Introduction
15============
16
17The user extensions manager allows the application developer to augment the
18executive by allowing them to supply extension routines which are invoked at
19critical system events.  The directives provided by the user extensions manager
20are:
21
22- rtems_extension_create_ - Create an extension set
23
24- rtems_extension_ident_ - Get ID of an extension set
25
26- rtems_extension_delete_ - Delete an extension set
27
28Background
29==========
30
31User extensions (call-back functions) are invoked by the system when the
32following events occur
33
34- thread creation,
35
36- thread start,
37
38- thread restart,
39
40- thread switch,
41
42- thread begin,
43
44- thread exitted (return from thread entry function),
45
46- thread termination,
47
48- thread deletion, and
49
50- fatal error detection (system termination).
51
52The user extensions have event-specific arguments, invocation orders and
53execution contexts.  Extension sets can be installed at run-time via
54:ref:`rtems_extension_create() <rtems_extension_create>` (dynamic extension
55sets) or at link-time via the application configuration option
56:ref:`CONFIGURE_INITIAL_EXTENSIONS <CONFIGURE_INITIAL_EXTENSIONS>` (initial
57extension sets).
58
59The execution context of user extensions varies.  Some user extensions are
60invoked with ownership of the allocator mutex.  The allocator mutex protects
61dynamic memory allocations and object creation/deletion.  Some user extensions
62are invoked with thread dispatching disabled.  The fatal error extension is
63invoked in an arbitrary context.
64
65.. index:: user extension set
66.. index:: rtems_extensions_table
67
68Extension Sets
69--------------
70
71User extensions are maintained as a set.  All user extensions are optional and
72may be `NULL`.  Together a set of these user extensions typically performs a
73specific functionality such as performance monitoring or debugger support.  The
74extension set is defined via the following structure.
75
76.. code-block:: c
77
78    typedef struct {
79      rtems_task_create_extension    thread_create;
80      rtems_task_start_extension     thread_start;
81      rtems_task_restart_extension   thread_restart;
82      rtems_task_delete_extension    thread_delete;
83      rtems_task_switch_extension    thread_switch;
84      rtems_task_begin_extension     thread_begin;
85      rtems_task_exitted_extension   thread_exitted;
86      rtems_fatal_extension          fatal;
87      rtems_task_terminate_extension thread_terminate;
88    } rtems_extensions_table;
89
90.. index:: TCB extension area
91
92TCB Extension Area
93------------------
94
95There is no system-provided storage for the initial extension sets.
96
97The task control block (TCB) contains a pointer for each dynamic extension set.
98The pointer is initialized to `NULL` during thread initialization before the
99thread create extension is invoked.  The pointer may be used by the dynamic
100extension set to maintain thread-specific data.
101
102The TCB extension is an array of pointers in the TCB. The index into the table
103can be obtained from the extension identifier returned when the extension
104object is created:
105
106.. index:: rtems extensions table index
107
108.. code-block:: c
109
110    index = rtems_object_id_get_index( extension_id );
111
112The number of pointers in the area is the same as the number of dynamic user
113extension sets configured.  This allows an application to augment the TCB with
114user-defined information.  For example, an application could implement task
115profiling by storing timing statistics in the TCB's extended memory area.  When
116a task context switch is being executed, the thread switch extension could read
117a real-time clock to calculate how long the task being swapped out has run as
118well as timestamp the starting time for the task being swapped in.
119
120If used, the extended memory area for the TCB should be allocated and the TCB
121extension pointer should be set at the time the task is created or started by
122either the thread create or thread start extension.  The application is
123responsible for managing this extended memory area for the TCBs.  The memory
124may be reinitialized by the thread restart extension and should be deallocated
125by the thread delete extension  when the task is deleted.  Since the TCB
126extension buffers would most likely be of a fixed size, the RTEMS partition
127manager could be used to manage the application's extended memory area.  The
128application could create a partition of fixed size TCB extension buffers and
129use the partition manager's allocation and deallocation directives to obtain
130and release the extension buffers.
131
132Order of Invocation
133-------------------
134
135The user extensions are invoked in either `forward` or `reverse` order.  In
136forward order, the user extensions of initial extension sets are invoked before
137the user extensions of the dynamic extension sets.  The forward order of
138initial extension sets is defined by the initial extension sets table index.
139The forward order of dynamic extension sets is defined by the order in which
140the dynamic extension sets were created.  The reverse order is defined
141accordingly.  By invoking the user extensions in this order, extensions can be
142built upon one another.  At the following system events, the user extensions
143are invoked in `forward` order
144
145- thread creation,
146
147- thread start,
148
149- thread restart,
150
151- thread switch,
152
153- thread begin,
154
155- thread exitted (return from thread entry function), and
156
157- fatal error detection.
158
159At the following system events, the user extensions are invoked in `reverse`
160order:
161
162- thread termination, and
163
164- thread deletion.
165
166At these system events, the user extensions are invoked in reverse order to insure
167that if an extension set is built upon another, the more complicated user extension
168is invoked before the user extension it is built upon.  An example is use of the
169thread delete extension by the Standard C Library.  Extension sets which are
170installed after the Standard C Library will operate correctly even if they
171utilize the C Library because the C Library's thread delete extension is
172invoked after that of the other thread delete extensions.
173
174.. index:: rtems_task_create_extension
175
176Thread Create Extension
177-----------------------
178
179The thread create extension is invoked during thread creation, for example
180via :ref:`rtems_task_create() <rtems_task_create>` or :c:func:`pthread_create`.
181The thread create extension is defined as follows.
182
183.. code-block:: c
184
185    typedef bool ( *rtems_task_create_extension )(
186      rtems_tcb *executing,
187      rtems_tcb *created
188    );
189
190The :c:data:`executing` is a pointer to the TCB of the currently executing
191thread.  The :c:data:`created` is a pointer to the TCB of the created thread.
192The created thread is completely initialized with respect to the operating
193system.
194
195The executing thread is the owner of the allocator mutex except during creation
196of the idle threads.  Since the allocator mutex allows nesting the normal
197memory allocation routines can be used.
198
199A thread create extension will frequently attempt to allocate resources.  If
200this allocation fails, then the thread create extension must return
201:c:data:`false` and the entire thread create operation will fail, otherwise it
202must return :c:data:`true`.
203
204The thread create extension is invoked in forward order with thread dispatching
205enabled (except during system initialization).
206
207.. index:: rtems_task_start_extension
208
209Thread Start Extension
210----------------------
211
212The thread start extension is invoked during a thread start, for example
213via :ref:`rtems_task_start() <rtems_task_start>` or :c:func:`pthread_create`.
214The thread start extension is defined as follows.
215
216.. code-block:: c
217
218    typedef void ( *rtems_task_start_extension )(
219      rtems_tcb *executing,
220      rtems_tcb *started
221    );
222
223The :c:data:`executing` is a pointer to the TCB of the currently executing
224thread.  The :c:data:`started` is a pointer to the TCB of the started thread.
225It is invoked after the environment of the started thread has been loaded and the
226started thread has been made ready.  So, in SMP configurations, the thread may
227already run on another processor before the thread start extension is actually
228invoked.
229
230The thread start extension is invoked in forward order with thread dispatching
231disabled.
232
233.. index:: rtems_task_restart_extension
234
235Thread Restart Extension
236------------------------
237
238The thread restart extension is invoked during a thread restart, for example
239via :ref:`rtems_task_restart() <rtems_task_start>`.
240The thread restart extension is defined as follows.
241
242.. code-block:: c
243
244    typedef void ( *rtems_task_restart_extension )(
245      rtems_tcb *executing,
246      rtems_tcb *restarted
247    );
248
249Both :c:data:`executing` and :c:data:`restarted` are pointers the TCB of the
250currently executing thread.  It is invoked in the context of the executing
251thread right before the execution context is reloaded.  The thread stack
252reflects the previous execution context.
253
254The thread restart extension is invoked in forward order with thread
255dispatching enabled (except during system initialization).  The thread life is
256protected.  Thread restart and delete requests issued by thread restart
257extensions lead to recursion.
258
259.. index:: rtems_task_switch_extension
260
261Thread Switch Extension
262-----------------------
263
264The thread switch extension is invoked before the context switch from the
265currently executing thread to the heir thread.  The thread switch extension is
266defined as follows.
267
268.. code-block:: c
269
270    typedef void ( *rtems_task_switch_extension )(
271      rtems_tcb *executing,
272      rtems_tcb *heir
273    );
274
275The :c:data:`executing` is a pointer to the TCB of the currently executing
276thread.  The :c:data:`heir` is a pointer to the TCB of the heir thread.
277
278The thread switch extension is invoked in forward order with thread dispatching
279disabled.  In SMP configurations, interrupts are disabled and the per-processor
280SMP lock is owned.
281
282The context switches initiated through the multitasking start are not covered
283by the thread switch extension.
284
285.. index:: rtems_task_begin_extension
286
287Thread Begin Extension
288----------------------
289
290The thread begin extension is invoked during a thread begin before the thread
291entry function is called.  The thread begin extension is defined as follows.
292
293.. code-block:: c
294
295    typedef void ( *rtems_task_begin_extension )(
296      rtems_tcb *executing
297    );
298
299The :c:data:`executing` is a pointer to the TCB of the currently executing
300thread.  The thread begin extension executes in a normal thread context and may
301allocate resources for the thread.  In particular it has access to thread-local
302storage of the thread.
303
304The thread begin extension is invoked in forward order with thread dispatching
305enabled.  The thread switch extension may be called multiple times for this
306thread before the thread begin extension is invoked.
307
308.. index:: rtems_task_exitted_extension
309
310Thread Exitted Extension
311------------------------
312
313The thread exitted extension is invoked once the thread entry function returns.
314The thread exitted extension is defined as follows.
315
316.. code-block:: c
317
318    typedef void ( *rtems_task_exitted_extension )(
319      rtems_tcb *executing
320    );
321
322The :c:data:`executing` is a pointer to the TCB of the currently executing
323thread.
324
325This extension is invoked in forward order with thread dispatching enabled.
326
327.. index:: rtems_task_terminate_extension
328
329Thread Termination Extension
330----------------------------
331
332The thread termination extension is invoked in case a termination request is
333recognized by the currently executing thread.  Termination requests may result
334due to calls of :ref:`rtems_task_delete() <rtems_task_delete>`,
335:c:func:`pthread_exit`, or :c:func:`pthread_cancel`.  The thread termination
336extension is defined as follows.
337
338.. code-block:: c
339
340    typedef void ( *rtems_task_terminate_extension )(
341      rtems_tcb *executing
342    );
343
344The :c:data:`executing` is a pointer to the TCB of the currently executing
345thread.
346
347It is invoked in the context of the terminated thread right before the
348thread dispatch to the heir thread.  The POSIX cleanup and key destructors
349execute in this context.  The thread termination extension has access to
350thread-local storage and thread-specific data of POSIX keys.
351
352The thread terminate extension is invoked in reverse order with thread
353dispatching enabled.  The thread life is protected.  Thread restart and delete
354requests issued by thread terminate extensions lead to recursion.
355
356.. index:: rtems_task_delete_extension
357
358Thread Delete Extension
359-----------------------
360
361The thread delete extension is invoked in case a zombie thread is killed.  A
362thread becomes a zombie thread after it terminated.  The thread delete
363extension is defined as follows.
364
365.. code-block:: c
366
367    typedef void ( *rtems_task_delete_extension )(
368      rtems_tcb *executing,
369      rtems_tcb *deleted
370    );
371
372The :c:data:`executing` is a pointer to the TCB of the currently executing
373thread.  The :c:data:`deleted` is a pointer to the TCB of the deleted thread.
374The :c:data:`executing` and :c:data:`deleted` pointers are never equal.
375
376The executing thread is the owner of the allocator mutex.  Since the allocator
377mutex allows nesting the normal memory allocation routines can be used.
378
379The thread delete extension is invoked in reverse order with thread dispatching
380enabled.
381
382Please note that a thread delete extension is not immediately invoked with a
383call to :ref:`rtems_task_delete() <rtems_task_delete>` or similar.  The thread
384must first terminate and this may take some time.  The thread delete extension
385is invoked by :ref:`rtems_task_create() <rtems_task_create>` or similar as a
386result of a lazy garbage collection of zombie threads.
387
388.. index:: rtems_fatal_extension
389
390Fatal Error Extension
391---------------------
392
393The fatal error extension is invoked during :ref:`system termination
394<Terminate>`.  The fatal error extension is defined as follows.
395
396.. code-block:: c
397
398    typedef void( *rtems_fatal_extension )(
399      rtems_fatal_source source,
400      bool               always_set_to_false,
401      rtems_fatal_code   code
402    );
403
404The :c:data:`source` parameter is the fatal source indicating the subsystem the
405fatal condition originated in.  The :c:data:`always_set_to_false` parameter is
406always set to :c:data:`false` and provided only for backward compatibility
407reasons.  The :c:data:`code` parameter is the fatal error code.  This value
408must be interpreted with respect to the source.
409
410The fatal error extension is invoked in forward order.
411
412It is strongly advised to use initial extension sets to install a fatal error
413extension.  Usually, the initial extension set of board support package
414provides a fatal error extension which resets the board.  In this case, the
415dynamic fatal error extensions are not invoked.
416
417Directives
418==========
419
420This section details the user extension manager's directives.  A subsection is
421dedicated to each of this manager's directives and describes the calling
422sequence, related constants, usage, and status codes.
423
424.. raw:: latex
425
426   \clearpage
427
428.. index:: create an extension set
429.. index:: rtems_extension_create
430
431.. _rtems_extension_create:
432
433EXTENSION_CREATE - Create a extension set
434-----------------------------------------
435
436CALLING SEQUENCE:
437    .. code-block:: c
438
439        rtems_status_code rtems_extension_create(
440          rtems_name                    name,
441          const rtems_extensions_table *table,
442          rtems_id                     *id
443        );
444
445DIRECTIVE STATUS CODES:
446    .. list-table::
447     :class: rtems-table
448
449     * - ``RTEMS_SUCCESSFUL``
450       - extension set created successfully
451     * - ``RTEMS_INVALID_NAME``
452       - invalid extension set name
453     * - ``RTEMS_TOO_MANY``
454       - too many extension sets created
455
456DESCRIPTION:
457
458    This directive creates an extension set object and initializes it using the
459    specified extension set table.  The assigned extension set identifier is
460    returned in :c:data:`id`.  This identifier is used to access the extension
461    set with other user extension manager directives.  For control and
462    maintenance of the extension set, RTEMS allocates an Extension Set Control
463    Block (ESCB) from the local ESCB free pool and initializes it.  The
464    user-specified :c:data:`name` is assigned to the ESCB and may be used to
465    identify the extension set via
466    :ref:`rtems_extension_ident() <rtems_extension_ident>`.  The extension set
467    specified by :c:data:`table` is copied to the ESCB.
468
469NOTES:
470
471    This directive will not cause the calling task to be preempted.
472
473.. raw:: latex
474
475   \clearpage
476
477.. index:: get ID of an extension set
478.. index:: obtain ID of an extension set
479.. index:: rtems_extension_ident
480
481.. _rtems_extension_ident:
482
483EXTENSION_IDENT - Get ID of a extension set
484-------------------------------------------
485
486CALLING SEQUENCE:
487    .. code-block:: c
488
489        rtems_status_code rtems_extension_ident(
490          rtems_name  name,
491          rtems_id   *id
492        );
493
494DIRECTIVE STATUS CODES:
495    .. list-table::
496     :class: rtems-table
497
498     * - ``RTEMS_SUCCESSFUL``
499       - extension set identified successfully
500     * - ``RTEMS_INVALID_NAME``
501       - extension set name not found
502
503DESCRIPTION:
504    This directive obtains the extension set identifier associated with the
505    extension set :c:data:`name` to be acquired and returns it in :c:data:`id`.
506    If the extension set name is not unique, then the extension set identifier
507    will match one of the extension sets with that name.  However, this
508    extension set identifier is not guaranteed to correspond to the desired
509    extension set.  The extension set identifier is used to access this
510    extension set in other extension set related directives.
511
512NOTES:
513    This directive will not cause the running task to be preempted.
514
515.. raw:: latex
516
517   \clearpage
518
519.. index:: delete an extension set
520.. index:: rtems_extension_delete
521
522.. _rtems_extension_delete:
523
524EXTENSION_DELETE - Delete a extension set
525-----------------------------------------
526
527CALLING SEQUENCE:
528    .. code-block:: c
529
530        rtems_status_code rtems_extension_delete(
531            rtems_id id
532        );
533
534DIRECTIVE STATUS CODES:
535    .. list-table::
536     :class: rtems-table
537
538     * - ``RTEMS_SUCCESSFUL``
539       - extension set deleted successfully
540     * - ``RTEMS_INVALID_ID``
541       - invalid extension set id
542
543DESCRIPTION:
544    This directive deletes the extension set specified by :c:data:`id`.  If the
545    extension set is running, it is automatically canceled.  The ESCB for the
546    deleted extension set is reclaimed by RTEMS.
547
548NOTES:
549    This directive will not cause the running task to be preempted.
550
551    A extension set can be deleted by a task other than the task which created
552    the extension set.
Note: See TracBrowser for help on using the repository browser.