atomic.h 1007 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef ATOMIC_H
  2. #define ATOMIC_H
  3. #include "irq.h"
  4. /*
  5. * Atomic operations (implemented by locking out IRQ)
  6. */
  7. #define atomic_cmpxchg(__p,__o,__n) \
  8. ({ \
  9. volatile __typeof__(__p) *___p = &(__p); \
  10. irqmask_t __irq = disable_irqs(); \
  11. bool __ok = *___p == (__o); \
  12. if (__ok) \
  13. *___p = (__n); \
  14. restore_irqs(__irq); \
  15. __ok; \
  16. })
  17. #define atomic_xchg_op(__p,__var,__op) \
  18. ({ \
  19. volatile __typeof__(__p) *___p = &(__p); \
  20. irqmask_t __irq = disable_irqs(); \
  21. __typeof__(*___p) __var = *___p; \
  22. *___p = (__op); \
  23. restore_irqs(__irq); \
  24. __var; \
  25. })
  26. #define atomic_xchg(__p,__a) atomic_xchg_op(__p,__v,__v)
  27. #define atomic_xchg_add(__p,__a) atomic_xchg_op(__p,__v,__v + (__a))
  28. #define atomic_xchg_and(__p,__a) atomic_xchg_op(__p,__v,__v & (__a))
  29. #define atomic_xchg_or(__p,__a) atomic_xchg_op(__p,__v,__v | (__a))
  30. #define atomic_xchg_xor(__p,__a) atomic_xchg_op(__p,__v,__v ^ (__a))
  31. #endif /* ATOMIC_H */