|
| 1 | +#ifndef DICE_TEMPLATELIBRARY_MUTEX_HPP |
| 2 | +#define DICE_TEMPLATELIBRARY_MUTEX_HPP |
| 3 | + |
| 4 | +#include <mutex> |
| 5 | +#include <optional> |
| 6 | +#include <type_traits> |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +namespace dice::template_library { |
| 10 | + |
| 11 | + template<typename T, typename Mutex = std::mutex> |
| 12 | + struct mutex; |
| 13 | + |
| 14 | + /** |
| 15 | + * An RAII guard for a value behind a mutex. |
| 16 | + * When this mutex_guard is dropped the lock is automatically released. |
| 17 | + * |
| 18 | + * @note to use this correctly it is important to understand that unlike rust, C++ cannot |
| 19 | + * enforce that you do not use pointers given out by this type beyond its lifetime. |
| 20 | + * You may not store pointers or references given out via this wrapper beyond its lifetime, otherwise behaviour is undefined. |
| 21 | + * |
| 22 | + * @tparam T the value type protected by the mutex |
| 23 | + * @tparam Mutex the mutex type |
| 24 | + */ |
| 25 | + template<typename T, typename Mutex = std::mutex> |
| 26 | + struct mutex_guard { |
| 27 | + using value_type = T; |
| 28 | + using mutex_type = Mutex; |
| 29 | + |
| 30 | + private: |
| 31 | + friend struct mutex<T, Mutex>; |
| 32 | + |
| 33 | + value_type *value_ptr_; |
| 34 | + std::unique_lock<mutex_type> lock_; |
| 35 | + |
| 36 | + mutex_guard(std::unique_lock<mutex_type> &&lock, T &value) noexcept |
| 37 | + : value_ptr_{&value}, |
| 38 | + lock_{std::move(lock)} { |
| 39 | + } |
| 40 | + |
| 41 | + public: |
| 42 | + mutex_guard() = delete; |
| 43 | + mutex_guard(mutex_guard const &other) noexcept = delete; |
| 44 | + mutex_guard &operator=(mutex_guard const &other) noexcept = delete; |
| 45 | + mutex_guard(mutex_guard &&other) noexcept = default; |
| 46 | + mutex_guard &operator=(mutex_guard &&other) noexcept = default; |
| 47 | + ~mutex_guard() = default; |
| 48 | + |
| 49 | + value_type *operator->() const noexcept { |
| 50 | + return value_ptr_; |
| 51 | + } |
| 52 | + |
| 53 | + value_type &operator*() const noexcept { |
| 54 | + return *value_ptr_; |
| 55 | + } |
| 56 | + |
| 57 | + friend void swap(mutex_guard const &lhs, mutex_guard const &rhs) noexcept { |
| 58 | + using std::swap; |
| 59 | + swap(lhs.value_ptr_, rhs.value_ptr_); |
| 60 | + swap(lhs.lock_, rhs.lock_); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + /** |
| 65 | + * A rust-like mutex type (https://doc.rust-lang.org/std/sync/struct.Mutex.html) that holds its data instead of living next to it. |
| 66 | + * |
| 67 | + * @note Because this is C++ it cannot be fully safe, like the rust version is, for more details see doc comment on mutex_guard. |
| 68 | + * @note This type is non-movable and non-copyable, if you need to do either of these things use `std::unique_ptr<mutex<T>>` or `std::shared_ptr<mutex<T>>` |
| 69 | + * |
| 70 | + * @tparam T value type stored |
| 71 | + * @tparam Mutex the mutex type |
| 72 | + */ |
| 73 | + template<typename T, typename Mutex> |
| 74 | + struct mutex { |
| 75 | + using value_type = T; |
| 76 | + using mutex_type = Mutex; |
| 77 | + |
| 78 | + private: |
| 79 | + value_type value_; |
| 80 | + mutex_type mutex_; |
| 81 | + |
| 82 | + public: |
| 83 | + constexpr mutex() noexcept(std::is_nothrow_default_constructible_v<value_type>) = default; |
| 84 | + constexpr ~mutex() noexcept(std::is_nothrow_destructible_v<value_type>) = default; |
| 85 | + |
| 86 | + mutex(mutex const &other) = delete; |
| 87 | + mutex(mutex &&other) = delete; |
| 88 | + mutex &operator=(mutex const &other) = delete; |
| 89 | + mutex &operator=(mutex &&other) = delete; |
| 90 | + |
| 91 | + explicit constexpr mutex(value_type const &value) noexcept(std::is_nothrow_copy_constructible_v<value_type>) |
| 92 | + : value_{value} { |
| 93 | + } |
| 94 | + |
| 95 | + explicit constexpr mutex(value_type &&value) noexcept(std::is_nothrow_move_constructible_v<value_type>) |
| 96 | + : value_{std::move(value)} { |
| 97 | + } |
| 98 | + |
| 99 | + template<typename ...Args> |
| 100 | + explicit constexpr mutex(std::in_place_t, Args &&...args) noexcept(std::is_nothrow_constructible_v<value_type, decltype(std::forward<Args>(args))...>) |
| 101 | + : value_{std::forward<Args>(args)...} { |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Lock the mutex and return a guard that will keep it locked until it goes out of scope and |
| 106 | + * allows access to the inner value. |
| 107 | + * |
| 108 | + * @return mutex guard for the inner value |
| 109 | + * @throws std::system_error in case the underlying mutex implementation throws it |
| 110 | + */ |
| 111 | + [[nodiscard]] mutex_guard<value_type> lock() { |
| 112 | + return mutex_guard<value_type>{std::unique_lock<mutex_type>{mutex_}, value_}; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Attempt to lock the mutex and return a guard that will keep it locked until it goes out of scope and |
| 117 | + * allows access to the inner value. |
| 118 | + * |
| 119 | + * @return nullopt in case the mutex could not be locked, otherwise a mutex guard for the inner value |
| 120 | + * @throws std::system_error in case the underlying mutex implementation throws it |
| 121 | + */ |
| 122 | + [[nodiscard]] std::optional<mutex_guard<value_type>> try_lock() { |
| 123 | + std::unique_lock<mutex_type> lock{mutex_, std::try_to_lock}; |
| 124 | + if (!lock.owns_lock()) { |
| 125 | + return std::nullopt; |
| 126 | + } |
| 127 | + |
| 128 | + return mutex_guard<value_type>{std::move(lock), value_}; |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | +} // namespace dice::template_library |
| 133 | + |
| 134 | +#endif // DICE_TEMPLATELIBRARY_MUTEX_HPP |
0 commit comments