On Wed, Apr 20, 2016 at 09:24:00PM +0800, Pan Xinhui wrote:

> +#define __XCHG_GEN(cmp, type, sfx, skip, v)                          \
> +static __always_inline unsigned long                                 \
> +__cmpxchg_u32##sfx(v unsigned int *p, unsigned long old,             \
> +                      unsigned long new);                            \
> +static __always_inline u32                                           \
> +__##cmp##xchg_##type##sfx(v void *ptr, u32 old, u32 new)             \
> +{                                                                    \
> +     int size = sizeof (type);                                       \
> +     int off = (unsigned long)ptr % sizeof(u32);                     \
> +     volatile u32 *p = ptr - off;                                    \
> +     int bitoff = BITOFF_CAL(size, off);                             \
> +     u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;    \
> +     u32 oldv, newv, tmp;                                            \
> +     u32 ret;                                                        \
> +     oldv = READ_ONCE(*p);                                           \
> +     do {                                                            \
> +             ret = (oldv & bitmask) >> bitoff;                       \
> +             if (skip && ret != old)                                 \
> +                     break;                                          \
> +             newv = (oldv & ~bitmask) | (new << bitoff);             \
> +             tmp = oldv;                                             \
> +             oldv = __cmpxchg_u32##sfx((v u32*)p, oldv, newv);       \
> +     } while (tmp != oldv);                                          \
> +     return ret;                                                     \
> +}

So for an LL/SC based arch using cmpxchg() like that is sub-optimal.

Why did you choose to write it entirely in C?

Reply via email to