|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Niklas Hauser |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include "functions.hpp" |
| 15 | +%% if multicore |
| 16 | +#include <modm/platform/core/multicore.hpp> |
| 17 | +%% endif |
| 18 | + |
| 19 | +namespace modm::fiber |
| 20 | +{ |
| 21 | + |
| 22 | +/// @ingroup modm_processing_fiber |
| 23 | +/// @{ |
| 24 | + |
| 25 | +/// Implements the `std::mutex` interface for fibers. |
| 26 | +/// @see https://en.cppreference.com/w/cpp/thread/mutex |
| 27 | +class mutex |
| 28 | +{ |
| 29 | + mutex(const mutex&) = delete; |
| 30 | + mutex& operator=(const mutex&) = delete; |
| 31 | + |
| 32 | + volatile bool locked{false}; |
| 33 | +public: |
| 34 | + constexpr mutex() = default; |
| 35 | + |
| 36 | + [[nodiscard]] bool inline |
| 37 | + try_lock() |
| 38 | + { |
| 39 | +%% if multicore |
| 40 | + modm::platform::multicore::SystemSpinLockGuard g; |
| 41 | +%% endif |
| 42 | + if (locked) return false; |
| 43 | + locked = true; |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + void inline |
| 48 | + lock() |
| 49 | + { |
| 50 | + while(not try_lock()) modm::this_fiber::yield(); |
| 51 | + } |
| 52 | + |
| 53 | + void inline |
| 54 | + unlock() |
| 55 | + { |
| 56 | + locked = false; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +/// Implements the `std::timed_mutex` interface for fibers. |
| 61 | +/// @see https://en.cppreference.com/w/cpp/thread/timed_mutex |
| 62 | +class timed_mutex : public mutex |
| 63 | +{ |
| 64 | +public: |
| 65 | + template< typename Rep, typename Period > |
| 66 | + [[nodiscard]] bool |
| 67 | + try_lock_for(std::chrono::duration<Rep, Period> sleep_duration) |
| 68 | + { |
| 69 | + return this_fiber::sleep_condition_for(sleep_duration, [this](){ return try_lock(); }); |
| 70 | + } |
| 71 | + |
| 72 | + template< class Clock, class Duration > |
| 73 | + [[nodiscard]] bool |
| 74 | + try_lock_until(std::chrono::time_point<Clock, Duration> sleep_time) |
| 75 | + { |
| 76 | + return this_fiber::sleep_condition_until(sleep_time, [this](){ return try_lock(); }); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +/// Implements the `std::recursive_mutex` interface for fibers. |
| 81 | +/// @see https://en.cppreference.com/w/cpp/thread/recursive_mutex |
| 82 | +class recursive_mutex |
| 83 | +{ |
| 84 | + recursive_mutex(const recursive_mutex&) = delete; |
| 85 | + recursive_mutex& operator=(const recursive_mutex&) = delete; |
| 86 | + using count_t = uint16_t; |
| 87 | + |
| 88 | + static constexpr fiber::id NoOwner{fiber::id(-1)}; |
| 89 | + volatile fiber::id owner{NoOwner}; |
| 90 | + static constexpr count_t countMax{count_t(-1)}; |
| 91 | + volatile count_t count{1}; |
| 92 | + |
| 93 | +public: |
| 94 | + constexpr recursive_mutex() = default; |
| 95 | + |
| 96 | + [[nodiscard]] bool inline |
| 97 | + try_lock() |
| 98 | + { |
| 99 | +%% if multicore |
| 100 | + modm::platform::multicore::SystemSpinLockGuard g; |
| 101 | +%% endif |
| 102 | + const auto id = modm::this_fiber::get_id(); |
| 103 | + if (owner == NoOwner) { |
| 104 | + owner = id; |
| 105 | + // count = 1; is implicit |
| 106 | + return true; |
| 107 | + } |
| 108 | + if (owner == id and count < countMax) { |
| 109 | + count++; |
| 110 | + return true; |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + void inline |
| 116 | + lock() |
| 117 | + { |
| 118 | + while(not try_lock()) modm::this_fiber::yield(); |
| 119 | + } |
| 120 | + |
| 121 | + void inline |
| 122 | + unlock() |
| 123 | + { |
| 124 | + if (count > 1) count--; |
| 125 | + else { |
| 126 | + // count = 1; is implicit |
| 127 | + owner = NoOwner; |
| 128 | + } |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +/// Implements the `std::timed_recursive_mutex` interface for fibers. |
| 133 | +/// @see https://en.cppreference.com/w/cpp/thread/recursive_mutex |
| 134 | +class timed_recursive_mutex : public recursive_mutex |
| 135 | +{ |
| 136 | +public: |
| 137 | + template< typename Rep, typename Period > |
| 138 | + [[nodiscard]] bool |
| 139 | + try_lock_for(std::chrono::duration<Rep, Period> sleep_duration) |
| 140 | + { |
| 141 | + return this_fiber::sleep_condition_for(sleep_duration, [this](){ return try_lock(); }); |
| 142 | + } |
| 143 | + |
| 144 | + template< class Clock, class Duration > |
| 145 | + [[nodiscard]] bool |
| 146 | + try_lock_until(std::chrono::time_point<Clock, Duration> sleep_time) |
| 147 | + { |
| 148 | + return this_fiber::sleep_condition_until(sleep_time, [this](){ return try_lock(); }); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +/// @} |
| 153 | + |
| 154 | +} |
0 commit comments