diff options
Diffstat (limited to 'core/os')
-rw-r--r-- | core/os/memory.cpp | 26 | ||||
-rw-r--r-- | core/os/memory.h | 6 | ||||
-rw-r--r-- | core/os/os.h | 5 | ||||
-rw-r--r-- | core/os/rw_lock.cpp | 43 | ||||
-rw-r--r-- | core/os/rw_lock.h | 84 | ||||
-rw-r--r-- | core/os/thread.cpp | 83 | ||||
-rw-r--r-- | core/os/thread.h | 72 | ||||
-rw-r--r-- | core/os/thread_dummy.cpp | 49 | ||||
-rw-r--r-- | core/os/thread_dummy.h | 62 | ||||
-rw-r--r-- | core/os/threaded_array_processor.h | 23 |
10 files changed, 206 insertions, 247 deletions
diff --git a/core/os/memory.cpp b/core/os/memory.cpp index 14808c2ce6..5910cb0e7b 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -60,11 +60,11 @@ void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_d #endif #ifdef DEBUG_ENABLED -uint64_t Memory::mem_usage = 0; -uint64_t Memory::max_usage = 0; +SafeNumeric<uint64_t> Memory::mem_usage; +SafeNumeric<uint64_t> Memory::max_usage; #endif -uint64_t Memory::alloc_count = 0; +SafeNumeric<uint64_t> Memory::alloc_count; void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { #ifdef DEBUG_ENABLED @@ -77,7 +77,7 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { ERR_FAIL_COND_V(!mem, nullptr); - atomic_increment(&alloc_count); + alloc_count.increment(); if (prepad) { uint64_t *s = (uint64_t *)mem; @@ -86,8 +86,8 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { uint8_t *s8 = (uint8_t *)mem; #ifdef DEBUG_ENABLED - atomic_add(&mem_usage, p_bytes); - atomic_exchange_if_greater(&max_usage, mem_usage); + uint64_t new_mem_usage = mem_usage.add(p_bytes); + max_usage.exchange_if_greater(new_mem_usage); #endif return s8 + PAD_ALIGN; } else { @@ -114,10 +114,10 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) { #ifdef DEBUG_ENABLED if (p_bytes > *s) { - atomic_add(&mem_usage, p_bytes - *s); - atomic_exchange_if_greater(&max_usage, mem_usage); + uint64_t new_mem_usage = mem_usage.add(p_bytes - *s); + max_usage.exchange_if_greater(new_mem_usage); } else { - atomic_sub(&mem_usage, *s - p_bytes); + mem_usage.sub(*s - p_bytes); } #endif @@ -156,14 +156,14 @@ void Memory::free_static(void *p_ptr, bool p_pad_align) { bool prepad = p_pad_align; #endif - atomic_decrement(&alloc_count); + alloc_count.decrement(); if (prepad) { mem -= PAD_ALIGN; #ifdef DEBUG_ENABLED uint64_t *s = (uint64_t *)mem; - atomic_sub(&mem_usage, *s); + mem_usage.sub(*s); #endif free(mem); @@ -178,7 +178,7 @@ uint64_t Memory::get_mem_available() { uint64_t Memory::get_mem_usage() { #ifdef DEBUG_ENABLED - return mem_usage; + return mem_usage.get(); #else return 0; #endif @@ -186,7 +186,7 @@ uint64_t Memory::get_mem_usage() { uint64_t Memory::get_mem_max_usage() { #ifdef DEBUG_ENABLED - return max_usage; + return max_usage.get(); #else return 0; #endif diff --git a/core/os/memory.h b/core/os/memory.h index c2ae3f4ae7..10e678103d 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -43,11 +43,11 @@ class Memory { Memory(); #ifdef DEBUG_ENABLED - static uint64_t mem_usage; - static uint64_t max_usage; + static SafeNumeric<uint64_t> mem_usage; + static SafeNumeric<uint64_t> max_usage; #endif - static uint64_t alloc_count; + static SafeNumeric<uint64_t> alloc_count; public: static void *alloc_static(size_t p_bytes, bool p_pad_align = false); diff --git a/core/os/os.h b/core/os/os.h index e02ce7d5ec..77a54ba68a 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -149,11 +149,6 @@ public: bool is_layered_allowed() const { return _allow_layered; } bool is_hidpi_allowed() const { return _allow_hidpi; } - virtual int get_tablet_driver_count() const { return 0; }; - virtual String get_tablet_driver_name(int p_driver) const { return ""; }; - virtual String get_current_tablet_driver() const { return ""; }; - virtual void set_current_tablet_driver(const String &p_driver){}; - void ensure_user_data_dir(); virtual MainLoop *get_main_loop() const = 0; diff --git a/core/os/rw_lock.cpp b/core/os/rw_lock.cpp deleted file mode 100644 index 26db0aab30..0000000000 --- a/core/os/rw_lock.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/*************************************************************************/ -/* rw_lock.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 "rw_lock.h" - -#include "core/error/error_macros.h" - -#include <stddef.h> - -RWLock *(*RWLock::create_func)() = nullptr; - -RWLock *RWLock::create() { - ERR_FAIL_COND_V(!create_func, nullptr); - - return create_func(); -} diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h index ff6ad3cc4a..560ec57a5f 100644 --- a/core/os/rw_lock.h +++ b/core/os/rw_lock.h @@ -33,55 +33,83 @@ #include "core/error/error_list.h" +#if !defined(NO_THREADS) + +#include <shared_mutex> + class RWLock { -protected: - static RWLock *(*create_func)(); + mutable std::shared_timed_mutex mutex; public: - virtual void read_lock() = 0; ///< Lock the rwlock, block if locked by someone else - virtual void read_unlock() = 0; ///< Unlock the rwlock, let other threads continue - virtual Error read_try_lock() = 0; ///< Attempt to lock the rwlock, OK on success, ERROR means it can't lock. + // Lock the rwlock, block if locked by someone else + void read_lock() const { + mutex.lock_shared(); + } - virtual void write_lock() = 0; ///< Lock the rwlock, block if locked by someone else - virtual void write_unlock() = 0; ///< Unlock the rwlock, let other thwrites continue - virtual Error write_try_lock() = 0; ///< Attempt to lock the rwlock, OK on success, ERROR means it can't lock. + // Unlock the rwlock, let other threads continue + void read_unlock() const { + mutex.unlock_shared(); + } - static RWLock *create(); ///< Create a rwlock + // Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock. + Error read_try_lock() const { + return mutex.try_lock_shared() ? OK : ERR_BUSY; + } + + // Lock the rwlock, block if locked by someone else + void write_lock() { + mutex.lock(); + } + + // Unlock the rwlock, let other thwrites continue + void write_unlock() { + mutex.unlock(); + } - virtual ~RWLock() {} + // Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock. + Error write_try_lock() { + return mutex.try_lock() ? OK : ERR_BUSY; + } +}; + +#else + +class RWLock { +public: + void read_lock() const {} + void read_unlock() const {} + Error read_try_lock() const { return OK; } + + void write_lock() {} + void write_unlock() {} + Error write_try_lock() { return OK; } }; +#endif + class RWLockRead { - RWLock *lock; + const RWLock &lock; public: - RWLockRead(const RWLock *p_lock) { - lock = const_cast<RWLock *>(p_lock); - if (lock) { - lock->read_lock(); - } + RWLockRead(const RWLock &p_lock) : + lock(p_lock) { + lock.read_lock(); } ~RWLockRead() { - if (lock) { - lock->read_unlock(); - } + lock.read_unlock(); } }; class RWLockWrite { - RWLock *lock; + RWLock &lock; public: - RWLockWrite(RWLock *p_lock) { - lock = p_lock; - if (lock) { - lock->write_lock(); - } + RWLockWrite(RWLock &p_lock) : + lock(p_lock) { + lock.write_lock(); } ~RWLockWrite() { - if (lock) { - lock->write_unlock(); - } + lock.write_unlock(); } }; diff --git a/core/os/thread.cpp b/core/os/thread.cpp index 0ed8825452..88744eed63 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -30,30 +30,72 @@ #include "thread.h" -Thread *(*Thread::create_func)(ThreadCreateCallback, void *, const Settings &) = nullptr; -Thread::ID (*Thread::get_thread_id_func)() = nullptr; -void (*Thread::wait_to_finish_func)(Thread *) = nullptr; +#include "core/object/script_language.h" + +#if !defined(NO_THREADS) + +#include "core/templates/safe_refcount.h" + Error (*Thread::set_name_func)(const String &) = nullptr; +void (*Thread::set_priority_func)(Thread::Priority) = nullptr; +void (*Thread::init_func)() = nullptr; +void (*Thread::term_func)() = nullptr; -Thread::ID Thread::_main_thread_id = 0; +Thread::ID Thread::main_thread_id = 1; +SafeNumeric<Thread::ID> Thread::last_thread_id{ 1 }; +thread_local Thread::ID Thread::caller_id = 1; -Thread::ID Thread::get_caller_id() { - if (get_thread_id_func) { - return get_thread_id_func(); +void Thread::_set_platform_funcs( + Error (*p_set_name_func)(const String &), + void (*p_set_priority_func)(Thread::Priority), + void (*p_init_func)(), + void (*p_term_func)()) { + Thread::set_name_func = p_set_name_func; + Thread::set_priority_func = p_set_priority_func; + Thread::init_func = p_init_func; + Thread::term_func = p_term_func; +} + +void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) { + Thread::caller_id = p_self->id; + if (set_priority_func) { + set_priority_func(p_settings.priority); + } + if (init_func) { + init_func(); + } + ScriptServer::thread_enter(); //scripts may need to attach a stack + p_callback(p_userdata); + ScriptServer::thread_exit(); + if (term_func) { + term_func(); } - return 0; } -Thread *Thread::create(ThreadCreateCallback p_callback, void *p_user, const Settings &p_settings) { - if (create_func) { - return create_func(p_callback, p_user, p_settings); +void Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) { + if (id != 0) { +#ifdef DEBUG_ENABLED + WARN_PRINT("A Thread object has been re-started without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread."); +#endif + thread.detach(); + std::thread empty_thread; + thread.swap(empty_thread); } - return nullptr; + id = last_thread_id.increment(); + std::thread new_thread(&Thread::callback, this, p_settings, p_callback, p_user); + thread.swap(new_thread); } -void Thread::wait_to_finish(Thread *p_thread) { - if (wait_to_finish_func) { - wait_to_finish_func(p_thread); +bool Thread::is_started() const { + return id != 0; +} + +void Thread::wait_to_finish() { + if (id != 0) { + thread.join(); + std::thread empty_thread; + thread.swap(empty_thread); + id = 0; } } @@ -64,3 +106,14 @@ Error Thread::set_name(const String &p_name) { return ERR_UNAVAILABLE; } + +Thread::~Thread() { + if (id != 0) { +#ifdef DEBUG_ENABLED + WARN_PRINT("A Thread object has been destroyed without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread."); +#endif + thread.detach(); + } +} + +#endif diff --git a/core/os/thread.h b/core/os/thread.h index 993c7ad33d..76f5be182e 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -31,13 +31,21 @@ #ifndef THREAD_H #define THREAD_H -#include "core/string/ustring.h" #include "core/typedefs.h" -typedef void (*ThreadCreateCallback)(void *p_userdata); +#if !defined(NO_THREADS) +#include "core/templates/safe_refcount.h" +#include <thread> +#endif + +class String; class Thread { public: + typedef void (*Callback)(void *p_userdata); + + typedef uint64_t ID; + enum Priority { PRIORITY_LOW, PRIORITY_NORMAL, @@ -49,30 +57,60 @@ public: Settings() { priority = PRIORITY_NORMAL; } }; - typedef uint64_t ID; +private: +#if !defined(NO_THREADS) + friend class Main; -protected: - static Thread *(*create_func)(ThreadCreateCallback p_callback, void *, const Settings &); - static ID (*get_thread_id_func)(); - static void (*wait_to_finish_func)(Thread *); - static Error (*set_name_func)(const String &); + static ID main_thread_id; + static SafeNumeric<Thread::ID> last_thread_id; - friend class Main; + ID id = 0; + static thread_local ID caller_id; + std::thread thread; - static ID _main_thread_id; + static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); - Thread() {} + static Error (*set_name_func)(const String &); + static void (*set_priority_func)(Thread::Priority); + static void (*init_func)(); + static void (*term_func)(); +#endif public: - virtual ID get_id() const = 0; + static void _set_platform_funcs( + Error (*p_set_name_func)(const String &), + void (*p_set_priority_func)(Thread::Priority), + void (*p_init_func)() = nullptr, + void (*p_term_func)() = nullptr); + +#if !defined(NO_THREADS) + _FORCE_INLINE_ ID get_id() const { return id; } + // get the ID of the caller thread + _FORCE_INLINE_ static ID get_caller_id() { return caller_id; } + // get the ID of the main thread + _FORCE_INLINE_ static ID get_main_id() { return main_thread_id; } static Error set_name(const String &p_name); - _FORCE_INLINE_ static ID get_main_id() { return _main_thread_id; } ///< get the ID of the main thread - static ID get_caller_id(); ///< get the ID of the caller function ID - static void wait_to_finish(Thread *p_thread); ///< waits until thread is finished, and deallocates it. - static Thread *create(ThreadCreateCallback p_callback, void *p_user, const Settings &p_settings = Settings()); ///< Static function to create a thread, will call p_callback - virtual ~Thread() {} + void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()); + bool is_started() const; + ///< waits until thread is finished, and deallocates it. + void wait_to_finish(); + + ~Thread(); +#else + _FORCE_INLINE_ ID get_id() const { return 0; } + // get the ID of the caller thread + _FORCE_INLINE_ static ID get_caller_id() { return 0; } + // get the ID of the main thread + _FORCE_INLINE_ static ID get_main_id() { return 0; } + + static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; } + + void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {} + bool is_started() const { return false; } + void wait_to_finish() {} +#endif }; #endif // THREAD_H diff --git a/core/os/thread_dummy.cpp b/core/os/thread_dummy.cpp deleted file mode 100644 index 006757b8e4..0000000000 --- a/core/os/thread_dummy.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/*************************************************************************/ -/* thread_dummy.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_dummy.h" - -#include "core/os/memory.h" - -Thread *ThreadDummy::create(ThreadCreateCallback p_callback, void *p_user, const Thread::Settings &p_settings) { - return memnew(ThreadDummy); -} - -void ThreadDummy::make_default() { - Thread::create_func = &ThreadDummy::create; -} - -RWLock *RWLockDummy::create() { - return memnew(RWLockDummy); -} - -void RWLockDummy::make_default() { - RWLock::create_func = &RWLockDummy::create; -} diff --git a/core/os/thread_dummy.h b/core/os/thread_dummy.h deleted file mode 100644 index 35e19732bf..0000000000 --- a/core/os/thread_dummy.h +++ /dev/null @@ -1,62 +0,0 @@ -/*************************************************************************/ -/* thread_dummy.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 THREAD_DUMMY_H -#define THREAD_DUMMY_H - -#include "core/os/rw_lock.h" -#include "core/os/semaphore.h" -#include "core/os/thread.h" - -class ThreadDummy : public Thread { - static Thread *create(ThreadCreateCallback p_callback, void *p_user, const Settings &p_settings = Settings()); - -public: - virtual ID get_id() const { return 0; }; - - static void make_default(); -}; - -class RWLockDummy : public RWLock { - static RWLock *create(); - -public: - virtual void read_lock() {} - virtual void read_unlock() {} - virtual Error read_try_lock() { return OK; } - - virtual void write_lock() {} - virtual void write_unlock() {} - virtual Error write_try_lock() { return OK; } - - static void make_default(); -}; - -#endif // THREAD_DUMMY_H diff --git a/core/os/threaded_array_processor.h b/core/os/threaded_array_processor.h index 57f3de20bf..4f270001d3 100644 --- a/core/os/threaded_array_processor.h +++ b/core/os/threaded_array_processor.h @@ -40,7 +40,7 @@ template <class C, class U> struct ThreadArrayProcessData { uint32_t elements; - uint32_t index; + SafeNumeric<uint32_t> index; C *instance; U userdata; void (C::*method)(uint32_t, U); @@ -56,7 +56,7 @@ template <class T> void process_array_thread(void *ud) { T &data = *(T *)ud; while (true) { - uint32_t index = atomic_increment(&data.index); + uint32_t index = data.index.increment(); if (index >= data.elements) { break; } @@ -70,22 +70,21 @@ void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_us data.method = p_method; data.instance = p_instance; data.userdata = p_userdata; - data.index = 0; + data.index.set(0); data.elements = p_elements; - data.process(data.index); //process first, let threads increment for next - - Vector<Thread *> threads; + data.process(0); //process first, let threads increment for next - threads.resize(OS::get_singleton()->get_processor_count()); + int thread_count = OS::get_singleton()->get_processor_count(); + Thread *threads = memnew_arr(Thread, thread_count); - for (int i = 0; i < threads.size(); i++) { - threads.write[i] = Thread::create(process_array_thread<ThreadArrayProcessData<C, U>>, &data); + for (int i = 0; i < thread_count; i++) { + threads[i].start(process_array_thread<ThreadArrayProcessData<C, U>>, &data); } - for (int i = 0; i < threads.size(); i++) { - Thread::wait_to_finish(threads[i]); - memdelete(threads[i]); + for (int i = 0; i < thread_count; i++) { + threads[i].wait_to_finish(); } + memdelete_arr(threads); } #else |