From 1029faa02f58a37b7aafc6c039d7469e906b7d5b Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Sat, 23 Nov 2024 13:20:47 +0100 Subject: [PATCH] Refactor PoolBlock's lock code --- src/pool_block.cpp | 8 +------- src/pool_block.h | 2 +- src/uv_util.h | 10 ++++++++++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 0099870..2063be2 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -29,13 +29,7 @@ LOG_CATEGORY(PoolBlock) namespace p2pool { -uv_rwlock_t PoolBlock::s_precalculatedSharesLock; - -static struct PrecalculatedLockInit -{ - FORCEINLINE PrecalculatedLockInit() { uv_rwlock_init(&PoolBlock::s_precalculatedSharesLock); } - FORCEINLINE ~PrecalculatedLockInit() { uv_rwlock_destroy(&PoolBlock::s_precalculatedSharesLock); } -} precalculated_lock_init; +ReadWriteLock PoolBlock::s_precalculatedSharesLock; PoolBlock::PoolBlock() : m_majorVersion(0) diff --git a/src/pool_block.h b/src/pool_block.h index 44634f5..866382d 100644 --- a/src/pool_block.h +++ b/src/pool_block.h @@ -162,7 +162,7 @@ struct PoolBlock bool m_precalculated; - static uv_rwlock_t s_precalculatedSharesLock; + static ReadWriteLock s_precalculatedSharesLock; std::vector m_precalculatedShares; uint64_t m_localTimestamp; diff --git a/src/uv_util.h b/src/uv_util.h index 78d04ea..e699cc4 100644 --- a/src/uv_util.h +++ b/src/uv_util.h @@ -25,6 +25,16 @@ static_assert(sizeof(in_addr) == 4, "struct in_addr has invalid size"); namespace p2pool { +struct ReadWriteLock : public nocopy_nomove { + FORCEINLINE ReadWriteLock() { uv_rwlock_init(&m_lock); } + FORCEINLINE ~ReadWriteLock() { uv_rwlock_destroy(&m_lock); } + + FORCEINLINE operator uv_rwlock_t&() { return m_lock; } + +private: + uv_rwlock_t m_lock; +}; + struct MutexLock : public nocopy_nomove { explicit FORCEINLINE MutexLock(uv_mutex_t& handle) : m_handle(&handle) { uv_mutex_lock(&handle); }