diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-03-05 16:33:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-05 16:33:45 +0100 |
commit | 42595085a5a22f3b5399844d06e89631c42e8981 (patch) | |
tree | da1c7092d1b0960891b4634358b1150f679fa805 /core | |
parent | bd553d072b65fe5359ab76e64566ff316d35c62e (diff) | |
parent | 9a3a2b03b8b718409eb26252d742d48091756ef7 (diff) |
Merge pull request #36752 from RandomShaper/rework_semaphore
Drop old semaphore implementation
Diffstat (limited to 'core')
-rw-r--r-- | core/bind/core_bind.cpp | 24 | ||||
-rw-r--r-- | core/bind/core_bind.h | 10 | ||||
-rw-r--r-- | core/command_queue_mt.cpp | 7 | ||||
-rw-r--r-- | core/command_queue_mt.h | 10 | ||||
-rw-r--r-- | core/io/file_access_network.cpp | 37 | ||||
-rw-r--r-- | core/io/file_access_network.h | 6 | ||||
-rw-r--r-- | core/io/ip.cpp | 23 | ||||
-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 |
12 files changed, 59 insertions, 125 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 264d8d415c..bfe07d61c5 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -2576,30 +2576,26 @@ void _Marshalls::_bind_methods() { //////////////// -Error _Semaphore::wait() { +void _Semaphore::wait() { - return semaphore->wait(); + semaphore.wait(); } -Error _Semaphore::post() { +Error _Semaphore::try_wait() { - return semaphore->post(); + return semaphore.try_wait() ? OK : ERR_BUSY; } -void _Semaphore::_bind_methods() { +void _Semaphore::post() { - ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait); - ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post); + semaphore.post(); } -_Semaphore::_Semaphore() { - - semaphore = SemaphoreOld::create(); -} - -_Semaphore::~_Semaphore() { +void _Semaphore::_bind_methods() { - memdelete(semaphore); + ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait); + ClassDB::bind_method(D_METHOD("try_wait"), &_Semaphore::try_wait); + ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post); } /////////////// diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index e7850de744..fc6419b7d8 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -635,16 +635,14 @@ public: class _Semaphore : public Reference { GDCLASS(_Semaphore, Reference); - SemaphoreOld *semaphore; + Semaphore semaphore; static void _bind_methods(); public: - Error wait(); - Error post(); - - _Semaphore(); - ~_Semaphore(); + void wait(); + Error try_wait(); + void post(); }; class _Thread : public Reference { diff --git a/core/command_queue_mt.cpp b/core/command_queue_mt.cpp index b88f773ed8..85e8a847a0 100644 --- a/core/command_queue_mt.cpp +++ b/core/command_queue_mt.cpp @@ -108,11 +108,10 @@ CommandQueueMT::CommandQueueMT(bool p_sync) { for (int i = 0; i < SYNC_SEMAPHORES; i++) { - sync_sems[i].sem = SemaphoreOld::create(); sync_sems[i].in_use = false; } if (p_sync) - sync = SemaphoreOld::create(); + sync = memnew(Semaphore); else sync = NULL; } @@ -121,9 +120,5 @@ CommandQueueMT::~CommandQueueMT() { if (sync) memdelete(sync); - for (int i = 0; i < SYNC_SEMAPHORES; i++) { - - memdelete(sync_sems[i].sem); - } memfree(command_mem); } diff --git a/core/command_queue_mt.h b/core/command_queue_mt.h index 7407557e7e..90231546ef 100644 --- a/core/command_queue_mt.h +++ b/core/command_queue_mt.h @@ -270,7 +270,7 @@ cmd->sync_sem = ss; \ unlock(); \ if (sync) sync->post(); \ - ss->sem->wait(); \ + ss->sem.wait(); \ ss->in_use = false; \ } @@ -287,7 +287,7 @@ cmd->sync_sem = ss; \ unlock(); \ if (sync) sync->post(); \ - ss->sem->wait(); \ + ss->sem.wait(); \ ss->in_use = false; \ } @@ -297,7 +297,7 @@ class CommandQueueMT { struct SyncSemaphore { - SemaphoreOld *sem; + Semaphore sem; bool in_use; }; @@ -313,7 +313,7 @@ class CommandQueueMT { SyncSemaphore *sync_sem; virtual void post() { - sync_sem->sem->post(); + sync_sem->sem.post(); } }; @@ -342,7 +342,7 @@ class CommandQueueMT { uint32_t dealloc_ptr; SyncSemaphore sync_sems[SYNC_SEMAPHORES]; Mutex mutex; - SemaphoreOld *sync; + Semaphore *sync; template <class T> T *allocate() { diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 7f1eb6fd90..370dd8f982 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -88,9 +88,7 @@ void FileAccessNetworkClient::_thread_func() { while (!quit) { DEBUG_PRINT("SEM WAIT - " + itos(sem->get())); - Error err = sem->wait(); - if (err != OK) - ERR_PRINT("sem->wait() failed"); + sem.wait(); DEBUG_TIME("sem_unlock"); //DEBUG_PRINT("semwait returned "+itos(werr)); DEBUG_PRINT("MUTEX LOCK " + itos(lockcount)); @@ -141,7 +139,7 @@ void FileAccessNetworkClient::_thread_func() { fa->_respond(len, Error(status)); } - fa->sem->post(); + fa->sem.post(); } break; case FileAccessNetwork::RESPONSE_DATA: { @@ -161,14 +159,14 @@ void FileAccessNetworkClient::_thread_func() { int status = get_32(); fa->exists_modtime = status != 0; - fa->sem->post(); + fa->sem.post(); } break; case FileAccessNetwork::RESPONSE_GET_MODTIME: { uint64_t status = get_64(); fa->exists_modtime = status; - fa->sem->post(); + fa->sem.post(); } break; } @@ -230,7 +228,6 @@ FileAccessNetworkClient::FileAccessNetworkClient() { singleton = this; last_id = 0; client.instance(); - sem = SemaphoreOld::create(); lockcount = 0; } @@ -238,12 +235,10 @@ FileAccessNetworkClient::~FileAccessNetworkClient() { if (thread) { quit = true; - sem->post(); + sem.post(); Thread::wait_to_finish(thread); memdelete(thread); } - - memdelete(sem); } void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block) { @@ -264,7 +259,7 @@ void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block) if (waiting_on_page == page) { waiting_on_page = -1; - page_sem->post(); + page_sem.post(); } } @@ -306,9 +301,9 @@ Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) { nc->unlock_mutex(); DEBUG_PRINT("OPEN POST"); DEBUG_TIME("open_post"); - nc->sem->post(); //awaiting answer + nc->sem.post(); //awaiting answer DEBUG_PRINT("WAIT..."); - sem->wait(); + sem.wait(); DEBUG_TIME("open_end"); DEBUG_PRINT("WAIT ENDED..."); @@ -393,7 +388,7 @@ void FileAccessNetwork::_queue_page(int p_page) const { pages.write[p_page].queued = true; } DEBUG_PRINT("QUEUE PAGE POST"); - nc->sem->post(); + nc->sem.post(); DEBUG_PRINT("queued " + itos(p_page)); } } @@ -426,7 +421,7 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { } buffer_mutex.unlock(); DEBUG_PRINT("wait"); - page_sem->wait(); + page_sem.wait(); DEBUG_PRINT("done"); } else { @@ -475,8 +470,8 @@ bool FileAccessNetwork::file_exists(const String &p_path) { nc->client->put_data((const uint8_t *)cs.ptr(), cs.length()); nc->unlock_mutex(); DEBUG_PRINT("FILE EXISTS POST"); - nc->sem->post(); - sem->wait(); + nc->sem.post(); + sem.wait(); return exists_modtime != 0; } @@ -492,8 +487,8 @@ uint64_t FileAccessNetwork::_get_modified_time(const String &p_file) { nc->client->put_data((const uint8_t *)cs.ptr(), cs.length()); nc->unlock_mutex(); DEBUG_PRINT("MODTIME POST"); - nc->sem->post(); - sem->wait(); + nc->sem.post(); + sem.wait(); return exists_modtime; } @@ -521,8 +516,6 @@ FileAccessNetwork::FileAccessNetwork() { eof_flag = false; opened = false; pos = 0; - sem = SemaphoreOld::create(); - page_sem = SemaphoreOld::create(); FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); id = nc->last_id++; @@ -538,8 +531,6 @@ FileAccessNetwork::FileAccessNetwork() { FileAccessNetwork::~FileAccessNetwork() { close(); - memdelete(sem); - memdelete(page_sem); FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index 38d9b8e8a6..7f664b46f7 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -49,7 +49,7 @@ class FileAccessNetworkClient { List<BlockRequest> block_requests; - SemaphoreOld *sem; + Semaphore sem; Thread *thread; bool quit; Mutex mutex; @@ -85,8 +85,8 @@ public: class FileAccessNetwork : public FileAccess { - SemaphoreOld *sem; - SemaphoreOld *page_sem; + Semaphore sem; + Semaphore page_sem; Mutex buffer_mutex; bool opened; size_t total_size; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index af534e4bb7..2143b84d15 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -71,7 +71,7 @@ struct _IP_ResolverPrivate { } Mutex mutex; - SemaphoreOld *sem; + Semaphore sem; Thread *thread; //Semaphore* semaphore; @@ -98,7 +98,7 @@ struct _IP_ResolverPrivate { while (!ipr->thread_abort) { - ipr->sem->wait(); + ipr->sem.wait(); MutexLock lock(ipr->mutex); ipr->resolve_queues(); @@ -148,7 +148,7 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ resolver->queue[id].response = IP_Address(); resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING; if (resolver->thread) - resolver->sem->post(); + resolver->sem.post(); else resolver->resolve_queues(); } @@ -300,23 +300,13 @@ IP::IP() { singleton = this; resolver = memnew(_IP_ResolverPrivate); - resolver->sem = NULL; #ifndef NO_THREADS - resolver->sem = SemaphoreOld::create(); - if (resolver->sem) { - resolver->thread_abort = false; + resolver->thread_abort = false; - resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver); - - if (!resolver->thread) - memdelete(resolver->sem); //wtf - } else { - resolver->thread = NULL; - } + resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver); #else - resolver->sem = NULL; resolver->thread = NULL; #endif } @@ -326,10 +316,9 @@ IP::~IP() { #ifndef NO_THREADS if (resolver->thread) { resolver->thread_abort = true; - resolver->sem->post(); + resolver->sem.post(); Thread::wait_to_finish(resolver->thread); memdelete(resolver->thread); - memdelete(resolver->sem); } #endif 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(); |