semaphore.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  32. static inline void sema_init(struct semaphore *sem, int val)
  33. {
  34. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
  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. asmlinkage void __down(struct semaphore *sem);
  45. asmlinkage int __down_interruptible(struct semaphore *sem);
  46. asmlinkage int __down_trylock(struct semaphore *sem);
  47. asmlinkage void __up(struct semaphore *sem);
  48. extern spinlock_t semaphore_wake_lock;
  49. /*
  50. * This is ugly, but we want the default case to fall through.
  51. * "down_failed" is a special asm handler that calls the C
  52. * routine that actually waits.
  53. */
  54. static inline void down(struct semaphore *sem)
  55. {
  56. might_sleep();
  57. if (atomic_dec_return(&sem->count) < 0)
  58. __down(sem);
  59. }
  60. static inline int down_interruptible(struct semaphore *sem)
  61. {
  62. int ret = 0;
  63. might_sleep();
  64. if (atomic_dec_return(&sem->count) < 0)
  65. ret = __down_interruptible(sem);
  66. return (ret);
  67. }
  68. static inline int down_trylock(struct semaphore *sem)
  69. {
  70. int ret = 0;
  71. if (atomic_dec_return(&sem->count) < 0)
  72. ret = __down_trylock(sem);
  73. return ret;
  74. }
  75. /*
  76. * Note! This is subtle. We jump to wake people up only if
  77. * the semaphore was negative (== somebody was waiting on it).
  78. * The default case (no contention) will result in NO
  79. * jumps for both down() and up().
  80. */
  81. static inline void up(struct semaphore *sem)
  82. {
  83. if (atomic_inc_return(&sem->count) <= 0)
  84. __up(sem);
  85. }
  86. #endif /* __ASSEMBLY__ */
  87. #endif /* _BFIN_SEMAPHORE_H */