.. include:: 2026-11-23 (3): RTOS, Multithreading, Race Conditions ===================================================== .. topic:: See also * :doc:`/foreign/why-os/index` * :doc:`/trainings/material/soup/linux/why/index` Realtime Recap -------------- .. topic:: Documentation * :doc:`/foreign/why-os/timespec` * :doc:`/foreign/why-os/time` * Consider work that must should committed within 1ms * Might take longer though |longrightarrow| *Glitch* * Why is this possible? * Task (process/thread) might be *preempted* by *scheduler* in favor of another task * |longrightarrow| Deadline-miss due to *preemption* .. image:: realtime-glitch.jpg :scale: 130% .. literalinclude:: code/sched-hiccup.cpp :caption: :download:`code/sched-hiccup.cpp` :language: c++ * Start 100 CPU intensive processes |longrightarrow| **competitors** for CPU time .. code-block:: console $ for i in $(seq 100); do sha256sum /dev/zero& done * Run program as normal user: *fair scheduling* (priority 0) .. code-block:: console $ ./2026-11-23-sched-hiccup ... RT-OOPS #510 (took 2.81667 ms) ... |longrightarrow| relatively large oopses * Run program with realtime priority 1 (as root). 1 is the lowest of the realtime priorities. .. code-block:: console $ sudo chrt -f 1 ./2026-11-23-sched-hiccup |longrightarrow| better. *Not perfect*, because desktop Linux is bad at realtime. .. _2026-11-23-race-condition: Mother Of Race Conditions: Load/Modify/Store Conflict ----------------------------------------------------- .. topic:: See also * :doc:`/trainings/material/soup/cxx/cxx11/multithreading/memory-model/index` Polished Course Material (In Standard C++) .......................................... * :ref:`cxx-memory-model-integer-increment-single-thread` * :ref:`cxx-memory-model-integer-increment-not-atomic` * :ref:`cxx-memory-model-integer-increment-mother` * :ref:`cxx-memory-model-integer-increment-lms` * :ref:`cxx-memory-model-anti-lms-mutex` * :ref:`cxx-memory-model-anti-lms-atomic` Live-Coded Programs (In WhyOS Equivalents) .......................................... .. topic:: See also * :doc:`/foreign/why-os/threads` * :doc:`/foreign/why-os/mutex` Unprotected Conflict ```````````````````` .. literalinclude:: code/load-modify-store.cpp :caption: :download:`code/load-modify-store.cpp` :language: c++ .. list-table:: :align: left :header-rows: 1 * * Expected value of ``poor_number`` * Actual value of ``poor_number`` * * 40000000 * Not 40000000 .. code-block:: console $ ./2026-11-23-load-modify-store 20818932 Protection Through ``Why::Mutex`` ````````````````````````````````` .. topic:: See also * :ref:`cxx-memory-model-anti-lms-mutex` .. literalinclude:: code/load-modify-store-mutex.cpp :caption: :download:`code/load-modify-store-mutex.cpp` :language: c++ Mutex Alternative For Simple Integer Types: ``std::atomic`` ``````````````````````````````````````````````````````````` .. topic:: See also * :ref:`cxx-memory-model-anti-lms-atomic` .. literalinclude:: code/load-modify-store-atomic.cpp :caption: :download:`code/load-modify-store-atomic.cpp` :language: c++ Project: Door Commandline Loop - Enter Multithreading ----------------------------------------------------- * Two threads * Both inject events into Door FSM * |longrightarrow| Races to no end! If even ``i++`` is not thread safe, how can this be? .. code-block:: c++ void DoorFSM::dwim_button_pressed() { switch (_state) { ... case DOOR_OPEN: _motor.set_direction(MOTOR_LEFT); _state = DOOR_CLOSING; break; ... } Realtime Operating Systems (RTOS) - WhyOS ----------------------------------------- .. topic:: See also * :doc:`/foreign/why-os/index` * :doc:`/trainings/material/soup/linux/why/index` .. topic:: Real RTOS Implementations * `Zephyr `__ * `FreeRTOS `__ Why WhyOS? .......... * Why switch to :doc:`WhyOS `? We want *the door* to be multithreaded, and create an architecture that is *message based* as many RTOS application are. :doc:`WhyOS ` is a shim layer on top of Linux that looks much like an RTOS * Why *message based*? FSM is shared by (currently 2, but generally many) threads |longrightarrow| :ref:`race conditions <2026-11-23-race-condition>` ahead! * :doc:`/trainings/material/soup/linux/why/overview/index` * :doc:`/trainings/material/soup/linux/why/interrupt/index` * :doc:`/trainings/material/soup/linux/why/threads/index` * :doc:`/trainings/material/soup/linux/why/semaphore/index` * :doc:`/trainings/material/soup/linux/why/messagequeue/index` .. _2026-11-23-message-based-arch: Using Message Queues - "Message Based Architecture" ................................................... .. literalinclude:: code/why-intro.cpp :caption: :download:`code/why-intro.cpp` :language: c++