[dmd-concurrency] CSP: Communicating sequential processes

Michel Fortin michel.fortin at michelf.com
Wed Jan 20 04:28:43 PST 2010


Le 2010-01-20 à 2:07, Sean Kelly a écrit :

> Is what you want in core.sync.rwmutex?  You can use it as:
> 
> synchronized( mut.reader ) {}
> synchronized( mut.writer ) {}
> 
> It doesn't currently allow upgrading read locks to write locks, though I think this wouldn't be difficult to add.  More importantly I suppose is that acquiring a concurrent read lock may be more expensive than you'd expect, so you really only want to use a ReadWriteMutex if the read operations take a reasonable amount of time to complete.

What happens if you call test() here?

	void test() {
		synchronized (mut.reader) {
			// read A
			testWrite();
			// read B
		}
	}
	
	void testWrite() {
		syncrhonized (mut.writer) {
			// write C
		}
	}

I guess that if you haven't implemented upgrades this will deadlock.

I'd like to note that even upgrading the lock to a write lock might be a problem: as I said in my other message, making this an automatic upgrade might force the release the reader lock until the writer lock is acquired, which would be unexpected from test()'s point of view.

This could work well as a transaction, where a failure to upgrade the lock would throw an exception, "read A" would be rollbacked, and the synchronized part would be attempted again. But without proper logic to deal with the unfortunate case of a failed upgrade you have either a race or a deadlock here.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the dmd-concurrency mailing list