[dmd-concurrency] shared arrays

Andrei Alexandrescu andrei at erdani.com
Thu Jan 14 13:05:00 PST 2010


Sean Kelly wrote:
> Earlier, I had been thinking it might be nice to have this though:
> 
> shared(char)[] a, b;
> 
> synchronized( lock( a, b ) ) { // some fancy algorithm on a and b }
> 
> Basically, use the hashtable of mutexes discussed earlier to allow
> users to obtain locks on a set of N arrays in a safe manner (because
> expecting them to do it manually will generally result in deadlock).
> This makes what's happening explicit and allows the whole mess to be
> handled in library code.  In theory, this same approach could work
> for any reference type.  The optimization issue would be making
> gc_query() not need to obtain the GC lock to return a valid result
> (this may be safe already, I haven't spent the time to figure it
> out).

Defining a hashtable of mutexes would be an interesting idea, but it has 
many aspects we need to think about. One extreme approach would be to 
always lock a region of memory, not a particular class object. In that 
case the mutex pointer stored in each class object could disappear, and 
you'd essentially just say "lock this guy/these guys" and the system 
would take care of locking the pages in which those guys reside. It 
might also reduce deadlock risks, but increase contention because of 
coarser granularity.

Anyhow, I think it would be quite a rush to go with this for D2.


Andrei


More information about the dmd-concurrency mailing list