semaphore.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef _BFIN_SEMAPHORE_H
  2. #define _BFIN_SEMAPHORE_H
  3. #ifndef __ASSEMBLY__
  4. #include <linux/linkage.h>
  5. #include <linux/wait.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/rwsem.h>
  8. #include <asm/atomic.h>
  9. /*
  10. * Interrupt-safe semaphores..
  11. *
  12. * (C) Copyright 1996 Linus Torvalds
  13. *
  14. * BFIN version by akbar hussain Lineo Inc April 2001
  15. *
  16. */
  17. struct semaphore {
  18. atomic_t count;
  19. int sleepers;
  20. wait_queue_head_t wait;
  21. };
  22. #define __SEMAPHORE_INITIALIZER(name, n) \
  23. { \
  24. .count = ATOMIC_INIT(n), \
  25. .sleepers = 0, \
  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. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
  34. }
  35. static inline void init_MUTEX(struct semaphore *sem)
  36. {
  37. sema_init(sem, 1);
  38. }
  39. static inline void init_MUTEX_LOCKED(struct semaphore *sem)
  40. {
  41. sema_init(sem, 0);
  42. }
  43. asmlinkage void __down(struct semaphore *sem);
  44. asmlinkage int __down_interruptible(struct semaphore *sem);
  45. asmlinkage int __down_trylock(struct semaphore *sem);
  46. asmlinkage void __up(struct semaphore *sem);
  47. extern spinlock_t semaphore_wake_lock;
  48. /*
  49. * This is ugly, but we want the default case to fall through.
  50. * "down_failed" is a special asm handler that calls the C
  51. * routine that actually waits.
  52. */
  53. static inline void down(struct semaphore *sem)
  54. {
  55. might_sleep();
  56. if (atomic_dec_return(&sem->count) < 0)
  57. __down(sem);
  58. }
  59. static inline int down_interruptible(struct semaphore *sem)
  60. {
  61. int ret = 0;
  62. might_sleep();
  63. if (atomic_dec_return(&sem->count) < 0)
  64. ret = __down_interruptible(sem);
  65. return (ret);
  66. }
  67. static inline int down_trylock(struct semaphore *sem)
  68. {
  69. int ret = 0;
  70. if (atomic_dec_return(&sem->count) < 0)
  71. ret = __down_trylock(sem);
  72. return ret;
  73. }
  74. /*
  75. * Note! This is subtle. We jump to wake people up only if
  76. * the semaphore was negative (== somebody was waiting on it).
  77. * The default case (no contention) will result in NO
  78. * jumps for both down() and up().
  79. */
  80. static inline void up(struct semaphore *sem)
  81. {
  82. if (atomic_inc_return(&sem->count) <= 0)
  83. __up(sem);
  84. }
  85. #endif /* __ASSEMBLY__ */
  86. #endif /* _BFIN_SEMAPHORE_H */