瀏覽代碼

Merge pull request #47368 from hpvb/fix-tls-mingw

[3.3] Fix thread_local, tls, ASLR, and DEP with MingW
Rémi Verschelde 4 年之前
父節點
當前提交
e0d261be00
共有 4 個文件被更改,包括 19 次插入12 次删除
  1. 14 6
      core/os/thread.cpp
  2. 1 3
      core/os/thread.h
  3. 1 0
      platform/windows/detect.py
  4. 3 3
      platform/windows/godot_windows.cpp

+ 14 - 6
core/os/thread.cpp

@@ -47,7 +47,8 @@ uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) {
 }
 
 Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id());
-thread_local Thread::ID Thread::caller_id = 0;
+static thread_local Thread::ID caller_id = 0;
+static thread_local bool caller_id_cached = false;
 
 void Thread::_set_platform_funcs(
 		Error (*p_set_name_func)(const String &),
@@ -61,7 +62,9 @@ void Thread::_set_platform_funcs(
 }
 
 void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) {
-	Thread::caller_id = _thread_id_hash(p_self->thread.get_id());
+	caller_id = _thread_id_hash(p_self->thread.get_id());
+	caller_id_cached = true;
+
 	if (set_priority_func) {
 		set_priority_func(p_settings.priority);
 	}
@@ -112,10 +115,6 @@ Error Thread::set_name(const String &p_name) {
 	return ERR_UNAVAILABLE;
 }
 
-Thread::Thread() {
-	caller_id = _thread_id_hash(std::this_thread::get_id());
-}
-
 Thread::~Thread() {
 	if (id != _thread_id_hash(std::thread::id())) {
 #ifdef DEBUG_ENABLED
@@ -125,4 +124,13 @@ Thread::~Thread() {
 	}
 }
 
+Thread::ID Thread::get_caller_id() {
+	if (likely(caller_id_cached)) {
+		return caller_id;
+	} else {
+		caller_id = _thread_id_hash(std::this_thread::get_id());
+		caller_id_cached = true;
+		return caller_id;
+	}
+}
 #endif

+ 1 - 3
core/os/thread.h

@@ -66,7 +66,6 @@ private:
 	static uint64_t _thread_id_hash(const std::thread::id &p_t);
 
 	ID id = _thread_id_hash(std::thread::id());
-	static thread_local ID caller_id;
 	std::thread thread;
 
 	static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
@@ -87,7 +86,7 @@ public:
 #if !defined(NO_THREADS)
 	_FORCE_INLINE_ ID get_id() const { return id; }
 	// get the ID of the caller thread
-	_FORCE_INLINE_ static ID get_caller_id() { return caller_id; }
+	static ID get_caller_id();
 	// get the ID of the main thread
 	_FORCE_INLINE_ static ID get_main_id() { return main_thread_id; }
 
@@ -98,7 +97,6 @@ public:
 	///< waits until thread is finished, and deallocates it.
 	void wait_to_finish();
 
-	Thread();
 	~Thread();
 #else
 	_FORCE_INLINE_ ID get_id() const { return 0; }

+ 1 - 0
platform/windows/detect.py

@@ -397,6 +397,7 @@ def configure_mingw(env):
     ## Compile flags
 
     env.Append(CCFLAGS=["-mwindows"])
+    env.Append(LINKFLAGS=["-Wl,--nxcompat", "-Wl,--dynamicbase"])
     env.Append(CPPDEFINES=["WINDOWS_ENABLED", "OPENGL_ENABLED", "WASAPI_ENABLED", "WINMIDI_ENABLED"])
     env.Append(CPPDEFINES=[("WINVER", env["target_win_version"]), ("_WIN32_WINNT", env["target_win_version"])])
     env.Append(

+ 3 - 3
platform/windows/godot_windows.cpp

@@ -135,7 +135,7 @@ char *wc_to_utf8(const wchar_t *wc) {
 	return ubuf;
 }
 
-int widechar_main(int argc, wchar_t **argv) {
+__declspec(dllexport) int widechar_main(int argc, wchar_t **argv) {
 
 	OS_Windows os(NULL);
 
@@ -169,7 +169,7 @@ int widechar_main(int argc, wchar_t **argv) {
 	return os.get_exit_code();
 };
 
-int _main() {
+__declspec(dllexport) int _main() {
 	LPWSTR *wc_argv;
 	int argc;
 	int result;
@@ -187,7 +187,7 @@ int _main() {
 	return result;
 }
 
-int main(int _argc, char **_argv) {
+__declspec(dllexport) int main(int _argc, char **_argv) {
 	// _argc and _argv are ignored
 	// we are going to use the WideChar version of them instead