semaphore.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 __MUTEX_INITIALIZER(name) \
  28. __SEMAPHORE_INITIALIZER(name, 1)
  29. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  30. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  31. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  32. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  33. static inline void sema_init (struct semaphore *sem, int val)
  34. {
  35. atomic_set(&sem->count, val);
  36. init_waitqueue_head(&sem->wait);
  37. }
  38. static inline void init_MUTEX (struct semaphore *sem)
  39. {
  40. sema_init(sem, 1);
  41. }
  42. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  43. {
  44. sema_init(sem, 0);
  45. }
  46. asmlinkage void __down(struct semaphore * sem);
  47. asmlinkage int __down_interruptible(struct semaphore * sem);
  48. asmlinkage int __down_trylock(struct semaphore * sem);
  49. asmlinkage void __up(struct semaphore * sem);
  50. extern spinlock_t semaphore_wake_lock;
  51. static inline void down(struct semaphore * sem)
  52. {
  53. might_sleep();
  54. if (atomic_sub_return(1, &sem->count) < 0)
  55. __down(sem);
  56. }
  57. static inline int down_interruptible(struct semaphore * sem)
  58. {
  59. int ret = 0;
  60. might_sleep();
  61. if (atomic_sub_return(1, &sem->count) < 0)
  62. ret = __down_interruptible(sem);
  63. return ret;
  64. }
  65. static inline int down_trylock(struct semaphore * sem)
  66. {
  67. int ret = 0;
  68. if (atomic_sub_return(1, &sem->count) < 0)
  69. ret = __down_trylock(sem);
  70. return ret;
  71. }
  72. /*
  73. * Note! This is subtle. We jump to wake people up only if
  74. * the semaphore was negative (== somebody was waiting on it).
  75. */
  76. static inline void up(struct semaphore * sem)
  77. {
  78. if (atomic_add_return(1, &sem->count) <= 0)
  79. __up(sem);
  80. }
  81. #endif /* _XTENSA_SEMAPHORE_H */