semaphore.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* semaphore.h: semaphores for the FR-V
  2. *
  3. * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_SEMAPHORE_H
  12. #define _ASM_SEMAPHORE_H
  13. #define RW_LOCK_BIAS 0x01000000
  14. #ifndef __ASSEMBLY__
  15. #include <linux/linkage.h>
  16. #include <linux/wait.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/rwsem.h>
  19. #define SEMAPHORE_DEBUG WAITQUEUE_DEBUG
  20. /*
  21. * the semaphore definition
  22. * - if counter is >0 then there are tokens available on the semaphore for down to collect
  23. * - if counter is <=0 then there are no spare tokens, and anyone that wants one must wait
  24. * - if wait_list is not empty, then there are processes waiting for the semaphore
  25. */
  26. struct semaphore {
  27. unsigned counter;
  28. spinlock_t wait_lock;
  29. struct list_head wait_list;
  30. #if SEMAPHORE_DEBUG
  31. unsigned __magic;
  32. #endif
  33. };
  34. #if SEMAPHORE_DEBUG
  35. # define __SEM_DEBUG_INIT(name) , (long)&(name).__magic
  36. #else
  37. # define __SEM_DEBUG_INIT(name)
  38. #endif
  39. #define __SEMAPHORE_INITIALIZER(name,count) \
  40. { count, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) __SEM_DEBUG_INIT(name) }
  41. #define __MUTEX_INITIALIZER(name) \
  42. __SEMAPHORE_INITIALIZER(name,1)
  43. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  44. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  45. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  46. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  47. static inline void sema_init (struct semaphore *sem, int val)
  48. {
  49. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  50. }
  51. static inline void init_MUTEX (struct semaphore *sem)
  52. {
  53. sema_init(sem, 1);
  54. }
  55. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  56. {
  57. sema_init(sem, 0);
  58. }
  59. extern void __down(struct semaphore *sem, unsigned long flags);
  60. extern int __down_interruptible(struct semaphore *sem, unsigned long flags);
  61. extern void __up(struct semaphore *sem);
  62. static inline void down(struct semaphore *sem)
  63. {
  64. unsigned long flags;
  65. #if SEMAPHORE_DEBUG
  66. CHECK_MAGIC(sem->__magic);
  67. #endif
  68. spin_lock_irqsave(&sem->wait_lock, flags);
  69. if (likely(sem->counter > 0)) {
  70. sem->counter--;
  71. spin_unlock_irqrestore(&sem->wait_lock, flags);
  72. }
  73. else {
  74. __down(sem, flags);
  75. }
  76. }
  77. static inline int down_interruptible(struct semaphore *sem)
  78. {
  79. unsigned long flags;
  80. int ret = 0;
  81. #if SEMAPHORE_DEBUG
  82. CHECK_MAGIC(sem->__magic);
  83. #endif
  84. spin_lock_irqsave(&sem->wait_lock, flags);
  85. if (likely(sem->counter > 0)) {
  86. sem->counter--;
  87. spin_unlock_irqrestore(&sem->wait_lock, flags);
  88. }
  89. else {
  90. ret = __down_interruptible(sem, flags);
  91. }
  92. return ret;
  93. }
  94. /*
  95. * non-blockingly attempt to down() a semaphore.
  96. * - returns zero if we acquired it
  97. */
  98. static inline int down_trylock(struct semaphore *sem)
  99. {
  100. unsigned long flags;
  101. int success = 0;
  102. #if SEMAPHORE_DEBUG
  103. CHECK_MAGIC(sem->__magic);
  104. #endif
  105. spin_lock_irqsave(&sem->wait_lock, flags);
  106. if (sem->counter > 0) {
  107. sem->counter--;
  108. success = 1;
  109. }
  110. spin_unlock_irqrestore(&sem->wait_lock, flags);
  111. return !success;
  112. }
  113. static inline void up(struct semaphore *sem)
  114. {
  115. unsigned long flags;
  116. #if SEMAPHORE_DEBUG
  117. CHECK_MAGIC(sem->__magic);
  118. #endif
  119. spin_lock_irqsave(&sem->wait_lock, flags);
  120. if (!list_empty(&sem->wait_list))
  121. __up(sem);
  122. else
  123. sem->counter++;
  124. spin_unlock_irqrestore(&sem->wait_lock, flags);
  125. }
  126. static inline int sem_getcount(struct semaphore *sem)
  127. {
  128. return sem->counter;
  129. }
  130. #endif /* __ASSEMBLY__ */
  131. #endif