semaphore.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef _PPC_SEMAPHORE_H
  2. #define _PPC_SEMAPHORE_H
  3. /*
  4. * Swiped from asm-sparc/semaphore.h and modified
  5. * -- Cort (cort@cs.nmt.edu)
  6. *
  7. * Stole some rw spinlock-based semaphore stuff from asm-alpha/semaphore.h
  8. * -- Ani Joshi (ajoshi@unixbox.com)
  9. *
  10. * Remove spinlock-based RW semaphores; RW semaphore definitions are
  11. * now in rwsem.h and we use the generic lib/rwsem.c implementation.
  12. * Rework semaphores to use atomic_dec_if_positive.
  13. * -- Paul Mackerras (paulus@samba.org)
  14. */
  15. #ifdef __KERNEL__
  16. #include <asm/atomic.h>
  17. #include <asm/system.h>
  18. #include <linux/wait.h>
  19. #include <linux/rwsem.h>
  20. struct semaphore {
  21. /*
  22. * Note that any negative value of count is equivalent to 0,
  23. * but additionally indicates that some process(es) might be
  24. * sleeping on `wait'.
  25. */
  26. atomic_t count;
  27. wait_queue_head_t wait;
  28. };
  29. #define __SEMAPHORE_INITIALIZER(name, n) \
  30. { \
  31. .count = ATOMIC_INIT(n), \
  32. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  33. }
  34. #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
  35. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  36. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  37. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
  38. static inline void sema_init (struct semaphore *sem, int val)
  39. {
  40. atomic_set(&sem->count, val);
  41. init_waitqueue_head(&sem->wait);
  42. }
  43. static inline void init_MUTEX (struct semaphore *sem)
  44. {
  45. sema_init(sem, 1);
  46. }
  47. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  48. {
  49. sema_init(sem, 0);
  50. }
  51. extern void __down(struct semaphore * sem);
  52. extern int __down_interruptible(struct semaphore * sem);
  53. extern void __up(struct semaphore * sem);
  54. extern inline void down(struct semaphore * sem)
  55. {
  56. might_sleep();
  57. /*
  58. * Try to get the semaphore, take the slow path if we fail.
  59. */
  60. if (atomic_dec_return(&sem->count) < 0)
  61. __down(sem);
  62. smp_wmb();
  63. }
  64. extern inline int down_interruptible(struct semaphore * sem)
  65. {
  66. int ret = 0;
  67. might_sleep();
  68. if (atomic_dec_return(&sem->count) < 0)
  69. ret = __down_interruptible(sem);
  70. smp_wmb();
  71. return ret;
  72. }
  73. extern inline int down_trylock(struct semaphore * sem)
  74. {
  75. int ret;
  76. ret = atomic_dec_if_positive(&sem->count) < 0;
  77. smp_wmb();
  78. return ret;
  79. }
  80. extern inline void up(struct semaphore * sem)
  81. {
  82. smp_wmb();
  83. if (atomic_inc_return(&sem->count) <= 0)
  84. __up(sem);
  85. }
  86. #endif /* __KERNEL__ */
  87. #endif /* !(_PPC_SEMAPHORE_H) */