cmpxchg-local.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef __ASM_GENERIC_CMPXCHG_LOCAL_H
  2. #define __ASM_GENERIC_CMPXCHG_LOCAL_H
  3. #include <linux/types.h>
  4. #include <linux/irqflags.h>
  5. extern unsigned long wrong_size_cmpxchg(volatile void *ptr);
  6. /*
  7. * Generic version of __cmpxchg_local (disables interrupts). Takes an unsigned
  8. * long parameter, supporting various types of architectures.
  9. */
  10. static inline unsigned long __cmpxchg_local_generic(volatile void *ptr,
  11. unsigned long old, unsigned long new, int size)
  12. {
  13. unsigned long flags, prev;
  14. /*
  15. * Sanity checking, compile-time.
  16. */
  17. if (size == 8 && sizeof(unsigned long) != 8)
  18. wrong_size_cmpxchg(ptr);
  19. local_irq_save(flags);
  20. switch (size) {
  21. case 1: prev = *(u8 *)ptr;
  22. if (prev == old)
  23. *(u8 *)ptr = (u8)new;
  24. break;
  25. case 2: prev = *(u16 *)ptr;
  26. if (prev == old)
  27. *(u16 *)ptr = (u16)new;
  28. break;
  29. case 4: prev = *(u32 *)ptr;
  30. if (prev == old)
  31. *(u32 *)ptr = (u32)new;
  32. break;
  33. case 8: prev = *(u64 *)ptr;
  34. if (prev == old)
  35. *(u64 *)ptr = (u64)new;
  36. break;
  37. default:
  38. wrong_size_cmpxchg(ptr);
  39. }
  40. local_irq_restore(flags);
  41. return prev;
  42. }
  43. /*
  44. * Generic version of __cmpxchg64_local. Takes an u64 parameter.
  45. */
  46. static inline u64 __cmpxchg64_local_generic(volatile void *ptr,
  47. u64 old, u64 new)
  48. {
  49. u64 prev;
  50. unsigned long flags;
  51. local_irq_save(flags);
  52. prev = *(u64 *)ptr;
  53. if (prev == old)
  54. *(u64 *)ptr = new;
  55. local_irq_restore(flags);
  56. return prev;
  57. }
  58. #endif