diff options
Diffstat (limited to 'core/os')
-rw-r--r-- | core/os/copymem.h | 4 | ||||
-rw-r--r-- | core/os/dir_access.cpp | 6 | ||||
-rw-r--r-- | core/os/dir_access.h | 7 | ||||
-rw-r--r-- | core/os/file_access.cpp | 30 | ||||
-rw-r--r-- | core/os/file_access.h | 7 | ||||
-rw-r--r-- | core/os/keyboard.cpp | 5 | ||||
-rw-r--r-- | core/os/keyboard.h | 9 | ||||
-rw-r--r-- | core/os/main_loop.cpp | 36 | ||||
-rw-r--r-- | core/os/main_loop.h | 21 | ||||
-rw-r--r-- | core/os/memory.cpp | 34 | ||||
-rw-r--r-- | core/os/memory.h | 14 | ||||
-rw-r--r-- | core/os/midi_driver.cpp | 4 | ||||
-rw-r--r-- | core/os/midi_driver.h | 6 | ||||
-rw-r--r-- | core/os/mutex.cpp | 4 | ||||
-rw-r--r-- | core/os/mutex.h | 6 | ||||
-rw-r--r-- | core/os/os.cpp | 14 | ||||
-rw-r--r-- | core/os/os.h | 31 | ||||
-rw-r--r-- | core/os/pool_allocator.cpp | 596 | ||||
-rw-r--r-- | core/os/pool_allocator.h | 149 | ||||
-rw-r--r-- | core/os/rw_lock.h | 90 | ||||
-rw-r--r-- | core/os/semaphore.h | 6 | ||||
-rw-r--r-- | core/os/spin_lock.h (renamed from core/os/rw_lock.cpp) | 32 | ||||
-rw-r--r-- | core/os/thread.cpp | 88 | ||||
-rw-r--r-- | core/os/thread.h | 77 | ||||
-rw-r--r-- | core/os/thread_dummy.cpp | 49 | ||||
-rw-r--r-- | core/os/thread_dummy.h | 62 | ||||
-rw-r--r-- | core/os/thread_safe.h | 4 | ||||
-rw-r--r-- | core/os/threaded_array_processor.h | 29 |
28 files changed, 1080 insertions, 340 deletions
diff --git a/core/os/copymem.h b/core/os/copymem.h index 04ea3caeff..6fd559356c 100644 --- a/core/os/copymem.h +++ b/core/os/copymem.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index 5e1cb8ea29..fb9da9fbed 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,10 +30,10 @@ #include "dir_access.h" +#include "core/config/project_settings.h" #include "core/os/file_access.h" #include "core/os/memory.h" #include "core/os/os.h" -#include "core/project_settings.h" String DirAccess::_get_root_path() const { switch (_access_type) { diff --git a/core/os/dir_access.h b/core/os/dir_access.h index 6bce9a4c12..c49c4cc4b8 100644 --- a/core/os/dir_access.h +++ b/core/os/dir_access.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,8 +31,8 @@ #ifndef DIR_ACCESS_H #define DIR_ACCESS_H +#include "core/string/ustring.h" #include "core/typedefs.h" -#include "core/ustring.h" //@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies class DirAccess { @@ -57,7 +57,6 @@ protected: String _get_root_string() const; String fix_path(String p_path) const; - bool next_is_dir; template <class T> static DirAccess *_create_builtin() { diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 20b3435911..5a3df88619 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,11 +30,11 @@ #include "file_access.h" +#include "core/config/project_settings.h" #include "core/crypto/crypto_core.h" #include "core/io/file_access_pack.h" #include "core/io/marshalls.h" #include "core/os/os.h" -#include "core/project_settings.h" FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { nullptr, nullptr }; @@ -51,7 +51,7 @@ FileAccess *FileAccess::create(AccessType p_access) { } bool FileAccess::exists(const String &p_name) { - if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name)) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_name)) { return true; } @@ -234,7 +234,7 @@ double FileAccess::get_double() const { String FileAccess::get_token() const { CharString token; - CharType c = get_8(); + char32_t c = get_8(); while (!eof_reached()) { if (c <= ' ') { @@ -254,8 +254,8 @@ class CharBuffer { Vector<char> vector; char stack_buffer[256]; - char *buffer; - int capacity; + char *buffer = nullptr; + int capacity = 0; int written = 0; bool grow() { @@ -299,7 +299,7 @@ public: String FileAccess::get_line() const { CharBuffer line; - CharType c = get_8(); + char32_t c = get_8(); while (!eof_reached()) { if (c == '\n' || c == '\0') { @@ -342,14 +342,14 @@ Vector<String> FileAccess::get_csv_line(const String &p_delim) const { bool in_quote = false; String current; for (int i = 0; i < l.length(); i++) { - CharType c = l[i]; - CharType s[2] = { 0, 0 }; + char32_t c = l[i]; + char32_t s[2] = { 0, 0 }; if (!in_quote && c == p_delim[0]) { strings.push_back(current); current = String(); } else if (c == '"') { - if (l[i + 1] == '"') { + if (l[i + 1] == '"' && in_quote) { s[0] = '"'; current += s; i++; @@ -456,7 +456,7 @@ void FileAccess::store_double(double p_dest) { } uint64_t FileAccess::get_modified_time(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { return 0; } @@ -469,7 +469,7 @@ uint64_t FileAccess::get_modified_time(const String &p_file) { } uint32_t FileAccess::get_unix_permissions(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { return 0; } @@ -482,6 +482,10 @@ uint32_t FileAccess::get_unix_permissions(const String &p_file) { } Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { + return ERR_UNAVAILABLE; + } + FileAccess *fa = create_for_path(p_file); ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'."); diff --git a/core/os/file_access.h b/core/os/file_access.h index 48b9ee4269..1c78204c1d 100644 --- a/core/os/file_access.h +++ b/core/os/file_access.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -33,8 +33,8 @@ #include "core/math/math_defs.h" #include "core/os/memory.h" +#include "core/string/ustring.h" #include "core/typedefs.h" -#include "core/ustring.h" /** * Multi-Platform abstraction for accessing to files. @@ -81,7 +81,6 @@ public: virtual void _set_access_type(AccessType p_access); enum ModeFlags { - READ = 1, WRITE = 2, READ_WRITE = 3, diff --git a/core/os/keyboard.cpp b/core/os/keyboard.cpp index d088151a6d..4b2cafd8fe 100644 --- a/core/os/keyboard.cpp +++ b/core/os/keyboard.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -38,7 +38,6 @@ struct _KeyCodeText { }; static const _KeyCodeText _keycodes[] = { - /* clang-format off */ {KEY_ESCAPE ,"Escape"}, {KEY_TAB ,"Tab"}, diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 5d11e6a378..3ef70e786f 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,7 +31,7 @@ #ifndef KEYBOARD_H #define KEYBOARD_H -#include "core/ustring.h" +#include "core/string/ustring.h" /* Special Key: @@ -294,11 +294,9 @@ enum KeyList { KEY_DIVISION = 0x00F7, KEY_YDIAERESIS = 0x00FF, - }; enum KeyModifierMask { - KEY_CODE_MASK = ((1 << 25) - 1), ///< Apply this mask to any keycode to remove modifiers. KEY_MODIFIER_MASK = (0xFF << 24), ///< Apply this mask to isolate modifiers. KEY_MASK_SHIFT = (1 << 25), @@ -314,7 +312,6 @@ enum KeyModifierMask { KEY_MASK_KPAD = (1 << 29), KEY_MASK_GROUP_SWITCH = (1 << 30) // bit 31 can't be used because variant uses regular 32 bits int as datatype - }; String keycode_get_string(uint32_t p_code); diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index 434f6fa300..016d9d0a09 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,17 +30,12 @@ #include "main_loop.h" -#include "core/script_language.h" +#include "core/object/script_language.h" void MainLoop::_bind_methods() { - ClassDB::bind_method(D_METHOD("init"), &MainLoop::init); - ClassDB::bind_method(D_METHOD("iteration", "delta"), &MainLoop::iteration); - ClassDB::bind_method(D_METHOD("idle", "delta"), &MainLoop::idle); - ClassDB::bind_method(D_METHOD("finish"), &MainLoop::finish); - BIND_VMETHOD(MethodInfo("_initialize")); - BIND_VMETHOD(MethodInfo(Variant::BOOL, "_iteration", PropertyInfo(Variant::FLOAT, "delta"))); - BIND_VMETHOD(MethodInfo(Variant::BOOL, "_idle", PropertyInfo(Variant::FLOAT, "delta"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_physics_process", PropertyInfo(Variant::FLOAT, "delta"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_process", PropertyInfo(Variant::FLOAT, "delta"))); BIND_VMETHOD(MethodInfo("_finalize")); BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING); @@ -52,17 +47,18 @@ void MainLoop::_bind_methods() { BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED); BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN); BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT); + BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED); ADD_SIGNAL(MethodInfo("on_request_permissions_result", PropertyInfo(Variant::STRING, "permission"), PropertyInfo(Variant::BOOL, "granted"))); }; -void MainLoop::set_init_script(const Ref<Script> &p_init_script) { - init_script = p_init_script; +void MainLoop::set_initialize_script(const Ref<Script> &p_initialize_script) { + initialize_script = p_initialize_script; } -void MainLoop::init() { - if (init_script.is_valid()) { - set_script(init_script); +void MainLoop::initialize() { + if (initialize_script.is_valid()) { + set_script(initialize_script); } if (get_script_instance()) { @@ -70,23 +66,23 @@ void MainLoop::init() { } } -bool MainLoop::iteration(float p_time) { +bool MainLoop::physics_process(float p_time) { if (get_script_instance()) { - return get_script_instance()->call("_iteration", p_time); + return get_script_instance()->call("_physics_process", p_time); } return false; } -bool MainLoop::idle(float p_time) { +bool MainLoop::process(float p_time) { if (get_script_instance()) { - return get_script_instance()->call("_idle", p_time); + return get_script_instance()->call("_process", p_time); } return false; } -void MainLoop::finish() { +void MainLoop::finalize() { if (get_script_instance()) { get_script_instance()->call("_finalize"); set_script(Variant()); //clear script diff --git a/core/os/main_loop.h b/core/os/main_loop.h index 2c34cf193c..25a09fe98f 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -32,14 +32,14 @@ #define MAIN_LOOP_H #include "core/input/input_event.h" -#include "core/reference.h" -#include "core/script_language.h" +#include "core/object/reference.h" +#include "core/object/script_language.h" class MainLoop : public Object { GDCLASS(MainLoop, Object); OBJ_CATEGORY("Main Loop"); - Ref<Script> init_script; + Ref<Script> initialize_script; protected: static void _bind_methods(); @@ -56,14 +56,15 @@ public: NOTIFICATION_APPLICATION_PAUSED = 2015, NOTIFICATION_APPLICATION_FOCUS_IN = 2016, NOTIFICATION_APPLICATION_FOCUS_OUT = 2017, + NOTIFICATION_TEXT_SERVER_CHANGED = 2018, }; - virtual void init(); - virtual bool iteration(float p_time); - virtual bool idle(float p_time); - virtual void finish(); + virtual void initialize(); + virtual bool physics_process(float p_time); + virtual bool process(float p_time); + virtual void finalize(); - void set_init_script(const Ref<Script> &p_init_script); + void set_initialize_script(const Ref<Script> &p_initialize_script); MainLoop() {} virtual ~MainLoop() {} diff --git a/core/os/memory.cpp b/core/os/memory.cpp index 8457c52092..5910cb0e7b 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,9 +30,9 @@ #include "memory.h" -#include "core/error_macros.h" +#include "core/error/error_macros.h" #include "core/os/copymem.h" -#include "core/safe_refcount.h" +#include "core/templates/safe_refcount.h" #include <stdio.h> #include <stdlib.h> @@ -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 46ffb4124b..10e678103d 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,8 +31,8 @@ #ifndef MEMORY_H #define MEMORY_H -#include "core/error_macros.h" -#include "core/safe_refcount.h" +#include "core/error/error_macros.h" +#include "core/templates/safe_refcount.h" #include <stddef.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/midi_driver.cpp b/core/os/midi_driver.cpp index e9919aeb86..a8be84c56c 100644 --- a/core/os/midi_driver.cpp +++ b/core/os/midi_driver.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ diff --git a/core/os/midi_driver.h b/core/os/midi_driver.h index bc922e1fcf..ccf624e07e 100644 --- a/core/os/midi_driver.h +++ b/core/os/midi_driver.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -32,7 +32,7 @@ #define MIDI_DRIVER_H #include "core/typedefs.h" -#include "core/variant.h" +#include "core/variant/variant.h" /** * Multi-Platform abstraction for accessing to MIDI. diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index 31a0dc2bfa..b7d7752d35 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ diff --git a/core/os/mutex.h b/core/os/mutex.h index d42cbed821..d77ec362a1 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,7 +31,7 @@ #ifndef MUTEX_H #define MUTEX_H -#include "core/error_list.h" +#include "core/error/error_list.h" #include "core/typedefs.h" #if !defined(NO_THREADS) diff --git a/core/os/os.cpp b/core/os/os.cpp index dc8bd5ee69..182bab4058 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,11 +30,11 @@ #include "os.h" +#include "core/config/project_settings.h" #include "core/input/input.h" #include "core/os/dir_access.h" #include "core/os/file_access.h" #include "core/os/midi_driver.h" -#include "core/project_settings.h" #include "core/version_generated.gen.h" #include "servers/audio_server.h" @@ -80,10 +80,6 @@ String OS::get_iso_date_time(bool local) const { timezone; } -uint64_t OS::get_splash_tick_msec() const { - return _msec_splash; -} - double OS::get_unix_time() const { return 0; } @@ -509,12 +505,8 @@ void OS::add_frame_delay(bool p_can_draw) { } OS::OS() { - void *volatile stack_bottom; - singleton = this; - _stack_bottom = (void *)(&stack_bottom); - Vector<Logger *> loggers; loggers.push_back(memnew(StdLogger)); _set_logger(memnew(CompositeLogger(loggers))); diff --git a/core/os/os.h b/core/os/os.h index 48dae99188..77a54ba68a 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,13 +31,13 @@ #ifndef OS_H #define OS_H -#include "core/engine.h" -#include "core/image.h" +#include "core/config/engine.h" +#include "core/io/image.h" #include "core/io/logger.h" -#include "core/list.h" #include "core/os/main_loop.h" -#include "core/ustring.h" -#include "core/vector.h" +#include "core/string/ustring.h" +#include "core/templates/list.h" +#include "core/templates/vector.h" #include <stdarg.h> @@ -52,7 +52,6 @@ class OS { bool _verbose_stdout = false; bool _debug_stdout = false; String _local_clipboard; - uint64_t _msec_splash; bool _no_window = false; int _exit_code = 0; int _orientation; @@ -63,8 +62,6 @@ class OS { char *last_error; - void *_stack_bottom; - CompositeLogger *_logger = nullptr; bool restart_on_exit = false; @@ -78,7 +75,6 @@ public: typedef bool (*HasServerFeatureCallback)(const String &p_feature); enum RenderThreadMode { - RENDER_THREAD_UNSAFE, RENDER_THREAD_SAFE, RENDER_SEPARATE_THREAD @@ -86,11 +82,13 @@ public: protected: friend class Main; + // Needed by tests to setup command-line args. + friend int test_main(int argc, char *argv[]); HasServerFeatureCallback has_server_feature_callback = nullptr; RenderThreadMode _render_thread_mode = RENDER_THREAD_SAFE; - // functions used by main to initialize/deinitialize the OS + // Functions used by Main to initialize/deinitialize the OS. void add_logger(Logger *p_logger); virtual void initialize() = 0; @@ -131,7 +129,8 @@ public: virtual int get_low_processor_usage_mode_sleep_usec() const; virtual String get_executable_path() const; - virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = nullptr, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr) = 0; + virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr) = 0; + virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr) = 0; virtual Error kill(const ProcessID &p_pid) = 0; virtual int get_process_id() const; virtual void vibrate_handheld(int p_duration_ms = 500); @@ -150,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; @@ -218,7 +212,6 @@ public: virtual uint64_t get_ticks_usec() const = 0; uint32_t get_ticks_msec() const; - uint64_t get_splash_tick_msec() const; virtual bool is_userfs_persistent() const { return true; } diff --git a/core/os/pool_allocator.cpp b/core/os/pool_allocator.cpp new file mode 100644 index 0000000000..b538ca0c96 --- /dev/null +++ b/core/os/pool_allocator.cpp @@ -0,0 +1,596 @@ +/*************************************************************************/ +/* pool_allocator.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 "pool_allocator.h" + +#include "core/error/error_macros.h" +#include "core/os/copymem.h" +#include "core/os/memory.h" +#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]; \ + void *_src = &((unsigned char *)pool)[(m_entry).pos]; \ + movemem(_dst, _src, aligned((m_entry).len)); \ + (m_entry).pos = m_to_pos; \ + } while (0); + +void PoolAllocator::mt_lock() const { +} + +void PoolAllocator::mt_unlock() const { +} + +bool PoolAllocator::get_free_entry(EntryArrayPos *p_pos) { + if (entry_count == entry_max) { + return false; + } + + for (int i = 0; i < entry_max; i++) { + if (entry_array[i].len == 0) { + *p_pos = i; + return true; + } + } + + ERR_PRINT("Out of memory Chunks!"); + + return false; // +} + +/** + * Find a hole + * @param p_pos The hole is behind the block pointed by this variable upon return. if pos==entry_count, then allocate at end + * @param p_for_size hole size + * @return false if hole found, true if no hole found + */ +bool PoolAllocator::find_hole(EntryArrayPos *p_pos, int p_for_size) { + /* position where previous entry ends. Defaults to zero (begin of pool) */ + + int prev_entry_end_pos = 0; + + for (int i = 0; i < entry_count; i++) { + Entry &entry = entry_array[entry_indices[i]]; + + /* determine hole size to previous entry */ + + int hole_size = entry.pos - prev_entry_end_pos; + + /* determine if what we want fits in that hole */ + if (hole_size >= p_for_size) { + *p_pos = i; + return true; + } + + /* prepare for next one */ + prev_entry_end_pos = entry_end(entry); + } + + /* No holes between entries, check at the end..*/ + + if ((pool_size - prev_entry_end_pos) >= p_for_size) { + *p_pos = entry_count; + return true; + } + + return false; +} + +void PoolAllocator::compact(int p_up_to) { + uint32_t prev_entry_end_pos = 0; + + if (p_up_to < 0) { + p_up_to = entry_count; + } + for (int i = 0; i < p_up_to; i++) { + Entry &entry = entry_array[entry_indices[i]]; + + /* determine hole size to previous entry */ + + int hole_size = entry.pos - prev_entry_end_pos; + + /* if we can compact, do it */ + if (hole_size > 0 && !entry.lock) { + COMPACT_CHUNK(entry, prev_entry_end_pos); + } + + /* prepare for next one */ + prev_entry_end_pos = entry_end(entry); + } +} + +void PoolAllocator::compact_up(int p_from) { + uint32_t next_entry_end_pos = pool_size; // - static_area_size; + + for (int i = entry_count - 1; i >= p_from; i--) { + Entry &entry = entry_array[entry_indices[i]]; + + /* determine hole size to nextious entry */ + + int hole_size = next_entry_end_pos - (entry.pos + aligned(entry.len)); + + /* if we can compact, do it */ + if (hole_size > 0 && !entry.lock) { + COMPACT_CHUNK(entry, (next_entry_end_pos - aligned(entry.len))); + } + + /* prepare for next one */ + next_entry_end_pos = entry.pos; + } +} + +bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos, Entry *p_entry) { + EntryArrayPos entry_pos = entry_max; + + for (int i = 0; i < entry_count; i++) { + if (&entry_array[entry_indices[i]] == p_entry) { + entry_pos = i; + break; + } + } + + if (entry_pos == entry_max) { + return false; + } + + *p_map_pos = entry_pos; + return true; +} + +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(); + + if (entry_count == entry_max) { + mt_unlock(); + ERR_PRINT("entry_count==entry_max"); + return POOL_ALLOCATOR_INVALID_ID; + } + + int size_to_alloc = aligned(p_size); + + EntryIndicesPos new_entry_indices_pos; + + if (!find_hole(&new_entry_indices_pos, size_to_alloc)) { + /* No hole could be found, try compacting mem */ + compact(); + /* Then search again */ + + if (!find_hole(&new_entry_indices_pos, size_to_alloc)) { + mt_unlock(); + ERR_FAIL_V_MSG(POOL_ALLOCATOR_INVALID_ID, "Memory can't be compacted further."); + } + } + + EntryArrayPos new_entry_array_pos; + + bool found_free_entry = get_free_entry(&new_entry_array_pos); + + if (!found_free_entry) { + mt_unlock(); + ERR_FAIL_V_MSG(POOL_ALLOCATOR_INVALID_ID, "No free entry found in PoolAllocator."); + } + + /* move all entry indices up, make room for this one */ + for (int i = entry_count; i > new_entry_indices_pos; i--) { + entry_indices[i] = entry_indices[i - 1]; + } + + entry_indices[new_entry_indices_pos] = new_entry_array_pos; + + entry_count++; + + Entry &entry = entry_array[entry_indices[new_entry_indices_pos]]; + + entry.len = p_size; + entry.pos = (new_entry_indices_pos == 0) ? 0 : entry_end(entry_array[entry_indices[new_entry_indices_pos - 1]]); //alloc either at beginning or end of previous + entry.lock = 0; + entry.check = (check_count++) & CHECK_MASK; + free_mem -= size_to_alloc; + if (free_mem < free_mem_peak) { + free_mem_peak = free_mem; + } + + ID retval = (entry_indices[new_entry_indices_pos] << CHECK_BITS) | entry.check; + mt_unlock(); + + //ERR_FAIL_COND_V( (uintptr_t)get(retval)%align != 0, retval ); + + return retval; +} + +PoolAllocator::Entry *PoolAllocator::get_entry(ID p_mem) { + unsigned int check = p_mem & CHECK_MASK; + int entry = p_mem >> CHECK_BITS; + ERR_FAIL_INDEX_V(entry, entry_max, nullptr); + ERR_FAIL_COND_V(entry_array[entry].check != check, nullptr); + ERR_FAIL_COND_V(entry_array[entry].len == 0, nullptr); + + return &entry_array[entry]; +} + +const PoolAllocator::Entry *PoolAllocator::get_entry(ID p_mem) const { + unsigned int check = p_mem & CHECK_MASK; + int entry = p_mem >> CHECK_BITS; + ERR_FAIL_INDEX_V(entry, entry_max, nullptr); + ERR_FAIL_COND_V(entry_array[entry].check != check, nullptr); + ERR_FAIL_COND_V(entry_array[entry].len == 0, nullptr); + + return &entry_array[entry]; +} + +void PoolAllocator::free(ID p_mem) { + mt_lock(); + Entry *e = get_entry(p_mem); + if (!e) { + mt_unlock(); + ERR_PRINT("!e"); + return; + } + if (e->lock) { + mt_unlock(); + ERR_PRINT("e->lock"); + return; + } + + EntryIndicesPos entry_indices_pos; + + bool index_found = find_entry_index(&entry_indices_pos, e); + if (!index_found) { + mt_unlock(); + ERR_FAIL_COND(!index_found); + } + + for (int i = entry_indices_pos; i < (entry_count - 1); i++) { + entry_indices[i] = entry_indices[i + 1]; + } + + entry_count--; + free_mem += aligned(e->len); + e->clear(); + mt_unlock(); +} + +int PoolAllocator::get_size(ID p_mem) const { + int size; + mt_lock(); + + const Entry *e = get_entry(p_mem); + if (!e) { + mt_unlock(); + ERR_PRINT("!e"); + return 0; + } + + size = e->len; + + mt_unlock(); + + return size; +} + +Error PoolAllocator::resize(ID p_mem, int p_new_size) { + mt_lock(); + Entry *e = get_entry(p_mem); + + if (!e) { + mt_unlock(); + ERR_FAIL_COND_V(!e, ERR_INVALID_PARAMETER); + } + + if (needs_locking && e->lock) { + mt_unlock(); + ERR_FAIL_COND_V(e->lock, ERR_ALREADY_IN_USE); + } + + uint32_t alloc_size = aligned(p_new_size); + + if ((uint32_t)aligned(e->len) == alloc_size) { + e->len = p_new_size; + mt_unlock(); + return OK; + } else if (e->len > (uint32_t)p_new_size) { + free_mem += aligned(e->len); + free_mem -= alloc_size; + e->len = p_new_size; + mt_unlock(); + return OK; + } + + //p_new_size = align(p_new_size) + int _free = free_mem; // - static_area_size; + + if (uint32_t(_free + aligned(e->len)) < alloc_size) { + mt_unlock(); + ERR_FAIL_V(ERR_OUT_OF_MEMORY); + } + + EntryIndicesPos entry_indices_pos; + + bool index_found = find_entry_index(&entry_indices_pos, e); + + if (!index_found) { + mt_unlock(); + ERR_FAIL_COND_V(!index_found, ERR_BUG); + } + + //no need to move stuff around, it fits before the next block + uint32_t next_pos; + if (entry_indices_pos + 1 == entry_count) { + next_pos = pool_size; // - static_area_size; + } else { + next_pos = entry_array[entry_indices[entry_indices_pos + 1]].pos; + } + + if ((next_pos - e->pos) > alloc_size) { + free_mem += aligned(e->len); + e->len = p_new_size; + free_mem -= alloc_size; + mt_unlock(); + return OK; + } + //it doesn't fit, compact around BEFORE current index (make room behind) + + compact(entry_indices_pos + 1); + + if ((next_pos - e->pos) > alloc_size) { + //now fits! hooray! + free_mem += aligned(e->len); + e->len = p_new_size; + free_mem -= alloc_size; + mt_unlock(); + if (free_mem < free_mem_peak) { + free_mem_peak = free_mem; + } + return OK; + } + + //STILL doesn't fit, compact around AFTER current index (make room after) + + compact_up(entry_indices_pos + 1); + + if ((entry_array[entry_indices[entry_indices_pos + 1]].pos - e->pos) > alloc_size) { + //now fits! hooray! + free_mem += aligned(e->len); + e->len = p_new_size; + free_mem -= alloc_size; + mt_unlock(); + if (free_mem < free_mem_peak) { + free_mem_peak = free_mem; + } + return OK; + } + + mt_unlock(); + ERR_FAIL_V(ERR_OUT_OF_MEMORY); +} + +Error PoolAllocator::lock(ID p_mem) { + if (!needs_locking) { + return OK; + } + mt_lock(); + Entry *e = get_entry(p_mem); + if (!e) { + mt_unlock(); + ERR_PRINT("!e"); + return ERR_INVALID_PARAMETER; + } + e->lock++; + mt_unlock(); + return OK; +} + +bool PoolAllocator::is_locked(ID p_mem) const { + if (!needs_locking) { + return false; + } + + mt_lock(); + const Entry *e = ((PoolAllocator *)(this))->get_entry(p_mem); + if (!e) { + mt_unlock(); + ERR_PRINT("!e"); + return false; + } + bool locked = e->lock; + mt_unlock(); + return locked; +} + +const void *PoolAllocator::get(ID p_mem) const { + if (!needs_locking) { + const Entry *e = get_entry(p_mem); + ERR_FAIL_COND_V(!e, nullptr); + return &pool[e->pos]; + } + + mt_lock(); + const Entry *e = get_entry(p_mem); + + if (!e) { + mt_unlock(); + ERR_FAIL_COND_V(!e, nullptr); + } + if (e->lock == 0) { + mt_unlock(); + ERR_PRINT("e->lock == 0"); + return nullptr; + } + + if ((int)e->pos >= pool_size) { + mt_unlock(); + ERR_PRINT("e->pos<0 || e->pos>=pool_size"); + return nullptr; + } + const void *ptr = &pool[e->pos]; + + mt_unlock(); + + return ptr; +} + +void *PoolAllocator::get(ID p_mem) { + if (!needs_locking) { + Entry *e = get_entry(p_mem); + ERR_FAIL_COND_V(!e, nullptr); + return &pool[e->pos]; + } + + mt_lock(); + Entry *e = get_entry(p_mem); + + if (!e) { + mt_unlock(); + ERR_FAIL_COND_V(!e, nullptr); + } + if (e->lock == 0) { + //assert(0); + mt_unlock(); + ERR_PRINT("e->lock == 0"); + return nullptr; + } + + if ((int)e->pos >= pool_size) { + mt_unlock(); + ERR_PRINT("e->pos<0 || e->pos>=pool_size"); + return nullptr; + } + void *ptr = &pool[e->pos]; + + mt_unlock(); + + return ptr; +} + +void PoolAllocator::unlock(ID p_mem) { + if (!needs_locking) { + return; + } + mt_lock(); + Entry *e = get_entry(p_mem); + if (!e) { + mt_unlock(); + ERR_FAIL_COND(!e); + } + if (e->lock == 0) { + mt_unlock(); + ERR_PRINT("e->lock == 0"); + return; + } + e->lock--; + mt_unlock(); +} + +int PoolAllocator::get_used_mem() const { + return pool_size - free_mem; +} + +int PoolAllocator::get_free_peak() { + return free_mem_peak; +} + +int PoolAllocator::get_free_mem() { + return free_mem; +} + +void PoolAllocator::create_pool(void *p_mem, int p_size, int p_max_entries) { + pool = (uint8_t *)p_mem; + pool_size = p_size; + + entry_array = memnew_arr(Entry, p_max_entries); + entry_indices = memnew_arr(int, p_max_entries); + entry_max = p_max_entries; + entry_count = 0; + + free_mem = p_size; + free_mem_peak = p_size; + + check_count = 0; +} + +PoolAllocator::PoolAllocator(int p_size, bool p_needs_locking, int p_max_entries) { + mem_ptr = memalloc(p_size); + ERR_FAIL_COND(!mem_ptr); + align = 1; + create_pool(mem_ptr, p_size, p_max_entries); + needs_locking = p_needs_locking; +} + +PoolAllocator::PoolAllocator(void *p_mem, int p_size, int p_align, bool p_needs_locking, int p_max_entries) { + if (p_align > 1) { + uint8_t *mem8 = (uint8_t *)p_mem; + uint64_t ofs = (uint64_t)mem8; + if (ofs % p_align) { + int dif = p_align - (ofs % p_align); + mem8 += p_align - (ofs % p_align); + p_size -= dif; + p_mem = (void *)mem8; + } + } + + create_pool(p_mem, p_size, p_max_entries); + needs_locking = p_needs_locking; + align = p_align; + mem_ptr = nullptr; +} + +PoolAllocator::PoolAllocator(int p_align, int p_size, bool p_needs_locking, int p_max_entries) { + ERR_FAIL_COND(p_align < 1); + mem_ptr = Memory::alloc_static(p_size + p_align, true); + uint8_t *mem8 = (uint8_t *)mem_ptr; + uint64_t ofs = (uint64_t)mem8; + if (ofs % p_align) { + mem8 += p_align - (ofs % p_align); + } + create_pool(mem8, p_size, p_max_entries); + needs_locking = p_needs_locking; + align = p_align; +} + +PoolAllocator::~PoolAllocator() { + if (mem_ptr) { + memfree(mem_ptr); + } + + memdelete_arr(entry_array); + memdelete_arr(entry_indices); +} diff --git a/core/os/pool_allocator.h b/core/os/pool_allocator.h new file mode 100644 index 0000000000..15e50dac90 --- /dev/null +++ b/core/os/pool_allocator.h @@ -0,0 +1,149 @@ +/*************************************************************************/ +/* pool_allocator.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 POOL_ALLOCATOR_H +#define POOL_ALLOCATOR_H + +#include "core/typedefs.h" + +/** + @author Juan Linietsky <reduzio@gmail.com> + * Generic Pool Allocator. + * This is a generic memory pool allocator, with locking, compacting and alignment. (@TODO alignment) + * It used as a standard way to manage alloction in a specific region of memory, such as texture memory, + * audio sample memory, or just any kind of memory overall. + * (@TODO) abstraction should be greater, because in many platforms, you need to manage a nonreachable memory. +*/ + +enum { + POOL_ALLOCATOR_INVALID_ID = -1 ///< default invalid value. use INVALID_ID( id ) to test +}; + +class PoolAllocator { +public: + typedef int ID; + +private: + enum { + CHECK_BITS = 8, + CHECK_LEN = (1 << CHECK_BITS), + CHECK_MASK = CHECK_LEN - 1 + + }; + + struct Entry { + unsigned int pos = 0; + unsigned int len = 0; + unsigned int lock = 0; + unsigned int check = 0; + + inline void clear() { + pos = 0; + len = 0; + lock = 0; + check = 0; + } + Entry() {} + }; + + typedef int EntryArrayPos; + typedef int EntryIndicesPos; + + Entry *entry_array; + int *entry_indices; + int entry_max; + int entry_count; + + uint8_t *pool; + void *mem_ptr; + int pool_size; + + int free_mem; + int free_mem_peak; + + unsigned int check_count; + int align; + + bool needs_locking; + + inline int entry_end(const Entry &p_entry) const { + return p_entry.pos + aligned(p_entry.len); + } + inline int aligned(int p_size) const { + int rem = p_size % align; + if (rem) { + p_size += align - rem; + } + + return p_size; + } + + void compact(int p_up_to = -1); + void compact_up(int p_from = 0); + bool get_free_entry(EntryArrayPos *p_pos); + bool find_hole(EntryArrayPos *p_pos, int p_for_size); + bool find_entry_index(EntryIndicesPos *p_map_pos, Entry *p_entry); + Entry *get_entry(ID p_mem); + const Entry *get_entry(ID p_mem) const; + + void create_pool(void *p_mem, int p_size, int p_max_entries); + +protected: + virtual void mt_lock() const; ///< Reimplement for custom mt locking + virtual void mt_unlock() const; ///< Reimplement for custom mt locking + +public: + enum { + DEFAULT_MAX_ALLOCS = 4096, + }; + + ID alloc(int p_size); ///< Alloc memory, get an ID on success, POOL_ALOCATOR_INVALID_ID on failure + void free(ID p_mem); ///< Free allocated memory + Error resize(ID p_mem, int p_new_size); ///< resize a memory chunk + int get_size(ID p_mem) const; + + int get_free_mem(); ///< get free memory + int get_used_mem() const; + int get_free_peak(); ///< get free memory + + Error lock(ID p_mem); //@todo move this out + void *get(ID p_mem); + const void *get(ID p_mem) const; + void unlock(ID p_mem); + bool is_locked(ID p_mem) const; + + PoolAllocator(int p_size, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS); + PoolAllocator(void *p_mem, int p_size, int p_align = 1, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS); + PoolAllocator(int p_align, int p_size, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS); + + virtual ~PoolAllocator(); +}; + +#endif // POOL_ALLOCATOR_H diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h index 1035072cce..560ec57a5f 100644 --- a/core/os/rw_lock.h +++ b/core/os/rw_lock.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,57 +31,85 @@ #ifndef RW_LOCK_H #define RW_LOCK_H -#include "core/error_list.h" +#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/semaphore.h b/core/os/semaphore.h index 077e04704b..01ae7a3c65 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -31,7 +31,7 @@ #ifndef SEMAPHORE_H #define SEMAPHORE_H -#include "core/error_list.h" +#include "core/error/error_list.h" #include "core/typedefs.h" #if !defined(NO_THREADS) diff --git a/core/os/rw_lock.cpp b/core/os/spin_lock.h index a668fe2b4c..929e8b9a58 100644 --- a/core/os/rw_lock.cpp +++ b/core/os/spin_lock.h @@ -1,12 +1,12 @@ /*************************************************************************/ -/* rw_lock.cpp */ +/* spin_lock.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -28,16 +28,24 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "rw_lock.h" +#ifndef SPIN_LOCK_H +#define SPIN_LOCK_H -#include "core/error_macros.h" +#include "core/typedefs.h" -#include <stddef.h> +#include <atomic> -RWLock *(*RWLock::create_func)() = nullptr; +class SpinLock { + std::atomic_flag locked = ATOMIC_FLAG_INIT; -RWLock *RWLock::create() { - ERR_FAIL_COND_V(!create_func, nullptr); - - return create_func(); -} +public: + _ALWAYS_INLINE_ void lock() { + while (locked.test_and_set(std::memory_order_acquire)) { + ; + } + } + _ALWAYS_INLINE_ void unlock() { + locked.clear(std::memory_order_release); + } +}; +#endif // SPIN_LOCK_H diff --git a/core/os/thread.cpp b/core/os/thread.cpp index fc0ce3c9b4..aea370787d 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,30 +30,73 @@ #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) { + ERR_FAIL_COND_MSG(id == get_caller_id(), "A Thread can't wait for itself to finish."); + thread.join(); + std::thread empty_thread; + thread.swap(empty_thread); + id = 0; } } @@ -64,3 +107,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 f761d4ca43..76f5be182e 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -32,14 +32,21 @@ #define THREAD_H #include "core/typedefs.h" -#include "core/ustring.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: - enum Priority { + typedef void (*Callback)(void *p_userdata); + + typedef uint64_t ID; + enum Priority { PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH @@ -50,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 2672cd7ad9..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-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 37d9ee0846..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-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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/thread_safe.h b/core/os/thread_safe.h index 670ee8b125..81de079bf3 100644 --- a/core/os/thread_safe.h +++ b/core/os/thread_safe.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ diff --git a/core/os/threaded_array_processor.h b/core/os/threaded_array_processor.h index d27399e4cc..4f270001d3 100644 --- a/core/os/threaded_array_processor.h +++ b/core/os/threaded_array_processor.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -35,12 +35,12 @@ #include "core/os/os.h" #include "core/os/thread.h" #include "core/os/thread_safe.h" -#include "core/safe_refcount.h" +#include "core/templates/safe_refcount.h" 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 |