semaphore.h 2.1 KB

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