diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 5750a6ef13f..6e19bda90d0 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -2717,17 +2717,17 @@ _Semaphore::~_Semaphore() { void _Mutex::lock() { - mutex->lock(); + mutex.lock(); } Error _Mutex::try_lock() { - return mutex->try_lock(); + return mutex.try_lock(); } void _Mutex::unlock() { - mutex->unlock(); + mutex.unlock(); } void _Mutex::_bind_methods() { @@ -2737,16 +2737,6 @@ void _Mutex::_bind_methods() { ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock); } -_Mutex::_Mutex() { - - mutex = Mutex::create(); -} - -_Mutex::~_Mutex() { - - memdelete(mutex); -} - /////////////// void _Thread::_start_func(void *ud) { diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index db0a9798d0c..3995c9f01d8 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -652,7 +652,7 @@ public: class _Mutex : public Reference { GDCLASS(_Mutex, Reference); - Mutex *mutex; + Mutex mutex; static void _bind_methods(); @@ -660,9 +660,6 @@ public: void lock(); Error try_lock(); void unlock(); - - _Mutex(); - ~_Mutex(); }; class _Semaphore : public Reference { diff --git a/core/command_queue_mt.cpp b/core/command_queue_mt.cpp index e4154423c79..5f0a257cbda 100644 --- a/core/command_queue_mt.cpp +++ b/core/command_queue_mt.cpp @@ -35,14 +35,12 @@ void CommandQueueMT::lock() { - if (mutex) - mutex->lock(); + mutex.lock(); } void CommandQueueMT::unlock() { - if (mutex) - mutex->unlock(); + mutex.unlock(); } void CommandQueueMT::wait_for_flush() { @@ -107,7 +105,6 @@ CommandQueueMT::CommandQueueMT(bool p_sync) { read_ptr_and_epoch = 0; write_ptr_and_epoch = 0; dealloc_ptr = 0; - mutex = Mutex::create(); command_mem_size = GLOBAL_DEF_RST("memory/limits/command_queue/multithreading_queue_size_kb", DEFAULT_COMMAND_MEM_SIZE_KB); ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/command_queue/multithreading_queue_size_kb", PropertyInfo(Variant::INT, "memory/limits/command_queue/multithreading_queue_size_kb", PROPERTY_HINT_RANGE, "1,4096,1,or_greater")); @@ -130,7 +127,6 @@ CommandQueueMT::~CommandQueueMT() { if (sync) memdelete(sync); - memdelete(mutex); for (int i = 0; i < SYNC_SEMAPHORES; i++) { memdelete(sync_sems[i].sem); diff --git a/core/command_queue_mt.h b/core/command_queue_mt.h index 9a8a920f9d1..ae0ca1d3150 100644 --- a/core/command_queue_mt.h +++ b/core/command_queue_mt.h @@ -321,7 +321,7 @@ class CommandQueueMT { uint32_t dealloc_ptr; uint32_t command_mem_size; SyncSemaphore sync_sems[SYNC_SEMAPHORES]; - Mutex *mutex; + Mutex mutex; Semaphore *sync; template diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index c110b4facfc..dc36c4159a3 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -42,14 +42,14 @@ void FileAccessNetworkClient::lock_mutex() { - mutex->lock(); + mutex.lock(); lockcount++; } void FileAccessNetworkClient::unlock_mutex() { lockcount--; - mutex->unlock(); + mutex.unlock(); } void FileAccessNetworkClient::put_32(int p_32) { @@ -97,7 +97,7 @@ void FileAccessNetworkClient::_thread_func() { lock_mutex(); DEBUG_PRINT("MUTEX PASS"); - blockrequest_mutex->lock(); + blockrequest_mutex.lock(); while (block_requests.size()) { put_32(block_requests.front()->get().id); put_32(FileAccessNetwork::COMMAND_READ_BLOCK); @@ -105,7 +105,7 @@ void FileAccessNetworkClient::_thread_func() { put_32(block_requests.front()->get().size); block_requests.pop_front(); } - blockrequest_mutex->unlock(); + blockrequest_mutex.unlock(); DEBUG_PRINT("THREAD ITER"); @@ -225,8 +225,6 @@ FileAccessNetworkClient *FileAccessNetworkClient::singleton = NULL; FileAccessNetworkClient::FileAccessNetworkClient() { thread = NULL; - mutex = Mutex::create(); - blockrequest_mutex = Mutex::create(); quit = false; singleton = this; last_id = 0; @@ -244,8 +242,6 @@ FileAccessNetworkClient::~FileAccessNetworkClient() { memdelete(thread); } - memdelete(blockrequest_mutex); - memdelete(mutex); memdelete(sem); } @@ -259,10 +255,10 @@ void FileAccessNetwork::_set_block(int p_offset, const Vector &p_block) ERR_FAIL_COND((p_block.size() != (int)(total_size % page_size))); } - buffer_mutex->lock(); + buffer_mutex.lock(); pages.write[page].buffer = p_block; pages.write[page].queued = false; - buffer_mutex->unlock(); + buffer_mutex.unlock(); if (waiting_on_page == page) { waiting_on_page = -1; @@ -385,14 +381,14 @@ void FileAccessNetwork::_queue_page(int p_page) const { FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; - nc->blockrequest_mutex->lock(); + nc->blockrequest_mutex.lock(); FileAccessNetworkClient::BlockRequest br; br.id = id; br.offset = size_t(p_page) * page_size; br.size = page_size; nc->block_requests.push_back(br); pages.write[p_page].queued = true; - nc->blockrequest_mutex->unlock(); + nc->blockrequest_mutex.unlock(); DEBUG_PRINT("QUEUE PAGE POST"); nc->sem->post(); DEBUG_PRINT("queued " + itos(p_page)); @@ -418,14 +414,14 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { int page = pos / page_size; if (page != last_page) { - buffer_mutex->lock(); + buffer_mutex.lock(); if (pages[page].buffer.empty()) { waiting_on_page = page; for (int j = 0; j < read_ahead; j++) { _queue_page(page + j); } - buffer_mutex->unlock(); + buffer_mutex.unlock(); DEBUG_PRINT("wait"); page_sem->wait(); DEBUG_PRINT("done"); @@ -436,7 +432,7 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { _queue_page(page + j); } //queue pages - buffer_mutex->unlock(); + buffer_mutex.unlock(); } buff = pages.write[page].buffer.ptrw(); @@ -524,7 +520,6 @@ FileAccessNetwork::FileAccessNetwork() { pos = 0; sem = Semaphore::create(); page_sem = Semaphore::create(); - buffer_mutex = Mutex::create(); FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); id = nc->last_id++; @@ -542,7 +537,6 @@ FileAccessNetwork::~FileAccessNetwork() { close(); memdelete(sem); memdelete(page_sem); - memdelete(buffer_mutex); FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index ad407199957..ab234cdbc45 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -52,8 +52,8 @@ class FileAccessNetworkClient { Semaphore *sem; Thread *thread; bool quit; - Mutex *mutex; - Mutex *blockrequest_mutex; + Mutex mutex; + Mutex blockrequest_mutex; Map accesses; Ref client; int last_id; @@ -87,7 +87,7 @@ class FileAccessNetwork : public FileAccess { Semaphore *sem; Semaphore *page_sem; - Mutex *buffer_mutex; + Mutex buffer_mutex; bool opened; size_t total_size; mutable size_t pos; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 6e8389d05e5..7e7dc252c1a 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -70,7 +70,7 @@ struct _IP_ResolverPrivate { return IP::RESOLVER_INVALID_ID; } - Mutex *mutex; + Mutex mutex; Semaphore *sem; Thread *thread; @@ -100,9 +100,9 @@ struct _IP_ResolverPrivate { ipr->sem->wait(); - ipr->mutex->lock(); + ipr->mutex.lock(); ipr->resolve_queues(); - ipr->mutex->unlock(); + ipr->mutex.unlock(); } } @@ -115,30 +115,30 @@ struct _IP_ResolverPrivate { IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) { - resolver->mutex->lock(); + resolver->mutex.lock(); String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type); if (resolver->cache.has(key) && resolver->cache[key].is_valid()) { IP_Address res = resolver->cache[key]; - resolver->mutex->unlock(); + resolver->mutex.unlock(); return res; } IP_Address res = _resolve_hostname(p_hostname, p_type); resolver->cache[key] = res; - resolver->mutex->unlock(); + resolver->mutex.unlock(); return res; } IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) { - resolver->mutex->lock(); + resolver->mutex.lock(); ResolverID id = resolver->find_empty_id(); if (id == RESOLVER_INVALID_ID) { WARN_PRINT("Out of resolver queries"); - resolver->mutex->unlock(); + resolver->mutex.unlock(); return id; } @@ -157,7 +157,7 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ resolver->resolve_queues(); } - resolver->mutex->unlock(); + resolver->mutex.unlock(); return id; } @@ -165,15 +165,15 @@ IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const { ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE); - resolver->mutex->lock(); + resolver->mutex.lock(); if (resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE) { ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE"); - resolver->mutex->unlock(); + resolver->mutex.unlock(); return IP::RESOLVER_STATUS_NONE; } IP::ResolverStatus res = resolver->queue[p_id].status; - resolver->mutex->unlock(); + resolver->mutex.unlock(); return res; } @@ -181,17 +181,17 @@ IP_Address IP::get_resolve_item_address(ResolverID p_id) const { ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address()); - resolver->mutex->lock(); + resolver->mutex.lock(); if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) { ERR_PRINTS("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet."); - resolver->mutex->unlock(); + resolver->mutex.unlock(); return IP_Address(); } IP_Address res = resolver->queue[p_id].response; - resolver->mutex->unlock(); + resolver->mutex.unlock(); return res; } @@ -199,16 +199,16 @@ void IP::erase_resolve_item(ResolverID p_id) { ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES); - resolver->mutex->lock(); + resolver->mutex.lock(); resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE; - resolver->mutex->unlock(); + resolver->mutex.unlock(); } void IP::clear_cache(const String &p_hostname) { - resolver->mutex->lock(); + resolver->mutex.lock(); if (p_hostname.empty()) { resolver->cache.clear(); @@ -219,7 +219,7 @@ void IP::clear_cache(const String &p_hostname) { resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY)); } - resolver->mutex->unlock(); + resolver->mutex.unlock(); } Array IP::_get_local_addresses() const { @@ -315,7 +315,6 @@ IP::IP() { singleton = this; resolver = memnew(_IP_ResolverPrivate); resolver->sem = NULL; - resolver->mutex = Mutex::create(); #ifndef NO_THREADS @@ -349,6 +348,5 @@ IP::~IP() { #endif - memdelete(resolver->mutex); memdelete(resolver); } diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 0697f291216..19cd36d2a95 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -289,9 +289,7 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c bool ResourceLoader::_add_to_loading_map(const String &p_path) { bool success; - if (loading_map_mutex) { - loading_map_mutex->lock(); - } + loading_map_mutex.lock(); LoadingMapKey key; key.path = p_path; @@ -304,17 +302,13 @@ bool ResourceLoader::_add_to_loading_map(const String &p_path) { success = true; } - if (loading_map_mutex) { - loading_map_mutex->unlock(); - } + loading_map_mutex.unlock(); return success; } void ResourceLoader::_remove_from_loading_map(const String &p_path) { - if (loading_map_mutex) { - loading_map_mutex->lock(); - } + loading_map_mutex.lock(); LoadingMapKey key; key.path = p_path; @@ -322,15 +316,11 @@ void ResourceLoader::_remove_from_loading_map(const String &p_path) { loading_map.erase(key); - if (loading_map_mutex) { - loading_map_mutex->unlock(); - } + loading_map_mutex.unlock(); } void ResourceLoader::_remove_from_loading_map_and_thread(const String &p_path, Thread::ID p_thread) { - if (loading_map_mutex) { - loading_map_mutex->lock(); - } + loading_map_mutex.lock(); LoadingMapKey key; key.path = p_path; @@ -338,9 +328,7 @@ void ResourceLoader::_remove_from_loading_map_and_thread(const String &p_path, T loading_map.erase(key); - if (loading_map_mutex) { - loading_map_mutex->unlock(); - } + loading_map_mutex.unlock(); } RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) { @@ -994,15 +982,9 @@ void ResourceLoader::remove_custom_loaders() { } } -Mutex *ResourceLoader::loading_map_mutex = NULL; +Mutex ResourceLoader::loading_map_mutex; HashMap ResourceLoader::loading_map; -void ResourceLoader::initialize() { -#ifndef NO_THREADS - loading_map_mutex = Mutex::create(); -#endif -} - void ResourceLoader::finalize() { #ifndef NO_THREADS const LoadingMapKey *K = NULL; @@ -1010,8 +992,6 @@ void ResourceLoader::finalize() { ERR_PRINTS("Exited while resource is being loaded: " + K->path); } loading_map.clear(); - memdelete(loading_map_mutex); - loading_map_mutex = NULL; #endif } diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index a071dfba561..9b4e824df88 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -120,7 +120,7 @@ class ResourceLoader { static ResourceLoadedCallback _loaded_callback; static Ref _find_custom_resource_format_loader(String path); - static Mutex *loading_map_mutex; + static Mutex loading_map_mutex; //used to track paths being loaded in a thread, avoids cyclic recursion struct LoadingMapKey { @@ -197,7 +197,6 @@ public: static void add_custom_loaders(); static void remove_custom_loaders(); - static void initialize(); static void finalize(); }; diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index 6a87a095a3f..d0582a4ee28 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -30,31 +30,19 @@ #include "mutex.h" -#include "core/error_macros.h" - -#include - -Mutex *(*Mutex::create_func)(bool) = 0; - -Mutex *Mutex::create(bool p_recursive) { - - ERR_FAIL_COND_V(!create_func, 0); - - return create_func(p_recursive); -} - -Mutex::~Mutex() { -} - -Mutex *_global_mutex = NULL; +static Mutex _global_mutex; void _global_lock() { - - if (_global_mutex) - _global_mutex->lock(); + _global_mutex.lock(); } + void _global_unlock() { - - if (_global_mutex) - _global_mutex->unlock(); + _global_mutex.unlock(); } + +#ifndef NO_THREADS + +template class MutexImpl; +template class MutexImpl; + +#endif diff --git a/core/os/mutex.h b/core/os/mutex.h index 53c171bd156..f282bb30cea 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -32,42 +32,89 @@ #define MUTEX_H #include "core/error_list.h" +#include "core/typedefs.h" -/** - * @class Mutex - * @author Juan Linietsky - * Portable Mutex (thread-safe locking) implementation. - * Mutexes are always recursive ( they don't self-lock in a single thread ). - * Mutexes can be used with a Lockp object like this, to avoid having to worry about unlocking: - * Lockp( mutex ); - */ +#if !defined(NO_THREADS) -class Mutex { -protected: - static Mutex *(*create_func)(bool); +#include + +template +class MutexImpl { + mutable StdMutexT mutex; + friend class MutexLock; public: - virtual void lock() = 0; ///< Lock the mutex, block if locked by someone else - virtual void unlock() = 0; ///< Unlock the mutex, let other threads continue - virtual Error try_lock() = 0; ///< Attempt to lock the mutex, OK on success, ERROR means it can't lock. + _ALWAYS_INLINE_ void lock() const { + mutex.lock(); + } - static Mutex *create(bool p_recursive = true); ///< Create a mutex + _ALWAYS_INLINE_ void unlock() const { + mutex.unlock(); + } - virtual ~Mutex(); + _ALWAYS_INLINE_ Error try_lock() const { + return mutex.try_lock() ? OK : ERR_BUSY; + } +}; + +// This is written this way instead of being a template to overcome a limitation of C++ pre-17 +// that would require MutexLock to be used like this: MutexLock lock; +class MutexLock { + union { + std::recursive_mutex *recursive_mutex; + std::mutex *mutex; + }; + bool recursive; + +public: + _ALWAYS_INLINE_ explicit MutexLock(const MutexImpl &p_mutex) : + recursive_mutex(&p_mutex.mutex), + recursive(true) { + recursive_mutex->lock(); + } + _ALWAYS_INLINE_ explicit MutexLock(const MutexImpl &p_mutex) : + mutex(&p_mutex.mutex), + recursive(false) { + mutex->lock(); + } + + _ALWAYS_INLINE_ ~MutexLock() { + if (recursive) { + recursive_mutex->unlock(); + } else { + mutex->unlock(); + } + } +}; + +using Mutex = MutexImpl; // Recursive, for general use +using BinaryMutex = MutexImpl; // Non-recursive, handle with care + +extern template class MutexImpl; +extern template class MutexImpl; + +#else + +class FakeMutex { + FakeMutex() {} +}; + +template +class MutexImpl { +public: + _ALWAYS_INLINE_ void lock() const {} + _ALWAYS_INLINE_ void unlock() const {} + _ALWAYS_INLINE_ Error try_lock() const { return OK; } }; class MutexLock { - - Mutex *mutex; - public: - MutexLock(Mutex *p_mutex) { - mutex = p_mutex; - if (mutex) mutex->lock(); - } - ~MutexLock() { - if (mutex) mutex->unlock(); - } + explicit MutexLock(const MutexImpl &p_mutex) {} }; -#endif +using Mutex = MutexImpl; +using BinaryMutex = MutexImpl; // Non-recursive, handle with care + +#endif // !NO_THREADS + +#endif // MUTEX_H diff --git a/core/os/os.h b/core/os/os.h index 227d1e73c16..29582991953 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -41,8 +41,6 @@ #include -class Mutex; - class OS { static OS *singleton; diff --git a/core/os/thread_dummy.cpp b/core/os/thread_dummy.cpp index e9298ccb37c..6f133370bee 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; }; -Mutex *MutexDummy::create(bool p_recursive) { - return memnew(MutexDummy); -}; - -void MutexDummy::make_default() { - Mutex::create_func = &MutexDummy::create; -}; - Semaphore *SemaphoreDummy::create() { return memnew(SemaphoreDummy); }; diff --git a/core/os/thread_dummy.h b/core/os/thread_dummy.h index 594b3c05454..2e36140760c 100644 --- a/core/os/thread_dummy.h +++ b/core/os/thread_dummy.h @@ -45,18 +45,6 @@ public: static void make_default(); }; -class MutexDummy : public Mutex { - - static Mutex *create(bool p_recursive); - -public: - virtual void lock(){}; - virtual void unlock(){}; - virtual Error try_lock() { return OK; }; - - static void make_default(); -}; - class SemaphoreDummy : public Semaphore { static Semaphore *create(); diff --git a/core/os/thread_safe.cpp b/core/os/thread_safe.cpp deleted file mode 100644 index 8c15636229e..00000000000 --- a/core/os/thread_safe.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/*************************************************************************/ -/* thread_safe.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "thread_safe.h" - -#include "core/error_macros.h" -#include "core/os/memory.h" - -ThreadSafe::ThreadSafe() { - - mutex = Mutex::create(); - if (!mutex) { - - WARN_PRINT("THREAD_SAFE defined, but no default mutex type"); - } -} - -ThreadSafe::~ThreadSafe() { - - if (mutex) - memdelete(mutex); -} diff --git a/core/os/thread_safe.h b/core/os/thread_safe.h index b799e219014..ae2110cb481 100644 --- a/core/os/thread_safe.h +++ b/core/os/thread_safe.h @@ -33,50 +33,9 @@ #include "core/os/mutex.h" -class ThreadSafe { - - Mutex *mutex; - -public: - inline void lock() const { - if (mutex) mutex->lock(); - } - inline void unlock() const { - if (mutex) mutex->unlock(); - } - - ThreadSafe(); - ~ThreadSafe(); -}; - -class ThreadSafeMethod { - - const ThreadSafe *_ts; - -public: - ThreadSafeMethod(const ThreadSafe *p_ts) { - - _ts = p_ts; - _ts->lock(); - } - - ~ThreadSafeMethod() { _ts->unlock(); } -}; - -#ifndef NO_THREADS - -#define _THREAD_SAFE_CLASS_ ThreadSafe __thread__safe__; -#define _THREAD_SAFE_METHOD_ ThreadSafeMethod __thread_safe_method__(&__thread__safe__); -#define _THREAD_SAFE_LOCK_ __thread__safe__.lock(); -#define _THREAD_SAFE_UNLOCK_ __thread__safe__.unlock(); - -#else - -#define _THREAD_SAFE_CLASS_ -#define _THREAD_SAFE_METHOD_ -#define _THREAD_SAFE_LOCK_ -#define _THREAD_SAFE_UNLOCK_ - -#endif +#define _THREAD_SAFE_CLASS_ mutable Mutex _thread_safe_; +#define _THREAD_SAFE_METHOD_ MutexLock _thread_safe_method_(_thread_safe_); +#define _THREAD_SAFE_LOCK_ _thread_safe_.lock(); +#define _THREAD_SAFE_UNLOCK_ _thread_safe_.unlock(); #endif diff --git a/core/pool_vector.cpp b/core/pool_vector.cpp index 263d9e9f3b0..98c5cfe2d66 100644 --- a/core/pool_vector.cpp +++ b/core/pool_vector.cpp @@ -30,7 +30,7 @@ #include "pool_vector.h" -Mutex *pool_vector_lock = NULL; +Mutex pool_vector_lock; PoolAllocator *MemoryPool::memory_pool = NULL; uint8_t *MemoryPool::pool_memory = NULL; @@ -40,7 +40,7 @@ MemoryPool::Alloc *MemoryPool::allocs = NULL; MemoryPool::Alloc *MemoryPool::free_list = NULL; uint32_t MemoryPool::alloc_count = 0; uint32_t MemoryPool::allocs_used = 0; -Mutex *MemoryPool::alloc_mutex = NULL; +Mutex MemoryPool::alloc_mutex; size_t MemoryPool::total_memory = 0; size_t MemoryPool::max_memory = 0; @@ -57,14 +57,11 @@ void MemoryPool::setup(uint32_t p_max_allocs) { } free_list = &allocs[0]; - - alloc_mutex = Mutex::create(); } void MemoryPool::cleanup() { memdelete_arr(allocs); - memdelete(alloc_mutex); ERR_FAIL_COND_MSG(allocs_used > 0, "There are still MemoryPool allocs in use at exit!"); } diff --git a/core/pool_vector.h b/core/pool_vector.h index f142fafdc7e..8798390d935 100644 --- a/core/pool_vector.h +++ b/core/pool_vector.h @@ -69,7 +69,7 @@ struct MemoryPool { static Alloc *free_list; static uint32_t alloc_count; static uint32_t allocs_used; - static Mutex *alloc_mutex; + static Mutex alloc_mutex; static size_t total_memory; static size_t max_memory; @@ -95,9 +95,9 @@ class PoolVector { //must allocate something - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); if (MemoryPool::allocs_used == MemoryPool::alloc_count) { - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); ERR_FAIL_MSG("All memory pool allocations are in use, can't COW."); } @@ -122,7 +122,7 @@ class PoolVector { } #endif - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); if (MemoryPool::memory_pool) { @@ -148,9 +148,9 @@ class PoolVector { //this should never happen but.. #ifdef DEBUG_ENABLED - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); MemoryPool::total_memory -= old_alloc->size; - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); #endif { @@ -174,11 +174,11 @@ class PoolVector { old_alloc->mem = NULL; old_alloc->size = 0; - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); old_alloc->free_list = MemoryPool::free_list; MemoryPool::free_list = old_alloc; MemoryPool::allocs_used--; - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); } } } @@ -227,9 +227,9 @@ class PoolVector { } #ifdef DEBUG_ENABLED - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); MemoryPool::total_memory -= alloc->size; - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); #endif if (MemoryPool::memory_pool) { @@ -242,11 +242,11 @@ class PoolVector { alloc->mem = NULL; alloc->size = 0; - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); alloc->free_list = MemoryPool::free_list; MemoryPool::free_list = alloc; MemoryPool::allocs_used--; - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); } alloc = NULL; @@ -523,9 +523,9 @@ Error PoolVector::resize(int p_size) { return OK; //nothing to do here //must allocate something - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); if (MemoryPool::allocs_used == MemoryPool::alloc_count) { - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "All memory pool allocations are in use."); } @@ -539,7 +539,7 @@ Error PoolVector::resize(int p_size) { alloc->size = 0; alloc->refcount.init(); alloc->pool_id = POOL_ALLOCATOR_INVALID_ID; - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); } else { @@ -559,13 +559,13 @@ Error PoolVector::resize(int p_size) { _copy_on_write(); // make it unique #ifdef DEBUG_ENABLED - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); MemoryPool::total_memory -= alloc->size; MemoryPool::total_memory += new_size; if (MemoryPool::total_memory > MemoryPool::max_memory) { MemoryPool::max_memory = MemoryPool::total_memory; } - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); #endif int cur_elements = alloc->size / sizeof(T); @@ -615,11 +615,11 @@ Error PoolVector::resize(int p_size) { alloc->mem = NULL; alloc->size = 0; - MemoryPool::alloc_mutex->lock(); + MemoryPool::alloc_mutex.lock(); alloc->free_list = MemoryPool::free_list; MemoryPool::free_list = alloc; MemoryPool::allocs_used--; - MemoryPool::alloc_mutex->unlock(); + MemoryPool::alloc_mutex.unlock(); } else { alloc->mem = memrealloc(alloc->mem, new_size); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index b973ac85d1d..3fa881e36c4 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -90,7 +90,7 @@ static IP *ip = NULL; static _Geometry *_geometry = NULL; -extern Mutex *_global_mutex; +extern Mutex _global_mutex; extern void register_global_constants(); extern void unregister_global_constants(); @@ -101,10 +101,7 @@ void register_core_types() { MemoryPool::setup(); - _global_mutex = Mutex::create(); - StringName::setup(); - ResourceLoader::initialize(); register_global_constants(); register_variant_methods(); @@ -316,10 +313,5 @@ void unregister_core_types() { CoreStringNames::free(); StringName::cleanup(); - if (_global_mutex) { - memdelete(_global_mutex); - _global_mutex = NULL; //still needed at a few places - }; - MemoryPool::cleanup(); } diff --git a/core/string_name.cpp b/core/string_name.cpp index ac2d467c9fe..d2e5ca26bef 100644 --- a/core/string_name.cpp +++ b/core/string_name.cpp @@ -47,12 +47,10 @@ StringName _scs_create(const char *p_chr) { } bool StringName::configured = false; -Mutex *StringName::lock = NULL; +Mutex StringName::lock; void StringName::setup() { - lock = Mutex::create(); - ERR_FAIL_COND(configured); for (int i = 0; i < STRING_TABLE_LEN; i++) { @@ -63,7 +61,7 @@ void StringName::setup() { void StringName::cleanup() { - lock->lock(); + lock.lock(); int lost_strings = 0; for (int i = 0; i < STRING_TABLE_LEN; i++) { @@ -87,9 +85,7 @@ void StringName::cleanup() { if (lost_strings) { print_verbose("StringName: " + itos(lost_strings) + " unclaimed string names at exit."); } - lock->unlock(); - - memdelete(lock); + lock.unlock(); } void StringName::unref() { @@ -98,7 +94,7 @@ void StringName::unref() { if (_data && _data->refcount.unref()) { - lock->lock(); + lock.lock(); if (_data->prev) { _data->prev->next = _data->next; @@ -113,7 +109,7 @@ void StringName::unref() { _data->next->prev = _data->prev; } memdelete(_data); - lock->unlock(); + lock.unlock(); } _data = NULL; @@ -184,7 +180,7 @@ StringName::StringName(const char *p_name) { if (!p_name || p_name[0] == 0) return; //empty, ignore - lock->lock(); + lock.lock(); uint32_t hash = String::hash(p_name); @@ -203,7 +199,7 @@ StringName::StringName(const char *p_name) { if (_data) { if (_data->refcount.ref()) { // exists - lock->unlock(); + lock.unlock(); return; } } @@ -220,7 +216,7 @@ StringName::StringName(const char *p_name) { _table[idx]->prev = _data; _table[idx] = _data; - lock->unlock(); + lock.unlock(); } StringName::StringName(const StaticCString &p_static_string) { @@ -231,7 +227,7 @@ StringName::StringName(const StaticCString &p_static_string) { ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]); - lock->lock(); + lock.lock(); uint32_t hash = String::hash(p_static_string.ptr); @@ -250,7 +246,7 @@ StringName::StringName(const StaticCString &p_static_string) { if (_data) { if (_data->refcount.ref()) { // exists - lock->unlock(); + lock.unlock(); return; } } @@ -267,7 +263,7 @@ StringName::StringName(const StaticCString &p_static_string) { _table[idx]->prev = _data; _table[idx] = _data; - lock->unlock(); + lock.unlock(); } StringName::StringName(const String &p_name) { @@ -279,7 +275,7 @@ StringName::StringName(const String &p_name) { if (p_name == String()) return; - lock->lock(); + lock.lock(); uint32_t hash = p_name.hash(); @@ -297,7 +293,7 @@ StringName::StringName(const String &p_name) { if (_data) { if (_data->refcount.ref()) { // exists - lock->unlock(); + lock.unlock(); return; } } @@ -314,7 +310,7 @@ StringName::StringName(const String &p_name) { _table[idx]->prev = _data; _table[idx] = _data; - lock->unlock(); + lock.unlock(); } StringName StringName::search(const char *p_name) { @@ -325,7 +321,7 @@ StringName StringName::search(const char *p_name) { if (!p_name[0]) return StringName(); - lock->lock(); + lock.lock(); uint32_t hash = String::hash(p_name); @@ -342,12 +338,12 @@ StringName StringName::search(const char *p_name) { } if (_data && _data->refcount.ref()) { - lock->unlock(); + lock.unlock(); return StringName(_data); } - lock->unlock(); + lock.unlock(); return StringName(); //does not exist } @@ -359,7 +355,7 @@ StringName StringName::search(const CharType *p_name) { if (!p_name[0]) return StringName(); - lock->lock(); + lock.lock(); uint32_t hash = String::hash(p_name); @@ -376,18 +372,18 @@ StringName StringName::search(const CharType *p_name) { } if (_data && _data->refcount.ref()) { - lock->unlock(); + lock.unlock(); return StringName(_data); } - lock->unlock(); + lock.unlock(); return StringName(); //does not exist } StringName StringName::search(const String &p_name) { ERR_FAIL_COND_V(p_name == "", StringName()); - lock->lock(); + lock.lock(); uint32_t hash = p_name.hash(); @@ -404,11 +400,11 @@ StringName StringName::search(const String &p_name) { } if (_data && _data->refcount.ref()) { - lock->unlock(); + lock.unlock(); return StringName(_data); } - lock->unlock(); + lock.unlock(); return StringName(); //does not exist } diff --git a/core/string_name.h b/core/string_name.h index 73ced5c456e..bb77be9450a 100644 --- a/core/string_name.h +++ b/core/string_name.h @@ -82,7 +82,7 @@ class StringName { friend void register_core_types(); friend void unregister_core_types(); - static Mutex *lock; + static Mutex lock; static void setup(); static void cleanup(); static bool configured; diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index 83ed02e1a30..45286d4b669 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -169,7 +169,6 @@ Error AudioDriverALSA::init() { Error err = init_device(); if (err == OK) { - mutex = Mutex::create(); thread = Thread::create(AudioDriverALSA::thread_func, this); } @@ -314,16 +313,16 @@ void AudioDriverALSA::set_device(String device) { void AudioDriverALSA::lock() { - if (!thread || !mutex) + if (!thread) return; - mutex->lock(); + mutex.lock(); } void AudioDriverALSA::unlock() { - if (!thread || !mutex) + if (!thread) return; - mutex->unlock(); + mutex.unlock(); } void AudioDriverALSA::finish_device() { @@ -342,11 +341,6 @@ void AudioDriverALSA::finish() { memdelete(thread); thread = NULL; - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } finish_device(); @@ -354,7 +348,6 @@ void AudioDriverALSA::finish() { AudioDriverALSA::AudioDriverALSA() : thread(NULL), - mutex(NULL), pcm_handle(NULL), device_name("Default"), new_device("Default") { diff --git a/drivers/alsa/audio_driver_alsa.h b/drivers/alsa/audio_driver_alsa.h index 0e8ff2228b5..7c3752fe6bb 100644 --- a/drivers/alsa/audio_driver_alsa.h +++ b/drivers/alsa/audio_driver_alsa.h @@ -42,7 +42,7 @@ class AudioDriverALSA : public AudioDriver { Thread *thread; - Mutex *mutex; + Mutex mutex; snd_pcm_t *pcm_handle; diff --git a/drivers/alsamidi/midi_driver_alsamidi.cpp b/drivers/alsamidi/midi_driver_alsamidi.cpp index 95a44664941..21d38695e6c 100644 --- a/drivers/alsamidi/midi_driver_alsamidi.cpp +++ b/drivers/alsamidi/midi_driver_alsamidi.cpp @@ -148,7 +148,6 @@ Error MIDIDriverALSAMidi::open() { } snd_device_name_free_hint(hints); - mutex = Mutex::create(); exit_thread = false; thread = Thread::create(MIDIDriverALSAMidi::thread_func, this); @@ -165,11 +164,6 @@ void MIDIDriverALSAMidi::close() { thread = NULL; } - if (mutex) { - memdelete(mutex); - mutex = NULL; - } - for (int i = 0; i < connected_inputs.size(); i++) { snd_rawmidi_t *midi_in = connected_inputs[i]; snd_rawmidi_close(midi_in); @@ -179,14 +173,12 @@ void MIDIDriverALSAMidi::close() { void MIDIDriverALSAMidi::lock() const { - if (mutex) - mutex->lock(); + mutex.lock(); } void MIDIDriverALSAMidi::unlock() const { - if (mutex) - mutex->unlock(); + mutex.unlock(); } PoolStringArray MIDIDriverALSAMidi::get_connected_inputs() { @@ -210,7 +202,6 @@ PoolStringArray MIDIDriverALSAMidi::get_connected_inputs() { MIDIDriverALSAMidi::MIDIDriverALSAMidi() { - mutex = NULL; thread = NULL; exit_thread = false; diff --git a/drivers/alsamidi/midi_driver_alsamidi.h b/drivers/alsamidi/midi_driver_alsamidi.h index ca2d8c040b9..138a89ed26e 100644 --- a/drivers/alsamidi/midi_driver_alsamidi.h +++ b/drivers/alsamidi/midi_driver_alsamidi.h @@ -44,7 +44,7 @@ class MIDIDriverALSAMidi : public MIDIDriver { Thread *thread; - Mutex *mutex; + Mutex mutex; Vector connected_inputs; diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index fd8d637782a..eab4ea99e61 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -69,8 +69,6 @@ OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID #endif Error AudioDriverCoreAudio::init() { - mutex = Mutex::create(); - AudioComponentDescription desc; zeromem(&desc, sizeof(desc)); desc.componentType = kAudioUnitType_Output; @@ -280,19 +278,15 @@ AudioDriver::SpeakerMode AudioDriverCoreAudio::get_speaker_mode() const { }; void AudioDriverCoreAudio::lock() { - if (mutex) - mutex->lock(); + mutex.lock(); }; void AudioDriverCoreAudio::unlock() { - if (mutex) - mutex->unlock(); + mutex.unlock(); }; bool AudioDriverCoreAudio::try_lock() { - if (mutex) - return mutex->try_lock() == OK; - return true; + return mutex.try_lock() == OK; } void AudioDriverCoreAudio::finish() { @@ -344,11 +338,6 @@ void AudioDriverCoreAudio::finish() { audio_unit = NULL; unlock(); } - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } Error AudioDriverCoreAudio::capture_init() { @@ -691,7 +680,6 @@ AudioDriverCoreAudio::AudioDriverCoreAudio() : audio_unit(NULL), input_unit(NULL), active(false), - mutex(NULL), device_name("Default"), capture_device_name("Default"), mix_rate(0), diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index 0719e923072..40f93950247 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -46,7 +46,7 @@ class AudioDriverCoreAudio : public AudioDriver { AudioComponentInstance input_unit; bool active; - Mutex *mutex; + Mutex mutex; String device_name; String capture_device_name; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 5de874301e6..5c57f9ee975 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -294,7 +294,6 @@ Error AudioDriverPulseAudio::init() { Error err = init_device(); if (err == OK) { - mutex = Mutex::create(); thread = Thread::create(AudioDriverPulseAudio::thread_func, this); } @@ -600,16 +599,16 @@ void AudioDriverPulseAudio::set_device(String device) { void AudioDriverPulseAudio::lock() { - if (!thread || !mutex) + if (!thread) return; - mutex->lock(); + mutex.lock(); } void AudioDriverPulseAudio::unlock() { - if (!thread || !mutex) + if (!thread) return; - mutex->unlock(); + mutex.unlock(); } void AudioDriverPulseAudio::finish_device() { @@ -643,10 +642,6 @@ void AudioDriverPulseAudio::finish() { } memdelete(thread); - if (mutex) { - memdelete(mutex); - mutex = NULL; - } thread = NULL; } @@ -803,7 +798,6 @@ String AudioDriverPulseAudio::capture_get_device() { AudioDriverPulseAudio::AudioDriverPulseAudio() : thread(NULL), - mutex(NULL), pa_ml(NULL), pa_ctx(NULL), pa_str(NULL), diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index cf5090730b5..27a2b1ede8e 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -42,7 +42,7 @@ class AudioDriverPulseAudio : public AudioDriver { Thread *thread; - Mutex *mutex; + Mutex mutex; pa_mainloop *pa_ml; pa_context *pa_ctx; diff --git a/drivers/unix/mutex_posix.cpp b/drivers/unix/mutex_posix.cpp deleted file mode 100644 index c731ecf683f..00000000000 --- a/drivers/unix/mutex_posix.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/*************************************************************************/ -/* mutex_posix.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "mutex_posix.h" - -#include "core/os/memory.h" - -#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) - -void MutexPosix::lock() { - - pthread_mutex_lock(&mutex); -} -void MutexPosix::unlock() { - - pthread_mutex_unlock(&mutex); -} -Error MutexPosix::try_lock() { - - return (pthread_mutex_trylock(&mutex) == 0) ? OK : ERR_BUSY; -} - -Mutex *MutexPosix::create_func_posix(bool p_recursive) { - - return memnew(MutexPosix(p_recursive)); -} - -void MutexPosix::make_default() { - - create_func = create_func_posix; -} - -MutexPosix::MutexPosix(bool p_recursive) { - - pthread_mutexattr_init(&attr); - if (p_recursive) - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mutex, &attr); -} - -MutexPosix::~MutexPosix() { - - pthread_mutex_destroy(&mutex); -} - -#endif diff --git a/drivers/unix/mutex_posix.h b/drivers/unix/mutex_posix.h deleted file mode 100644 index c3ef6e1cf62..00000000000 --- a/drivers/unix/mutex_posix.h +++ /dev/null @@ -1,61 +0,0 @@ -/*************************************************************************/ -/* mutex_posix.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef MUTEX_POSIX_H -#define MUTEX_POSIX_H - -#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) - -#include "core/os/mutex.h" - -#include - -class MutexPosix : public Mutex { - - pthread_mutexattr_t attr; - pthread_mutex_t mutex; - - static Mutex *create_func_posix(bool p_recursive); - -public: - virtual void lock(); - virtual void unlock(); - virtual Error try_lock(); - - static void make_default(); - - MutexPosix(bool p_recursive); - - ~MutexPosix(); -}; - -#endif - -#endif diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 9fd8c2d855a..b3102dc9681 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -36,7 +36,6 @@ #include "core/project_settings.h" #include "drivers/unix/dir_access_unix.h" #include "drivers/unix/file_access_unix.h" -#include "drivers/unix/mutex_posix.h" #include "drivers/unix/net_socket_posix.h" #include "drivers/unix/semaphore_posix.h" #include "drivers/unix/thread_posix.h" @@ -63,6 +62,7 @@ #include #include #include +#include #include /// Clock Setup function (used by get_ticks_usec) @@ -122,13 +122,11 @@ void OS_Unix::initialize_core() { #ifdef NO_THREADS ThreadDummy::make_default(); SemaphoreDummy::make_default(); - MutexDummy::make_default(); #else ThreadPosix::make_default(); #if !defined(OSX_ENABLED) && !defined(IPHONE_ENABLED) SemaphorePosix::make_default(); #endif - MutexPosix::make_default(); #endif FileAccess::make_default(FileAccess::ACCESS_RESOURCES); FileAccess::make_default(FileAccess::ACCESS_USERDATA); @@ -307,13 +305,9 @@ Error OS_Unix::execute(const String &p_path, const List &p_arguments, bo while (fgets(buf, 65535, f)) { - if (p_pipe_mutex) { - p_pipe_mutex->lock(); - } + p_pipe_mutex->lock(); (*r_pipe) += String::utf8(buf); - if (p_pipe_mutex) { - p_pipe_mutex->unlock(); - } + p_pipe_mutex->unlock(); } int rv = pclose(f); if (r_exitcode) diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 0fa5565dbb5..a0d1458aec2 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -406,7 +406,6 @@ Error AudioDriverWASAPI::init() { exit_thread = false; thread_exited = false; - mutex = Mutex::create(true); thread = Thread::create(thread_func, this); return OK; @@ -782,14 +781,12 @@ void AudioDriverWASAPI::start() { void AudioDriverWASAPI::lock() { - if (mutex) - mutex->lock(); + mutex.lock(); } void AudioDriverWASAPI::unlock() { - if (mutex) - mutex->unlock(); + mutex.unlock(); } void AudioDriverWASAPI::finish() { @@ -804,11 +801,6 @@ void AudioDriverWASAPI::finish() { finish_capture_device(); finish_render_device(); - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } Error AudioDriverWASAPI::capture_start() { @@ -863,7 +855,6 @@ String AudioDriverWASAPI::capture_get_device() { AudioDriverWASAPI::AudioDriverWASAPI() { - mutex = NULL; thread = NULL; samples_in.clear(); diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index 59387c7474d..a49257bb52e 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -75,7 +75,7 @@ class AudioDriverWASAPI : public AudioDriver { AudioDeviceWASAPI audio_input; AudioDeviceWASAPI audio_output; - Mutex *mutex; + Mutex mutex; Thread *thread; Vector samples_in; diff --git a/drivers/windows/mutex_windows.cpp b/drivers/windows/mutex_windows.cpp deleted file mode 100644 index 847f625f952..00000000000 --- a/drivers/windows/mutex_windows.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/*************************************************************************/ -/* mutex_windows.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "mutex_windows.h" - -#include "core/os/memory.h" - -#ifdef WINDOWS_ENABLED - -void MutexWindows::lock() { - -#ifdef WINDOWS_USE_MUTEX - WaitForSingleObject(mutex, INFINITE); -#else - EnterCriticalSection(&mutex); -#endif -} - -void MutexWindows::unlock() { - -#ifdef WINDOWS_USE_MUTEX - ReleaseMutex(mutex); -#else - LeaveCriticalSection(&mutex); -#endif -} - -Error MutexWindows::try_lock() { - -#ifdef WINDOWS_USE_MUTEX - return (WaitForSingleObject(mutex, 0) == WAIT_TIMEOUT) ? ERR_BUSY : OK; -#else - - if (TryEnterCriticalSection(&mutex)) - return OK; - else - return ERR_BUSY; -#endif -} - -Mutex *MutexWindows::create_func_windows(bool p_recursive) { - - return memnew(MutexWindows); -} - -void MutexWindows::make_default() { - - create_func = create_func_windows; -} - -MutexWindows::MutexWindows() { - -#ifdef WINDOWS_USE_MUTEX - mutex = CreateMutex(NULL, FALSE, NULL); -#else -#ifdef UWP_ENABLED - InitializeCriticalSectionEx(&mutex, 0, 0); -#else - InitializeCriticalSection(&mutex); -#endif -#endif -} - -MutexWindows::~MutexWindows() { - -#ifdef WINDOWS_USE_MUTEX - CloseHandle(mutex); -#else - - DeleteCriticalSection(&mutex); -#endif -} - -#endif diff --git a/drivers/windows/mutex_windows.h b/drivers/windows/mutex_windows.h deleted file mode 100644 index 0fb994773ac..00000000000 --- a/drivers/windows/mutex_windows.h +++ /dev/null @@ -1,63 +0,0 @@ -/*************************************************************************/ -/* mutex_windows.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef MUTEX_WINDOWS_H -#define MUTEX_WINDOWS_H - -#ifdef WINDOWS_ENABLED - -#include "core/os/mutex.h" - -#include - -class MutexWindows : public Mutex { - -#ifdef WINDOWS_USE_MUTEX - HANDLE mutex; -#else - CRITICAL_SECTION mutex; -#endif - - static Mutex *create_func_windows(bool p_recursive); - -public: - virtual void lock(); - virtual void unlock(); - virtual Error try_lock(); - - static void make_default(); - - MutexWindows(); - ~MutexWindows(); -}; - -#endif - -#endif diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index 871b13de0e1..6888f0a4d67 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -79,7 +79,6 @@ Error AudioDriverXAudio2::init() { hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, &voice_callback); ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 source voice. Error code: " + itos(hr) + "."); - mutex = Mutex::create(); thread = Thread::create(AudioDriverXAudio2::thread_func, this); return OK; @@ -158,15 +157,15 @@ float AudioDriverXAudio2::get_latency() { void AudioDriverXAudio2::lock() { - if (!thread || !mutex) + if (!thread) return; - mutex->lock(); + mutex.lock(); } void AudioDriverXAudio2::unlock() { - if (!thread || !mutex) + if (!thread) return; - mutex->unlock(); + mutex.unlock(); } void AudioDriverXAudio2::finish() { @@ -194,14 +193,11 @@ void AudioDriverXAudio2::finish() { mastering_voice->DestroyVoice(); memdelete(thread); - if (mutex) - memdelete(mutex); thread = NULL; } AudioDriverXAudio2::AudioDriverXAudio2() : thread(NULL), - mutex(NULL), current_buffer(0) { wave_format = { 0 }; for (int i = 0; i < AUDIO_BUFFERS; i++) { diff --git a/drivers/xaudio2/audio_driver_xaudio2.h b/drivers/xaudio2/audio_driver_xaudio2.h index e9002ca12ce..6319d462b9a 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.h +++ b/drivers/xaudio2/audio_driver_xaudio2.h @@ -65,7 +65,7 @@ class AudioDriverXAudio2 : public AudioDriver { }; Thread *thread; - Mutex *mutex; + Mutex mutex; int32_t *samples_in; int16_t *samples_out[AUDIO_BUFFERS]; diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index d72d7d78b8e..75b4c343061 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -5736,7 +5736,7 @@ void EditorNode::_print_handler(void *p_this, const String &p_string, bool p_err static void _execute_thread(void *p_ud) { EditorNode::ExecuteThreadArgs *eta = (EditorNode::ExecuteThreadArgs *)p_ud; - Error err = OS::get_singleton()->execute(eta->path, eta->args, true, NULL, &eta->output, &eta->exitcode, true, eta->execute_output_mutex); + Error err = OS::get_singleton()->execute(eta->path, eta->args, true, NULL, &eta->output, &eta->exitcode, true, &eta->execute_output_mutex); print_verbose("Thread exit status: " + itos(eta->exitcode)); if (err != OK) { eta->exitcode = err; @@ -5756,7 +5756,6 @@ int EditorNode::execute_and_show_output(const String &p_title, const String &p_p ExecuteThreadArgs eta; eta.path = p_path; eta.args = p_arguments; - eta.execute_output_mutex = Mutex::create(); eta.exitcode = 255; eta.done = false; @@ -5767,20 +5766,19 @@ int EditorNode::execute_and_show_output(const String &p_title, const String &p_p ERR_FAIL_COND_V(!eta.execute_output_thread, 0); while (!eta.done) { - eta.execute_output_mutex->lock(); + eta.execute_output_mutex.lock(); if (prev_len != eta.output.length()) { String to_add = eta.output.substr(prev_len, eta.output.length()); prev_len = eta.output.length(); execute_outputs->add_text(to_add); Main::iteration(); } - eta.execute_output_mutex->unlock(); + eta.execute_output_mutex.unlock(); OS::get_singleton()->delay_usec(1000); } Thread::wait_to_finish(eta.execute_output_thread); memdelete(eta.execute_output_thread); - memdelete(eta.execute_output_mutex); execute_outputs->add_text("\nExit Code: " + itos(eta.exitcode)); if (p_close_on_errors && eta.exitcode != 0) { diff --git a/editor/editor_node.h b/editor/editor_node.h index 6356b217297..f175573451d 100644 --- a/editor/editor_node.h +++ b/editor/editor_node.h @@ -107,7 +107,7 @@ public: List args; String output; Thread *execute_output_thread; - Mutex *execute_output_mutex; + Mutex execute_output_mutex; int exitcode; volatile bool done; }; diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index 71c340a93aa..2e6508146e8 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -109,7 +109,7 @@ void EditorResourcePreview::_thread_func(void *ud) { void EditorResourcePreview::_preview_ready(const String &p_str, const Ref &p_texture, const Ref &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud) { - preview_mutex->lock(); + preview_mutex.lock(); String path = p_str; uint32_t hash = 0; @@ -131,7 +131,7 @@ void EditorResourcePreview::_preview_ready(const String &p_str, const Refunlock(); + preview_mutex.unlock(); MessageQueue::get_singleton()->push_call(id, p_func, path, p_texture, p_small_texture, p_ud); } @@ -221,7 +221,7 @@ void EditorResourcePreview::_thread() { while (!exit) { preview_sem->wait(); - preview_mutex->lock(); + preview_mutex.lock(); if (queue.size()) { @@ -237,10 +237,10 @@ void EditorResourcePreview::_thread() { _preview_ready(path, cache[item.path].preview, cache[item.path].small_preview, item.id, item.function, item.userdata); - preview_mutex->unlock(); + preview_mutex.unlock(); } else { - preview_mutex->unlock(); + preview_mutex.unlock(); Ref texture; Ref small_texture; @@ -347,7 +347,7 @@ void EditorResourcePreview::_thread() { } } else { - preview_mutex->unlock(); + preview_mutex.unlock(); } } exited = true; @@ -358,7 +358,7 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref &p ERR_FAIL_NULL(p_receiver); ERR_FAIL_COND(!p_res.is_valid()); - preview_mutex->lock(); + preview_mutex.lock(); String path_id = "ID:" + itos(p_res->get_instance_id()); @@ -366,7 +366,7 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref &p cache[path_id].order = order++; p_receiver->call(p_receiver_func, path_id, cache[path_id].preview, cache[path_id].small_preview, p_userdata); - preview_mutex->unlock(); + preview_mutex.unlock(); return; } @@ -380,18 +380,18 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref &p item.userdata = p_userdata; queue.push_back(item); - preview_mutex->unlock(); + preview_mutex.unlock(); preview_sem->post(); } void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) { ERR_FAIL_NULL(p_receiver); - preview_mutex->lock(); + preview_mutex.lock(); if (cache.has(p_path)) { cache[p_path].order = order++; p_receiver->call(p_receiver_func, p_path, cache[p_path].preview, cache[p_path].small_preview, p_userdata); - preview_mutex->unlock(); + preview_mutex.unlock(); return; } @@ -402,7 +402,7 @@ void EditorResourcePreview::queue_resource_preview(const String &p_path, Object item.userdata = p_userdata; queue.push_back(item); - preview_mutex->unlock(); + preview_mutex.unlock(); preview_sem->post(); } @@ -436,7 +436,7 @@ void EditorResourcePreview::_bind_methods() { void EditorResourcePreview::check_for_invalidation(const String &p_path) { - preview_mutex->lock(); + preview_mutex.lock(); bool call_invalidated = false; if (cache.has(p_path)) { @@ -448,7 +448,7 @@ void EditorResourcePreview::check_for_invalidation(const String &p_path) { } } - preview_mutex->unlock(); + preview_mutex.unlock(); if (call_invalidated) { //do outside mutex call_deferred("emit_signal", "preview_invalidated", p_path); @@ -477,7 +477,6 @@ void EditorResourcePreview::stop() { EditorResourcePreview::EditorResourcePreview() { thread = NULL; singleton = this; - preview_mutex = Mutex::create(); preview_sem = Semaphore::create(); order = 0; exit = false; @@ -487,6 +486,5 @@ EditorResourcePreview::EditorResourcePreview() { EditorResourcePreview::~EditorResourcePreview() { stop(); - memdelete(preview_mutex); memdelete(preview_sem); } diff --git a/editor/editor_resource_preview.h b/editor/editor_resource_preview.h index 243f8b423ff..dae8ce30944 100644 --- a/editor/editor_resource_preview.h +++ b/editor/editor_resource_preview.h @@ -70,7 +70,7 @@ class EditorResourcePreview : public Node { List queue; - Mutex *preview_mutex; + Mutex preview_mutex; Semaphore *preview_sem; Thread *thread; volatile bool exit; diff --git a/editor/fileserver/editor_file_server.cpp b/editor/fileserver/editor_file_server.cpp index 7af3f71d58d..ef1f9e4e627 100644 --- a/editor/fileserver/editor_file_server.cpp +++ b/editor/fileserver/editor_file_server.cpp @@ -42,9 +42,9 @@ void EditorFileServer::_close_client(ClientData *cd) { cd->connection->disconnect_from_host(); - cd->efs->wait_mutex->lock(); + cd->efs->wait_mutex.lock(); cd->efs->to_wait.insert(cd->thread); - cd->efs->wait_mutex->unlock(); + cd->efs->wait_mutex.unlock(); while (cd->files.size()) { memdelete(cd->files.front()->get()); cd->files.erase(cd->files.front()); @@ -295,16 +295,16 @@ void EditorFileServer::_thread_start(void *s) { } } - self->wait_mutex->lock(); + self->wait_mutex.lock(); while (self->to_wait.size()) { Thread *w = self->to_wait.front()->get(); self->to_wait.erase(w); - self->wait_mutex->unlock(); + self->wait_mutex.unlock(); Thread::wait_to_finish(w); memdelete(w); - self->wait_mutex->lock(); + self->wait_mutex.lock(); } - self->wait_mutex->unlock(); + self->wait_mutex.unlock(); OS::get_singleton()->delay_usec(100000); } @@ -331,7 +331,6 @@ void EditorFileServer::stop() { EditorFileServer::EditorFileServer() { server.instance(); - wait_mutex = Mutex::create(); quit = false; active = false; cmd = CMD_NONE; @@ -346,5 +345,4 @@ EditorFileServer::~EditorFileServer() { quit = true; Thread::wait_to_finish(thread); memdelete(thread); - memdelete(wait_mutex); } diff --git a/editor/fileserver/editor_file_server.h b/editor/fileserver/editor_file_server.h index f0394753db1..efd86168ac9 100644 --- a/editor/fileserver/editor_file_server.h +++ b/editor/fileserver/editor_file_server.h @@ -62,7 +62,7 @@ class EditorFileServer : public Object { static void _close_client(ClientData *cd); static void _subthread_start(void *s); - Mutex *wait_mutex; + Mutex wait_mutex; Thread *thread; static void _thread_start(void *); bool quit; diff --git a/editor/import/resource_importer_texture.cpp b/editor/import/resource_importer_texture.cpp index e830b4e027d..5f01c0352d1 100644 --- a/editor/import/resource_importer_texture.cpp +++ b/editor/import/resource_importer_texture.cpp @@ -38,7 +38,7 @@ void ResourceImporterTexture::_texture_reimport_srgb(const Ref &p_tex) { - singleton->mutex->lock(); + singleton->mutex.lock(); StringName path = p_tex->get_path(); if (!singleton->make_flags.has(path)) { @@ -47,12 +47,12 @@ void ResourceImporterTexture::_texture_reimport_srgb(const Ref &p singleton->make_flags[path] |= MAKE_SRGB_FLAG; - singleton->mutex->unlock(); + singleton->mutex.unlock(); } void ResourceImporterTexture::_texture_reimport_3d(const Ref &p_tex) { - singleton->mutex->lock(); + singleton->mutex.lock(); StringName path = p_tex->get_path(); if (!singleton->make_flags.has(path)) { @@ -61,12 +61,12 @@ void ResourceImporterTexture::_texture_reimport_3d(const Ref &p_t singleton->make_flags[path] |= MAKE_3D_FLAG; - singleton->mutex->unlock(); + singleton->mutex.unlock(); } void ResourceImporterTexture::_texture_reimport_normal(const Ref &p_tex) { - singleton->mutex->lock(); + singleton->mutex.lock(); StringName path = p_tex->get_path(); if (!singleton->make_flags.has(path)) { @@ -75,7 +75,7 @@ void ResourceImporterTexture::_texture_reimport_normal(const Ref singleton->make_flags[path] |= MAKE_NORMAL_FLAG; - singleton->mutex->unlock(); + singleton->mutex.unlock(); } void ResourceImporterTexture::update_imports() { @@ -83,10 +83,10 @@ void ResourceImporterTexture::update_imports() { if (EditorFileSystem::get_singleton()->is_scanning() || EditorFileSystem::get_singleton()->is_importing()) { return; // do nothing for now } - mutex->lock(); + mutex.lock(); if (make_flags.empty()) { - mutex->unlock(); + mutex.unlock(); return; } @@ -128,7 +128,7 @@ void ResourceImporterTexture::update_imports() { make_flags.clear(); - mutex->unlock(); + mutex.unlock(); if (to_reimport.size()) { EditorFileSystem::get_singleton()->reimport_files(to_reimport); @@ -611,10 +611,4 @@ ResourceImporterTexture::ResourceImporterTexture() { StreamTexture::request_3d_callback = _texture_reimport_3d; StreamTexture::request_srgb_callback = _texture_reimport_srgb; StreamTexture::request_normal_callback = _texture_reimport_normal; - mutex = Mutex::create(); -} - -ResourceImporterTexture::~ResourceImporterTexture() { - - memdelete(mutex); } diff --git a/editor/import/resource_importer_texture.h b/editor/import/resource_importer_texture.h index be9a88ac600..9a2c3a37a1b 100644 --- a/editor/import/resource_importer_texture.h +++ b/editor/import/resource_importer_texture.h @@ -46,7 +46,7 @@ protected: MAKE_NORMAL_FLAG = 4 }; - Mutex *mutex; + Mutex mutex; Map make_flags; static void _texture_reimport_srgb(const Ref &p_tex); @@ -94,7 +94,6 @@ public: virtual String get_import_settings_string() const; ResourceImporterTexture(); - ~ResourceImporterTexture(); }; #endif // RESOURCEIMPORTTEXTURE_H diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp index 5c0f8a74a0b..008e9cedc4f 100644 --- a/modules/gdnative/nativescript/nativescript.cpp +++ b/modules/gdnative/nativescript/nativescript.cpp @@ -220,15 +220,9 @@ ScriptInstance *NativeScript::instance_create(Object *p_this) { nsi->userdata = script_data->create_func.create_func((godot_object *)p_this, script_data->create_func.method_data); #endif -#ifndef NO_THREADS - owners_lock->lock(); -#endif - + owners_lock.lock(); instance_owners.insert(p_this); - -#ifndef NO_THREADS - owners_lock->unlock(); -#endif + owners_lock.unlock(); return nsi; } @@ -524,17 +518,10 @@ NativeScript::NativeScript() { library = Ref(); lib_path = ""; class_name = ""; -#ifndef NO_THREADS - owners_lock = Mutex::create(); -#endif } NativeScript::~NativeScript() { NSL->unregister_script(this); - -#ifndef NO_THREADS - memdelete(owners_lock); -#endif } #define GET_SCRIPT_DESC() script->get_script_desc() @@ -911,15 +898,9 @@ NativeScriptInstance::~NativeScriptInstance() { if (owner) { -#ifndef NO_THREADS - script->owners_lock->lock(); -#endif - + script->owners_lock.lock(); script->instance_owners.erase(owner); - -#ifndef NO_THREADS - script->owners_lock->unlock(); -#endif + script->owners_lock.unlock(); } } @@ -1016,7 +997,6 @@ NativeScriptLanguage::NativeScriptLanguage() { NativeScriptLanguage::singleton = this; #ifndef NO_THREADS has_objects_to_register = false; - mutex = Mutex::create(); #endif #ifdef DEBUG_ENABLED @@ -1053,10 +1033,6 @@ NativeScriptLanguage::~NativeScriptLanguage() { NSL->library_classes.clear(); NSL->library_gdnatives.clear(); NSL->library_script_users.clear(); - -#ifndef NO_THREADS - memdelete(mutex); -#endif } String NativeScriptLanguage::get_name() const { diff --git a/modules/gdnative/nativescript/nativescript.h b/modules/gdnative/nativescript/nativescript.h index de0dfe56fdc..f36a5366a0a 100644 --- a/modules/gdnative/nativescript/nativescript.h +++ b/modules/gdnative/nativescript/nativescript.h @@ -121,9 +121,7 @@ class NativeScript : public Script { String script_class_name; String script_class_icon_path; -#ifndef NO_THREADS - Mutex *owners_lock; -#endif + Mutex owners_lock; Set instance_owners; protected: @@ -238,7 +236,7 @@ private: void _unload_stuff(bool p_reload = false); #ifndef NO_THREADS - Mutex *mutex; + Mutex mutex; Set > libs_to_init; Set scripts_to_register; diff --git a/modules/gdnative/pluginscript/pluginscript_language.cpp b/modules/gdnative/pluginscript/pluginscript_language.cpp index 35fe2c2fe2c..46e621fba72 100644 --- a/modules/gdnative/pluginscript/pluginscript_language.cpp +++ b/modules/gdnative/pluginscript/pluginscript_language.cpp @@ -400,39 +400,15 @@ void PluginScriptLanguage::reload_tool_script(const Ref