[D-runtime] Adding atomic ops to Druntime

Sean Kelly sean at invisibleduck.org
Sun Dec 6 21:10:14 PST 2009

The further we get with adding concurrency features to D the more I find myself wishing there were a module for doing atomic ops in Druntime.  I have one sitting around that I'd love to roll in if it passes muster.  It's based on Alexander Terekhov's atomic<> class proposed in comp.programming.threads ages ago, and is very similar to a subset of what the C++ 0x committee settled on--the relevant portion of the C++ 0x proposal is here:


Here's an outline of the API:

T atomicLoad(msync ms = msync.seq, T)( ref T val );
void atomicStore(msync ms = msync.seq, T)( inout T val, T newval );
bool atomicStoreIf(msync ms = msync.seq, T)( inout T val, T newval, T equalTo );
T atomicIncrement(msync ms = msync.seq, T)( inout T val );
T atomicDecrement(msync ms = msync.seq, T)( inout T val );

struct Atomic(T)
    T load(msync ms = msync.seq)();
    void store(msync ms = msync.seq)( T val );
    bool storeIf(msync ms = msync.seq)( T newval, T equalTo );
  static if( isNumericType!T )
    T increment(msync ms = msync.seq)();
    T decrement(msync ms = msync.seq)();

enum msync
    raw,    /// not sequenced
    hlb,    /// hoist-load barrier
    hsb,    /// hoist-store barrier
    slb,    /// sink-load barrier
    ssb,    /// sink-store barrier
    acq,    /// hoist-load + hoist-store barrier
    rel,    /// sink-load + sink-store barrier
    seq,    /// fully sequenced (acq + rel)

(atomicStoreIf is equivalent to atomic_compare_swap in C++)

The Atomic struct could probably be done away with, given the existence of shared in D2, and all but acq, rel, seq, and raw could be eliminated from msync as well.  From a functionality standpoint, I think it's fairly minimal for an atomics package, but I'd rather add than cut stuff later.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d-runtime/attachments/20091206/a4c3fc96/attachment.htm>

More information about the D-runtime mailing list