semaphore.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef _ASM_IA64_SEMAPHORE_H
  2. #define _ASM_IA64_SEMAPHORE_H
  3. /*
  4. * Copyright (C) 1998-2000 Hewlett-Packard Co
  5. * Copyright (C) 1998-2000 David Mosberger-Tang <davidm@hpl.hp.com>
  6. */
  7. #include <linux/wait.h>
  8. #include <linux/rwsem.h>
  9. #include <asm/atomic.h>
  10. struct semaphore {
  11. atomic_t count;
  12. int sleepers;
  13. wait_queue_head_t wait;
  14. };
  15. #define __SEMAPHORE_INITIALIZER(name, n) \
  16. { \
  17. .count = ATOMIC_INIT(n), \
  18. .sleepers = 0, \
  19. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  20. }
  21. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  22. struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
  23. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  24. static inline void
  25. sema_init (struct semaphore *sem, int val)
  26. {
  27. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  28. }
  29. static inline void
  30. init_MUTEX (struct semaphore *sem)
  31. {
  32. sema_init(sem, 1);
  33. }
  34. static inline void
  35. init_MUTEX_LOCKED (struct semaphore *sem)
  36. {
  37. sema_init(sem, 0);
  38. }
  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. /*
  44. * Atomically decrement the semaphore's count. If it goes negative,
  45. * block the calling thread in the TASK_UNINTERRUPTIBLE state.
  46. */
  47. static inline void
  48. down (struct semaphore *sem)
  49. {
  50. might_sleep();
  51. if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
  52. __down(sem);
  53. }
  54. /*
  55. * Atomically decrement the semaphore's count. If it goes negative,
  56. * block the calling thread in the TASK_INTERRUPTIBLE state.
  57. */
  58. static inline int
  59. down_interruptible (struct semaphore * sem)
  60. {
  61. int ret = 0;
  62. might_sleep();
  63. if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
  64. ret = __down_interruptible(sem);
  65. return ret;
  66. }
  67. static inline int
  68. down_trylock (struct semaphore *sem)
  69. {
  70. int ret = 0;
  71. if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
  72. ret = __down_trylock(sem);
  73. return ret;
  74. }
  75. static inline void
  76. up (struct semaphore * sem)
  77. {
  78. if (ia64_fetchadd(1, &sem->count.counter, rel) <= -1)
  79. __up(sem);
  80. }
  81. #endif /* _ASM_IA64_SEMAPHORE_H */