[Issue 8831] core.atomic: add compare-and-swap function with other result type

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 17 13:08:28 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8831



--- Comment #4 from mimocrocodil <4denizzz at gmail.com> 2012-10-17 13:08:25 PDT ---
bool casw( shared (size_t)* here, size_t ifThis, size_t writeThis, size_t*
comparedWith ) nothrow
{
    static if( size_t.sizeof == long.sizeof )
    {
        asm
        {
            mov RDX, writeThis;
            mov RAX, ifThis;
            mov RCX, here;
            mov RBX, comparedWith;
            lock; // lock always needed to make this op atomic
            cmpxchg [RCX], RDX;
            mov [RBX], RAX;
            setz AL;
        }
    }
    else
        static assert(false, "Unsupported architecture");
}

unittest
{
    import std.stdio;
    shared(size_t) o = 3;
    shared(size_t) n = 4;
    shared(size_t)* a = &n;

    size_t compared;

    auto r = casw( a, o, n, &compared );
    assert( !r );
    assert( compared == 4 );

    a = &o;
    r = casw( a, o, n, &compared );

    assert( r );
    assert( compared == 3 );
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list