semaphore.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. static inline void sema_init (struct semaphore *sem, int val)
  32. {
  33. atomic_set(&sem->count, val);
  34. init_waitqueue_head(&sem->wait);
  35. }
  36. static inline void init_MUTEX (struct semaphore *sem)
  37. {
  38. sema_init(sem, 1);
  39. }
  40. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  41. {
  42. sema_init(sem, 0);
  43. }
  44. extern void __down(struct semaphore * sem);
  45. extern int __down_interruptible(struct semaphore * sem);
  46. extern void __up(struct semaphore * sem);
  47. static inline void down(struct semaphore * sem)
  48. {
  49. might_sleep();
  50. /*
  51. * Try to get the semaphore, take the slow path if we fail.
  52. */
  53. if (unlikely(atomic_dec_return(&sem->count) < 0))
  54. __down(sem);
  55. }
  56. static inline int down_interruptible(struct semaphore * sem)
  57. {
  58. int ret = 0;
  59. might_sleep();
  60. if (unlikely(atomic_dec_return(&sem->count) < 0))
  61. ret = __down_interruptible(sem);
  62. return ret;
  63. }
  64. static inline int down_trylock(struct semaphore * sem)
  65. {
  66. return atomic_dec_if_positive(&sem->count) < 0;
  67. }
  68. static inline void up(struct semaphore * sem)
  69. {
  70. if (unlikely(atomic_inc_return(&sem->count) <= 0))
  71. __up(sem);
  72. }
  73. #endif /* __KERNEL__ */
  74. #endif /* _ASM_POWERPC_SEMAPHORE_H */