[EMAIL PROTECTED] wrote:
Ian Appru wrote:
While atomic increment/decrements (using cas) are suffiently well documented 
for me to be pretty
 confident in my implementation I would apreciate any input for an atomic 
conditional increment.
I'd expect the following to work on OpenSolaris:

#include <atomic.h>

int atomic_conditional_increment(int *target)
{
    for (;;) {
        int old = *target;
        if (old == 0)
            return 0;

        if (atomic_cas_uint((uint_t *) target, old, old + 1) == old)
            return old + 1;
    }
}

You will certainly need a membar before reading *target.

(If not, it can happen that one thread continuously sees "0" while
another CPU runs away incrementing the counter.

Can you elaborate? Which membar do you think is necessary? I don't see any ordering requirement outside of atomic_cas_uint(). (Data cache coherency is guaranteed by SPARC V9. membars only offer ordering and sequencing guarantees.)

(And you function has a bug in that it will not always update a non-zero
target)

Again, can you elaborate? I think the load of a 0 target will always result in an atomic_cas_uint(). That atomic_cas_uint() may fail to increment *target, but we'll follow that up with another load of the target and another atomic_cas_uint(), ad infinitum.
_______________________________________________
opensolaris-discuss mailing list
[email protected]

Reply via email to