semaphore.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  44. static inline void sema_init (struct semaphore *sem, int val)
  45. {
  46. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
  47. }
  48. static inline void init_MUTEX (struct semaphore *sem)
  49. {
  50. sema_init(sem, 1);
  51. }
  52. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  53. {
  54. sema_init(sem, 0);
  55. }
  56. extern void __down(struct semaphore *sem, unsigned long flags);
  57. extern int __down_interruptible(struct semaphore *sem, unsigned long flags);
  58. extern void __up(struct semaphore *sem);
  59. static inline void down(struct semaphore *sem)
  60. {
  61. unsigned long flags;
  62. #ifdef CONFIG_DEBUG_SEMAPHORE
  63. CHECK_MAGIC(sem->__magic);
  64. #endif
  65. spin_lock_irqsave(&sem->wait_lock, flags);
  66. if (likely(sem->counter > 0)) {
  67. sem->counter--;
  68. spin_unlock_irqrestore(&sem->wait_lock, flags);
  69. }
  70. else {
  71. __down(sem, flags);
  72. }
  73. }
  74. static inline int down_interruptible(struct semaphore *sem)
  75. {
  76. unsigned long flags;
  77. int ret = 0;
  78. #ifdef CONFIG_DEBUG_SEMAPHORE
  79. CHECK_MAGIC(sem->__magic);
  80. #endif
  81. spin_lock_irqsave(&sem->wait_lock, flags);
  82. if (likely(sem->counter > 0)) {
  83. sem->counter--;
  84. spin_unlock_irqrestore(&sem->wait_lock, flags);
  85. }
  86. else {
  87. ret = __down_interruptible(sem, flags);
  88. }
  89. return ret;
  90. }
  91. /*
  92. * non-blockingly attempt to down() a semaphore.
  93. * - returns zero if we acquired it
  94. */
  95. static inline int down_trylock(struct semaphore *sem)
  96. {
  97. unsigned long flags;
  98. int success = 0;
  99. #ifdef CONFIG_DEBUG_SEMAPHORE
  100. CHECK_MAGIC(sem->__magic);
  101. #endif
  102. spin_lock_irqsave(&sem->wait_lock, flags);
  103. if (sem->counter > 0) {
  104. sem->counter--;
  105. success = 1;
  106. }
  107. spin_unlock_irqrestore(&sem->wait_lock, flags);
  108. return !success;
  109. }
  110. static inline void up(struct semaphore *sem)
  111. {
  112. unsigned long flags;
  113. #ifdef CONFIG_DEBUG_SEMAPHORE
  114. CHECK_MAGIC(sem->__magic);
  115. #endif
  116. spin_lock_irqsave(&sem->wait_lock, flags);
  117. if (!list_empty(&sem->wait_list))
  118. __up(sem);
  119. else
  120. sem->counter++;
  121. spin_unlock_irqrestore(&sem->wait_lock, flags);
  122. }
  123. static inline int sem_getcount(struct semaphore *sem)
  124. {
  125. return sem->counter;
  126. }
  127. #endif /* __ASSEMBLY__ */
  128. #endif