Carlos Valiente wrote:

> On Friday 23 February 2001 10:32, Edouard G. Parmelan wrote:
> 
> > > I've copied and pasted the C version of COMPARE_AND_EXCHANGE from
> > > config/mips/common.h and everything seems to work OK.
> >
> > Be careful, this C version will only work with unix-jthread subsystem.
> 
> Oops, I hadn't thought about it :-( What would be a cleaner solution? Perhaps 
> an implementation in PowerPC assembler?

Yep.  Could you test this:

/*
 * Do an atomic compare and exchange.  The address 'A' is checked against 
 * value 'O' and if they match it's exchanged with value 'N'.
 * We return '1' if the exchange is sucessful, otherwise 0.
 */
#define COMPARE_AND_EXCHANGE(A,O,N)             \
({                                              \
        int tmp, ret = 0;                       \
                                                \
        asm volatile(                           \
        "1:     lwarx   %0,0,%3\n"              \
        "       cmpw    0,%0,%4\n"              \
        "       bne     2f\n"                   \
        "       stwcx   %5,0,%3\n"              \
        "       bne-    1b\n"                   \
        "       sync\n"                         \
        "       li      %1,1\n"                 \
        "2:\n"                                  \
        : "=&r"(tmp), "=&r"(ret), "=m"(*(A))    \
        : "r"(A), "r"(O), "r"(N), "m"(*(A))     \
        : "cc", "memory");                      \
                                                \
        ret;                                    \
})

-- 
Edouard G. Parmelan
http://egp.free.fr

Reply via email to