[dmd-concurrency] Defining shared delegates

Michel Fortin michel.fortin at michelf.com
Tue Jan 19 11:27:28 PST 2010


I think we should define a shared delegate as a delegate you can pass to other threads, and which other threads can call.

The delegate contains a data pointer and a function pointer. The function's code is always immutable so there is never a problem sharing it. The data it carries can be part thread-local, part shared, part immutable. So here are the proposed rules:

- If the data carried by the delegate is thread-local, in whole or in part, 
  then the delegate is thread-local.
- If the data is all shared or immutable, then the delegate can be shared.

I believe all delegates should be shared when they refer only to immutable and shared data, and a shared delegate should implicitly convert to a thread-local delegate when necessary.

Does it make sense that shared delegates convert implicitly to thread-local ones? This might look a little suspicious at first because you can't do that with other types, but with a delegate the data is private to only the associated code, and the associated code already knows whether the data is really shared or not, it is not relying on the caller's knowledge to call the right function like elsewhere. The result is that we can safely discard shared from the type the caller sees as it has no consequence.

So this should be an error (and already is) because the delegate is accessing thread-local, mutable variables a and b:

	alias shared int delegate() SampleSharedDelegate;

	SampleSharedDelegate func1() {
		int a, b;
		return { return a + b; }; // error: cannot make delegate shared
	}

This should work since a and b are shared:

	SampleSharedDelegate func2() {
		shared int a, b;
		return { return a + b; }; // ok, delegate can be shared
	}

This too should work because a and b are immutable:

	SampleSharedDelegate func3() {
		immutable int a, b;
		return { return a + b; }; // ok, delegate can be shared
	}

Does all this makes sense?

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





More information about the dmd-concurrency mailing list