semaphore.c 3.7 KB

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