spinlock-cas.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  27. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  28. {
  29. smp_cond_load_acquire(&lock->lock, VAL > 0);
  30. }
  31. static inline void arch_spin_lock(arch_spinlock_t *lock)
  32. {
  33. while (!__sl_cas(&lock->lock, 1, 0));
  34. }
  35. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  36. {
  37. __sl_cas(&lock->lock, 0, 1);
  38. }
  39. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  40. {
  41. return __sl_cas(&lock->lock, 1, 0);
  42. }
  43. /*
  44. * Read-write spinlocks, allowing multiple readers but only one writer.
  45. *
  46. * NOTE! it is quite common to have readers in interrupts but no interrupt
  47. * writers. For those circumstances we can "mix" irq-safe locks - any writer
  48. * needs to get a irq-safe write-lock, but readers can get non-irqsafe
  49. * read-locks.
  50. */
  51. /**
  52. * read_can_lock - would read_trylock() succeed?
  53. * @lock: the rwlock in question.
  54. */
  55. #define arch_read_can_lock(x) ((x)->lock > 0)
  56. /**
  57. * write_can_lock - would write_trylock() succeed?
  58. * @lock: the rwlock in question.
  59. */
  60. #define arch_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  61. static inline void arch_read_lock(arch_rwlock_t *rw)
  62. {
  63. unsigned old;
  64. do old = rw->lock;
  65. while (!old || __sl_cas(&rw->lock, old, old-1) != old);
  66. }
  67. static inline void arch_read_unlock(arch_rwlock_t *rw)
  68. {
  69. unsigned old;
  70. do old = rw->lock;
  71. while (__sl_cas(&rw->lock, old, old+1) != old);
  72. }
  73. static inline void arch_write_lock(arch_rwlock_t *rw)
  74. {
  75. while (__sl_cas(&rw->lock, RW_LOCK_BIAS, 0) != RW_LOCK_BIAS);
  76. }
  77. static inline void arch_write_unlock(arch_rwlock_t *rw)
  78. {
  79. __sl_cas(&rw->lock, 0, RW_LOCK_BIAS);
  80. }
  81. static inline int arch_read_trylock(arch_rwlock_t *rw)
  82. {
  83. unsigned old;
  84. do old = rw->lock;
  85. while (old && __sl_cas(&rw->lock, old, old-1) != old);
  86. return !!old;
  87. }
  88. static inline int arch_write_trylock(arch_rwlock_t *rw)
  89. {
  90. return __sl_cas(&rw->lock, RW_LOCK_BIAS, 0) == RW_LOCK_BIAS;
  91. }
  92. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  93. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  94. #define arch_spin_relax(lock) cpu_relax()
  95. #define arch_read_relax(lock) cpu_relax()
  96. #define arch_write_relax(lock) cpu_relax()
  97. #endif /* __ASM_SH_SPINLOCK_CAS_H */