semaphore.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. static inline void sema_init (struct semaphore *sem, int val)
  31. {
  32. atomic_set(&sem->count, val);
  33. sem->sleepers = 0;
  34. init_waitqueue_head(&sem->wait);
  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. static inline void down(struct semaphore * sem)
  50. {
  51. might_sleep();
  52. if (atomic_sub_return(1, &sem->count) < 0)
  53. __down(sem);
  54. }
  55. static inline int down_interruptible(struct semaphore * sem)
  56. {
  57. int ret = 0;
  58. might_sleep();
  59. if (atomic_sub_return(1, &sem->count) < 0)
  60. ret = __down_interruptible(sem);
  61. return ret;
  62. }
  63. static inline int down_trylock(struct semaphore * sem)
  64. {
  65. int ret = 0;
  66. if (atomic_sub_return(1, &sem->count) < 0)
  67. ret = __down_trylock(sem);
  68. return ret;
  69. }
  70. /*
  71. * Note! This is subtle. We jump to wake people up only if
  72. * the semaphore was negative (== somebody was waiting on it).
  73. */
  74. static inline void up(struct semaphore * sem)
  75. {
  76. if (atomic_add_return(1, &sem->count) <= 0)
  77. __up(sem);
  78. }
  79. #endif /* _XTENSA_SEMAPHORE_H */