semaphore.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef __V850_SEMAPHORE_H__
  2. #define __V850_SEMAPHORE_H__
  3. #include <linux/linkage.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/wait.h>
  6. #include <linux/rwsem.h>
  7. #include <asm/atomic.h>
  8. struct semaphore {
  9. atomic_t count;
  10. int sleepers;
  11. wait_queue_head_t wait;
  12. };
  13. #define __SEMAPHORE_INITIALIZER(name,count) \
  14. { ATOMIC_INIT (count), 0, \
  15. __WAIT_QUEUE_HEAD_INITIALIZER ((name).wait) }
  16. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  17. struct semaphore name = __SEMAPHORE_INITIALIZER (name,count)
  18. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC (name,1)
  19. static inline void sema_init (struct semaphore *sem, int val)
  20. {
  21. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  22. }
  23. static inline void init_MUTEX (struct semaphore *sem)
  24. {
  25. sema_init (sem, 1);
  26. }
  27. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  28. {
  29. sema_init (sem, 0);
  30. }
  31. /*
  32. * special register calling convention
  33. */
  34. asmlinkage void __down_failed (void);
  35. asmlinkage int __down_interruptible_failed (void);
  36. asmlinkage int __down_trylock_failed (void);
  37. asmlinkage void __up_wakeup (void);
  38. extern void __down (struct semaphore * sem);
  39. extern int __down_interruptible (struct semaphore * sem);
  40. extern int __down_trylock (struct semaphore * sem);
  41. extern void __up (struct semaphore * sem);
  42. static inline void down (struct semaphore * sem)
  43. {
  44. might_sleep();
  45. if (atomic_dec_return (&sem->count) < 0)
  46. __down (sem);
  47. }
  48. static inline int down_interruptible (struct semaphore * sem)
  49. {
  50. int ret = 0;
  51. might_sleep();
  52. if (atomic_dec_return (&sem->count) < 0)
  53. ret = __down_interruptible (sem);
  54. return ret;
  55. }
  56. static inline int down_trylock (struct semaphore *sem)
  57. {
  58. int ret = 0;
  59. if (atomic_dec_return (&sem->count) < 0)
  60. ret = __down_trylock (sem);
  61. return ret;
  62. }
  63. static inline void up (struct semaphore * sem)
  64. {
  65. if (atomic_inc_return (&sem->count) <= 0)
  66. __up (sem);
  67. }
  68. #endif /* __V850_SEMAPHORE_H__ */