Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 1 | .. _module-pw_thread: |
| 2 | |
Ewout van Bekkum | a082d7f | 2021-04-15 14:36:37 -0700 | [diff] [blame] | 3 | ========= |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 4 | pw_thread |
Ewout van Bekkum | a082d7f | 2021-04-15 14:36:37 -0700 | [diff] [blame] | 5 | ========= |
| 6 | The ``pw_thread`` module contains utilities for thread creation and thread |
| 7 | execution. |
Ewout van Bekkum | e3b5603 | 2020-12-22 12:00:18 -0800 | [diff] [blame] | 8 | |
Ewout van Bekkum | a082d7f | 2021-04-15 14:36:37 -0700 | [diff] [blame] | 9 | .. contents:: |
| 10 | :local: |
| 11 | :depth: 2 |
| 12 | |
| 13 | .. Warning:: |
| 14 | This module is still under construction, the API is not yet stable. |
| 15 | |
| 16 | --------------- |
| 17 | Thread Creation |
| 18 | --------------- |
| 19 | The class ``pw::thread::Thread`` can represent a single thread of execution. |
| 20 | Threads allow multiple functions to execute concurrently. |
| 21 | |
| 22 | The Thread's API is C++11 STL |
| 23 | `std::thread <https://en.cppreference.com/w/cpp/thread/thread>`_ like, meaning |
| 24 | the object is effectively a thread handle and not an object which contains the |
| 25 | thread's context. Unlike ``std::thread``, the API requires |
| 26 | ``pw::thread::Options`` as an argument and is limited to only work with |
| 27 | ``pw::thread::ThreadCore`` objects and functions which match the |
| 28 | ``pw::thread::Thread::ThreadRoutine`` signature. |
| 29 | |
Ewout van Bekkum | cc756c8 | 2021-05-12 07:57:43 -0700 | [diff] [blame] | 30 | We recognize that the C++11's STL ``std::thread``` API has some drawbacks where |
| 31 | it is easy to forget to join or detach the thread handle. Because of this, we |
| 32 | offer helper wrappers like the ``pw::thread::DetachedThread``. Soon we will |
| 33 | extend this by also adding a ``pw::thread::JoiningThread`` helper wrapper which |
| 34 | will also have a lighter weight C++20 ``std::jthread`` like cooperative |
| 35 | cancellation contract to make joining safer and easier. |
| 36 | |
Ewout van Bekkum | a082d7f | 2021-04-15 14:36:37 -0700 | [diff] [blame] | 37 | Threads may begin execution immediately upon construction of the associated |
| 38 | thread object (pending any OS scheduling delays), starting at the top-level |
| 39 | function provided as a constructor argument. The return value of the |
| 40 | top-level function is ignored. The top-level function may communicate its |
| 41 | return value by modifying shared variables (which may require |
| 42 | synchronization, see :ref:`module-pw_sync`) |
| 43 | |
| 44 | Thread objects may also be in the state that does not represent any thread |
| 45 | (after default construction, move from, detach, or join), and a thread of |
| 46 | execution may be not associated with any thread objects (after detach). |
| 47 | |
| 48 | No two Thread objects may represent the same thread of execution; Thread is |
| 49 | not CopyConstructible or CopyAssignable, although it is MoveConstructible and |
| 50 | MoveAssignable. |
| 51 | |
| 52 | .. list-table:: |
| 53 | |
| 54 | * - *Supported on* |
| 55 | - *Backend module* |
| 56 | * - FreeRTOS |
| 57 | - :ref:`module-pw_thread_freertos` |
| 58 | * - ThreadX |
| 59 | - :ref:`module-pw_thread_threadx` |
| 60 | * - embOS |
Ewout van Bekkum | 6e5d43e | 2021-05-06 15:29:44 -0700 | [diff] [blame] | 61 | - :ref:`module-pw_thread_embos` |
Ewout van Bekkum | a082d7f | 2021-04-15 14:36:37 -0700 | [diff] [blame] | 62 | * - STL |
| 63 | - :ref:`module-pw_thread_stl` |
| 64 | * - Zephyr |
| 65 | - Planned |
| 66 | * - CMSIS-RTOS API v2 & RTX5 |
| 67 | - Planned |
| 68 | |
| 69 | |
| 70 | Options |
| 71 | ======= |
| 72 | The ``pw::thread::Options`` contains the parameters or attributes needed for a |
| 73 | thread to start. |
| 74 | |
| 75 | Pigweed does not generalize options, instead we strive to give you full control |
| 76 | where we provide helpers to do this. |
| 77 | |
| 78 | Options are backend specific and ergo the generic base class cannot be |
| 79 | directly instantiated. |
| 80 | |
| 81 | The attributes which can be set through the options are backend specific |
| 82 | but may contain things like the thread name, priority, scheduling policy, |
| 83 | core/processor affinity, and/or an optional reference to a pre-allocated |
| 84 | Context (the collection of memory allocations needed for a thread to run). |
| 85 | |
| 86 | Options shall NOT permit starting as detached, this must be done explicitly |
| 87 | through the Thread API. |
| 88 | |
| 89 | Options must not contain any memory needed for a thread to run (TCB, |
| 90 | stack, etc.). The Options may be deleted or re-used immediately after |
| 91 | starting a thread. |
| 92 | |
| 93 | Please see the thread creation backend documentation for how their Options work. |
| 94 | |
| 95 | .. Note:: |
| 96 | Options have a default constructor, however default options are not portable! |
| 97 | Default options can only work if threads are dynamically allocated by default, |
| 98 | meaning default options cannot work on backends which require static thread |
| 99 | allocations. In addition on some schedulers, default options will not work |
| 100 | for other reasons. |
| 101 | |
| 102 | Detaching & Joining |
| 103 | =================== |
| 104 | The ``Thread::detach()`` API is always available, to let you separate the |
| 105 | thread of execution from the thread object, allowing execution to continue |
| 106 | independently. |
| 107 | |
| 108 | The joining API, more specifically ``Thread::join()``, is conditionally |
| 109 | available depending on the selected backend for thread creation and how it is |
| 110 | configured. The backend is responsible for providing the |
| 111 | ``PW_THREAD_JOINING_ENABLED`` macro through |
| 112 | ``pw_thread_backend/thread_native.h``. This ensures that any users which include |
| 113 | ``pw_thread/thread.h`` can use this macro if needed. |
| 114 | |
| 115 | Please see the selected thread creation backend documentation for how to |
| 116 | enable joining if it's not already enabled by default. |
| 117 | |
| 118 | .. Warning:: |
| 119 | A constructed ``pw::thread::Thread`` which represents a thread of execution |
| 120 | must be EITHER detached or joined, else the destructor will assert! |
| 121 | |
Ewout van Bekkum | 19e753a | 2021-04-28 18:06:55 -0700 | [diff] [blame] | 122 | DetachedThread |
| 123 | ============== |
| 124 | To make it slightly easier and cleaner to spawn detached threads without having |
| 125 | to worry about thread handles, a wrapper ``DetachedThread()`` function is |
| 126 | provided which creates a ``Thread`` and immediately detaches it. For example |
| 127 | instead of: |
| 128 | |
| 129 | .. code-block:: cpp |
| 130 | |
| 131 | Thread(options, foo).detach(); |
| 132 | |
| 133 | You can instead use this helper wrapper to: |
| 134 | |
| 135 | .. code-block:: cpp |
| 136 | |
| 137 | DetachedThread(options, foo); |
| 138 | |
| 139 | The arguments are directly forwarded to the Thread constructor and ergo exactly |
| 140 | match the Thread constuctor arguments for creating a thread of execution. |
| 141 | |
| 142 | |
Ewout van Bekkum | a082d7f | 2021-04-15 14:36:37 -0700 | [diff] [blame] | 143 | ThreadRoutine & ThreadCore |
| 144 | ========================== |
| 145 | Threads must either be invoked through a |
| 146 | ``pw::thread::Thread::ThreadRoutine``` style function or implement the |
| 147 | ``pw::thread::ThreadCore`` interface. |
| 148 | |
| 149 | .. code-block:: cpp |
| 150 | |
| 151 | namespace pw::thread { |
| 152 | |
| 153 | // This function may return. |
| 154 | using Thread::ThreadRoutine = void (*)(void* arg); |
| 155 | |
| 156 | class ThreadCore { |
| 157 | public: |
| 158 | virtual ~ThreadCore() = default; |
| 159 | |
| 160 | // The public API to start a ThreadCore, note that this may return. |
| 161 | void Start() { Run(); } |
| 162 | |
| 163 | private: |
| 164 | // This function may return. |
| 165 | virtual void Run() = 0; |
| 166 | }; |
| 167 | |
| 168 | } // namespace pw::thread; |
| 169 | |
| 170 | |
| 171 | To use the ``pw::thread::Thread::ThreadRoutine``, your function must have the |
| 172 | following signature: |
| 173 | |
| 174 | .. code-block:: cpp |
| 175 | |
| 176 | void example_thread_entry_function(void *arg); |
| 177 | |
| 178 | |
| 179 | To invoke a member method of a class a static lambda closure can be used |
| 180 | to ensure the dispatching closure is not destructed before the thread is |
| 181 | done executing. For example: |
| 182 | |
| 183 | .. code-block:: cpp |
| 184 | |
| 185 | class Foo { |
| 186 | public: |
| 187 | void DoBar() {} |
| 188 | }; |
| 189 | Foo foo; |
| 190 | |
| 191 | static auto invoke_foo_do_bar = [](void *void_foo_ptr) { |
| 192 | // If needed, additional arguments could be set here. |
| 193 | static_cast<Foo*>(void_foo_ptr)->DoBar(); |
| 194 | }; |
| 195 | |
| 196 | // Now use the lambda closure as the thread entry, passing the foo's |
| 197 | // this as the argument. |
| 198 | Thread thread(options, invoke_foo_do_bar, &foo); |
| 199 | thread.detach(); |
| 200 | |
| 201 | |
| 202 | Alternatively, the aforementioned ``pw::thread::ThreadCore`` interface can be |
| 203 | be implemented by an object by overriding the private |
| 204 | ``void ThreadCore::Run();`` method. This makes it easier to create a thread, as |
| 205 | a static lambda closure or function is not needed to dispatch to a member |
| 206 | function without arguments. For example: |
| 207 | |
| 208 | .. code-block:: cpp |
| 209 | |
| 210 | class Foo : public ThreadCore { |
| 211 | private: |
| 212 | void Run() override {} |
| 213 | }; |
| 214 | Foo foo; |
| 215 | |
| 216 | // Now create the thread, using foo directly. |
| 217 | Thread(options, foo).detach(); |
| 218 | |
| 219 | .. Warning:: |
| 220 | Because the thread may start after the pw::Thread creation, an object which |
| 221 | implements the ThreadCore MUST meet or exceed the lifetime of its thread of |
| 222 | execution! |