semaphore.c 3.6 KB

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