Fix code style and consistency of RWLock and Semaphore

This commit is contained in:
Pedro J. Estébanez 2023-01-27 12:36:01 +01:00
parent 9b0f0a7136
commit 7d135cb962
2 changed files with 34 additions and 32 deletions

View file

@ -34,6 +34,7 @@
#include "core/error_list.h" #include "core/error_list.h"
#if !defined(NO_THREADS) #if !defined(NO_THREADS)
#include "core/typedefs.h"
#include <shared_mutex> #include <shared_mutex>
@ -41,33 +42,33 @@ class RWLock {
mutable std::shared_timed_mutex mutex; mutable std::shared_timed_mutex mutex;
public: public:
// Lock the rwlock, block if locked by someone else // Lock the RWLock, block if locked by someone else.
void read_lock() const { _ALWAYS_INLINE_ void read_lock() const {
mutex.lock_shared(); mutex.lock_shared();
} }
// Unlock the rwlock, let other threads continue // Unlock the RWLock, let other threads continue.
void read_unlock() const { _ALWAYS_INLINE_ void read_unlock() const {
mutex.unlock_shared(); mutex.unlock_shared();
} }
// Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock. // Attempt to lock the RWLock for reading. True on success, false means it can't lock.
Error read_try_lock() const { _ALWAYS_INLINE_ Error read_try_lock() const {
return mutex.try_lock_shared() ? OK : ERR_BUSY; return mutex.try_lock_shared() ? OK : ERR_BUSY;
} }
// Lock the rwlock, block if locked by someone else // Lock the RWLock, block if locked by someone else.
void write_lock() { _ALWAYS_INLINE_ void write_lock() {
mutex.lock(); mutex.lock();
} }
// Unlock the rwlock, let other thwrites continue // Unlock the RWLock, let other threads continue.
void write_unlock() { _ALWAYS_INLINE_ void write_unlock() {
mutex.unlock(); mutex.unlock();
} }
// Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock. // Attempt to lock the RWLock for writing. True on success, false means it can't lock.
Error write_try_lock() { _ALWAYS_INLINE_ Error write_try_lock() {
return mutex.try_lock() ? OK : ERR_BUSY; return mutex.try_lock() ? OK : ERR_BUSY;
} }
}; };
@ -91,11 +92,11 @@ class RWLockRead {
const RWLock &lock; const RWLock &lock;
public: public:
RWLockRead(const RWLock &p_lock) : _ALWAYS_INLINE_ RWLockRead(const RWLock &p_lock) :
lock(p_lock) { lock(p_lock) {
lock.read_lock(); lock.read_lock();
} }
~RWLockRead() { _ALWAYS_INLINE_ ~RWLockRead() {
lock.read_unlock(); lock.read_unlock();
} }
}; };
@ -104,11 +105,11 @@ class RWLockWrite {
RWLock &lock; RWLock &lock;
public: public:
RWLockWrite(RWLock &p_lock) : _ALWAYS_INLINE_ RWLockWrite(RWLock &p_lock) :
lock(p_lock) { lock(p_lock) {
lock.write_lock(); lock.write_lock();
} }
~RWLockWrite() { _ALWAYS_INLINE_ ~RWLockWrite() {
lock.write_unlock(); lock.write_unlock();
} }
}; };

View file

@ -41,37 +41,38 @@
class Semaphore { class Semaphore {
private: private:
mutable std::mutex mutex_; mutable std::mutex mutex;
mutable std::condition_variable condition_; mutable std::condition_variable condition;
mutable unsigned long count_ = 0; // Initialized as locked. mutable uint32_t count = 0; // Initialized as locked.
public: public:
_ALWAYS_INLINE_ void post() const { _ALWAYS_INLINE_ void post() const {
std::lock_guard<decltype(mutex_)> lock(mutex_); std::lock_guard<std::mutex> lock(mutex);
++count_; count++;
condition_.notify_one(); condition.notify_one();
} }
_ALWAYS_INLINE_ void wait() const { _ALWAYS_INLINE_ void wait() const {
std::unique_lock<decltype(mutex_)> lock(mutex_); std::unique_lock<std::mutex> lock(mutex);
while (!count_) { // Handle spurious wake-ups. while (!count) { // Handle spurious wake-ups.
condition_.wait(lock); condition.wait(lock);
} }
--count_; count--;
} }
_ALWAYS_INLINE_ bool try_wait() const { _ALWAYS_INLINE_ bool try_wait() const {
std::lock_guard<decltype(mutex_)> lock(mutex_); std::lock_guard<std::mutex> lock(mutex);
if (count_) { if (count) {
--count_; count--;
return true; return true;
} else {
return false;
} }
return false;
} }
_ALWAYS_INLINE_ int get() const { _ALWAYS_INLINE_ int get() const {
std::lock_guard<decltype(mutex_)> lock(mutex_); std::lock_guard<std::mutex> lock(mutex);
return count_; return count;
} }
}; };