semaphore.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996 Linus Torvalds
  7. * Copyright (C) 1998, 99, 2000, 01, 04 Ralf Baechle
  8. * Copyright (C) 1999, 2000, 01 Silicon Graphics, Inc.
  9. * Copyright (C) 2000, 01 MIPS Technologies, Inc.
  10. *
  11. * In all honesty, little of the old MIPS code left - the PPC64 variant was
  12. * just looking nice and portable so I ripped it. Credits to whoever wrote
  13. * it.
  14. */
  15. #ifndef __ASM_SEMAPHORE_H
  16. #define __ASM_SEMAPHORE_H
  17. /*
  18. * Remove spinlock-based RW semaphores; RW semaphore definitions are
  19. * now in rwsem.h and we use the generic lib/rwsem.c implementation.
  20. * Rework semaphores to use atomic_dec_if_positive.
  21. * -- Paul Mackerras (paulus@samba.org)
  22. */
  23. #ifdef __KERNEL__
  24. #include <asm/atomic.h>
  25. #include <asm/system.h>
  26. #include <linux/wait.h>
  27. #include <linux/rwsem.h>
  28. struct semaphore {
  29. /*
  30. * Note that any negative value of count is equivalent to 0,
  31. * but additionally indicates that some process(es) might be
  32. * sleeping on `wait'.
  33. */
  34. atomic_t count;
  35. wait_queue_head_t wait;
  36. };
  37. #define __SEMAPHORE_INITIALIZER(name, n) \
  38. { \
  39. .count = ATOMIC_INIT(n), \
  40. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  41. }
  42. #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
  43. struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
  44. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  45. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
  46. static inline void sema_init(struct semaphore *sem, int val)
  47. {
  48. atomic_set(&sem->count, val);
  49. init_waitqueue_head(&sem->wait);
  50. }
  51. static inline void init_MUTEX(struct semaphore *sem)
  52. {
  53. sema_init(sem, 1);
  54. }
  55. static inline void init_MUTEX_LOCKED(struct semaphore *sem)
  56. {
  57. sema_init(sem, 0);
  58. }
  59. extern void __down(struct semaphore * sem);
  60. extern int __down_interruptible(struct semaphore * sem);
  61. extern void __up(struct semaphore * sem);
  62. static inline void down(struct semaphore * sem)
  63. {
  64. might_sleep();
  65. /*
  66. * Try to get the semaphore, take the slow path if we fail.
  67. */
  68. if (unlikely(atomic_dec_return(&sem->count) < 0))
  69. __down(sem);
  70. }
  71. static inline int down_interruptible(struct semaphore * sem)
  72. {
  73. int ret = 0;
  74. might_sleep();
  75. if (unlikely(atomic_dec_return(&sem->count) < 0))
  76. ret = __down_interruptible(sem);
  77. return ret;
  78. }
  79. static inline int down_trylock(struct semaphore * sem)
  80. {
  81. return atomic_dec_if_positive(&sem->count) < 0;
  82. }
  83. static inline void up(struct semaphore * sem)
  84. {
  85. if (unlikely(atomic_inc_return(&sem->count) <= 0))
  86. __up(sem);
  87. }
  88. #endif /* __KERNEL__ */
  89. #endif /* __ASM_SEMAPHORE_H */