semaphore.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 __MUTEX_INITIALIZER(name) \
  35. __SEMAPHORE_INITIALIZER(name, 1)
  36. #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
  37. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  38. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  39. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
  40. static inline void sema_init (struct semaphore *sem, int val)
  41. {
  42. atomic_set(&sem->count, val);
  43. init_waitqueue_head(&sem->wait);
  44. }
  45. static inline void init_MUTEX (struct semaphore *sem)
  46. {
  47. sema_init(sem, 1);
  48. }
  49. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  50. {
  51. sema_init(sem, 0);
  52. }
  53. extern void __down(struct semaphore * sem);
  54. extern int __down_interruptible(struct semaphore * sem);
  55. extern void __up(struct semaphore * sem);
  56. extern inline void down(struct semaphore * sem)
  57. {
  58. might_sleep();
  59. /*
  60. * Try to get the semaphore, take the slow path if we fail.
  61. */
  62. if (atomic_dec_return(&sem->count) < 0)
  63. __down(sem);
  64. smp_wmb();
  65. }
  66. extern inline int down_interruptible(struct semaphore * sem)
  67. {
  68. int ret = 0;
  69. might_sleep();
  70. if (atomic_dec_return(&sem->count) < 0)
  71. ret = __down_interruptible(sem);
  72. smp_wmb();
  73. return ret;
  74. }
  75. extern inline int down_trylock(struct semaphore * sem)
  76. {
  77. int ret;
  78. ret = atomic_dec_if_positive(&sem->count) < 0;
  79. smp_wmb();
  80. return ret;
  81. }
  82. extern inline void up(struct semaphore * sem)
  83. {
  84. smp_wmb();
  85. if (atomic_inc_return(&sem->count) <= 0)
  86. __up(sem);
  87. }
  88. #endif /* __KERNEL__ */
  89. #endif /* !(_PPC_SEMAPHORE_H) */