semaphore.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SMP- and interrupt-safe semaphores.
  2. * PA-RISC version by Matthew Wilcox
  3. *
  4. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  5. * Copyright (C) 1996 Linus Torvalds
  6. * Copyright (C) 1999-2001 Matthew Wilcox < willy at debian d0T org >
  7. * Copyright (C) 2000 Grant Grundler < grundler a debian org >
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #ifndef _ASM_PARISC_SEMAPHORE_H
  24. #define _ASM_PARISC_SEMAPHORE_H
  25. #include <linux/spinlock.h>
  26. #include <linux/wait.h>
  27. #include <linux/rwsem.h>
  28. #include <asm/system.h>
  29. /*
  30. * The `count' is initialised to the number of people who are allowed to
  31. * take the lock. (Normally we want a mutex, so this is `1'). if
  32. * `count' is positive, the lock can be taken. if it's 0, no-one is
  33. * waiting on it. if it's -1, at least one task is waiting.
  34. */
  35. struct semaphore {
  36. spinlock_t sentry;
  37. int count;
  38. wait_queue_head_t wait;
  39. };
  40. #define __SEMAPHORE_INITIALIZER(name, n) \
  41. { \
  42. .sentry = SPIN_LOCK_UNLOCKED, \
  43. .count = n, \
  44. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  45. }
  46. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  47. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  48. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  49. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  50. extern inline void sema_init (struct semaphore *sem, int val)
  51. {
  52. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  53. }
  54. static inline void init_MUTEX (struct semaphore *sem)
  55. {
  56. sema_init(sem, 1);
  57. }
  58. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  59. {
  60. sema_init(sem, 0);
  61. }
  62. static inline int sem_getcount(struct semaphore *sem)
  63. {
  64. return sem->count;
  65. }
  66. asmlinkage void __down(struct semaphore * sem);
  67. asmlinkage int __down_interruptible(struct semaphore * sem);
  68. asmlinkage void __up(struct semaphore * sem);
  69. /* Semaphores can be `tried' from irq context. So we have to disable
  70. * interrupts while we're messing with the semaphore. Sorry.
  71. */
  72. extern __inline__ void down(struct semaphore * sem)
  73. {
  74. might_sleep();
  75. spin_lock_irq(&sem->sentry);
  76. if (sem->count > 0) {
  77. sem->count--;
  78. } else {
  79. __down(sem);
  80. }
  81. spin_unlock_irq(&sem->sentry);
  82. }
  83. extern __inline__ int down_interruptible(struct semaphore * sem)
  84. {
  85. int ret = 0;
  86. might_sleep();
  87. spin_lock_irq(&sem->sentry);
  88. if (sem->count > 0) {
  89. sem->count--;
  90. } else {
  91. ret = __down_interruptible(sem);
  92. }
  93. spin_unlock_irq(&sem->sentry);
  94. return ret;
  95. }
  96. /*
  97. * down_trylock returns 0 on success, 1 if we failed to get the lock.
  98. * May not sleep, but must preserve irq state
  99. */
  100. extern __inline__ int down_trylock(struct semaphore * sem)
  101. {
  102. int flags, count;
  103. spin_lock_irqsave(&sem->sentry, flags);
  104. count = sem->count - 1;
  105. if (count >= 0)
  106. sem->count = count;
  107. spin_unlock_irqrestore(&sem->sentry, flags);
  108. return (count < 0);
  109. }
  110. /*
  111. * Note! This is subtle. We jump to wake people up only if
  112. * the semaphore was negative (== somebody was waiting on it).
  113. */
  114. extern __inline__ void up(struct semaphore * sem)
  115. {
  116. int flags;
  117. spin_lock_irqsave(&sem->sentry, flags);
  118. if (sem->count < 0) {
  119. __up(sem);
  120. } else {
  121. sem->count++;
  122. }
  123. spin_unlock_irqrestore(&sem->sentry, flags);
  124. }
  125. #endif /* _ASM_PARISC_SEMAPHORE_H */