blob: 16de09050986fcb3b56855e5377d233eb0382daf [file] [log] [blame]
Ewout van Bekkum8b72a572021-03-09 08:53:39 -08001.. _module-pw_thread_embos:
2
Ewout van Bekkum6e5d43e2021-05-06 15:29:44 -07003===============
Ewout van Bekkum8b72a572021-03-09 08:53:39 -08004pw_thread_embos
Ewout van Bekkum6e5d43e2021-05-06 15:29:44 -07005===============
6This is a set of backends for pw_thread based on embOS v4.
Ewout van Bekkum8b72a572021-03-09 08:53:39 -08007
Ewout van Bekkum6e5d43e2021-05-06 15:29:44 -07008.. contents::
9 :local:
10 :depth: 1
11
12.. Warning::
13 This module is still under construction, the API is not yet stable.
14
15-----------------------
16Thread Creation Backend
17-----------------------
18A backend or ``pw::thread::Thread`` is offered using ``OS_CreateTaskEx()``.
19Optional joining support is enabled via an ``OS_EVENT`` in each thread's
20context.
21
22This backend permits users to start threads where contexts must be explicitly
23allocated and passed in as an option. As a quick example, a detached thread
24can be created as follows:
25
26.. code-block:: cpp
27
28 #include "pw_thread/detached_thread.h"
29 #include "pw_thread_embos/context.h"
30 #include "pw_thread_embos/options.h"
31 #include "RTOS.h" // For the embOS types.
32
33 pw::thread::embos::ContextWithStack<42> example_thread_context;
34 void StartExampleThread() {
35 pw::thread::DetachedThread(
36 pw::thread::embos::Options()
37 .set_name("static_example_thread")
38 .set_priority(kFooPriority)
39 .set_time_slice_interval(kFooTimeSliceInterval)
40 .set_context(example_thread_context),
41 example_thread_function)
42 }
43
44
45Module Configuration Options
46============================
47The following configurations can be adjusted via compile-time configuration of
48this module, see the
49:ref:`module documentation <module-structure-compile-time-configuration>` for
50more details.
51
52.. c:macro:: PW_THREAD_EMBOS_CONFIG_JOINING_ENABLED
53
54 Whether thread joining is enabled. By default this is disabled.
55
56 We suggest only enabling this when thread joining is required to minimize
57 the RAM and ROM cost of threads.
58
59 Enabling this grows the RAM footprint of every pw::thread::Thread as it adds
60 an OS_EVENT to every thread's pw::thread::embos::Context. In addition, there
61 is a minute ROM cost to construct and destroy this added object.
62
63 PW_THREAD_JOINING_ENABLED gets set to this value.
64
65.. c:macro:: PW_THREAD_EMBOS_CONFIG_MINIMUM_STACK_SIZE_WORDS
66
67 The minimum stack size in words. By default this uses Segger's recommendation
68 of 68 bytes.
69
70.. c:macro:: PW_THREAD_EMBOS_CONFIG_DEFAULT_STACK_SIZE_WORDS
71
72 The default stack size in words. By default this uses Segger's recommendation
73 of 256 bytes to start.
74
75.. c:macro:: PW_THREAD_EMBOS_CONFIG_MAX_THREAD_NAME_LEN
76
77 The maximum length of a thread's name, not including null termination. By
78 default this is arbitrarily set to 15. This results in an array of characters
79 which is this length + 1 bytes in every ``pw::thread::Thread``'s context.
80
81.. c:macro:: PW_THREAD_EMBOS_CONFIG_MIN_PRIORITY
82
83 The minimum priority level, this is normally 1, since 0 is not a valid
84 priority level.
85
86.. c:macro:: PW_THREAD_EMBOS_CONFIG_DEFAULT_PRIORITY
87
88 The default priority level. By default this uses the minimal embOS priority.
89
90.. c:macro:: PW_THREAD_EMBOS_CONFIG_DEFAULT_TIME_SLICE_INTERVAL
91
92 The round robin time slice tick interval for threads at the same priority.
93 By default this is set to 2 ticks based on the embOS default.
94
95
96embOS Thread Options
97====================
98.. cpp:class:: pw::thread::embos::Options
99
100 .. cpp:function:: set_name(const char* name)
101
102 Sets the name for the embOS task, this is optional.
103 Note that this will be deep copied into the context and may be truncated
104 based on ``PW_THREAD_EMBOS_CONFIG_MAX_THREAD_NAME_LEN``.
105
106 .. cpp:function:: set_priority(OS_PRIO priority)
107
108 Sets the priority for the embOS task. Higher values are higher priority,
109 see embOS OS_CreateTaskEx for more detail.
110 Precondition: This must be >= ``PW_THREAD_EMBOS_CONFIG_MIN_PRIORITY``.
111
112 .. cpp:function:: set_time_slice_interval(OS_UINT time_slice_interval)
113
114 Sets the number of ticks this thread is allowed to run before other ready
115 threads of the same priority are given a chance to run.
116
117 A value of 0 disables time-slicing of this thread.
118
119 Precondition: This must be <= 255 ticks.
120
121 .. cpp:function:: set_context(pw::thread::embos::Context& context)
122
123 Set the pre-allocated context (all memory needed to run a thread). Note that
124 this is required for this thread creation backend! The ``Context`` can
125 either be constructed with an externally provided ``std::span<OS_UINT>``
126 stack or the templated form of ``ContextWithStack<kStackSizeWords>`` can
127 be used.
128
129
130-----------------------------
131Thread Identification Backend
132-----------------------------
133A backend for ``pw::thread::Id`` and ``pw::thread::get_id()`` is offerred using
134``OS_GetTaskID()``. It uses ``DASSERT`` to ensure that the scheduler has started
135via ``OS_IsRunning()``.
136
137--------------------
138Thread Sleep Backend
139--------------------
140A backend for ``pw::thread::sleep_for()`` and ``pw::thread::sleep_until()`` is
141offerred using ``OS_Delay()`` if the duration is at least one tick, else
142``OS_Yield()`` is used. It uses ``pw::this_thread::get_id() != thread::Id()`` to
143ensure it invoked only from a thread.
144
145--------------------
146Thread Yield Backend
147--------------------
148A backend for ``pw::thread::yield()`` is offered using via ``OS_Yield()``.
149It uses ``pw::this_thread::get_id() != thread::Id()`` to ensure it invoked only
150from a thread.