aboutsummaryrefslogtreecommitdiffstats
path: root/src/3rdparty/PhysX/0005-FixDeadlockStartingThreads.patch
blob: 5b3a124a5e7706641ef9b6630238556494b6022e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
diff --git a/src/3rdparty/PhysX/source/foundation/src/unix/PsUnixThread.cpp b/src/3rdparty/PhysX/source/foundation/src/unix/PsUnixThread.cpp
index 9389b4d..0fb6851 100644
--- a/src/3rdparty/PhysX/source/foundation/src/unix/PsUnixThread.cpp
+++ b/src/3rdparty/PhysX/source/foundation/src/unix/PsUnixThread.cpp
@@ -97,8 +97,7 @@ class _ThreadImpl
 	volatile int32_t state;
 
 	pthread_t thread;
-	pid_t tid;
-
+	pid_t tid;	// Not used for PX_EMSCRIPTEN
 	uint32_t affinityMask;
 	const char* name;
 };
@@ -116,7 +115,7 @@ static void setTid(_ThreadImpl& threadImpl)
 #elif PX_APPLE_FAMILY
 	threadImpl.tid = syscall(SYS_gettid);
 #elif PX_EMSCRIPTEN
-	threadImpl.tid = pthread_self();
+	// No thread id for emscripten
 #else
 	threadImpl.tid = syscall(__NR_gettid);
 #endif
@@ -128,11 +127,16 @@ static void setTid(_ThreadImpl& threadImpl)
 void* PxThreadStart(void* arg)
 {
 	_ThreadImpl* impl = getThread(reinterpret_cast<ThreadImpl*>(arg));
+#ifndef PX_EMSCRIPTEN
 	impl->state = _PxThreadStarted;
+#endif
 
+#ifdef PX_EMSCRIPTEN
+	while(atomicCompareExchange(&(impl->threadStarted), 1, 1) == 0)
+		sched_yield();
+#endif
 	// run setTid in thread's context
 	setTid(*impl);
-
 	// then run either the passed in function or execute from the derived class (Runnable).
 	if(impl->fn)
 		(*impl->fn)(impl->arg);
@@ -202,7 +206,9 @@ void ThreadImpl::start(uint32_t stackSize, Runnable* runnable)
 		stackSize = PTHREAD_STACK_MIN;
 	}
 #endif
-
+#ifdef PX_EMSCRIPTEN
+	getThread(this)->state = _PxThreadStarted;
+#endif
 	if(runnable && !getThread(this)->arg && !getThread(this)->fn)
 		getThread(this)->arg = runnable;
 
@@ -220,10 +226,17 @@ void ThreadImpl::start(uint32_t stackSize, Runnable* runnable)
 #endif
 	PX_ASSERT(!status);
 
+#ifdef PX_EMSCRIPTEN
+	// The waiting is reversed, the started thread wait for us to
+	// reach this point. We do this because waiting for the started
+	// thread to signal us might deadlock wasm.
+	atomicCompareExchange(&(getThread(this)->threadStarted), 1, 0);
+#else
 	// wait for thread to startup and write out TID
 	// otherwise TID dependent calls like setAffinity will fail.
 	while(atomicCompareExchange(&(getThread(this)->threadStarted), 1, 1) == 0)
 		yield();
+#endif
 
 	// here we are sure that getThread(this)->state >= _PxThreadStarted