semaphore.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * linux/include/asm-xtensa/semaphore.h
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_SEMAPHORE_H
  11. #define _XTENSA_SEMAPHORE_H
  12. #include <asm/atomic.h>
  13. #include <asm/system.h>
  14. #include <linux/wait.h>
  15. #include <linux/rwsem.h>
  16. struct semaphore {
  17. atomic_t count;
  18. int sleepers;
  19. wait_queue_head_t wait;
  20. };
  21. #define __SEMAPHORE_INITIALIZER(name,n) \
  22. { \
  23. .count = ATOMIC_INIT(n), \
  24. .sleepers = 0, \
  25. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  26. }
  27. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  28. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  29. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  30. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  31. static inline void sema_init (struct semaphore *sem, int val)
  32. {
  33. atomic_set(&sem->count, val);
  34. sem->sleepers = 0;
  35. init_waitqueue_head(&sem->wait);
  36. }
  37. static inline void init_MUTEX (struct semaphore *sem)
  38. {
  39. sema_init(sem, 1);
  40. }
  41. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  42. {
  43. sema_init(sem, 0);
  44. }
  45. asmlinkage void __down(struct semaphore * sem);
  46. asmlinkage int __down_interruptible(struct semaphore * sem);
  47. asmlinkage int __down_trylock(struct semaphore * sem);
  48. asmlinkage void __up(struct semaphore * sem);
  49. extern spinlock_t semaphore_wake_lock;
  50. static inline void down(struct semaphore * sem)
  51. {
  52. might_sleep();
  53. if (atomic_sub_return(1, &sem->count) < 0)
  54. __down(sem);
  55. }
  56. static inline int down_interruptible(struct semaphore * sem)
  57. {
  58. int ret = 0;
  59. might_sleep();
  60. if (atomic_sub_return(1, &sem->count) < 0)
  61. ret = __down_interruptible(sem);
  62. return ret;
  63. }
  64. static inline int down_trylock(struct semaphore * sem)
  65. {
  66. int ret = 0;
  67. if (atomic_sub_return(1, &sem->count) < 0)
  68. ret = __down_trylock(sem);
  69. return ret;
  70. }
  71. /*
  72. * Note! This is subtle. We jump to wake people up only if
  73. * the semaphore was negative (== somebody was waiting on it).
  74. */
  75. static inline void up(struct semaphore * sem)
  76. {
  77. if (atomic_add_return(1, &sem->count) <= 0)
  78. __up(sem);
  79. }
  80. #endif /* _XTENSA_SEMAPHORE_H */