All pastes #2065008 Raw Edit

pseudo atomic test and clear bit

public c v1 · immutable
#2065008 ·published 2011-05-19 19:42 UTC
rendered paste body
/*  pseudo atomic test and clear bit * *  Useful for scanning a bitfield while not dirtying shared *  cache lines. * *  This function behaves like the atomic op "test_and_clear_bit" *  except if the bit is already clear it doesn't write to the *  address. It is prone to interrupts but recovers if the data *  at the address is altered during the interrupt. When it does *  change the data at the address it does so atomicly. */ static inline int pseudo_atomic_test_and_clear_bit(int nr,volatile unsigned long * addr){         unsigned long mask = BIT_MASK(nr);         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);         unsigned long old;missed:         old = *p;if(!(old & mask)) return 0;        if(old != cmpxchg(p,old,old & ~mask)) goto missed;        smp_wmb();         return 1;}