Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mingw-std-threads in MinGW builds #85039

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ Comment: meshoptimizer
Copyright: 2016-2022, Arseny Kapoulkine
License: Expat

Files: ./thirdparty/mingw-std-threads/
Comment: mingw-std-threads
Copyright: 2016, Mega Limited
License: BSD-2-clause

Files: ./thirdparty/minimp3/
Comment: MiniMP3
Copyright: lieff
Expand Down
5 changes: 5 additions & 0 deletions core/math/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ AABB AABB::intersection(const AABB &p_aabb) const {
return AABB(min, max - min);
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
#ifdef MATH_CHECKS
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
Expand Down
2 changes: 1 addition & 1 deletion core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ class Object {
friend class RefCounted;
bool type_is_reference = false;

std::mutex _instance_binding_mutex;
BinaryMutex _instance_binding_mutex;
struct InstanceBinding {
void *binding = nullptr;
void *token = nullptr;
Expand Down
11 changes: 9 additions & 2 deletions core/os/condition_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@
#ifndef CONDITION_VARIABLE_H
#define CONDITION_VARIABLE_H

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.condition_variable.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <condition_variable>
#define THREADING_NAMESPACE std
#endif

// 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
Expand All @@ -40,12 +47,12 @@
// own mutex to tie the wait-notify to some other behavior, you need to use this.

class ConditionVariable {
mutable std::condition_variable condition;
mutable THREADING_NAMESPACE::condition_variable condition;

public:
template <class BinaryMutexT>
_ALWAYS_INLINE_ void wait(const MutexLock<BinaryMutexT> &p_lock) const {
condition.wait(const_cast<std::unique_lock<std::mutex> &>(p_lock.lock));
condition.wait(const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(p_lock.lock));
}

_ALWAYS_INLINE_ void notify_one() const {
Expand Down
8 changes: 4 additions & 4 deletions core/os/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void _global_unlock() {
_global_mutex.unlock();
}

template class MutexImpl<std::recursive_mutex>;
template class MutexImpl<std::mutex>;
template class MutexLock<MutexImpl<std::recursive_mutex>>;
template class MutexLock<MutexImpl<std::mutex>>;
template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>;
template class MutexImpl<THREADING_NAMESPACE::mutex>;
template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>;
template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>;
27 changes: 17 additions & 10 deletions core/os/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@
#include "core/error/error_macros.h"
#include "core/typedefs.h"

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.mutex.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <mutex>
#define THREADING_NAMESPACE std
#endif

template <class MutexT>
class MutexLock;
Expand Down Expand Up @@ -73,9 +80,9 @@ template <int Tag>
class SafeBinaryMutex {
friend class MutexLock<SafeBinaryMutex>;

using StdMutexType = std::mutex;
using StdMutexType = THREADING_NAMESPACE::mutex;

mutable std::mutex mutex;
mutable THREADING_NAMESPACE::mutex mutex;
static thread_local uint32_t count;

public:
Expand Down Expand Up @@ -115,7 +122,7 @@ template <class MutexT>
class MutexLock {
friend class ConditionVariable;

std::unique_lock<typename MutexT::StdMutexType> lock;
THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;

public:
_ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) :
Expand All @@ -128,7 +135,7 @@ template <int Tag>
class MutexLock<SafeBinaryMutex<Tag>> {
friend class ConditionVariable;

std::unique_lock<std::mutex> lock;
THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;

public:
_ALWAYS_INLINE_ explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
Expand All @@ -140,12 +147,12 @@ class MutexLock<SafeBinaryMutex<Tag>> {
};
};

using Mutex = MutexImpl<std::recursive_mutex>; // Recursive, for general use
using BinaryMutex = MutexImpl<std::mutex>; // Non-recursive, handle with care
using Mutex = MutexImpl<THREADING_NAMESPACE::recursive_mutex>; // Recursive, for general use
using BinaryMutex = MutexImpl<THREADING_NAMESPACE::mutex>; // Non-recursive, handle with care

extern template class MutexImpl<std::recursive_mutex>;
extern template class MutexImpl<std::mutex>;
extern template class MutexLock<MutexImpl<std::recursive_mutex>>;
extern template class MutexLock<MutexImpl<std::mutex>>;
extern template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>;
extern template class MutexImpl<THREADING_NAMESPACE::mutex>;
extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>;
extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>;

#endif // MUTEX_H
10 changes: 9 additions & 1 deletion core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@
#include "core/version_generated.gen.h"

#include <stdarg.h>

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <thread>
#define THREADING_NAMESPACE std
#endif

OS *OS::singleton = nullptr;
uint64_t OS::target_ticks = 0;
Expand Down Expand Up @@ -359,7 +367,7 @@ String OS::get_unique_id() const {
}

int OS::get_processor_count() const {
return std::thread::hardware_concurrency();
return THREADING_NAMESPACE::thread::hardware_concurrency();
}

String OS::get_processor_name() const {
Expand Down
9 changes: 8 additions & 1 deletion core/os/rw_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@

#include "core/typedefs.h"

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.shared_mutex.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <shared_mutex>
#define THREADING_NAMESPACE std
#endif

class RWLock {
mutable std::shared_timed_mutex mutex;
mutable THREADING_NAMESPACE::shared_timed_mutex mutex;

public:
// Lock the RWLock, block if locked by someone else.
Expand Down
16 changes: 12 additions & 4 deletions core/os/semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@
#include "core/error/error_macros.h"
#endif

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.condition_variable.h"
#include "thirdparty/mingw-std-threads/mingw.mutex.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <condition_variable>
#include <mutex>
#define THREADING_NAMESPACE std
#endif

class Semaphore {
private:
mutable std::mutex mutex;
mutable std::condition_variable condition;
mutable THREADING_NAMESPACE::mutex mutex;
mutable THREADING_NAMESPACE::condition_variable condition;
mutable uint32_t count = 0; // Initialized as locked.
#ifdef DEBUG_ENABLED
mutable uint32_t awaiters = 0;
Expand All @@ -57,7 +65,7 @@ class Semaphore {
}

_ALWAYS_INLINE_ void wait() const {
std::unique_lock lock(mutex);
THREADING_NAMESPACE::unique_lock lock(mutex);
#ifdef DEBUG_ENABLED
++awaiters;
#endif
Expand Down Expand Up @@ -116,7 +124,7 @@ class Semaphore {
"A Semaphore object is being destroyed while one or more threads are still waiting on it.\n"
"Please call post() on it as necessary to prevent such a situation and so ensure correct cleanup.");
// And now, the hacky countermeasure (i.e., leak the condition variable).
new (&condition) std::condition_variable();
new (&condition) THREADING_NAMESPACE::condition_variable();
}
}
#endif
Expand Down
6 changes: 2 additions & 4 deletions core/os/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_cal
Thread::ID Thread::start(Thread::Callback p_callback, void *p_user, const Settings &p_settings) {
ERR_FAIL_COND_V_MSG(id != UNASSIGNED_ID, UNASSIGNED_ID, "A Thread object has been re-started without wait_to_finish() having been called on it.");
id = id_counter.increment();
std::thread new_thread(&Thread::callback, id, p_settings, p_callback, p_user);
thread.swap(new_thread);
thread = THREADING_NAMESPACE::thread(&Thread::callback, id, p_settings, p_callback, p_user);
return id;
}

Expand All @@ -82,8 +81,7 @@ void Thread::wait_to_finish() {
ERR_FAIL_COND_MSG(id == UNASSIGNED_ID, "Attempt of waiting to finish on a thread that was never started.");
ERR_FAIL_COND_MSG(id == get_caller_id(), "Threads can't wait to finish on themselves, another thread must wait.");
thread.join();
std::thread empty_thread;
thread.swap(empty_thread);
thread = THREADING_NAMESPACE::thread();
id = UNASSIGNED_ID;
}

Expand Down
9 changes: 8 additions & 1 deletion core/os/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@
#include "core/templates/safe_refcount.h"
#include "core/typedefs.h"

#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <thread>
#define THREADING_NAMESPACE std
#endif

class String;

Expand Down Expand Up @@ -82,7 +89,7 @@ class Thread {
ID id = UNASSIGNED_ID;
static SafeNumeric<uint64_t> id_counter;
static thread_local ID caller_id;
std::thread thread;
THREADING_NAMESPACE::thread thread;

static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);

Expand Down
4 changes: 4 additions & 0 deletions editor/dependency_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class DependencyEditor : public AcceptDialog {
DependencyEditor();
};

#ifdef MINGW_ENABLED
#undef FILE_OPEN
#endif

class DependencyEditorOwners : public AcceptDialog {
GDCLASS(DependencyEditorOwners, AcceptDialog);

Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class EditorScriptCodeCompletionCache;
class FindInFilesDialog;
class FindInFilesPanel;

#ifdef MINGW_ENABLED
#undef FILE_OPEN
#endif

class ScriptEditor : public PanelContainer {
GDCLASS(ScriptEditor, PanelContainer);

Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/shader_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class TextShaderEditor;
class VisualShaderEditor;
class WindowWrapper;

#ifdef MINGW_ENABLED
#undef FILE_OPEN
#endif

class ShaderEditorPlugin : public EditorPlugin {
GDCLASS(ShaderEditorPlugin, EditorPlugin);

Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/visual_shader_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ class VisualShaderEditor : public VBoxContainer {
COLLAPSE_ALL
};

#ifdef MINGW_ENABLED
#undef DELETE
#endif

enum NodeMenuOptions {
ADD,
SEPARATOR, // ignore
Expand Down
6 changes: 6 additions & 0 deletions modules/gdscript/gdscript_tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#include "core/templates/vector.h"
#include "core/variant/variant.h"

#ifdef MINGW_ENABLED
#undef CONST
#undef IN
#undef VOID
#endif

class GDScriptTokenizer {
public:
enum CursorPlace {
Expand Down
5 changes: 5 additions & 0 deletions modules/gltf/editor/editor_scene_importer_blend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
#include "main/main.h"
#include "scene/gui/line_edit.h"

#ifdef MINGW_ENABLED
#define near
#define far
#endif

#ifdef WINDOWS_ENABLED
#include <shlwapi.h>
#endif
Expand Down
5 changes: 5 additions & 0 deletions scene/3d/camera_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
#include "scene/resources/camera_attributes.h"
#include "scene/resources/environment.h"

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

class Camera3D : public Node3D {
GDCLASS(Camera3D, Node3D);

Expand Down
5 changes: 5 additions & 0 deletions scene/debugger/scene_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ void SceneDebugger::deinitialize() {
}
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

#ifdef DEBUG_ENABLED
Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
SceneTree *scene_tree = SceneTree::get_singleton();
Expand Down
5 changes: 5 additions & 0 deletions scene/resources/camera_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ real_t CameraAttributesPhysical::get_fov() const {
return frustum_fov;
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

void CameraAttributesPhysical::_update_frustum() {
//https://en.wikipedia.org/wiki/Circle_of_confusion#Circle_of_confusion_diameter_limit_based_on_d/1500
Vector2i sensor_size = Vector2i(36, 24); // Matches high-end DSLR, could be made variable if there is demand.
Expand Down
5 changes: 5 additions & 0 deletions servers/rendering/renderer_scene_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ void RendererSceneRender::CameraData::set_camera(const Transform3D p_transform,
taa_jitter = p_taa_jitter;
}

#ifdef MINGW_ENABLED
#undef near
#undef far
#endif

void RendererSceneRender::CameraData::set_multiview_camera(uint32_t p_view_count, const Transform3D *p_transforms, const Projection *p_projections, bool p_is_orthogonal, bool p_vaspect) {
ERR_FAIL_COND_MSG(p_view_count != 2, "Incorrect view count for stereoscopic view");

Expand Down
Loading
Loading