Disabling synchronized blocks locking overhead?

Khint Enco khint at none.net
Fri Jun 24 02:40:36 PDT 2011


On 24/06/11 09:57, d coder wrote:
> Hello List
>
> I am working on an application where I would normally be using
> multithreading. But sometimes, depending on how much data sharing is
> being shared, I might want to invoke the same application in
> single-threaded mode.
>
> Now when I invoke it in single-threaded mode, I would not like to have
> all the overhead incurred due to mutex/monitor locking. Does DMD have
> some compile time flags to achieve that.
>
> One way I can think about achieving this is by enclosing all the
> 'synchronized' declarations inside small version blocks. But I am afraid
> this would make the code quite unwieldy.
> Is there a recommended way of achieving this end?
>
> Regards
> - Puneet

Yes, there is a flag .. in the source code! Just download the source and 
apply this patch:

diff -r 2c283d0f3a2c dmd/func.c
--- a/dmd/func.c	Fri Jun 24 10:26:15 2011 +0100
+++ b/dmd/func.c	Fri Jun 24 10:26:31 2011 +0100
@@ -183,7 +183,7 @@
              stc |= STCimmutable;
          if (type->isConst())
              stc |= STCconst;
-        if (type->isShared() || storage_class & STCsynchronized)
+        if (type->isShared())
              stc |= STCshared;
          if (type->isWild())
              stc |= STCwild;

Then compile and keep both on standby for when you need it!

Doing the above removes the following stuff from the object file, which 
is not present with unsynchronized methods.

  		sub	ESP,8
  		mov	-4[EBP],EAX
  		mov	-8[EBP],EAX
  		push	EAX
  		call	  _d_monitorenter at PC32
  		mov	EAX,-4[EBP]
86a94,96
  		push	dword ptr -8[EBP]
  		call	  _d_monitorexit at PC32
  		add	ESP,8


More information about the Digitalmars-d mailing list