semaphore.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * MIPS-specific semaphore code.
  3. *
  4. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  5. * Copyright (C) 2004 Ralf Baechle <ralf@linux-mips.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * April 2001 - Reworked by Paul Mackerras <paulus@samba.org>
  13. * to eliminate the SMP races in the old version between the updates
  14. * of `count' and `waking'. Now we use negative `count' values to
  15. * indicate that some process(es) are waiting for the semaphore.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/init.h>
  20. #include <asm/atomic.h>
  21. #include <asm/cpu-features.h>
  22. #include <asm/errno.h>
  23. #include <asm/semaphore.h>
  24. #include <asm/war.h>
  25. /*
  26. * Atomically update sem->count.
  27. * This does the equivalent of the following:
  28. *
  29. * old_count = sem->count;
  30. * tmp = MAX(old_count, 0) + incr;
  31. * sem->count = tmp;
  32. * return old_count;
  33. *
  34. * On machines without lld/scd we need a spinlock to make the manipulation of
  35. * sem->count and sem->waking atomic. Scalability isn't an issue because
  36. * this lock is used on UP only so it's just an empty variable.
  37. */
  38. static inline int __sem_update_count(struct semaphore *sem, int incr)
  39. {
  40. int old_count, tmp;
  41. if (cpu_has_llsc && R10000_LLSC_WAR) {
  42. __asm__ __volatile__(
  43. "1: ll %0, %2 \n"
  44. " sra %1, %0, 31 \n"
  45. " not %1 \n"
  46. " and %1, %0, %1 \n"
  47. " add %1, %1, %3 \n"
  48. " sc %1, %2 \n"
  49. " beqzl %1, 1b \n"
  50. : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
  51. : "r" (incr), "m" (sem->count));
  52. } else if (cpu_has_llsc) {
  53. __asm__ __volatile__(
  54. "1: ll %0, %2 \n"
  55. " sra %1, %0, 31 \n"
  56. " not %1 \n"
  57. " and %1, %0, %1 \n"
  58. " add %1, %1, %3 \n"
  59. " sc %1, %2 \n"
  60. " beqz %1, 1b \n"
  61. : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
  62. : "r" (incr), "m" (sem->count));
  63. } else {
  64. static DEFINE_SPINLOCK(semaphore_lock);
  65. unsigned long flags;
  66. spin_lock_irqsave(&semaphore_lock, flags);
  67. old_count = atomic_read(&sem->count);
  68. tmp = max_t(int, old_count, 0) + incr;
  69. atomic_set(&sem->count, tmp);
  70. spin_unlock_irqrestore(&semaphore_lock, flags);
  71. }
  72. return old_count;
  73. }
  74. void __up(struct semaphore *sem)
  75. {
  76. /*
  77. * Note that we incremented count in up() before we came here,
  78. * but that was ineffective since the result was <= 0, and
  79. * any negative value of count is equivalent to 0.
  80. * This ends up setting count to 1, unless count is now > 0
  81. * (i.e. because some other cpu has called up() in the meantime),
  82. * in which case we just increment count.
  83. */
  84. __sem_update_count(sem, 1);
  85. wake_up(&sem->wait);
  86. }
  87. EXPORT_SYMBOL(__up);
  88. /*
  89. * Note that when we come in to __down or __down_interruptible,
  90. * we have already decremented count, but that decrement was
  91. * ineffective since the result was < 0, and any negative value
  92. * of count is equivalent to 0.
  93. * Thus it is only when we decrement count from some value > 0
  94. * that we have actually got the semaphore.
  95. */
  96. void __sched __down(struct semaphore *sem)
  97. {
  98. struct task_struct *tsk = current;
  99. DECLARE_WAITQUEUE(wait, tsk);
  100. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  101. add_wait_queue_exclusive(&sem->wait, &wait);
  102. /*
  103. * Try to get the semaphore. If the count is > 0, then we've
  104. * got the semaphore; we decrement count and exit the loop.
  105. * If the count is 0 or negative, we set it to -1, indicating
  106. * that we are asleep, and then sleep.
  107. */
  108. while (__sem_update_count(sem, -1) <= 0) {
  109. schedule();
  110. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  111. }
  112. remove_wait_queue(&sem->wait, &wait);
  113. __set_task_state(tsk, TASK_RUNNING);
  114. /*
  115. * If there are any more sleepers, wake one of them up so
  116. * that it can either get the semaphore, or set count to -1
  117. * indicating that there are still processes sleeping.
  118. */
  119. wake_up(&sem->wait);
  120. }
  121. EXPORT_SYMBOL(__down);
  122. int __sched __down_interruptible(struct semaphore * sem)
  123. {
  124. int retval = 0;
  125. struct task_struct *tsk = current;
  126. DECLARE_WAITQUEUE(wait, tsk);
  127. __set_task_state(tsk, TASK_INTERRUPTIBLE);
  128. add_wait_queue_exclusive(&sem->wait, &wait);
  129. while (__sem_update_count(sem, -1) <= 0) {
  130. if (signal_pending(current)) {
  131. /*
  132. * A signal is pending - give up trying.
  133. * Set sem->count to 0 if it is negative,
  134. * since we are no longer sleeping.
  135. */
  136. __sem_update_count(sem, 0);
  137. retval = -EINTR;
  138. break;
  139. }
  140. schedule();
  141. set_task_state(tsk, TASK_INTERRUPTIBLE);
  142. }
  143. remove_wait_queue(&sem->wait, &wait);
  144. __set_task_state(tsk, TASK_RUNNING);
  145. wake_up(&sem->wait);
  146. return retval;
  147. }
  148. EXPORT_SYMBOL(__down_interruptible);