feat: updated engine version to 4.4-rc1

This commit is contained in:
Sara 2025-02-23 14:38:14 +01:00
parent ee00efde1f
commit 21ba8e33af
5459 changed files with 1128836 additions and 198305 deletions

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")

View file

@ -32,6 +32,7 @@
#define CONDITION_VARIABLE_H
#include "core/os/mutex.h"
#include "core/os/safe_binary_mutex.h"
#ifdef THREADS_ENABLED
@ -56,7 +57,12 @@ class ConditionVariable {
public:
template <typename BinaryMutexT>
_ALWAYS_INLINE_ void wait(const MutexLock<BinaryMutexT> &p_lock) const {
condition.wait(const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(p_lock.lock));
condition.wait(p_lock._get_lock());
}
template <int Tag>
_ALWAYS_INLINE_ void wait(const MutexLock<SafeBinaryMutex<Tag>> &p_lock) const {
condition.wait(p_lock.mutex._get_lock());
}
_ALWAYS_INLINE_ void notify_one() const {

View file

@ -249,7 +249,7 @@ enum class Key {
enum class KeyModifierMask {
CODE_MASK = ((1 << 23) - 1), ///< Apply this mask to any keycode to remove modifiers.
MODIFIER_MASK = (0x7F << 22), ///< Apply this mask to isolate modifiers.
MODIFIER_MASK = (0x7F << 24), ///< Apply this mask to isolate modifiers.
//RESERVED = (1 << 23),
CMD_OR_CTRL = (1 << 24),
SHIFT = (1 << 25),

View file

@ -30,8 +30,6 @@
#include "main_loop.h"
#include "core/object/script_language.h"
void MainLoop::_bind_methods() {
BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);

View file

@ -64,6 +64,7 @@ public:
virtual void initialize();
virtual void iteration_prepare() {}
virtual bool physics_process(double p_time);
virtual void iteration_end() {}
virtual bool process(double p_time);
virtual void finalize();

View file

@ -30,11 +30,10 @@
#include "memory.h"
#include "core/error/error_macros.h"
#include "core/templates/safe_refcount.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *operator new(size_t p_size, const char *p_description) {
return Memory::alloc_static(p_size, false);
@ -65,6 +64,38 @@ SafeNumeric<uint64_t> Memory::max_usage;
SafeNumeric<uint64_t> Memory::alloc_count;
void *Memory::alloc_aligned_static(size_t p_bytes, size_t p_alignment) {
DEV_ASSERT(is_power_of_2(p_alignment));
void *p1, *p2;
if ((p1 = (void *)malloc(p_bytes + p_alignment - 1 + sizeof(uint32_t))) == nullptr) {
return nullptr;
}
p2 = (void *)(((uintptr_t)p1 + sizeof(uint32_t) + p_alignment - 1) & ~((p_alignment)-1));
*((uint32_t *)p2 - 1) = (uint32_t)((uintptr_t)p2 - (uintptr_t)p1);
return p2;
}
void *Memory::realloc_aligned_static(void *p_memory, size_t p_bytes, size_t p_prev_bytes, size_t p_alignment) {
if (p_memory == nullptr) {
return alloc_aligned_static(p_bytes, p_alignment);
}
void *ret = alloc_aligned_static(p_bytes, p_alignment);
if (ret) {
memcpy(ret, p_memory, p_prev_bytes);
}
free_aligned_static(p_memory);
return ret;
}
void Memory::free_aligned_static(void *p_memory) {
uint32_t offset = *((uint32_t *)p_memory - 1);
void *p = (void *)((uint8_t *)p_memory - offset);
free(p);
}
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
#ifdef DEBUG_ENABLED
bool prepad = true;

View file

@ -62,6 +62,30 @@ public:
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
static void free_static(void *p_ptr, bool p_pad_align = false);
// ↓ return value of alloc_aligned_static
// ┌─────────────────┬─────────┬─────────┬──────────────────┐
// │ padding (up to │ uint32_t│ void* │ padding (up to │
// │ p_alignment - 1)│ offset │ p_bytes │ p_alignment - 1) │
// └─────────────────┴─────────┴─────────┴──────────────────┘
//
// alloc_aligned_static will allocate p_bytes + p_alignment - 1 + sizeof(uint32_t) and
// then offset the pointer until alignment is satisfied.
//
// This offset is stored before the start of the returned ptr so we can retrieve the original/real
// start of the ptr in order to free it.
//
// The rest is wasted as padding in the beginning and end of the ptr. The sum of padding at
// both start and end of the block must add exactly to p_alignment - 1.
//
// p_alignment MUST be a power of 2.
static void *alloc_aligned_static(size_t p_bytes, size_t p_alignment);
static void *realloc_aligned_static(void *p_memory, size_t p_bytes, size_t p_prev_bytes, size_t p_alignment);
// Pass the ptr returned by alloc_aligned_static to free it.
// e.g.
// void *data = realloc_aligned_static( bytes, 16 );
// free_aligned_static( data );
static void free_aligned_static(void *p_memory);
static uint64_t get_mem_available();
static uint64_t get_mem_usage();
static uint64_t get_mem_max_usage();
@ -98,10 +122,10 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
return p_obj;
}
#define memnew(m_class) _post_initialize(new ("") m_class)
#define memnew(m_class) _post_initialize(::new ("") m_class)
#define memnew_allocator(m_class, m_allocator) _post_initialize(new (m_allocator::alloc) m_class)
#define memnew_placement(m_placement, m_class) _post_initialize(new (m_placement) m_class)
#define memnew_allocator(m_class, m_allocator) _post_initialize(::new (m_allocator::alloc) m_class)
#define memnew_placement(m_placement, m_class) _post_initialize(::new (m_placement) m_class)
_ALWAYS_INLINE_ bool predelete_handler(void *) {
return true;
@ -165,7 +189,7 @@ T *memnew_arr_template(size_t p_elements) {
/* call operator new */
for (size_t i = 0; i < p_elements; i++) {
new (&elems[i]) T;
::new (&elems[i]) T;
}
}

View file

@ -31,7 +31,6 @@
#ifndef MUTEX_H
#define MUTEX_H
#include "core/error/error_macros.h"
#include "core/typedefs.h"
#ifdef MINGW_ENABLED
@ -72,13 +71,28 @@ public:
template <typename MutexT>
class MutexLock {
friend class ConditionVariable;
THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;
mutable THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;
public:
explicit MutexLock(const MutexT &p_mutex) :
lock(p_mutex.mutex) {}
// Clarification: all the funny syntax is needed so this function exists only for binary mutexes.
template <typename T = MutexT>
_ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock(
typename std::enable_if<std::is_same<T, THREADING_NAMESPACE::mutex>::value> * = nullptr) const {
return lock;
}
_ALWAYS_INLINE_ void temp_relock() const {
lock.lock();
}
_ALWAYS_INLINE_ void temp_unlock() const {
lock.unlock();
}
// TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
};
using Mutex = MutexImpl<THREADING_NAMESPACE::recursive_mutex>; // Recursive, for general use
@ -104,6 +118,9 @@ template <typename MutexT>
class MutexLock {
public:
MutexLock(const MutexT &p_mutex) {}
void temp_relock() const {}
void temp_unlock() const {}
};
using Mutex = MutexImpl;

View file

@ -31,7 +31,6 @@
#include "os.h"
#include "core/config/project_settings.h"
#include "core/input/input.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/io/json.h"
@ -223,6 +222,23 @@ uint64_t OS::get_embedded_pck_offset() const {
return 0;
}
// Default boot screen rect scale mode is "Keep Aspect Centered"
Rect2 OS::calculate_boot_screen_rect(const Size2 &p_window_size, const Size2 &p_imgrect_size) const {
Rect2 screenrect;
if (p_window_size.width > p_window_size.height) {
// Scale horizontally.
screenrect.size.y = p_window_size.height;
screenrect.size.x = p_imgrect_size.x * p_window_size.height / p_imgrect_size.y;
screenrect.position.x = (p_window_size.width - screenrect.size.x) / 2;
} else {
// Scale vertically.
screenrect.size.x = p_window_size.width;
screenrect.size.y = p_imgrect_size.y * p_window_size.width / p_imgrect_size.x;
screenrect.position.y = (p_window_size.height - screenrect.size.y) / 2;
}
return screenrect;
}
// Helper function to ensure that a dir name/path will be valid on the OS
String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_paths) const {
String safe_dir_name = p_dir_name;
@ -276,6 +292,10 @@ String OS::get_cache_path() const {
return ".";
}
String OS::get_temp_path() const {
return ".";
}
// Path to macOS .app bundle resources
String OS::get_bundle_resource_dir() const {
return ".";
@ -287,10 +307,28 @@ String OS::get_bundle_icon_path() const {
}
// OS specific path for user://
String OS::get_user_data_dir() const {
String OS::get_user_data_dir(const String &p_user_dir) const {
return ".";
}
String OS::get_user_data_dir() const {
String appname = get_safe_dir_name(GLOBAL_GET("application/config/name"));
if (!appname.is_empty()) {
bool use_custom_dir = GLOBAL_GET("application/config/use_custom_user_dir");
if (use_custom_dir) {
String custom_dir = get_safe_dir_name(GLOBAL_GET("application/config/custom_user_dir_name"), true);
if (custom_dir.is_empty()) {
custom_dir = appname;
}
return get_user_data_dir(custom_dir);
} else {
return get_user_data_dir(get_godot_dir_name().path_join("app_userdata").path_join(appname));
}
} else {
return get_user_data_dir(get_godot_dir_name().path_join("app_userdata").path_join("[unnamed project]"));
}
}
// Absolute path to res://
String OS::get_resource_dir() const {
return ProjectSettings::get_singleton()->get_resource_path();
@ -301,6 +339,23 @@ String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
return ".";
}
void OS::create_lock_file() {
if (Engine::get_singleton()->is_recovery_mode_hint()) {
return;
}
String lock_file_path = get_user_data_dir().path_join(".recovery_mode_lock");
Ref<FileAccess> lock_file = FileAccess::open(lock_file_path, FileAccess::WRITE);
if (lock_file.is_valid()) {
lock_file->close();
}
}
void OS::remove_lock_file() {
String lock_file_path = get_user_data_dir().path_join(".recovery_mode_lock");
DirAccess::remove_absolute(lock_file_path);
}
Error OS::shell_open(const String &p_uri) {
return ERR_UNAVAILABLE;
}
@ -352,7 +407,7 @@ void OS::ensure_user_data_dir() {
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = da->make_dir_recursive(dd);
ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + ".");
ERR_FAIL_COND_MSG(err != OK, vformat("Error attempting to create data dir: %s.", dd));
}
String OS::get_model_name() const {
@ -405,6 +460,8 @@ bool OS::has_feature(const String &p_feature) {
return _in_editor;
} else if (p_feature == "editor_runtime") {
return !_in_editor;
} else if (p_feature == "embedded_in_editor") {
return _embedded_in_editor;
}
#else
if (p_feature == "template") {
@ -439,6 +496,11 @@ bool OS::has_feature(const String &p_feature) {
}
#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
#if defined(MACOS_ENABLED)
if (p_feature == "universal") {
return true;
}
#endif
if (p_feature == "x86_64") {
return true;
}
@ -452,6 +514,11 @@ bool OS::has_feature(const String &p_feature) {
}
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
#if defined(__aarch64__) || defined(_M_ARM64)
#if defined(MACOS_ENABLED)
if (p_feature == "universal") {
return true;
}
#endif
if (p_feature == "arm64") {
return true;
}
@ -504,6 +571,10 @@ bool OS::has_feature(const String &p_feature) {
if (p_feature == "wasm") {
return true;
}
#elif defined(__loongarch64)
if (p_feature == "loongarch64") {
return true;
}
#endif
#if defined(IOS_SIMULATOR)

View file

@ -32,7 +32,6 @@
#define OS_H
#include "core/config/engine.h"
#include "core/io/image.h"
#include "core/io/logger.h"
#include "core/io/remote_filesystem_client.h"
#include "core/os/time_enums.h"
@ -40,7 +39,6 @@
#include "core/templates/list.h"
#include "core/templates/vector.h"
#include <stdarg.h>
#include <stdlib.h>
class OS {
@ -64,17 +62,16 @@ class OS {
bool _stderr_enabled = true;
bool _writing_movie = false;
bool _in_editor = false;
bool _embedded_in_editor = false;
CompositeLogger *_logger = nullptr;
bool restart_on_exit = false;
List<String> restart_commandline;
// for the user interface we keep a record of the current display driver
// so we can retrieve the rendering drivers available
int _display_driver_id = -1;
String _current_rendering_driver_name;
String _current_rendering_method;
bool _is_gles_over_gl = false;
RemoteFilesystemClient default_rfs;
@ -94,7 +91,15 @@ public:
enum RenderThreadMode {
RENDER_THREAD_UNSAFE,
RENDER_THREAD_SAFE,
RENDER_SEPARATE_THREAD
RENDER_SEPARATE_THREAD,
};
enum StdHandleType {
STD_HANDLE_INVALID,
STD_HANDLE_CONSOLE,
STD_HANDLE_FILE,
STD_HANDLE_PIPE,
STD_HANDLE_UNKNOWN,
};
protected:
@ -103,7 +108,7 @@ protected:
friend int test_main(int argc, char *argv[]);
HasServerFeatureCallback has_server_feature_callback = nullptr;
RenderThreadMode _render_thread_mode = RENDER_THREAD_SAFE;
bool _separate_thread_render = false;
// Functions used by Main to initialize/deinitialize the OS.
void add_logger(Logger *p_logger);
@ -111,11 +116,6 @@ protected:
virtual void initialize() = 0;
virtual void initialize_joypads() = 0;
void set_current_rendering_driver_name(const String &p_driver_name) { _current_rendering_driver_name = p_driver_name; }
void set_current_rendering_method(const 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;
virtual void delete_main_loop() = 0;
@ -131,19 +131,28 @@ public:
static OS *get_singleton();
void set_current_rendering_driver_name(const String &p_driver_name) { _current_rendering_driver_name = p_driver_name; }
void set_current_rendering_method(const String &p_name) { _current_rendering_method = p_name; }
void set_gles_over_gl(bool p_enabled) { _is_gles_over_gl = p_enabled; }
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; }
bool get_gles_over_gl() const { return _is_gles_over_gl; }
virtual Vector<String> get_video_adapter_driver_info() const = 0;
virtual bool get_user_prefers_integrated_gpu() const { return false; }
void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify = false, Logger::ErrorType p_type = Logger::ERR_ERROR);
void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
void print_rich(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
virtual String get_stdin_string() = 0;
virtual String get_stdin_string(int64_t p_buffer_size = 1024) = 0;
virtual PackedByteArray get_stdin_buffer(int64_t p_buffer_size = 1024) = 0;
virtual StdHandleType get_stdin_type() const { return STD_HANDLE_UNKNOWN; }
virtual StdHandleType get_stdout_type() const { return STD_HANDLE_UNKNOWN; }
virtual StdHandleType get_stderr_type() const { return STD_HANDLE_UNKNOWN; }
virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) = 0; // Should return cryptographically-safe random bytes.
virtual String get_system_ca_certificates() { return ""; } // Concatenated certificates in PEM format.
@ -152,6 +161,8 @@ public:
virtual void open_midi_inputs();
virtual void close_midi_inputs();
virtual Rect2 calculate_boot_screen_rect(const Size2 &p_window_size, const Size2 &p_imgrect_size) const;
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
struct GDExtensionData {
@ -173,14 +184,14 @@ public:
void set_delta_smoothing(bool p_enabled);
bool is_delta_smoothing_enabled() const;
virtual Vector<String> get_system_fonts() const { return Vector<String>(); };
virtual String get_system_font_path(const String &p_font_name, int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return String(); };
virtual Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return Vector<String>(); };
virtual Vector<String> get_system_fonts() const { return Vector<String>(); }
virtual String get_system_font_path(const String &p_font_name, int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return String(); }
virtual Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const { return Vector<String>(); }
virtual String get_executable_path() const;
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, bool p_open_console = false) = 0;
virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments) { return Dictionary(); }
virtual Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments, bool p_blocking = true) { return Dictionary(); }
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) = 0;
virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); };
virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); }
virtual Error kill(const ProcessID &p_pid) = 0;
virtual int get_process_id() const;
virtual bool is_process_running(const ProcessID &p_pid) const = 0;
@ -200,6 +211,7 @@ public:
virtual String get_identifier() const;
virtual String get_distribution_name() const = 0;
virtual String get_version() const = 0;
virtual String get_version_alias() const { return get_version(); }
virtual List<String> get_cmdline_args() const { return _cmdline; }
virtual List<String> get_cmdline_user_args() const { return _user_args; }
virtual List<String> get_cmdline_platform_args() const { return List<String>(); }
@ -258,7 +270,7 @@ public:
virtual uint64_t get_static_memory_peak_usage() const;
virtual Dictionary get_memory_info() const;
RenderThreadMode get_render_thread_mode() const { return _render_thread_mode; }
bool is_separate_thread_rendering_enabled() const { return _separate_thread_render; }
virtual String get_locale() const;
String get_locale_language() const;
@ -271,9 +283,11 @@ public:
virtual String get_data_path() const;
virtual String get_config_path() const;
virtual String get_cache_path() const;
virtual String get_temp_path() const;
virtual String get_bundle_resource_dir() const;
virtual String get_bundle_icon_path() const;
virtual String get_user_data_dir(const String &p_user_dir) const;
virtual String get_user_data_dir() const;
virtual String get_resource_dir() const;
@ -292,6 +306,9 @@ public:
virtual Error move_to_trash(const String &p_path) { return FAILED; }
void create_lock_file();
void remove_lock_file();
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`).

View file

@ -1,588 +0,0 @@
/**************************************************************************/
/* pool_allocator.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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/memory.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#define COMPACT_CHUNK(m_entry, m_to_pos) \
if constexpr (true) { \
void *_dst = &((unsigned char *)pool)[m_to_pos]; \
void *_src = &((unsigned char *)pool)[(m_entry).pos]; \
memmove(_dst, _src, aligned((m_entry).len)); \
(m_entry).pos = m_to_pos; \
} else \
((void)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 for next 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, const 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);
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_NULL_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 = const_cast<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_NULL_V(e, nullptr);
return &pool[e->pos];
}
mt_lock();
const Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_FAIL_NULL_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_NULL_V(e, nullptr);
return &pool[e->pos];
}
mt_lock();
Entry *e = get_entry(p_mem);
if (!e) {
mt_unlock();
ERR_FAIL_NULL_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;
}
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_NULL(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_NULL(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);
}

View file

@ -1,148 +0,0 @@
/**************************************************************************/
/* pool_allocator.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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"
/**
* 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 allocation 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 = nullptr;
int *entry_indices = nullptr;
int entry_max = 0;
int entry_count = 0;
uint8_t *pool = nullptr;
void *mem_ptr = nullptr;
int pool_size = 0;
int free_mem = 0;
int free_mem_peak = 0;
unsigned int check_count = 0;
int align = 1;
bool needs_locking = false;
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, const 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

View file

@ -37,6 +37,11 @@
#ifdef THREADS_ENABLED
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundefined-var-template"
#endif
// A very special kind of mutex, used in scenarios where these
// requirements hold at the same time:
// - Must be used with a condition variable (only binary mutexes are suitable).
@ -47,69 +52,90 @@
// Also, don't forget to declare the thread_local variable on each use.
template <int Tag>
class SafeBinaryMutex {
friend class MutexLock<SafeBinaryMutex>;
friend class MutexLock<SafeBinaryMutex<Tag>>;
using StdMutexType = THREADING_NAMESPACE::mutex;
mutable THREADING_NAMESPACE::mutex mutex;
static thread_local uint32_t count;
struct TLSData {
mutable THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
uint32_t count = 0;
TLSData(SafeBinaryMutex<Tag> &p_mutex) :
lock(p_mutex.mutex, THREADING_NAMESPACE::defer_lock) {}
};
static thread_local TLSData tls_data;
public:
_ALWAYS_INLINE_ void lock() const {
if (++count == 1) {
mutex.lock();
if (++tls_data.count == 1) {
tls_data.lock.lock();
}
}
_ALWAYS_INLINE_ void unlock() const {
DEV_ASSERT(count);
if (--count == 0) {
mutex.unlock();
DEV_ASSERT(tls_data.count);
if (--tls_data.count == 0) {
tls_data.lock.unlock();
}
}
_ALWAYS_INLINE_ bool try_lock() const {
if (count) {
count++;
return true;
} else {
if (mutex.try_lock()) {
count++;
return true;
} else {
return false;
}
}
_ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock() const {
return const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(tls_data.lock);
}
~SafeBinaryMutex() {
DEV_ASSERT(!count);
_ALWAYS_INLINE_ SafeBinaryMutex() {
}
_ALWAYS_INLINE_ ~SafeBinaryMutex() {
DEV_ASSERT(!tls_data.count);
}
};
// This specialization is needed so manual locking and MutexLock can be used
// at the same time on a SafeBinaryMutex.
template <int Tag>
class MutexLock<SafeBinaryMutex<Tag>> {
friend class ConditionVariable;
THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
const SafeBinaryMutex<Tag> &mutex;
public:
_ALWAYS_INLINE_ explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
lock(p_mutex.mutex) {
SafeBinaryMutex<Tag>::count++;
};
_ALWAYS_INLINE_ ~MutexLock() {
SafeBinaryMutex<Tag>::count--;
};
explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
mutex(p_mutex) {
mutex.lock();
}
~MutexLock() {
mutex.unlock();
}
_ALWAYS_INLINE_ void temp_relock() const {
mutex.lock();
}
_ALWAYS_INLINE_ void temp_unlock() const {
mutex.unlock();
}
// TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#else // No threads.
template <int Tag>
class SafeBinaryMutex : public MutexImpl {
static thread_local uint32_t count;
class SafeBinaryMutex {
struct TLSData {
TLSData(SafeBinaryMutex<Tag> &p_mutex) {}
};
static thread_local TLSData tls_data;
public:
void lock() const {}
void unlock() const {}
};
template <int Tag>
@ -117,6 +143,9 @@ class MutexLock<SafeBinaryMutex<Tag>> {
public:
MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {}
~MutexLock() {}
void temp_relock() const {}
void temp_unlock() const {}
};
#endif // THREADS_ENABLED

View file

@ -31,14 +31,28 @@
#ifndef SPIN_LOCK_H
#define SPIN_LOCK_H
#include "core/os/thread.h"
#include "core/typedefs.h"
#ifdef THREADS_ENABLED
// Note the implementations below avoid false sharing by ensuring their
// sizes match the assumed cache line. We can't use align attributes
// because these objects may end up unaligned in semi-tightly packed arrays.
#ifdef _MSC_VER
#include <intrin.h>
#endif
#if defined(__APPLE__)
#include <os/lock.h>
class SpinLock {
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
union {
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
char aligner[Thread::CACHE_LINE_BYTES];
};
public:
_ALWAYS_INLINE_ void lock() const {
@ -50,24 +64,68 @@ public:
}
};
#else
#else // __APPLE__
#include <atomic>
_ALWAYS_INLINE_ static void _cpu_pause() {
#if defined(_MSC_VER)
// ----- MSVC.
#if defined(_M_ARM) || defined(_M_ARM64) // ARM.
__yield();
#elif defined(_M_IX86) || defined(_M_X64) // x86.
_mm_pause();
#endif
#elif defined(__GNUC__) || defined(__clang__)
// ----- GCC/Clang.
#if defined(__i386__) || defined(__x86_64__) // x86.
__builtin_ia32_pause();
#elif defined(__arm__) || defined(__aarch64__) // ARM.
asm volatile("yield");
#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) // PowerPC.
asm volatile("or 27,27,27");
#elif defined(__riscv) // RISC-V.
asm volatile(".insn i 0x0F, 0, x0, x0, 0x010");
#endif
#endif
}
static_assert(std::atomic_bool::is_always_lock_free);
class SpinLock {
mutable std::atomic_flag locked = ATOMIC_FLAG_INIT;
union {
mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
char aligner[Thread::CACHE_LINE_BYTES];
};
public:
_ALWAYS_INLINE_ void lock() const {
while (locked.test_and_set(std::memory_order_acquire)) {
// Continue.
while (true) {
bool expected = false;
if (locked.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
break;
}
do {
_cpu_pause();
} while (locked.load(std::memory_order_relaxed));
}
}
_ALWAYS_INLINE_ void unlock() const {
locked.clear(std::memory_order_release);
locked.store(false, std::memory_order_release);
}
};
#endif // __APPLE__
#else // THREADS_ENABLED
class SpinLock {
public:
void lock() const {}
void unlock() const {}
};
#endif // THREADS_ENABLED
#endif // SPIN_LOCK_H

View file

@ -29,17 +29,17 @@
/**************************************************************************/
#include "platform_config.h"
#ifndef PLATFORM_THREAD_OVERRIDE // See details in thread.h
#ifndef PLATFORM_THREAD_OVERRIDE // See details in thread.h.
#include "thread.h"
#ifdef THREADS_ENABLED
#include "core/object/script_language.h"
#include "core/templates/safe_refcount.h"
SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1.
thread_local Thread::ID Thread::caller_id = Thread::id_counter.increment();
thread_local Thread::ID Thread::caller_id = Thread::UNASSIGNED_ID;
#endif
Thread::PlatformFunctions Thread::platform_functions;

View file

@ -29,19 +29,28 @@
/**************************************************************************/
#include "platform_config.h"
// Define PLATFORM_THREAD_OVERRIDE in your platform's `platform_config.h`
// to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h`
// Overriding the platform implementation is required in some proprietary platforms
// to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h`.
// Overriding the Thread implementation is required in some proprietary platforms.
#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"
#ifdef THREADS_ENABLED
#include "core/templates/safe_refcount.h"
#include <new> // IWYU pragma: keep // For hardware interference size.
#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
@ -53,8 +62,6 @@
class String;
#ifdef THREADS_ENABLED
class Thread {
public:
typedef void (*Callback)(void *p_userdata);
@ -85,12 +92,27 @@ public:
void (*term)() = nullptr;
};
#if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED) // This would be OK with NDK >= 26.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winterference-size"
#endif
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#else
// At a negligible memory cost, we use a conservatively high value.
static constexpr size_t CACHE_LINE_BYTES = 128;
#endif
private:
friend class Main;
static PlatformFunctions platform_functions;
ID id = UNASSIGNED_ID;
static SafeNumeric<uint64_t> id_counter;
static thread_local ID caller_id;
THREADING_NAMESPACE::thread thread;
@ -98,7 +120,7 @@ private:
static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
static void make_main_thread() { caller_id = MAIN_ID; }
static void release_main_thread() { caller_id = UNASSIGNED_ID; }
static void release_main_thread() { caller_id = id_counter.increment(); }
public:
static void _set_platform_functions(const PlatformFunctions &p_functions);
@ -106,9 +128,6 @@ public:
_FORCE_INLINE_ ID get_id() const { return id; }
// get the ID of the caller thread
_FORCE_INLINE_ static ID get_caller_id() {
if (unlikely(caller_id == UNASSIGNED_ID)) {
caller_id = id_counter.increment();
}
return caller_id;
}
// get the ID of the main thread
@ -129,12 +148,16 @@ public:
#else // No threads.
class String;
class Thread {
public:
typedef void (*Callback)(void *p_userdata);
typedef uint64_t ID;
static constexpr size_t CACHE_LINE_BYTES = sizeof(void *);
enum : ID {
UNASSIGNED_ID = 0,
MAIN_ID = 1

View file

@ -31,7 +31,7 @@
#ifndef THREAD_SAFE_H
#define THREAD_SAFE_H
#include "core/os/mutex.h"
#include "core/os/mutex.h" // IWYU pragma: keep // Used in macro.
#define _THREAD_SAFE_CLASS_ mutable Mutex _thread_safe_;
#define _THREAD_SAFE_METHOD_ MutexLock _thread_safe_method_(_thread_safe_);