2015-08-10 17:28:37 -04:00
|
|
|
.. _context_services:
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
Execution Context Services
|
|
|
|
##########################
|
2015-08-10 17:28:37 -04:00
|
|
|
|
|
|
|
Concepts
|
|
|
|
********
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
Each kernel execution context has an associated *type*, which indicates whether
|
2015-08-10 17:28:37 -04:00
|
|
|
the context is a task, a fiber, or the kernel's interrupt handling context.
|
2015-08-20 11:04:01 -04:00
|
|
|
Task and fiber contexts also have an associated *thread identifier* value,
|
|
|
|
which is used to uniquely identify these threads.
|
2015-08-10 17:28:37 -04:00
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
Each task and fiber may also support a 32-bit *thread custom data* value.
|
2015-08-10 17:28:37 -04:00
|
|
|
This value is accessible only by the task or fiber itself, and can be used
|
|
|
|
by the application for any purpose. The default custom data value for a
|
|
|
|
task or fiber is zero.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
The custom data value is not available to ISRs, which operate in the shared
|
|
|
|
kernel interrupt handling context.
|
|
|
|
|
|
|
|
|
|
|
|
Purpose
|
|
|
|
*******
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
Use the kernel execution context services when writing code that needs to
|
|
|
|
operate differently when executed by different contexts.
|
2015-08-10 17:28:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
*****
|
|
|
|
|
|
|
|
Configuring Custom Data Support
|
|
|
|
===============================
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
Use the :option:`THREAD_CUSTOM_DATA` configuration option
|
|
|
|
to enable support for thread custom data. By default, custom data
|
2015-08-10 17:28:37 -04:00
|
|
|
support is disabled.
|
|
|
|
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
Example: Performing Execution Context-Specific Processing
|
|
|
|
=========================================================
|
|
|
|
This code shows how a routine can use a thread's custom data value
|
|
|
|
to limit the number of times a thread may call the routine.
|
2015-08-10 17:28:37 -04:00
|
|
|
Counting is not performed when the routine is called by an ISR, which does not
|
|
|
|
have a custom data value.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Obviously, only a single routine can use this technique
|
|
|
|
since it monopolizes the use of the custom data value.
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
#define CALL_LIMIT 7
|
|
|
|
|
|
|
|
int call_tracking_routine(void)
|
|
|
|
{
|
|
|
|
uint32_t call_count;
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
if (sys_execution_context_type_get() != NANO_CTX_ISR) {
|
|
|
|
call_count = (uint32_t)sys_thread_custom_data_get();
|
2015-08-10 17:28:37 -04:00
|
|
|
if (call_count == CALL_LIMIT)
|
|
|
|
return -1;
|
|
|
|
call_count++;
|
2015-08-20 11:04:01 -04:00
|
|
|
sys_thread_custom_data_set((void *)call_count);
|
2015-08-10 17:28:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do rest of routine's processing */
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
APIs
|
|
|
|
****
|
|
|
|
|
2015-08-20 11:04:01 -04:00
|
|
|
The following kernel execution context APIs are provided by
|
|
|
|
:file:`microkernel.h` and by :file:`nanokernel.h`:
|
|
|
|
|
|
|
|
+--------------------------------------------+---------------------------------------+
|
|
|
|
| Call | Description |
|
|
|
|
+============================================+=======================================+
|
|
|
|
| :c:func:`sys_thread_self_get()` | Gets thread identifier of currently |
|
|
|
|
| | executing task or fiber. |
|
|
|
|
+--------------------------------------------+---------------------------------------+
|
|
|
|
| :c:func:`sys_execution_context_type_get()` | Gets type of currently executing |
|
|
|
|
| | context (i.e. task, fiber, or ISR). |
|
|
|
|
+--------------------------------------------+---------------------------------------+
|
|
|
|
| :c:func:`sys_thread_custom_data_set()` | Writes custom data for currently |
|
|
|
|
| | executing task or fiber. |
|
|
|
|
+--------------------------------------------+---------------------------------------+
|
|
|
|
| :c:func:`sys_thread_custom_data_get()` | Reads custom data for currently |
|
|
|
|
| | executing task or fiber. |
|
|
|
|
+--------------------------------------------+---------------------------------------+
|