www.digitalmars.com         C & C++   DMDScript  

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

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831

           Summary: core.atomic: add compare-and-swap function with other
                    result type
           Product: D
           Version: D2
          Platform: x86
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: 4denizzz gmail.com



---
Many algorithms require to know what value it was under a pointer at the time
of comparison. (This value is unknown only when compare-and-swap fails, of
course.) 
For example, it is required for RTCSS algorithm from which it can be obtained
CASN (compare-and-swap for any number of the elements).

Probably, CMPXCHG can do that.

Ideally it would be able to get value under pointer at the time of comparison
and the result of compare (true/false) as a struct. I do not know about
performance of that behaviour but in terms of more high-level programming it
will be useful. (CAS can be called often by its nature, and some of its result
will not be used usually, but I do not know, may be such cases will be
optimized by compiler.)

Or may be it can be three functions with different names.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 16 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831




---
names: "cas", "cas1" (it seems name from Java) for function who returns
comparision value and "cass" for function who returns struct. (And can stop at
first two - they cover all necessary cases.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831




---

 names: "cas", "cas1" (it seems name from Java) for function who returns
 comparision value and "cass" for function who returns struct. (And can stop at
 first two - they cover all necessary cases.)
Huh, sorry, remark: that need all three functions - not all cases will be covered by the first two. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831


mimocrocodil <4denizzz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|Phobos                      |druntime



---
(component changed to druntime)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831




---
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: -------
Oct 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831


Weed <resume755 mail.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |resume755 mail.ru



more intuitive test:

unittest // casw
{
    shared size_t v = 2;
    shared(size_t)* p = &v;
    size_t compared;

    auto r = casw( p, 3, 4, &compared );
    assert( !r );
    assert( v == 2 );
    assert( compared == 2 );

    compared = 0;

    r = casw( p, 2, 4, &compared );
    assert( r );
    assert( v == 4 );
    assert( compared == 2 );
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 17 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831


Alex Rønne Petersen <alex lycus.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |alex lycus.org



CEST ---
Please submit a pull request to:
https://github.com/D-Programming-Language/druntime

Thanks!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 17 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8831




---

 Please submit a pull request to:
 https://github.com/D-Programming-Language/druntime
 
 Thanks!
Sorry, иге my clone of the druntime isn't builds now (with many warnings and error, probably because old stable dmd compiler) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 17 2012