|
| 1 | +#ifndef FINE_SYNC_HPP |
| 2 | +#define FINE_SYNC_HPP |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <cstddef> |
| 6 | +#include <memory> |
| 7 | +#include <mutex> // IWYU pragma: keep |
| 8 | +#include <optional> |
| 9 | +#include <shared_mutex> // IWYU pragma: keep |
| 10 | +#include <sstream> |
| 11 | +#include <stdexcept> |
| 12 | +#include <string> |
| 13 | +#include <string_view> |
| 14 | + |
| 15 | +#include <erl_nif.h> |
| 16 | + |
| 17 | +namespace fine { |
| 18 | +// Creates a mutually exclusive lock backed by Erlang. |
| 19 | +// |
| 20 | +// This mutex type implements the Lockable requirement, ensuring it can be used |
| 21 | +// with `std::unique_lock` and family. |
| 22 | +class Mutex final { |
| 23 | +public: |
| 24 | + // Creates an unnamed Mutex. |
| 25 | + Mutex() : m_handle(enif_mutex_create(nullptr)) { |
| 26 | + if (!m_handle) { |
| 27 | + throw std::runtime_error("failed to create mutex"); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Creates a Mutex from a ErlNifMutex handle. |
| 32 | + explicit Mutex(ErlNifMutex *handle) noexcept : m_handle(handle) {} |
| 33 | + |
| 34 | + // Creates a Mutex with the given debug information. |
| 35 | + Mutex(std::string_view app, std::string_view type, |
| 36 | + std::optional<std::string_view> instance = std::nullopt) { |
| 37 | + std::stringstream stream; |
| 38 | + stream << app << "." << type; |
| 39 | + |
| 40 | + if (instance) { |
| 41 | + stream << "[" << *instance << "]"; |
| 42 | + } |
| 43 | + |
| 44 | + std::string str = std::move(stream).str(); |
| 45 | + |
| 46 | + // We make use of `const_cast` to create the mutex, but this exceptional |
| 47 | + // situation is acceptable, as `enif_mutex_create` doesn't modify the name: |
| 48 | + // https://github.com/erlang/otp/blob/a87183f1eb847119b6ecc83054bf13c26b8ccfaa/erts/emulator/beam/erl_drv_thread.c#L166-L169 |
| 49 | + auto *handle = enif_mutex_create(const_cast<char *>(str.c_str())); |
| 50 | + if (handle == nullptr) { |
| 51 | + throw std::runtime_error("failed to create mutex"); |
| 52 | + } |
| 53 | + m_handle.reset(handle); |
| 54 | + } |
| 55 | + |
| 56 | + // Converts this Mutex to a ErlNifMutex handle. |
| 57 | + // |
| 58 | + // Ownership still belongs to this instance. |
| 59 | + operator ErlNifMutex *() const & noexcept { return m_handle.get(); } |
| 60 | + |
| 61 | + // Releases ownership of the ErlNifMutex handle to the caller. |
| 62 | + // |
| 63 | + // This operation is only possible by: |
| 64 | + // ``` |
| 65 | + // static_cast<ErlNifMutex*>(std::move(mutex)) |
| 66 | + // ``` |
| 67 | + explicit operator ErlNifMutex *() && noexcept { return m_handle.release(); } |
| 68 | + |
| 69 | + // Locks the Mutex. The calling thread is blocked until the Mutex has been |
| 70 | + // locked. A thread that has currently locked the Mutex cannot lock the same |
| 71 | + // Mutex again. |
| 72 | + // |
| 73 | + // This function is thread-safe. |
| 74 | + void lock() noexcept { enif_mutex_lock(m_handle.get()); } |
| 75 | + |
| 76 | + // Unlocks a Mutex. The Mutex currently must be locked by the calling thread. |
| 77 | + // |
| 78 | + // This function is thread-safe. |
| 79 | + void unlock() noexcept { enif_mutex_unlock(m_handle.get()); } |
| 80 | + |
| 81 | + // Tries to lock a Mutex. A thread that has currently locked the Mutex cannot |
| 82 | + // try to lock the same Mutex again. |
| 83 | + // |
| 84 | + // This function is thread-safe. |
| 85 | + bool try_lock() noexcept { return enif_mutex_trylock(m_handle.get()) == 0; } |
| 86 | + |
| 87 | +private: |
| 88 | + struct Deleter { |
| 89 | + void operator()(ErlNifMutex *handle) const noexcept { |
| 90 | + enif_mutex_destroy(handle); |
| 91 | + } |
| 92 | + }; |
| 93 | + std::unique_ptr<ErlNifMutex, Deleter> m_handle; |
| 94 | +}; |
| 95 | + |
| 96 | +// Creates a read-write lock backed by Erlang. |
| 97 | +// |
| 98 | +// This lock type implements the Lockable and SharedLockable requirements, |
| 99 | +// ensuring it can be used with `std::unique_lock`, `std::shared_lock`, etc. |
| 100 | +class SharedMutex final { |
| 101 | +public: |
| 102 | + // Creates an unnamed SharedMutex. |
| 103 | + SharedMutex() : m_handle(enif_rwlock_create(nullptr)) { |
| 104 | + if (!m_handle) { |
| 105 | + throw std::runtime_error("failed to create rwlock"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Creates a SharedMutex from a ErlNifRWLock handle. |
| 110 | + explicit SharedMutex(ErlNifRWLock *handle) noexcept : m_handle(handle) {} |
| 111 | + |
| 112 | + // Creates a SharedMutex with the given name. |
| 113 | + SharedMutex(std::string_view app, std::string_view type, |
| 114 | + std::optional<std::string_view> instance = std::nullopt) { |
| 115 | + std::stringstream stream; |
| 116 | + stream << app << "." << type; |
| 117 | + |
| 118 | + if (instance) { |
| 119 | + stream << "[" << *instance << "]"; |
| 120 | + } |
| 121 | + |
| 122 | + std::string str = std::move(stream).str(); |
| 123 | + |
| 124 | + // We make use of `const_cast` to create the rwlock, but this exceptional |
| 125 | + // situation is acceptable, as `enif_rwlock_create` doesn't modify the name: |
| 126 | + // https://github.com/erlang/otp/blob/a87183f1eb847119b6ecc83054bf13c26b8ccfaa/ert/emulator/beam/erl_drv_thread.c#L337-L340 |
| 127 | + auto *handle = enif_rwlock_create(const_cast<char *>(str.c_str())); |
| 128 | + if (handle == nullptr) { |
| 129 | + throw std::runtime_error("failed to create rwlock"); |
| 130 | + } |
| 131 | + m_handle.reset(handle); |
| 132 | + } |
| 133 | + |
| 134 | + // Converts this SharedMutex to a ErlNifSharedMutex handle. |
| 135 | + // |
| 136 | + // Ownership still belongs to this instance. |
| 137 | + operator ErlNifRWLock *() const & noexcept { return m_handle.get(); } |
| 138 | + |
| 139 | + // Releases ownership of the ErlNifRWLock handle to the caller. |
| 140 | + // |
| 141 | + // This operation is only possible by: |
| 142 | + // ``` |
| 143 | + // static_cast<ErlNifRWLock*>(std::move(rwlock)) |
| 144 | + // ``` |
| 145 | + explicit operator ErlNifRWLock *() && noexcept { return m_handle.release(); } |
| 146 | + |
| 147 | + // Read locks a SharedMutex. The calling thread is blocked until the |
| 148 | + // SharedMutex has been read locked. A thread that currently has read or |
| 149 | + // read/write locked the SharedMutex cannot lock the same SharedMutex again. |
| 150 | + // |
| 151 | + // This function is thread-safe. |
| 152 | + void lock_shared() noexcept { enif_rwlock_rlock(m_handle.get()); } |
| 153 | + |
| 154 | + // Read unlocks a SharedMutex. The SharedMutex currently must be read locked |
| 155 | + // by the calling thread. |
| 156 | + // |
| 157 | + // This function is thread-safe. |
| 158 | + void unlock_shared() noexcept { enif_rwlock_runlock(m_handle.get()); } |
| 159 | + |
| 160 | + // Read/write locks a SharedMutex. The calling thread is blocked until the |
| 161 | + // SharedMutex has been read/write locked. A thread that currently has read or |
| 162 | + // read/write locked the SharedMutex cannot lock the same SharedMutex again. |
| 163 | + // |
| 164 | + // This function is thread-safe. |
| 165 | + void lock() noexcept { enif_rwlock_rwlock(m_handle.get()); } |
| 166 | + |
| 167 | + // Read/write unlocks a SharedMutex. The SharedMutex currently must be |
| 168 | + // read/write locked by the calling thread. |
| 169 | + // |
| 170 | + // This function is thread-safe. |
| 171 | + void unlock() noexcept { enif_rwlock_rwunlock(m_handle.get()); } |
| 172 | + |
| 173 | + // Tries to read lock a SharedMutex. |
| 174 | + // |
| 175 | + // This function is thread-safe. |
| 176 | + bool try_lock_shared() noexcept { |
| 177 | + return enif_rwlock_tryrlock(m_handle.get()) == 0; |
| 178 | + } |
| 179 | + |
| 180 | + // Tries to read/write lock a SharedMutex. A thread that currently has read |
| 181 | + // or read/write locked the SharedMutex cannot try to lock the same |
| 182 | + // SharedMutex again. |
| 183 | + // |
| 184 | + // This function is thread-safe. |
| 185 | + bool try_lock() noexcept { |
| 186 | + return enif_rwlock_tryrwlock(m_handle.get()) == 0; |
| 187 | + } |
| 188 | + |
| 189 | +private: |
| 190 | + struct Deleter { |
| 191 | + void operator()(ErlNifRWLock *handle) const noexcept { |
| 192 | + enif_rwlock_destroy(handle); |
| 193 | + } |
| 194 | + }; |
| 195 | + std::unique_ptr<ErlNifRWLock, Deleter> m_handle; |
| 196 | +}; |
| 197 | +} // namespace fine |
| 198 | + |
| 199 | +#endif |
0 commit comments