www.digitalmars.com         C & C++   DMDScript  

D - Idea: Method wrappers

reply Niall Douglas <Niall_member pathlink.com> writes:
Since D is apparently multithreading friendly (and hence I hope I won't get
flamed to death for suggesting this), I once proposed a syntax for wrapping
class methods in a pre and post call wrapper. To clarify, you can specify
certain code to be called before every method and after every method, either
globally for the class or per-method.

There are tons of uses for this: hacking someone else's binaries, locking data
access, profiling, patches etc. Building synchronisation support into the
language is nice, but I see you've done it like Java which I find impossible to
write high-performance multithreaded apps in simply because often it locks when
you really don't need it to. Mutexes will always be an expensive operation given
a processor SWAP instruction hangs the entire motherboard for its duration. A
good example where you don't need to lock is reading processor-atomic values.

Also, language supported synchronisation done Java style I find tends to lead to
a lot of deadlocks and it's simply bad practice. One needs the ability to
"unlock, wait, relock" which I didn't notice support for in D. Also, if you're
supporting recursive mutexes (as you should), you can get into some interesting
nesting deadlocks.

I personally use a C++ based model of lock keys. You basically take out a key
when you lock something for access, but the keys auto-unlock themselves
intelligently when you are taking out another key. The trick here is to never
use raw pointers across a lock state change as objects can move - atomic smart
pointers here are your friend. With my system, I recently wrote a 30,000 line
fully multithreaded industrial control program which was virtually bugless (six
major bugs) and had excellent i/o latency, so I'm pretty sure I'm on the right

I also noticed D doesn't appear to have read-write mutex support - if it did,
all const methods could be called with read only lock which can vastly improve
performance as it avoids kernel waits.

Anyway, just my ha'pennies worth!

Jan 12 2003
parent "Walter" <walter digitalmars.com> writes:
Some good comments about mutexes, I've saved your post for reference.
Thanks, -Walter
Feb 06 2003