diff options
Diffstat (limited to 'core/os')
-rw-r--r-- | core/os/mutex.cpp | 4 | ||||
-rw-r--r-- | core/os/mutex.h | 27 | ||||
-rw-r--r-- | core/os/os.cpp | 21 | ||||
-rw-r--r-- | core/os/os.h | 9 | ||||
-rw-r--r-- | core/os/pool_allocator.cpp | 8 | ||||
-rw-r--r-- | core/os/rw_lock.h | 17 | ||||
-rw-r--r-- | core/os/semaphore.h | 13 | ||||
-rw-r--r-- | core/os/thread.cpp | 44 | ||||
-rw-r--r-- | core/os/thread.h | 40 | ||||
-rw-r--r-- | core/os/threaded_array_processor.h | 19 |
10 files changed, 39 insertions, 163 deletions
diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index 1d4400bfc1..512db1737a 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -40,11 +40,7 @@ void _global_unlock() { _global_mutex.unlock(); } -#ifndef NO_THREADS - template class MutexImpl<std::recursive_mutex>; template class MutexImpl<std::mutex>; template class MutexLock<MutexImpl<std::recursive_mutex>>; template class MutexLock<MutexImpl<std::mutex>>; - -#endif diff --git a/core/os/mutex.h b/core/os/mutex.h index a51248807b..eb58418bd6 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -34,8 +34,6 @@ #include "core/error/error_list.h" #include "core/typedefs.h" -#if !defined(NO_THREADS) - #include <mutex> template <class StdMutexT> @@ -79,29 +77,4 @@ extern template class MutexImpl<std::mutex>; extern template class MutexLock<MutexImpl<std::recursive_mutex>>; extern template class MutexLock<MutexImpl<std::mutex>>; -#else - -class FakeMutex { - FakeMutex() {} -}; - -template <class MutexT> -class MutexImpl { -public: - _ALWAYS_INLINE_ void lock() const {} - _ALWAYS_INLINE_ void unlock() const {} - _ALWAYS_INLINE_ Error try_lock() const { return OK; } -}; - -template <class MutexT> -class MutexLock { -public: - explicit MutexLock(const MutexT &p_mutex) {} -}; - -using Mutex = MutexImpl<FakeMutex>; -using BinaryMutex = MutexImpl<FakeMutex>; // Non-recursive, handle with care - -#endif // !NO_THREADS - #endif // MUTEX_H diff --git a/core/os/os.cpp b/core/os/os.cpp index 526b31ae7e..bbb2a94fe7 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -38,6 +38,7 @@ #include "core/version_generated.gen.h" #include <stdarg.h> +#include <thread> OS *OS::singleton = nullptr; uint64_t OS::target_ticks = 0; @@ -54,10 +55,6 @@ double OS::get_unix_time() const { return 0; } -void OS::debug_break() { - // something -} - void OS::_set_logger(CompositeLogger *p_logger) { if (_logger) { memdelete(_logger); @@ -325,21 +322,13 @@ String OS::get_unique_id() const { } int OS::get_processor_count() const { - return 1; + return std::thread::hardware_concurrency(); } String OS::get_processor_name() const { return ""; } -bool OS::can_use_threads() const { -#ifdef NO_THREADS - return false; -#else - return true; -#endif -} - void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) { has_server_feature_callback = p_callback; } @@ -523,10 +512,10 @@ void OS::add_frame_delay(bool p_can_draw) { if (is_in_low_processor_usage_mode() || !p_can_draw) { dynamic_delay = get_low_processor_usage_mode_sleep_usec(); } - const int target_fps = Engine::get_singleton()->get_target_fps(); - if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { + const int max_fps = Engine::get_singleton()->get_max_fps(); + if (max_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { // Override the low processor usage mode sleep delay if the target FPS is lower. - dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / target_fps)); + dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / max_fps)); } if (dynamic_delay > 0) { diff --git a/core/os/os.h b/core/os/os.h index c8821d07bf..af7b40f3ec 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -69,6 +69,7 @@ class OS { // so we can retrieve the rendering drivers available int _display_driver_id = -1; String _current_rendering_driver_name; + String _current_rendering_method; protected: void _set_logger(CompositeLogger *p_logger); @@ -98,6 +99,8 @@ protected: virtual void initialize_joypads() = 0; void set_current_rendering_driver_name(String p_driver_name) { _current_rendering_driver_name = p_driver_name; } + void set_current_rendering_method(String p_name) { _current_rendering_method = p_name; } + void set_display_driver_id(int p_display_driver_id) { _display_driver_id = p_display_driver_id; } virtual void set_main_loop(MainLoop *p_main_loop) = 0; @@ -116,6 +119,8 @@ public: static OS *get_singleton(); String get_current_rendering_driver_name() const { return _current_rendering_driver_name; } + String get_current_rendering_method() const { return _current_rendering_method; } + int get_display_driver_id() const { return _display_driver_id; } virtual Vector<String> get_video_adapter_driver_info() const = 0; @@ -284,8 +289,6 @@ public: virtual Error move_to_trash(const String &p_path) { return FAILED; } - virtual void debug_break(); - virtual int get_exit_code() const; // `set_exit_code` should only be used from `SceneTree` (or from a similar // level, e.g. from the `Main::start` if leaving without creating a `SceneTree`). @@ -298,8 +301,6 @@ public: virtual String get_unique_id() const; - virtual bool can_use_threads() const; - bool has_feature(const String &p_feature); void set_has_server_feature_callback(HasServerFeatureCallback p_callback); diff --git a/core/os/pool_allocator.cpp b/core/os/pool_allocator.cpp index f622e2c7c5..e7f2cff7c5 100644 --- a/core/os/pool_allocator.cpp +++ b/core/os/pool_allocator.cpp @@ -35,8 +35,6 @@ #include "core/os/os.h" #include "core/string/print_string.h" -#include <assert.h> - #define COMPACT_CHUNK(m_entry, m_to_pos) \ do { \ void *_dst = &((unsigned char *)pool)[m_to_pos]; \ @@ -169,11 +167,6 @@ bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos, const Entry *p_ PoolAllocator::ID PoolAllocator::alloc(int p_size) { ERR_FAIL_COND_V(p_size < 1, POOL_ALLOCATOR_INVALID_ID); -#ifdef DEBUG_ENABLED - if (p_size > free_mem) { - OS::get_singleton()->debug_break(); - } -#endif ERR_FAIL_COND_V(p_size > free_mem, POOL_ALLOCATOR_INVALID_ID); mt_lock(); @@ -482,7 +475,6 @@ void *PoolAllocator::get(ID p_mem) { ERR_FAIL_COND_V(!e, nullptr); } if (e->lock == 0) { - //assert(0); mt_unlock(); ERR_PRINT("e->lock == 0"); return nullptr; diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h index a046f474ea..d3206547c7 100644 --- a/core/os/rw_lock.h +++ b/core/os/rw_lock.h @@ -33,8 +33,6 @@ #include "core/error/error_list.h" -#if !defined(NO_THREADS) - #include <shared_mutex> class RWLock { @@ -72,21 +70,6 @@ public: } }; -#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 { const RWLock &lock; diff --git a/core/os/semaphore.h b/core/os/semaphore.h index 72df52dd34..1a93d3ee2c 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -34,8 +34,6 @@ #include "core/error/error_list.h" #include "core/typedefs.h" -#if !defined(NO_THREADS) - #include <condition_variable> #include <mutex> @@ -70,15 +68,4 @@ public: } }; -#else - -class Semaphore { -public: - _ALWAYS_INLINE_ void post() const {} - _ALWAYS_INLINE_ void wait() const {} - _ALWAYS_INLINE_ bool try_wait() const { return true; } -}; - -#endif - #endif // SEMAPHORE_H diff --git a/core/os/thread.cpp b/core/os/thread.cpp index c8072b7280..712f4793eb 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -34,15 +34,9 @@ #include "thread.h" #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::PlatformFunctions Thread::platform_functions; uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) { static std::hash<std::thread::id> hasher; @@ -52,30 +46,27 @@ uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) { Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id()); thread_local Thread::ID Thread::caller_id = 0; -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::_set_platform_functions(const PlatformFunctions &p_functions) { + platform_functions = p_functions; } void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) { Thread::caller_id = _thread_id_hash(p_self->thread.get_id()); - if (set_priority_func) { - set_priority_func(p_settings.priority); + if (platform_functions.set_priority) { + platform_functions.set_priority(p_settings.priority); } - if (init_func) { - init_func(); + if (platform_functions.init) { + platform_functions.init(); + } + ScriptServer::thread_enter(); // Scripts may need to attach a stack. + if (platform_functions.wrapper) { + platform_functions.wrapper(p_callback, p_userdata); + } else { + p_callback(p_userdata); } - ScriptServer::thread_enter(); //scripts may need to attach a stack - p_callback(p_userdata); ScriptServer::thread_exit(); - if (term_func) { - term_func(); + if (platform_functions.term) { + platform_functions.term(); } } @@ -108,8 +99,8 @@ void Thread::wait_to_finish() { } Error Thread::set_name(const String &p_name) { - if (set_name_func) { - return set_name_func(p_name); + if (platform_functions.set_name) { + return platform_functions.set_name(p_name); } return ERR_UNAVAILABLE; @@ -128,5 +119,4 @@ Thread::~Thread() { } } -#endif #endif // PLATFORM_THREAD_OVERRIDE diff --git a/core/os/thread.h b/core/os/thread.h index 0fb283e224..86442de760 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -35,15 +35,14 @@ #ifdef PLATFORM_THREAD_OVERRIDE #include "platform_thread.h" #else + #ifndef THREAD_H #define THREAD_H +#include "core/templates/safe_refcount.h" #include "core/typedefs.h" -#if !defined(NO_THREADS) -#include "core/templates/safe_refcount.h" #include <thread> -#endif class String; @@ -64,8 +63,15 @@ public: Settings() { priority = PRIORITY_NORMAL; } }; + struct PlatformFunctions { + Error (*set_name)(const String &) = nullptr; + void (*set_priority)(Thread::Priority) = nullptr; + void (*init)() = nullptr; + void (*wrapper)(Thread::Callback, void *) = nullptr; + void (*term)() = nullptr; + }; + private: -#if !defined(NO_THREADS) friend class Main; static ID main_thread_id; @@ -78,20 +84,11 @@ private: static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); - static Error (*set_name_func)(const String &); - static void (*set_priority_func)(Thread::Priority); - static void (*init_func)(); - static void (*term_func)(); -#endif + static PlatformFunctions platform_functions; public: - 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); + static void _set_platform_functions(const PlatformFunctions &p_functions); -#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; } @@ -107,19 +104,6 @@ public: Thread(); ~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/threaded_array_processor.h b/core/os/threaded_array_processor.h index 935fc7a360..95a2253f14 100644 --- a/core/os/threaded_array_processor.h +++ b/core/os/threaded_array_processor.h @@ -49,8 +49,6 @@ struct ThreadArrayProcessData { } }; -#ifndef NO_THREADS - template <class T> void process_array_thread(void *ud) { T &data = *(T *)ud; @@ -86,21 +84,4 @@ void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_us memdelete_arr(threads); } -#else - -template <class C, class M, class U> -void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) { - ThreadArrayProcessData<C, U> data; - data.method = p_method; - data.instance = p_instance; - data.userdata = p_userdata; - data.index.set(0); - data.elements = p_elements; - for (uint32_t i = 0; i < p_elements; i++) { - data.process(i); - } -} - -#endif - #endif // THREADED_ARRAY_PROCESSOR_H |