Ahah, yes I see how to do it. See below for an implementation of something similar, which (I cannot resist adding) I though of myself just before.

http://msdn.microsoft.com/en-us/magazine/cc163642.aspx

I was struggling to see how to use the test-and-set instruction to do more general operations (eg Interlocked.Increment() in the example).