Browse Source

Fix code style and consistency of RWLock and Semaphore

Pedro J. Estébanez 2 years ago
parent
commit
1de616d8dc
2 changed files with 31 additions and 28 deletions
  1. 16 14
      core/os/rw_lock.h
  2. 15 14
      core/os/semaphore.h

+ 16 - 14
core/os/rw_lock.h

@@ -31,39 +31,41 @@
 #ifndef RW_LOCK_H
 #ifndef RW_LOCK_H
 #define RW_LOCK_H
 #define RW_LOCK_H
 
 
+#include "core/typedefs.h"
+
 #include <shared_mutex>
 #include <shared_mutex>
 
 
 class RWLock {
 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
-	void read_lock() const {
+	// Lock the RWLock, block if locked by someone else.
+	_ALWAYS_INLINE_ void read_lock() const {
 		mutex.lock_shared();
 		mutex.lock_shared();
 	}
 	}
 
 
-	// Unlock the rwlock, let other threads continue
-	void read_unlock() const {
+	// Unlock the RWLock, let other threads continue.
+	_ALWAYS_INLINE_ void read_unlock() const {
 		mutex.unlock_shared();
 		mutex.unlock_shared();
 	}
 	}
 
 
 	// Attempt to lock the RWLock for reading. True on success, false means it can't lock.
 	// Attempt to lock the RWLock for reading. True on success, false means it can't lock.
-	bool read_try_lock() const {
+	_ALWAYS_INLINE_ bool read_try_lock() const {
 		return mutex.try_lock_shared();
 		return mutex.try_lock_shared();
 	}
 	}
 
 
-	// Lock the rwlock, block if locked by someone else
-	void write_lock() {
+	// Lock the RWLock, block if locked by someone else.
+	_ALWAYS_INLINE_ void write_lock() {
 		mutex.lock();
 		mutex.lock();
 	}
 	}
 
 
-	// Unlock the rwlock, let other thwrites continue
-	void write_unlock() {
+	// Unlock the RWLock, let other threads continue.
+	_ALWAYS_INLINE_ void write_unlock() {
 		mutex.unlock();
 		mutex.unlock();
 	}
 	}
 
 
 	// Attempt to lock the RWLock for writing. True on success, false means it can't lock.
 	// Attempt to lock the RWLock for writing. True on success, false means it can't lock.
-	bool write_try_lock() {
+	_ALWAYS_INLINE_ bool write_try_lock() {
 		return mutex.try_lock();
 		return mutex.try_lock();
 	}
 	}
 };
 };
@@ -72,11 +74,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();
 	}
 	}
 };
 };
@@ -85,11 +87,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();
 	}
 	}
 };
 };

+ 15 - 14
core/os/semaphore.h

@@ -39,32 +39,33 @@
 
 
 class Semaphore {
 class Semaphore {
 private:
 private:
-	mutable std::mutex mutex_;
-	mutable std::condition_variable condition_;
-	mutable unsigned long count_ = 0; // Initialized as locked.
+	mutable std::mutex mutex;
+	mutable std::condition_variable condition;
+	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_);
-		++count_;
-		condition_.notify_one();
+		std::lock_guard lock(mutex);
+		count++;
+		condition.notify_one();
 	}
 	}
 
 
 	_ALWAYS_INLINE_ void wait() const {
 	_ALWAYS_INLINE_ void wait() const {
-		std::unique_lock<decltype(mutex_)> lock(mutex_);
-		while (!count_) { // Handle spurious wake-ups.
-			condition_.wait(lock);
+		std::unique_lock lock(mutex);
+		while (!count) { // Handle spurious wake-ups.
+			condition.wait(lock);
 		}
 		}
-		--count_;
+		count--;
 	}
 	}
 
 
 	_ALWAYS_INLINE_ bool try_wait() const {
 	_ALWAYS_INLINE_ bool try_wait() const {
-		std::lock_guard<decltype(mutex_)> lock(mutex_);
-		if (count_) {
-			--count_;
+		std::lock_guard lock(mutex);
+		if (count) {
+			count--;
 			return true;
 			return true;
+		} else {
+			return false;
 		}
 		}
-		return false;
 	}
 	}
 };
 };