Bug 8831 – core.atomic: add compare-and-swap function with other result type

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2012-10-16T15:08:40Z
Last change time
2019-08-31T10:37:48Z
Keywords
pull
Assigned to
No Owner
Creator
mimocrocodil

Comments

Comment #0 by 4denizzz — 2012-10-16T15:08:40Z
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.
Comment #1 by 4denizzz — 2012-10-16T15:15:57Z
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.)
Comment #2 by 4denizzz — 2012-10-16T15:35:10Z
(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.
Comment #3 by 4denizzz — 2012-10-16T15:48:20Z
(component changed to druntime)
Comment #4 by 4denizzz — 2012-10-17T13:08:25Z
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 ); }
Comment #5 by resume755 — 2012-10-17T19:49:02Z
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 ); }
Comment #6 by alex — 2012-10-17T20:00:07Z
Please submit a pull request to: https://github.com/D-Programming-Language/druntime Thanks!
Comment #7 by 4denizzz — 2012-10-17T20:45:45Z
(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)
Comment #8 by turkeyman — 2019-08-20T06:42:43Z
Comment #9 by dlang-bot — 2019-08-20T07:25:14Z
@TurkeyMan updated dlang/druntime pull request #2745 "Renovate core.atomic, improve and simplify the constraints on functions" fixing this issue: - Add missing features to the API. Renovate core.atomic, improve and simplify the constraints on functions. Most functions are slightly more efficient. Concentrate more specific work into less surface area. Fixes issues 20107, 20106, 20105, 18643, 15007, 8831 https://github.com/dlang/druntime/pull/2745
Comment #10 by dlang-bot — 2019-08-31T10:37:48Z
dlang/druntime pull request #2745 "Renovate core.atomic, improve and simplify the constraints on functions" was merged into master: - 1b139035a32f6aeedcdb8d1589af45e9deb73f88 by Manu Evans: Renovate core.atomic, improve and simplify the constraints on functions. Most functions are slightly more efficient. Concentrate more specific work into less surface area. Fixes issues 20107, 18643, 15007, 8831 https://github.com/dlang/druntime/pull/2745