semaphore.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC (name,0)
  20. static inline void sema_init (struct semaphore *sem, int val)
  21. {
  22. *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  23. }
  24. static inline void init_MUTEX (struct semaphore *sem)
  25. {
  26. sema_init (sem, 1);
  27. }
  28. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  29. {
  30. sema_init (sem, 0);
  31. }
  32. /*
  33. * special register calling convention
  34. */
  35. asmlinkage void __down_failed (void);
  36. asmlinkage int __down_interruptible_failed (void);
  37. asmlinkage int __down_trylock_failed (void);
  38. asmlinkage void __up_wakeup (void);
  39. extern void __down (struct semaphore * sem);
  40. extern int __down_interruptible (struct semaphore * sem);
  41. extern int __down_trylock (struct semaphore * sem);
  42. extern void __up (struct semaphore * sem);
  43. static inline void down (struct semaphore * sem)
  44. {
  45. might_sleep();
  46. if (atomic_dec_return (&sem->count) < 0)
  47. __down (sem);
  48. }
  49. static inline int down_interruptible (struct semaphore * sem)
  50. {
  51. int ret = 0;
  52. might_sleep();
  53. if (atomic_dec_return (&sem->count) < 0)
  54. ret = __down_interruptible (sem);
  55. return ret;
  56. }
  57. static inline int down_trylock (struct semaphore *sem)
  58. {
  59. int ret = 0;
  60. if (atomic_dec_return (&sem->count) < 0)
  61. ret = __down_trylock (sem);
  62. return ret;
  63. }
  64. static inline void up (struct semaphore * sem)
  65. {
  66. if (atomic_inc_return (&sem->count) <= 0)
  67. __up (sem);
  68. }
  69. #endif /* __V850_SEMAPHORE_H__ */