|
| 1 | +/** |
| 2 | + * Copyright Quadrivium LLC |
| 3 | + * All Rights Reserved |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <chrono> |
| 10 | +#include <condition_variable> |
| 11 | +#include <mutex> |
| 12 | +#include <shared_mutex> |
| 13 | + |
| 14 | +namespace jam::se::utils { |
| 15 | + |
| 16 | + /** |
| 17 | + * @brief Creates a weak_ptr from a shared_ptr |
| 18 | + * |
| 19 | + * Utility function that simplifies creating a weak_ptr from a shared_ptr. |
| 20 | + * |
| 21 | + * @tparam T The type of object pointed to |
| 22 | + * @param ptr The shared_ptr to create a weak_ptr from |
| 23 | + * @return A weak_ptr that shares ownership with the input shared_ptr |
| 24 | + */ |
| 25 | + template <typename T> |
| 26 | + inline std::weak_ptr<T> make_weak(const std::shared_ptr<T> &ptr) noexcept { |
| 27 | + return ptr; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @brief Thread-safe wrapper for any object |
| 32 | + * |
| 33 | + * SafeObject provides exclusive and shared access patterns to an internal |
| 34 | + * object with proper synchronization. It uses a mutex to protect the object |
| 35 | + * from concurrent access. |
| 36 | + * |
| 37 | + * @tparam T The type of object to wrap |
| 38 | + * @tparam M The mutex type to use for synchronization (defaults to |
| 39 | + * std::shared_mutex) |
| 40 | + */ |
| 41 | + template <typename T, typename M = std::shared_mutex> |
| 42 | + struct SafeObject { |
| 43 | + using Type = T; |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Constructor that forwards arguments to the wrapped object |
| 47 | + * |
| 48 | + * @tparam Args Parameter pack of argument types |
| 49 | + * @param args Arguments to forward to the wrapped object's constructor |
| 50 | + */ |
| 51 | + template <typename... Args> |
| 52 | + SafeObject(Args &&...args) : t_(std::forward<Args>(args)...) {} |
| 53 | + |
| 54 | + /** |
| 55 | + * @brief Provides exclusive (write) access to the wrapped object |
| 56 | + * |
| 57 | + * Locks the mutex to ensure exclusive access and applies the provided |
| 58 | + * function to the wrapped object. |
| 59 | + * |
| 60 | + * @tparam F Type of the function to apply |
| 61 | + * @param f Function to apply to the wrapped object |
| 62 | + * @return The result of applying the function to the wrapped object |
| 63 | + */ |
| 64 | + template <typename F> |
| 65 | + inline auto exclusiveAccess(F &&f) { |
| 66 | + std::unique_lock lock(cs_); |
| 67 | + return std::forward<F>(f)(t_); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Attempts to get exclusive access without blocking |
| 72 | + * |
| 73 | + * Tries to lock the mutex. If successful, applies the provided function to |
| 74 | + * the wrapped object and returns the result. If unsuccessful, returns an |
| 75 | + * empty optional. |
| 76 | + * |
| 77 | + * @tparam F Type of the function to apply |
| 78 | + * @param f Function to apply to the wrapped object |
| 79 | + * @return An optional containing the result of the function, or empty if |
| 80 | + * the lock was not acquired |
| 81 | + */ |
| 82 | + template <typename F> |
| 83 | + inline auto try_exclusiveAccess(F &&f) { |
| 84 | + std::unique_lock lock(cs_, std::try_to_lock); |
| 85 | + using ResultType = decltype(std::forward<F>(f)(t_)); |
| 86 | + constexpr bool is_void = std::is_void_v<ResultType>; |
| 87 | + using OptionalType = std::conditional_t<is_void, |
| 88 | + std::optional<std::monostate>, |
| 89 | + std::optional<ResultType>>; |
| 90 | + |
| 91 | + if (lock.owns_lock()) { |
| 92 | + if constexpr (is_void) { |
| 93 | + std::forward<F>(f)(t_); |
| 94 | + return OptionalType(std::in_place); |
| 95 | + } else { |
| 96 | + return OptionalType(std::forward<F>(f)(t_)); |
| 97 | + } |
| 98 | + } else { |
| 99 | + return OptionalType(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Provides shared (read) access to the wrapped object |
| 105 | + * |
| 106 | + * Acquires a shared lock on the mutex and applies the provided function |
| 107 | + * to the wrapped object. |
| 108 | + * |
| 109 | + * @tparam F Type of the function to apply |
| 110 | + * @param f Function to apply to the wrapped object |
| 111 | + * @return The result of applying the function to the wrapped object |
| 112 | + */ |
| 113 | + template <typename F> |
| 114 | + inline auto sharedAccess(F &&f) const { |
| 115 | + std::shared_lock lock(cs_); |
| 116 | + return std::forward<F>(f)(t_); |
| 117 | + } |
| 118 | + |
| 119 | + private: |
| 120 | + T t_; ///< The wrapped object |
| 121 | + mutable M cs_; ///< Mutex for synchronization |
| 122 | + }; |
| 123 | + |
| 124 | + /** |
| 125 | + * @brief Alias for SafeObject with a more descriptive name |
| 126 | + * |
| 127 | + * Provides the same functionality as SafeObject but with a name that better |
| 128 | + * describes the read-write access pattern. |
| 129 | + * |
| 130 | + * @tparam T The type of object to wrap |
| 131 | + * @tparam M The mutex type to use for synchronization (defaults to |
| 132 | + * std::shared_mutex) |
| 133 | + */ |
| 134 | + template <typename T, typename M = std::shared_mutex> |
| 135 | + using ReadWriteObject = SafeObject<T, M>; |
| 136 | + |
| 137 | + /** |
| 138 | + * @brief A synchronization primitive similar to a manual reset event |
| 139 | + * |
| 140 | + * WaitForSingleObject provides a way to signal and wait for a condition |
| 141 | + * between threads. It's similar to a manual reset event, where one thread |
| 142 | + * can wait until another thread signals the event. |
| 143 | + */ |
| 144 | + class WaitForSingleObject final { |
| 145 | + std::condition_variable wait_cv_; ///< Condition variable for waiting |
| 146 | + std::mutex wait_m_; ///< Mutex for synchronization |
| 147 | + bool flag_; ///< Flag that represents the state (true = not signaled, false |
| 148 | + ///< = signaled) |
| 149 | + |
| 150 | + public: |
| 151 | + /** |
| 152 | + * @brief Constructor that initializes the object in the not signaled state |
| 153 | + */ |
| 154 | + WaitForSingleObject() : flag_{true} {} |
| 155 | + |
| 156 | + // Deleted copy and move operations to prevent improper synchronization |
| 157 | + WaitForSingleObject(WaitForSingleObject &&) = delete; |
| 158 | + WaitForSingleObject(const WaitForSingleObject &) = delete; |
| 159 | + WaitForSingleObject &operator=(WaitForSingleObject &&) = delete; |
| 160 | + WaitForSingleObject &operator=(const WaitForSingleObject &) = delete; |
| 161 | + |
| 162 | + /** |
| 163 | + * @brief Waits for the object to be signaled with a timeout |
| 164 | + * |
| 165 | + * Blocks the current thread until the object is signaled or the timeout |
| 166 | + * expires. The state is automatically reset to not signaled after a |
| 167 | + * successful wait. |
| 168 | + * |
| 169 | + * @param wait_timeout Maximum time to wait |
| 170 | + * @return true if the object was signaled, false if the timeout expired |
| 171 | + */ |
| 172 | + bool wait(std::chrono::microseconds wait_timeout) { |
| 173 | + std::unique_lock<std::mutex> _lock(wait_m_); |
| 174 | + return wait_cv_.wait_for(_lock, wait_timeout, [&]() { |
| 175 | + auto prev = !flag_; |
| 176 | + flag_ = true; |
| 177 | + return prev; |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @brief Waits indefinitely for the object to be signaled |
| 183 | + * |
| 184 | + * Blocks the current thread until the object is signaled. |
| 185 | + * The state is automatically reset to not signaled after a successful wait. |
| 186 | + */ |
| 187 | + void wait() { |
| 188 | + std::unique_lock<std::mutex> _lock(wait_m_); |
| 189 | + wait_cv_.wait(_lock, [&]() { |
| 190 | + auto prev = !flag_; |
| 191 | + flag_ = true; |
| 192 | + return prev; |
| 193 | + }); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @brief Signals the object |
| 198 | + * |
| 199 | + * Sets the object to the signaled state and wakes one waiting thread. |
| 200 | + */ |
| 201 | + void set() { |
| 202 | + { |
| 203 | + std::unique_lock<std::mutex> _lock(wait_m_); |
| 204 | + flag_ = false; |
| 205 | + } |
| 206 | + wait_cv_.notify_one(); |
| 207 | + } |
| 208 | + }; |
| 209 | +} // namespace jam::se::utils |
0 commit comments