cmpxchg_32.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* 32-bit atomic xchg() and cmpxchg() definitions.
  2. *
  3. * Copyright (C) 1996 David S. Miller ([email protected])
  4. * Copyright (C) 2000 Anton Blanchard ([email protected])
  5. * Copyright (C) 2007 Kyle McMartin ([email protected])
  6. *
  7. * Additions by Keith M Wesolowski ([email protected]) based
  8. * on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf <[email protected]>.
  9. */
  10. #ifndef __ARCH_SPARC_CMPXCHG__
  11. #define __ARCH_SPARC_CMPXCHG__
  12. unsigned long __xchg_u32(volatile u32 *m, u32 new);
  13. void __xchg_called_with_bad_pointer(void);
  14. static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr, int size)
  15. {
  16. switch (size) {
  17. case 4:
  18. return __xchg_u32(ptr, x);
  19. }
  20. __xchg_called_with_bad_pointer();
  21. return x;
  22. }
  23. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  24. /* Emulate cmpxchg() the same way we emulate atomics,
  25. * by hashing the object address and indexing into an array
  26. * of spinlocks to get a bit of performance...
  27. *
  28. * See arch/sparc/lib/atomic32.c for implementation.
  29. *
  30. * Cribbed from <asm-parisc/atomic.h>
  31. */
  32. /* bug catcher for when unsupported size is used - won't link */
  33. void __cmpxchg_called_with_bad_pointer(void);
  34. /* we only need to support cmpxchg of a u32 on sparc */
  35. unsigned long __cmpxchg_u32(volatile u32 *m, u32 old, u32 new_);
  36. /* don't worry...optimizer will get rid of most of this */
  37. static inline unsigned long
  38. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
  39. {
  40. switch (size) {
  41. case 4:
  42. return __cmpxchg_u32((u32 *)ptr, (u32)old, (u32)new_);
  43. default:
  44. __cmpxchg_called_with_bad_pointer();
  45. break;
  46. }
  47. return old;
  48. }
  49. #define cmpxchg(ptr, o, n) \
  50. ({ \
  51. __typeof__(*(ptr)) _o_ = (o); \
  52. __typeof__(*(ptr)) _n_ = (n); \
  53. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  54. (unsigned long)_n_, sizeof(*(ptr))); \
  55. })
  56. #include <asm-generic/cmpxchg-local.h>
  57. /*
  58. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  59. * them available.
  60. */
  61. #define cmpxchg_local(ptr, o, n) \
  62. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  63. (unsigned long)(n), sizeof(*(ptr))))
  64. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  65. #endif /* __ARCH_SPARC_CMPXCHG__ */