www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Disabling synchronized blocks locking overhead?

reply d coder <dlang.coder gmail.com> writes:
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
Jun 24 2011
parent reply Khint Enco <khint none.net> writes:
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 PC32 mov EAX,-4[EBP] 86a94,96 push dword ptr -8[EBP] call _d_monitorexit PC32 add ESP,8
Jun 24 2011
next sibling parent reply d coder <dlang.coder gmail.com> writes:
 Yes, there is a flag .. in the source code! Just download the source and
 apply this patch:
Thanks Most of the times I use explicit synchronized code blocks, instead of adding "synchronized" attribute to the function itself. Would the patch you sent still work? Regards - Puneet
Jun 24 2011
parent reply Khint Enco <khint none.net> writes:
On 24/06/11 13:04, d coder wrote:
 Would the patch you sent still work?

 Regards
 - Puneet
Misfire! No, that 'patch' does nothing .. admittedly I'm still finding my way through the compiler source. It looks like I simply skipped the semantic check for the synchronized storage class .. or something like that.
Jun 24 2011
parent reply Khint Enco <khint none.net> writes:
On 25/06/11 00:50, Khint Enco wrote:
 Misfire! No, that 'patch' does nothing ..
I've added a -nosync flag to the compiler (v2.053) that removes all the effects of synchronized. It has been tested and I ran all unittests, or at least I think I did .. it took less than half a second .. It's a small patch, but does anyone mind checking it?
Jun 24 2011
parent d coder <dlang.coder gmail.com> writes:
 I've added a -nosync flag to the compiler (v2.053) that removes all the
effects of synchronized. It has been tested and I ran all unittests, or at least I think I did .. it took less than half a second .. It's a small patch, but does anyone mind checking it?
Thanks. But I get loads of errors from druntime when I try your patch and turn on the -nosync compiler flag. I have patched against the latest dmd from github. Regards - Puneet /home/puneet/work/nosync-d/src/druntime/import/object.di(27): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/druntime/import/object.di(27): no identifier for declarator immutable(char) /home/puneet/work/nosync-d/src/druntime/import/object.di(27): semicolon expected to close alias declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(27): Declaration expected, not '[' /home/puneet/work/nosync-d/src/druntime/import/object.di(28): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/druntime/import/object.di(28): no identifier for declarator immutable(wchar) /home/puneet/work/nosync-d/src/druntime/import/object.di(28): semicolon expected to close alias declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(28): Declaration expected, not '[' /home/puneet/work/nosync-d/src/druntime/import/object.di(29): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/druntime/import/object.di(29): no identifier for declarator immutable(dchar) /home/puneet/work/nosync-d/src/druntime/import/object.di(29): semicolon expected to close alias declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(29): Declaration expected, not '[' /home/puneet/work/nosync-d/src/druntime/import/object.di(51): found 'ownee' when expecting ')' /home/puneet/work/nosync-d/src/druntime/import/object.di(51): semicolon expected following function declaration /home/puneet/work/nosync-d/src/druntime/import/object.di(51): Declaration expected, not ',' /home/puneet/work/nosync-d/src/phobos/std/stdio.d(776): found 'buf' when expecting ';' following statement /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): function declaration without return type. (Note that constructors are always named 'this') /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): found '[' when expecting ')' /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): semicolon expected following auto declaration, not ']' /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1861): found ']' instead of statement /home/puneet/work/nosync-d/src/phobos/std/stdio.d(1952): found ')' when expecting '.' following ubyte
Jul 10 2011
prev sibling parent Sean Kelly <sean invisibleduck.org> writes:
	charset=us-ascii

If you synchronize via a Mutex you could create a type that detects whatever=
 and doesn't actually lock if single threaded.  core.thread.thread_needLock(=
) is an option if you can be sure a thread won't be spawned inside your magi=
cally not-locked function.=20

Sent from my iPhone

On Jun 24, 2011, at 5:04 AM, d coder <dlang.coder gmail.com> wrote:

=20
 Yes, there is a flag .. in the source code! Just download the source and a=
pply this patch:
=20
=20
 Thanks
=20
 Most of the times I use explicit synchronized code blocks, instead of addi=
ng "synchronized" attribute to the function itself.
=20
 Would the patch you sent still work?
=20
 Regards
 - Puneet
Jun 24 2011