spinlock.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * include/asm-s390/spinlock.h
  3. *
  4. * S390 version
  5. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. *
  8. * Derived from "include/asm-i386/spinlock.h"
  9. */
  10. #ifndef __ASM_SPINLOCK_H
  11. #define __ASM_SPINLOCK_H
  12. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  13. static inline int
  14. _raw_compare_and_swap(volatile unsigned int *lock,
  15. unsigned int old, unsigned int new)
  16. {
  17. asm volatile(
  18. " cs %0,%3,%1"
  19. : "=d" (old), "=Q" (*lock)
  20. : "0" (old), "d" (new), "Q" (*lock)
  21. : "cc", "memory" );
  22. return old;
  23. }
  24. #else /* __GNUC__ */
  25. static inline int
  26. _raw_compare_and_swap(volatile unsigned int *lock,
  27. unsigned int old, unsigned int new)
  28. {
  29. asm volatile(
  30. " cs %0,%3,0(%4)"
  31. : "=d" (old), "=m" (*lock)
  32. : "0" (old), "d" (new), "a" (lock), "m" (*lock)
  33. : "cc", "memory" );
  34. return old;
  35. }
  36. #endif /* __GNUC__ */
  37. /*
  38. * Simple spin lock operations. There are two variants, one clears IRQ's
  39. * on the local processor, one does not.
  40. *
  41. * We make no fairness assumptions. They have a cost.
  42. *
  43. * (the type definitions are in asm/spinlock_types.h)
  44. */
  45. #define __raw_spin_is_locked(x) ((x)->lock != 0)
  46. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  47. #define __raw_spin_unlock_wait(lock) \
  48. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  49. extern void _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc);
  50. extern int _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc);
  51. static inline void __raw_spin_lock(raw_spinlock_t *lp)
  52. {
  53. unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
  54. if (unlikely(_raw_compare_and_swap(&lp->lock, 0, pc) != 0))
  55. _raw_spin_lock_wait(lp, pc);
  56. }
  57. static inline int __raw_spin_trylock(raw_spinlock_t *lp)
  58. {
  59. unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
  60. if (likely(_raw_compare_and_swap(&lp->lock, 0, pc) == 0))
  61. return 1;
  62. return _raw_spin_trylock_retry(lp, pc);
  63. }
  64. static inline void __raw_spin_unlock(raw_spinlock_t *lp)
  65. {
  66. _raw_compare_and_swap(&lp->lock, lp->lock, 0);
  67. }
  68. /*
  69. * Read-write spinlocks, allowing multiple readers
  70. * but only one writer.
  71. *
  72. * NOTE! it is quite common to have readers in interrupts
  73. * but no interrupt writers. For those circumstances we
  74. * can "mix" irq-safe locks - any writer needs to get a
  75. * irq-safe write-lock, but readers can get non-irqsafe
  76. * read-locks.
  77. */
  78. /**
  79. * read_can_lock - would read_trylock() succeed?
  80. * @lock: the rwlock in question.
  81. */
  82. #define __raw_read_can_lock(x) ((int)(x)->lock >= 0)
  83. /**
  84. * write_can_lock - would write_trylock() succeed?
  85. * @lock: the rwlock in question.
  86. */
  87. #define __raw_write_can_lock(x) ((x)->lock == 0)
  88. extern void _raw_read_lock_wait(raw_rwlock_t *lp);
  89. extern int _raw_read_trylock_retry(raw_rwlock_t *lp);
  90. extern void _raw_write_lock_wait(raw_rwlock_t *lp);
  91. extern int _raw_write_trylock_retry(raw_rwlock_t *lp);
  92. static inline void __raw_read_lock(raw_rwlock_t *rw)
  93. {
  94. unsigned int old;
  95. old = rw->lock & 0x7fffffffU;
  96. if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
  97. _raw_read_lock_wait(rw);
  98. }
  99. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  100. {
  101. unsigned int old, cmp;
  102. old = rw->lock;
  103. do {
  104. cmp = old;
  105. old = _raw_compare_and_swap(&rw->lock, old, old - 1);
  106. } while (cmp != old);
  107. }
  108. static inline void __raw_write_lock(raw_rwlock_t *rw)
  109. {
  110. if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
  111. _raw_write_lock_wait(rw);
  112. }
  113. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  114. {
  115. _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
  116. }
  117. static inline int __raw_read_trylock(raw_rwlock_t *rw)
  118. {
  119. unsigned int old;
  120. old = rw->lock & 0x7fffffffU;
  121. if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
  122. return 1;
  123. return _raw_read_trylock_retry(rw);
  124. }
  125. static inline int __raw_write_trylock(raw_rwlock_t *rw)
  126. {
  127. if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
  128. return 1;
  129. return _raw_write_trylock_retry(rw);
  130. }
  131. #endif /* __ASM_SPINLOCK_H */