diff options
Diffstat (limited to 'core/os')
-rw-r--r-- | core/os/mutex.h | 2 | ||||
-rw-r--r-- | core/os/semaphore.cpp | 14 | ||||
-rw-r--r-- | core/os/semaphore.h | 31 | ||||
-rw-r--r-- | core/os/thread_dummy.cpp | 8 | ||||
-rw-r--r-- | core/os/thread_dummy.h | 12 |
5 files changed, 16 insertions, 51 deletions
diff --git a/core/os/mutex.h b/core/os/mutex.h index 6cf8ee7c40..8d7b378d60 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -34,7 +34,7 @@ #include "core/error_list.h" #include "core/typedefs.h" -#if !(defined NO_THREADS) +#if !defined(NO_THREADS) #include <mutex> diff --git a/core/os/semaphore.cpp b/core/os/semaphore.cpp index 2c20f234d0..93f1e2dff4 100644 --- a/core/os/semaphore.cpp +++ b/core/os/semaphore.cpp @@ -29,17 +29,3 @@ /*************************************************************************/ #include "semaphore.h" - -#include "core/error_macros.h" - -SemaphoreOld *(*SemaphoreOld::create_func)() = 0; - -SemaphoreOld *SemaphoreOld::create() { - - ERR_FAIL_COND_V(!create_func, 0); - - return create_func(); -} - -SemaphoreOld::~SemaphoreOld() { -} diff --git a/core/os/semaphore.h b/core/os/semaphore.h index f16a15a6db..6f194d4887 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -34,30 +34,32 @@ #include "core/error_list.h" #include "core/typedefs.h" +#if !defined(NO_THREADS) + #include <condition_variable> #include <mutex> class Semaphore { private: - std::mutex mutex_; - std::condition_variable condition_; - unsigned long count_ = 0; // Initialized as locked. + mutable std::mutex mutex_; + mutable std::condition_variable condition_; + mutable unsigned long count_ = 0; // Initialized as locked. public: - _ALWAYS_INLINE_ void post() { + _ALWAYS_INLINE_ void post() const { std::lock_guard<decltype(mutex_)> lock(mutex_); ++count_; condition_.notify_one(); } - _ALWAYS_INLINE_ void wait() { + _ALWAYS_INLINE_ void wait() const { std::unique_lock<decltype(mutex_)> lock(mutex_); while (!count_) // Handle spurious wake-ups. condition_.wait(lock); --count_; } - _ALWAYS_INLINE_ bool try_wait() { + _ALWAYS_INLINE_ bool try_wait() const { std::lock_guard<decltype(mutex_)> lock(mutex_); if (count_) { --count_; @@ -67,18 +69,15 @@ public: } }; -class SemaphoreOld { -protected: - static SemaphoreOld *(*create_func)(); +#else +class Semaphore { public: - virtual Error wait() = 0; ///< wait until semaphore has positive value, then decrement and pass - virtual Error post() = 0; ///< unlock the semaphore, incrementing the value - virtual int get() const = 0; ///< get semaphore value - - static SemaphoreOld *create(); ///< Create a mutex - - virtual ~SemaphoreOld(); + _ALWAYS_INLINE_ void post() const {} + _ALWAYS_INLINE_ void wait() const {} + _ALWAYS_INLINE_ bool try_wait() const { return true; } }; #endif + +#endif diff --git a/core/os/thread_dummy.cpp b/core/os/thread_dummy.cpp index 097c90c1fb..9dcddcae11 100644 --- a/core/os/thread_dummy.cpp +++ b/core/os/thread_dummy.cpp @@ -40,14 +40,6 @@ void ThreadDummy::make_default() { Thread::create_func = &ThreadDummy::create; }; -SemaphoreOld *SemaphoreDummy::create() { - return memnew(SemaphoreDummy); -}; - -void SemaphoreDummy::make_default() { - SemaphoreOld::create_func = &SemaphoreDummy::create; -}; - RWLock *RWLockDummy::create() { return memnew(RWLockDummy); }; diff --git a/core/os/thread_dummy.h b/core/os/thread_dummy.h index c0976ec299..da8188f983 100644 --- a/core/os/thread_dummy.h +++ b/core/os/thread_dummy.h @@ -45,18 +45,6 @@ public: static void make_default(); }; -class SemaphoreDummy : public SemaphoreOld { - - static SemaphoreOld *create(); - -public: - virtual Error wait() { return OK; }; - virtual Error post() { return OK; }; - virtual int get() const { return 0; }; ///< get semaphore value - - static void make_default(); -}; - class RWLockDummy : public RWLock { static RWLock *create(); |