semaphore.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /*
  20. * the semaphore definition
  21. * - if counter is >0 then there are tokens available on the semaphore for down to collect
  22. * - if counter is <=0 then there are no spare tokens, and anyone that wants one must wait
  23. * - if wait_list is not empty, then there are processes waiting for the semaphore
  24. */
  25. struct semaphore {
  26. unsigned counter;
  27. spinlock_t wait_lock;
  28. struct list_head wait_list;
  29. #ifdef CONFIG_DEBUG_SEMAPHORE
  30. unsigned __magic;
  31. #endif
  32. };
  33. #ifdef CONFIG_DEBUG_SEMAPHORE
  34. # define __SEM_DEBUG_INIT(name) , (long)&(name).__magic
  35. #else
  36. # define __SEM_DEBUG_INIT(name)
  37. #endif
  38. #define __SEMAPHORE_INITIALIZER(name,count) \
  39. { count, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) __SEM_DEBUG_INIT(name) }
  40. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  41. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  42. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  43. static inline void sema_init (struct semaphore *sem, int val)
  44. {
  45. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  46. }
  47. static inline void init_MUTEX (struct semaphore *sem)
  48. {
  49. sema_init(sem, 1);
  50. }
  51. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  52. {
  53. sema_init(sem, 0);
  54. }
  55. extern void __down(struct semaphore *sem, unsigned long flags);
  56. extern int __down_interruptible(struct semaphore *sem, unsigned long flags);
  57. extern void __up(struct semaphore *sem);
  58. static inline void down(struct semaphore *sem)
  59. {
  60. unsigned long flags;
  61. #ifdef CONFIG_DEBUG_SEMAPHORE
  62. CHECK_MAGIC(sem->__magic);
  63. #endif
  64. spin_lock_irqsave(&sem->wait_lock, flags);
  65. if (likely(sem->counter > 0)) {
  66. sem->counter--;
  67. spin_unlock_irqrestore(&sem->wait_lock, flags);
  68. }
  69. else {
  70. __down(sem, flags);
  71. }
  72. }
  73. static inline int down_interruptible(struct semaphore *sem)
  74. {
  75. unsigned long flags;
  76. int ret = 0;
  77. #ifdef CONFIG_DEBUG_SEMAPHORE
  78. CHECK_MAGIC(sem->__magic);
  79. #endif
  80. spin_lock_irqsave(&sem->wait_lock, flags);
  81. if (likely(sem->counter > 0)) {
  82. sem->counter--;
  83. spin_unlock_irqrestore(&sem->wait_lock, flags);
  84. }
  85. else {
  86. ret = __down_interruptible(sem, flags);
  87. }
  88. return ret;
  89. }
  90. /*
  91. * non-blockingly attempt to down() a semaphore.
  92. * - returns zero if we acquired it
  93. */
  94. static inline int down_trylock(struct semaphore *sem)
  95. {
  96. unsigned long flags;
  97. int success = 0;
  98. #ifdef CONFIG_DEBUG_SEMAPHORE
  99. CHECK_MAGIC(sem->__magic);
  100. #endif
  101. spin_lock_irqsave(&sem->wait_lock, flags);
  102. if (sem->counter > 0) {
  103. sem->counter--;
  104. success = 1;
  105. }
  106. spin_unlock_irqrestore(&sem->wait_lock, flags);
  107. return !success;
  108. }
  109. static inline void up(struct semaphore *sem)
  110. {
  111. unsigned long flags;
  112. #ifdef CONFIG_DEBUG_SEMAPHORE
  113. CHECK_MAGIC(sem->__magic);
  114. #endif
  115. spin_lock_irqsave(&sem->wait_lock, flags);
  116. if (!list_empty(&sem->wait_list))
  117. __up(sem);
  118. else
  119. sem->counter++;
  120. spin_unlock_irqrestore(&sem->wait_lock, flags);
  121. }
  122. static inline int sem_getcount(struct semaphore *sem)
  123. {
  124. return sem->counter;
  125. }
  126. #endif /* __ASSEMBLY__ */
  127. #endif