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


--- Comment #0 from mimocrocodil <4denizzz gmail.com> 2012-10-16 15:08:40 PDT
---
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



--- Comment #1 from mimocrocodil <4denizzz gmail.com> 2012-10-16 15:15:57 PDT
---
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



--- Comment #2 from mimocrocodil <4denizzz gmail.com> 2012-10-16 15:35:10 PDT
---
(In reply to comment #1)
 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


--- Comment #3 from mimocrocodil <4denizzz gmail.com> 2012-10-16 15:48:20 PDT
---
(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



--- Comment #4 from mimocrocodil <4denizzz 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: -------
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


--- Comment #5 from Weed <resume755 mail.ru> 2012-10-17 19:49:02 PDT ---
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 Rnne Petersen <alex lycus.org> changed:

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


--- Comment #6 from Alex Rnne Petersen <alex lycus.org> 2012-10-18 05:00:07
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



--- Comment #7 from mimocrocodil <4denizzz gmail.com> 2012-10-17 20:45:45 PDT
---
(In reply to comment #6)
 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