Re: [PATCH] udev enhancements to use kernel event queue

Paul Mackerras
2003-06-12T23:31:50Z

Patrick Mochel writes:

> It seems like the following should work. Would someone mind commenting on
> it?

> +/**
> + * atomic_inc_and_read - increment atomic variable and return new value
> + * @v: pointer of type atomic_t
> + *
> + * Atomically increments @v by 1. Note that the guaranteed
> + * useful range of an atomic_t is only 24 bits.
> + */
> +static inline int atomic_inc_and_read(atomic_t *v)
> +{
> + __asm__ __volatile__(
> + LOCK "incl %0"
> + :"=m" (v->counter)
> + :"m" (v->counter));
> + return v->counter;
> +}

BZZZT. If another CPU is also doing atomic_inc_and_read you could end
up with both calls returning the same value.

You can't do atomic_inc_and_read on 386. You can on cpus that have
cmpxchg (e.g. later x86). You can also on machines with load-locked
and store-conditional instructions (alpha, ppc, probably most other
RISCs).

Paul.