BEPU deadlock in Unity3D

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Lorodion
Posts: 2
Joined: Wed Jan 08, 2014 3:06 pm

BEPU deadlock in Unity3D

Post by Lorodion » Thu Jan 09, 2014 3:26 pm

Hello,

I started working with BEPU physics this week and must say that this library looks very promising!
My idea is to integrate BEPU with Unity3D (that lacks multithreaded physics and the ability to create spaces) but have stumbled upon problems with deadlocks and would very much appreciate help!
I have found two cases where deadlocks occur:

My first test updates an empty space with multiple threads:

Code: Select all

class Test1
{
	static void RunOnce()
	{
		var looper = new ParallelLooper();

		looper.AddThread();
		looper.AddThread();

		var space = new Space(looper);
		space.Update(); //Deadlock here
	}
}
When pausing execution and inspecting the running threads you can see one thread blocking here:

Code: Select all

Not Flagged	>	139063000	0	Worker Thread	
Boolean System.Threading.WaitHandle:WaitOne ()+0x23 at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Threading/WaitHandle.cs:334	 
Void BEPUphysics.Threading.ParallelLooper:ForLoop (Int32, Int32, Action`1)+0xed at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\Threading\Modified Pool\ParallelLooper.cs:143	 
Void BEPUphysics.DeactivationManagement.DeactivationManager:UpdateMultithreaded ()+0x1e at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\DeactivationManagement\DeactivationManager.cs:210	 
Void BEPUphysics.MultithreadedProcessingStage:Update ()+0x25 at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\MultithreadedProcessingStage.cs:83	 
Void BEPUphysics.Space:DoTimeStep ()+0x1c at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\Space.cs:503	 
Void BEPUphysics.Space:Update ()+0x1 at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\Space.cs:529	 
Void Test:Start ()+0x21 at C:\Users\Lorodion\Desktop\BepuTest\Assets\Test\Test.cs:18
My second test updates an empty space with a single thread and then disposes the looper:

Code: Select all

class Test2
{
	static void RunOnce()
	{
		var looper = new ParallelLooper();

		looper.AddThread();

		var space = new Space(looper);
		space.Update();
		looper.Dispose(); //Deadlock here
	}
}
When pausing execution and inspecting the running threads you can see one thread blocking here:

Code: Select all

Not Flagged	>	137490136	0	Worker Thread
Boolean System.Threading.WaitHandle:WaitOne ()+0x23 at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Threading/WaitHandle.cs:334	 
Void BEPUphysics.Threading.ParallelLooper:RemoveThread ()+0x67 at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\Threading\Modified Pool\ParallelLooper.cs:99	 
Void BEPUphysics.Threading.ParallelLooper:Dispose ()+0x1f at c:\Users\Lorodion\Desktop\bepuphysics-0354fd402025\bepuphysics_0354fd402025\BEPUphysics\Threading\Modified Pool\ParallelLooper.cs:168	 
Void Test:Start ()+0x21 at C:\Users\Lorodion\Desktop\BepuTest\Assets\Test\Test.cs:19
What's interesting is that running the code without a debugger never produces any deadlock, however when my debugger (VS 2012) is attached to the process the deadlocks always occur.
I am using BEPU 1.3 compiled with ALLOWUNSAFE (only) for .Net 3.5 in Unity 4.3.1.

I have not been able to reproduce this problem outside of Unity so I'm guessing that their modified Mono CLR is what's causing the trouble.
Still if anyone has any ideas on how to solve this I'm grateful!

/ Jonathan

Norbo
Site Admin
Posts: 4587
Joined: Tue Jul 04, 2006 4:45 am

Re: BEPU deadlock in Unity3D

Post by Norbo » Thu Jan 09, 2014 7:21 pm

This could be a nasty one.

A stopgap solution would be to fall back to some other thread manager while debugging. The ThreadTaskManager or SimpleLooper might work, but they are significantly worse in terms of performance and load balancing.

Other than that, I don't really have any special recommendations. The fact that it only deadlocks while debugging is the most concerning. Working without the debugger is evidence against small environmental implementation issues, like a difference in memory barriers.

Lorodion
Posts: 2
Joined: Wed Jan 08, 2014 3:06 pm

Re: BEPU deadlock in Unity3D

Post by Lorodion » Fri Jan 10, 2014 10:24 am

I'll try to avoid these problems for now by not disposing or running multiple threads when debugging, but will report back if I find a solution!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests