spinlock-cas.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * include/asm-sh/spinlock-cas.h
  3. *
  4. * Copyright (C) 2015 SEI
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #ifndef __ASM_SH_SPINLOCK_CAS_H
  11. #define __ASM_SH_SPINLOCK_CAS_H
  12. #include <asm/barrier.h>
  13. #include <asm/processor.h>
  14. static inline unsigned __sl_cas(volatile unsigned *p, unsigned old, unsigned new)
  15. {
  16. __asm__ __volatile__("cas.l %1,%0,@r0"
  17. : "+r"(new)
  18. : "r"(old), "z"(p)
  19. : "t", "memory" );
  20. return new;
  21. }
  22. /*
  23. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  24. */
  25. #define arch_spin_is_locked(x) ((x)->lock <= 0)
  26. static inline void arch_spin_lock(arch_spinlock_t *lock)
  27. {
  28. while (!__sl_cas(&lock->lock, 1, 0));
  29. }
  30. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  31. {
  32. __sl_cas(&lock->lock, 0, 1);
  33. }
  34. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  35. {
  36. return __sl_cas(&lock->lock, 1, 0);
  37. }
  38. /*
  39. * Read-write spinlocks, allowing multiple readers but only one writer.
  40. *
  41. * NOTE! it is quite common to have readers in interrupts but no interrupt
  42. * writers. For those circumstances we can "mix" irq-safe locks - any writer
  43. * needs to get a irq-safe write-lock, but readers can get non-irqsafe
  44. * read-locks.
  45. */
  46. static inline void arch_read_lock(arch_rwlock_t *rw)
  47. {
  48. unsigned old;
  49. do old = rw->lock;
  50. while (!old || __sl_cas(&rw->lock, old, old-1) != old);
  51. }
  52. static inline void arch_read_unlock(arch_rwlock_t *rw)
  53. {
  54. unsigned old;
  55. do old = rw->lock;
  56. while (__sl_cas(&rw->lock, old, old+1) != old);
  57. }
  58. static inline void arch_write_lock(arch_rwlock_t *rw)
  59. {
  60. while (__sl_cas(&rw->lock, RW_LOCK_BIAS, 0) != RW_LOCK_BIAS);
  61. }
  62. static inline void arch_write_unlock(arch_rwlock_t *rw)
  63. {
  64. __sl_cas(&rw->lock, 0, RW_LOCK_BIAS);
  65. }
  66. static inline int arch_read_trylock(arch_rwlock_t *rw)
  67. {
  68. unsigned old;
  69. do old = rw->lock;
  70. while (old && __sl_cas(&rw->lock, old, old-1) != old);
  71. return !!old;
  72. }
  73. static inline int arch_write_trylock(arch_rwlock_t *rw)
  74. {
  75. return __sl_cas(&rw->lock, RW_LOCK_BIAS, 0) == RW_LOCK_BIAS;
  76. }
  77. #endif /* __ASM_SH_SPINLOCK_CAS_H */