[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.

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

Casper
_______________________________________________
opensolaris-discuss mailing list
[email protected]

Since SPARC is total store order (module vis) , and x86 is strongly
ordered (modulo SSE non-temporal writes and write-combining), the
membars aren't needed. The compiler shouldn't need target to be
declared volatile, either; passing the pointer to atomic_cas_uint
should force re-evaluation of *target inside the loop.

- Bart


--
Bart Smaalders                  Solaris Kernel Performance
[EMAIL PROTECTED]               http://blogs.sun.com/barts
_______________________________________________
opensolaris-discuss mailing list
[email protected]

Reply via email to