spinlock.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. #include <asm/system.h>
  4. #include <asm/processor.h>
  5. #include <asm/spinlock_types.h>
  6. /* Note that PA-RISC has to use `1' to mean unlocked and `0' to mean locked
  7. * since it only has load-and-zero. Moreover, at least on some PA processors,
  8. * the semaphore address has to be 16-byte aligned.
  9. */
  10. static inline int __raw_spin_is_locked(raw_spinlock_t *x)
  11. {
  12. volatile unsigned int *a = __ldcw_align(x);
  13. return *a == 0;
  14. }
  15. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  16. #define __raw_spin_unlock_wait(x) \
  17. do { cpu_relax(); } while (__raw_spin_is_locked(x))
  18. static inline void __raw_spin_lock(raw_spinlock_t *x)
  19. {
  20. volatile unsigned int *a;
  21. mb();
  22. a = __ldcw_align(x);
  23. while (__ldcw(a) == 0)
  24. while (*a == 0);
  25. mb();
  26. }
  27. static inline void __raw_spin_unlock(raw_spinlock_t *x)
  28. {
  29. volatile unsigned int *a;
  30. mb();
  31. a = __ldcw_align(x);
  32. *a = 1;
  33. mb();
  34. }
  35. static inline int __raw_spin_trylock(raw_spinlock_t *x)
  36. {
  37. volatile unsigned int *a;
  38. int ret;
  39. mb();
  40. a = __ldcw_align(x);
  41. ret = __ldcw(a) != 0;
  42. mb();
  43. return ret;
  44. }
  45. /*
  46. * Read-write spinlocks, allowing multiple readers
  47. * but only one writer.
  48. */
  49. #define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
  50. /* read_lock, read_unlock are pretty straightforward. Of course it somehow
  51. * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
  52. static __inline__ void __raw_read_lock(raw_rwlock_t *rw)
  53. {
  54. unsigned long flags;
  55. local_irq_save(flags);
  56. __raw_spin_lock(&rw->lock);
  57. rw->counter++;
  58. __raw_spin_unlock(&rw->lock);
  59. local_irq_restore(flags);
  60. }
  61. static __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
  62. {
  63. unsigned long flags;
  64. local_irq_save(flags);
  65. __raw_spin_lock(&rw->lock);
  66. rw->counter--;
  67. __raw_spin_unlock(&rw->lock);
  68. local_irq_restore(flags);
  69. }
  70. /* write_lock is less trivial. We optimistically grab the lock and check
  71. * if we surprised any readers. If so we release the lock and wait till
  72. * they're all gone before trying again
  73. *
  74. * Also note that we don't use the _irqsave / _irqrestore suffixes here.
  75. * If we're called with interrupts enabled and we've got readers (or other
  76. * writers) in interrupt handlers someone fucked up and we'd dead-lock
  77. * sooner or later anyway. prumpf */
  78. static __inline__ void __raw_write_lock(raw_rwlock_t *rw)
  79. {
  80. retry:
  81. __raw_spin_lock(&rw->lock);
  82. if(rw->counter != 0) {
  83. /* this basically never happens */
  84. __raw_spin_unlock(&rw->lock);
  85. while (rw->counter != 0)
  86. cpu_relax();
  87. goto retry;
  88. }
  89. /* got it. now leave without unlocking */
  90. rw->counter = -1; /* remember we are locked */
  91. }
  92. /* write_unlock is absolutely trivial - we don't have to wait for anything */
  93. static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
  94. {
  95. rw->counter = 0;
  96. __raw_spin_unlock(&rw->lock);
  97. }
  98. static __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
  99. {
  100. __raw_spin_lock(&rw->lock);
  101. if (rw->counter != 0) {
  102. /* this basically never happens */
  103. __raw_spin_unlock(&rw->lock);
  104. return 0;
  105. }
  106. /* got it. now leave without unlocking */
  107. rw->counter = -1; /* remember we are locked */
  108. return 1;
  109. }
  110. static __inline__ int __raw_is_read_locked(raw_rwlock_t *rw)
  111. {
  112. return rw->counter > 0;
  113. }
  114. static __inline__ int __raw_is_write_locked(raw_rwlock_t *rw)
  115. {
  116. return rw->counter < 0;
  117. }
  118. #endif /* __ASM_SPINLOCK_H */