From c4992641447dac06a26eebb2ddb4ae1d544d7538 Mon Sep 17 00:00:00 2001 From: Louis Poirier Date: Thu, 5 Nov 2026 19:09:13 +0100 Subject: [PATCH 1/2] refactor: use recent form to check for build with threads enabled --- include/godot_cpp/templates/safe_refcount.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/godot_cpp/templates/safe_refcount.hpp b/include/godot_cpp/templates/safe_refcount.hpp index 693694878..dac062b4f 100644 --- a/include/godot_cpp/templates/safe_refcount.hpp +++ b/include/godot_cpp/templates/safe_refcount.hpp @@ -30,7 +30,7 @@ #pragma once -#if !defined(NO_THREADS) +#ifdef THREADS_ENABLED #include #include @@ -223,4 +223,4 @@ class SafeRefCount { } // namespace godot -#endif // !defined(NO_THREADS) +#endif // THREADS_ENABLED From c3aa69ff250c7492510944d0ea5adbcd4fcbd600 Mon Sep 17 00:00:00 2001 From: Louis Poirier Date: Thu, 5 Nov 2026 19:13:39 +0100 Subject: [PATCH 2/2] feat: add missing core containers/synchronization types --- .../templates/condition_variable.hpp | 90 +++++++ include/godot_cpp/templates/fixed_vector.hpp | 213 +++++++++++++++++ include/godot_cpp/templates/lru.hpp | 154 ++++++++++++ include/godot_cpp/templates/ring_buffer.hpp | 220 ++++++++++++++++++ include/godot_cpp/templates/rw_lock.hpp | 132 +++++++++++ .../godot_cpp/templates/safe_binary_mutex.hpp | 145 ++++++++++++ include/godot_cpp/templates/tuple.hpp | 126 ++++++++++ 7 files changed, 1080 insertions(+) create mode 100644 include/godot_cpp/templates/condition_variable.hpp create mode 100644 include/godot_cpp/templates/fixed_vector.hpp create mode 100644 include/godot_cpp/templates/lru.hpp create mode 100644 include/godot_cpp/templates/ring_buffer.hpp create mode 100644 include/godot_cpp/templates/rw_lock.hpp create mode 100644 include/godot_cpp/templates/safe_binary_mutex.hpp create mode 100644 include/godot_cpp/templates/tuple.hpp diff --git a/include/godot_cpp/templates/condition_variable.hpp b/include/godot_cpp/templates/condition_variable.hpp new file mode 100644 index 000000000..1a6c9f219 --- /dev/null +++ b/include/godot_cpp/templates/condition_variable.hpp @@ -0,0 +1,90 @@ +/**************************************************************************/ +/* condition_variable.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include +#include + +#ifdef MINGW_ENABLED +#define MINGW_STDTHREAD_REDUNDANCY_WARNING +#include +#define THREADING_NAMESPACE mingw_stdthread +#else +#include +#define THREADING_NAMESPACE std +#endif + +namespace godot { + +#ifdef THREADS_ENABLED + +// An object one or multiple threads can wait on a be notified by some other. +// Normally, you want to use a semaphore for such scenarios, but when the +// condition is something different than a count being greater than zero +// (which is the built-in logic in a semaphore) or you want to provide your +// own mutex to tie the wait-notify to some other behavior, you need to use this. + +class ConditionVariable { + mutable THREADING_NAMESPACE::condition_variable condition; + +public: + template + _ALWAYS_INLINE_ void wait(const MutexLock &p_lock) const { + condition.wait(p_lock._get_lock()); + } + + template + _ALWAYS_INLINE_ void wait(const MutexLock> &p_lock) const { + condition.wait(p_lock.mutex._get_lock()); + } + + _ALWAYS_INLINE_ void notify_one() const { + condition.notify_one(); + } + + _ALWAYS_INLINE_ void notify_all() const { + condition.notify_all(); + } +}; + +#else // No threads. + +class ConditionVariable { +public: + template + void wait(const MutexLock &p_lock) const {} + void notify_one() const {} + void notify_all() const {} +}; + +#endif // THREADS_ENABLED + +} // namespace godot diff --git a/include/godot_cpp/templates/fixed_vector.hpp b/include/godot_cpp/templates/fixed_vector.hpp new file mode 100644 index 000000000..b9c0e2e31 --- /dev/null +++ b/include/godot_cpp/templates/fixed_vector.hpp @@ -0,0 +1,213 @@ +/**************************************************************************/ +/* fixed_vector.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace godot { + +/** + * Array-like storage that's local (no allocations). + * + * Elements are relocated by naive memory moves; they must not store their own address (see GH-100509). + * + * Core container guidance: + * https://docs.godotengine.org/en/latest/engine_details/architecture/core_types.html#containers + * + * @tparam CAPACITY Must be enough for all elements that could be added through all code branches. + */ + +template +class _WARN_UNUSED_ FixedVector { + // This declaration allows us to access other FixedVector's private members. + template + friend class FixedVector; + + uint32_t _size = 0; + alignas(T) uint8_t _data[CAPACITY * sizeof(T)]; + + constexpr static uint32_t DATA_PADDING = MAX(alignof(T), alignof(uint32_t)) - alignof(uint32_t); + +public: + _FORCE_INLINE_ constexpr FixedVector() = default; + + constexpr FixedVector(std::initializer_list p_init) { + ERR_FAIL_COND(p_init.size() > CAPACITY); + for (const T &element : p_init) { + memnew_placement(ptr() + _size++, T(element)); + } + } + + constexpr FixedVector(const FixedVector &p_from) { + if constexpr (std::is_trivially_copyable_v) { + // Copy size and all provided elements at once. + memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T)); + } else { + for (const T &element : p_from) { + memnew_placement(ptr() + _size++, T(element)); + } + } + } + + constexpr FixedVector(FixedVector &&p_from) { + // Copy size and all provided elements at once. + // Note: Assumes trivial relocatability. + memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T)); + p_from._size = 0; + } + + constexpr FixedVector &operator=(const FixedVector &p_from) { + if constexpr (std::is_trivially_copyable_v) { + // Copy size and all provided elements at once. + memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T)); + } else { + // Destruct extraneous elements. + if constexpr (!std::is_trivially_destructible_v) { + for (uint32_t i = p_from.size(); i < _size; i++) { + ptr()[i].~T(); + } + } + + _size = 0; // Loop-assign the rest. + for (const T &element : p_from) { + ptr()[_size++] = element; + } + } + return *this; + } + + constexpr FixedVector &operator=(FixedVector &&p_from) { + // Destruct extraneous elements. + if constexpr (!std::is_trivially_destructible_v) { + for (uint32_t i = p_from.size(); i < _size; i++) { + ptr()[i].~T(); + } + } + + // Relocate elements (and size) into our buffer. + // Assumes trivial relocatability (see GH-100509). + memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T)); + p_from._size = 0; + + return *this; + } + + ~FixedVector() { + if constexpr (!std::is_trivially_destructible_v) { + for (uint32_t i = 0; i < _size; i++) { + ptr()[i].~T(); + } + } + } + + _FORCE_INLINE_ constexpr T *ptr() _LIFETIME_BOUND_ { return (T *)(_data); } + _FORCE_INLINE_ constexpr const T *ptr() const _LIFETIME_BOUND_ { return (const T *)(_data); } + + _FORCE_INLINE_ constexpr operator Span() const _LIFETIME_BOUND_ { return Span(ptr(), size()); } + _FORCE_INLINE_ constexpr Span span() const _LIFETIME_BOUND_ { return operator Span(); } + + _FORCE_INLINE_ constexpr uint32_t size() const { return _size; } + _FORCE_INLINE_ constexpr bool is_empty() const { return !_size; } + _FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; } + _FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; } + + _FORCE_INLINE_ constexpr void clear() { resize_initialized(0); } + + /// Changes the size of the vector. + /// If p_size > size(), constructs new elements. + /// If p_size < size(), destructs new elements. + constexpr Error resize_initialized(uint32_t p_size) { + if (p_size > _size) { + ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY); + memnew_arr_placement(ptr() + _size, p_size - _size); + } else if (p_size < _size) { + if constexpr (!std::is_trivially_destructible_v) { + for (uint32_t i = p_size; i < _size; i++) { + ptr()[i].~T(); + } + } + } + + _size = p_size; + return OK; + } + + /// Changes the size of the vector. + /// The initializer of new elements is skipped, making this function faster than resize_initialized. + /// The caller is required to initialize the new values. + constexpr Error resize_uninitialized(uint32_t p_size) { + static_assert(std::is_trivially_destructible_v, "resize_uninitialized is unsafe to call if T is not trivially destructible."); + ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY); + _size = p_size; + return OK; + } + + constexpr void push_back(const T &p_val) { + ERR_FAIL_COND(_size >= CAPACITY); + memnew_placement(ptr() + _size, T(p_val)); + _size++; + } + + constexpr void push_back(T &&p_val) { + ERR_FAIL_COND(_size >= CAPACITY); + memnew_placement(ptr() + _size, T(std::move(p_val))); + _size++; + } + + constexpr void pop_back() { + ERR_FAIL_COND(_size == 0); + _size--; + ptr()[_size].~T(); + } + + // NOTE: Subscripts sanity check the bounds to avoid undefined behavior. + // This is slower than direct buffer access and can prevent autovectorization. + // If the bounds are known, use ptr() subscript instead. + constexpr const T &operator[](uint32_t p_index) const _LIFETIME_BOUND_ { + CRASH_COND(p_index >= _size); + return ptr()[p_index]; + } + + constexpr T &operator[](uint32_t p_index) _LIFETIME_BOUND_ { + CRASH_COND(p_index >= _size); + return ptr()[p_index]; + } + + _FORCE_INLINE_ constexpr T *begin() _LIFETIME_BOUND_ { return ptr(); } + _FORCE_INLINE_ constexpr T *end() _LIFETIME_BOUND_ { return ptr() + _size; } + + _FORCE_INLINE_ constexpr const T *begin() const _LIFETIME_BOUND_ { return ptr(); } + _FORCE_INLINE_ constexpr const T *end() const _LIFETIME_BOUND_ { return ptr() + _size; } +}; + +} //namespace godot \ No newline at end of file diff --git a/include/godot_cpp/templates/lru.hpp b/include/godot_cpp/templates/lru.hpp new file mode 100644 index 000000000..83bed44de --- /dev/null +++ b/include/godot_cpp/templates/lru.hpp @@ -0,0 +1,154 @@ +/**************************************************************************/ +/* lru.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include +#include + +namespace godot { + +template , void (*BeforeEvict)(TKey &, TData &) = nullptr> +class _WARN_UNUSED_ LRUCache { +public: + struct Pair { + TKey key; + TData data; + + Pair() {} + Pair(const TKey &p_key, const TData &p_data) : + key(p_key), + data(p_data) { + } + }; + + typedef typename List::Element *Element; + +private: + List _list; + HashMap _map; + size_t capacity; + +public: + const Pair *insert(const TKey &p_key, const TData &p_value) { + Element *e = _map.getptr(p_key); + Element n = _list.push_front(Pair(p_key, p_value)); + + if (e) { + GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress") + if constexpr (BeforeEvict != nullptr) { + BeforeEvict((*e)->get().key, (*e)->get().data); + } + GODOT_GCC_WARNING_POP + _list.erase(*e); + _map.erase(p_key); + } + _map[p_key] = _list.front(); + + while (_map.size() > capacity) { + Element d = _list.back(); + GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress") + if constexpr (BeforeEvict != nullptr) { + BeforeEvict(d->get().key, d->get().data); + } + GODOT_GCC_WARNING_POP + _map.erase(d->get().key); + _list.pop_back(); + } + + return &n->get(); + } + + void clear() { + _map.clear(); + _list.clear(); + } + + bool has(const TKey &p_key) const { + return _map.getptr(p_key); + } + + bool erase(const TKey &p_key) { + Element *e = _map.getptr(p_key); + if (!e) { + return false; + } + _list.move_to_front(*e); + _map.erase(p_key); + _list.pop_front(); + return true; + } + + const TData &get(const TKey &p_key) { + Element *e = _map.getptr(p_key); + CRASH_COND(!e); + _list.move_to_front(*e); + return (*e)->get().data; + } + + const TData *getptr(const TKey &p_key) { + Element *e = _map.getptr(p_key); + if (!e) { + return nullptr; + } else { + _list.move_to_front(*e); + return &(*e)->get().data; + } + } + + _FORCE_INLINE_ size_t get_capacity() const { return capacity; } + _FORCE_INLINE_ size_t get_size() const { return _map.size(); } + + void set_capacity(size_t p_capacity) { + if (capacity > 0) { + capacity = p_capacity; + while (_map.size() > capacity) { + Element d = _list.back(); + GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Waddress") + if constexpr (BeforeEvict != nullptr) { + BeforeEvict(d->get().key, d->get().data); + } + GODOT_GCC_WARNING_POP + _map.erase(d->get().key); + _list.pop_back(); + } + } + } + + LRUCache() { + capacity = 64; + } + + LRUCache(int p_capacity) { + capacity = p_capacity; + } + +}; +} //namespace godot \ No newline at end of file diff --git a/include/godot_cpp/templates/ring_buffer.hpp b/include/godot_cpp/templates/ring_buffer.hpp new file mode 100644 index 000000000..c1be7ca4e --- /dev/null +++ b/include/godot_cpp/templates/ring_buffer.hpp @@ -0,0 +1,220 @@ +/**************************************************************************/ +/* ring_buffer.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include + +namespace godot { + +template +class _WARN_UNUSED_ RingBuffer { + LocalVector data; + int read_pos = 0; + int write_pos = 0; + int size_mask; + + inline int inc(int &p_var, int p_size) const { + int ret = p_var; + p_var += p_size; + p_var = p_var & size_mask; + return ret; + } + +public: + T read() { + ERR_FAIL_COND_V(data_left() < 1, T()); + return data.ptr()[inc(read_pos, 1)]; + } + + int read(T *p_buf, int p_size, bool p_advance = true) { + int left = data_left(); + p_size = MIN(left, p_size); + int pos = read_pos; + int to_read = p_size; + int dst = 0; + while (to_read) { + int end = pos + to_read; + end = MIN(end, size()); + int total = end - pos; + const T *read = data.ptr(); + for (int i = 0; i < total; i++) { + p_buf[dst++] = read[pos + i]; + } + to_read -= total; + pos = 0; + } + if (p_advance) { + inc(read_pos, p_size); + } + return p_size; + } + + int copy(T *p_buf, int p_offset, int p_size) const { + int left = data_left(); + if ((p_offset + p_size) > left) { + p_size = left - p_offset; + if (p_size <= 0) { + return 0; + } + } + p_size = MIN(left, p_size); + int pos = read_pos; + inc(pos, p_offset); + int to_read = p_size; + int dst = 0; + while (to_read) { + int end = pos + to_read; + end = MIN(end, size()); + int total = end - pos; + for (int i = 0; i < total; i++) { + p_buf[dst++] = data[pos + i]; + } + to_read -= total; + pos = 0; + } + return p_size; + } + + int find(const T &p_value, int p_offset, int p_max_size) const { + int left = data_left(); + if ((p_offset + p_max_size) > left) { + p_max_size = left - p_offset; + if (p_max_size <= 0) { + return 0; + } + } + p_max_size = MIN(left, p_max_size); + int pos = read_pos; + inc(pos, p_offset); + int to_read = p_max_size; + while (to_read) { + int end = pos + to_read; + end = MIN(end, size()); + int total = end - pos; + for (int i = 0; i < total; i++) { + if (data[pos + i] == p_value) { + return i + (p_max_size - to_read); + } + } + to_read -= total; + pos = 0; + } + return -1; + } + + inline int advance_read(int p_n) { + p_n = MIN(p_n, data_left()); + inc(read_pos, p_n); + return p_n; + } + + inline int decrease_write(int p_n) { + p_n = MIN(p_n, data_left()); + inc(write_pos, size_mask + 1 - p_n); + return p_n; + } + + Error write(const T &p_v) { + ERR_FAIL_COND_V(space_left() < 1, FAILED); + data[inc(write_pos, 1)] = p_v; + return OK; + } + + int write(const T *p_buf, int p_size) { + int left = space_left(); + p_size = MIN(left, p_size); + + int pos = write_pos; + int to_write = p_size; + int src = 0; + while (to_write) { + int end = pos + to_write; + end = MIN(end, size()); + int total = end - pos; + + for (int i = 0; i < total; i++) { + data[pos + i] = p_buf[src++]; + } + to_write -= total; + pos = 0; + } + + inc(write_pos, p_size); + return p_size; + } + + inline int space_left() const { + int left = read_pos - write_pos; + if (left < 0) { + return size() + left - 1; + } + if (left == 0) { + return size() - 1; + } + return left - 1; + } + inline int data_left() const { + return size() - space_left() - 1; + } + + inline int size() const { + return data.size(); + } + + inline void clear() { + read_pos = 0; + write_pos = 0; + } + + void resize(int p_power) { + int old_size = size(); + int new_size = 1 << p_power; + int mask = new_size - 1; + data.resize(int64_t(1) << int64_t(p_power)); + if (old_size < new_size && read_pos > write_pos) { + for (int i = 0; i < write_pos; i++) { + data[(old_size + i) & mask] = data[i]; + } + write_pos = (old_size + write_pos) & mask; + } else { + read_pos = read_pos & mask; + write_pos = write_pos & mask; + } + + size_mask = mask; + } + + RingBuffer(int p_power = 0) { + resize(p_power); + } +}; + +} //namespace godot \ No newline at end of file diff --git a/include/godot_cpp/templates/rw_lock.hpp b/include/godot_cpp/templates/rw_lock.hpp new file mode 100644 index 000000000..f4bd0823a --- /dev/null +++ b/include/godot_cpp/templates/rw_lock.hpp @@ -0,0 +1,132 @@ +/**************************************************************************/ +/* rw_lock.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include + +#ifdef MINGW_ENABLED +#define MINGW_STDTHREAD_REDUNDANCY_WARNING +#include +#define THREADING_NAMESPACE mingw_stdthread +#else +#include +#define THREADING_NAMESPACE std +#endif + +namespace godot { + +#ifdef THREADS_ENABLED + +class RWLock { + mutable THREADING_NAMESPACE::shared_mutex mutex; + +public: + _ALWAYS_INLINE_ void read_lock() const { + mutex.lock_shared(); + } + _ALWAYS_INLINE_ void read_unlock() const { + mutex.unlock_shared(); + } + _ALWAYS_INLINE_ bool read_try_lock() const { + return mutex.try_lock_shared(); + } + + _ALWAYS_INLINE_ void write_lock() { + mutex.lock(); + } + _ALWAYS_INLINE_ void write_unlock() { + mutex.unlock(); + } + _ALWAYS_INLINE_ bool write_try_lock() { + return mutex.try_lock(); + } +}; + +class RWLockRead { + const RWLock &lock; + +public: + _ALWAYS_INLINE_ RWLockRead(const RWLock &p_lock) : + lock(p_lock) { + lock.read_lock(); + } + _ALWAYS_INLINE_ ~RWLockRead() { + lock.read_unlock(); + } +}; + +class RWLockWrite { + RWLock &lock; + +public: + _ALWAYS_INLINE_ RWLockWrite(RWLock &p_lock) : + lock(p_lock) { + lock.write_lock(); + } + _ALWAYS_INLINE_ ~RWLockWrite() { + lock.write_unlock(); + } +}; + +#else // No threads. + +class RWLock { + mutable THREADING_NAMESPACE::shared_mutex mutex; + +public: + _ALWAYS_INLINE_ void read_lock() const {} + _ALWAYS_INLINE_ void read_unlock() const {} + _ALWAYS_INLINE_ bool read_try_lock() const {} + + _ALWAYS_INLINE_ void write_lock() {} + _ALWAYS_INLINE_ void write_unlock() {} + _ALWAYS_INLINE_ bool write_try_lock() {} +}; + +class RWLockRead { + const RWLock &lock; + +public: + _ALWAYS_INLINE_ RWLockRead(const RWLock &p_lock) : lock(p_lock) {} + _ALWAYS_INLINE_ ~RWLockRead() {} +}; + +class RWLockWrite { + RWLock &lock; + +public: + _ALWAYS_INLINE_ RWLockWrite(RWLock &p_lock) : lock(p_lock) {} + _ALWAYS_INLINE_ ~RWLockWrite() {} +}; + +#endif // THREADS_ENABLED + +} //namespace godot diff --git a/include/godot_cpp/templates/safe_binary_mutex.hpp b/include/godot_cpp/templates/safe_binary_mutex.hpp new file mode 100644 index 000000000..fed047d89 --- /dev/null +++ b/include/godot_cpp/templates/safe_binary_mutex.hpp @@ -0,0 +1,145 @@ +/**************************************************************************/ +/* safe_binary_mutex.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace godot { + +#ifdef THREADS_ENABLED + +// 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). +// - Must have recursive semantics (or simulate, as this one does). +// The implementation keeps the lock count in TS. Therefore, only +// one object of each version of the template can exists; hence the Tag argument. +// Tags must be unique across the Godot codebase. +// Also, don't forget to declare the thread_local variable on each use. + +template +class SafeBinaryMutex { + friend class MutexLock>; + + using StdMutexType = THREADING_NAMESPACE::mutex; + + mutable THREADING_NAMESPACE::mutex mutex; + + struct TLSData { + mutable THREADING_NAMESPACE::unique_lock lock; + uint32_t count = 0; + + TLSData(SafeBinaryMutex &p_mutex) : + lock(p_mutex.mutex, THREADING_NAMESPACE::defer_lock) {} + }; + static thread_local TLSData tls_data; + +public: + _ALWAYS_INLINE_ void lock() const { + if (++tls_data.count == 1) { + tls_data.lock.lock(); + } + } + + _ALWAYS_INLINE_ void unlock() const { + DEV_ASSERT(tls_data.count); + if (--tls_data.count == 0) { + tls_data.lock.unlock(); + } + } + + _ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock &_get_lock() const { + return const_cast &>(tls_data.lock); + } + + _ALWAYS_INLINE_ SafeBinaryMutex() { + } + + _ALWAYS_INLINE_ ~SafeBinaryMutex() { + DEV_ASSERT(!tls_data.count); + } +}; + +template +class MutexLock> { + friend class ConditionVariable; + + const SafeBinaryMutex &mutex; + +public: + explicit MutexLock(const SafeBinaryMutex &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). +}; + +#else // No threads. + +template +class SafeBinaryMutex { + struct TLSData { + TLSData(SafeBinaryMutex &p_mutex) {} + }; + static thread_local TLSData tls_data; + +public: + void lock() const {} + void unlock() const {} +}; + +template +class MutexLock> { +public: + MutexLock(const SafeBinaryMutex &p_mutex) {} + + void temp_relock() const {} + void temp_unlock() const {} +}; + +#endif // THREADS_ENABLED + +} // namespace godot diff --git a/include/godot_cpp/templates/tuple.hpp b/include/godot_cpp/templates/tuple.hpp new file mode 100644 index 000000000..d2381399a --- /dev/null +++ b/include/godot_cpp/templates/tuple.hpp @@ -0,0 +1,126 @@ +/**************************************************************************/ +/* tuple.hpp */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include + +namespace godot { + +// Simple recursive Tuple type that has no runtime overhead. +// +// The compile-time recursion works as follows: +// Assume the following: Tuple my_tuple(42, 3.14f); +// This expands to a class hierarchy that inherits from the previous step. +// So in this case this leads to: +// - struct Tuple : Tuple <--- This contains the int value. +// - struct Tuple <--- This contains the float value. +// where each of the classes has a single field of the type for that step in the +// recursion. So: float value; int value; etc. +// +// This works by splitting up the parameter pack for each step in the recursion minus the first. +// so the first step creates the "T value" from the first template parameter. +// any further template arguments end up in "Rest", which we then use to instantiate a new +// tuple, but now minus the first argument. To write this all out: +// +// Tuple +// step 1: Tuple T = int, Rest = float. Results in a Tuple : Tuple +// step 2: Tuple T = float, no Rest. Results in a Tuple +// +// tuple_get works through a similar recursion, using the inheritance chain to walk to the right node. +// In order to tuple_get<1>(my_tuple), from the example tuple above: +// +// 1. We want tuple_get<1> to return the float, which is one level "up" from Tuple : Tuple, +// (the real type of the Tuple "root"). +// 2. Since index 1 > 0, it casts the tuple to its parent type (Tuple). This works because +// we cast to Tuple which in this case is just float. +// 3. Now we're looking for index 0 in Tuple, which directly returns its value field. Note +// how get<0> is a template specialization. +// +// At compile time, this gets fully resolved. The compiler sees get<1>(my_tuple) and: +// 1. Creates TupleGet<1, Tuple>::tuple_get which contains the cast to Tuple. +// 2. Creates TupleGet<0, Tuple>::tuple_get which directly returns the value. +// 3. The compiler will then simply optimize all of this nonsense away and return the float directly. + +template +struct Tuple; + +template <> +struct Tuple<> {}; + +template +struct Tuple : Tuple { + T value; + + Tuple() = default; + + template + _FORCE_INLINE_ Tuple(F &&p_first, R &&...p_rest) : + Tuple(std::forward(p_rest)...), + value(std::forward(p_first)) {} +}; + +// Tuple is zero-constructible if and only if all constrained types are zero-constructible. +template +struct is_zero_constructible> : std::conjunction...> {}; + +template +struct TupleGet; + +template +struct TupleGet<0, Tuple> { + _FORCE_INLINE_ static First &tuple_get(Tuple &p_tuple) { + return p_tuple.value; + } +}; + +// Rationale for using auto here is that the alternative is writing a +// helper struct to create an otherwise useless type. we would have to write +// a second recursive template chain like: TupleGetType>::type +// just to recover the type in the most baroque way possible. + +template +struct TupleGet> { + _FORCE_INLINE_ static auto &tuple_get(Tuple &p_tuple) { + return TupleGet>::tuple_get(static_cast &>(p_tuple)); + } +}; + +template +_FORCE_INLINE_ auto &tuple_get(Tuple &p_tuple) { + return TupleGet>::tuple_get(p_tuple); +} + +template +_FORCE_INLINE_ const auto &tuple_get(const Tuple &p_tuple) { + return TupleGet>::tuple_get(p_tuple); +} + +} //namespace godot \ No newline at end of file