semaphore.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef _I386_SEMAPHORE_H
  2. #define _I386_SEMAPHORE_H
  3. #include <linux/linkage.h>
  4. #ifdef __KERNEL__
  5. /*
  6. * SMP- and interrupt-safe semaphores..
  7. *
  8. * (C) Copyright 1996 Linus Torvalds
  9. *
  10. * Modified 1996-12-23 by Dave Grothe <dave@gcom.com> to fix bugs in
  11. * the original code and to make semaphore waits
  12. * interruptible so that processes waiting on
  13. * semaphores can be killed.
  14. * Modified 1999-02-14 by Andrea Arcangeli, split the sched.c helper
  15. * functions in asm/sempahore-helper.h while fixing a
  16. * potential and subtle race discovered by Ulrich Schmid
  17. * in down_interruptible(). Since I started to play here I
  18. * also implemented the `trylock' semaphore operation.
  19. * 1999-07-02 Artur Skawina <skawina@geocities.com>
  20. * Optimized "0(ecx)" -> "(ecx)" (the assembler does not
  21. * do this). Changed calling sequences from push/jmp to
  22. * traditional call/ret.
  23. * Modified 2001-01-01 Andreas Franck <afranck@gmx.de>
  24. * Some hacks to ensure compatibility with recent
  25. * GCC snapshots, to avoid stack corruption when compiling
  26. * with -fomit-frame-pointer. It's not sure if this will
  27. * be fixed in GCC, as our previous implementation was a
  28. * bit dubious.
  29. *
  30. * If you would like to see an analysis of this implementation, please
  31. * ftp to gcom.com and download the file
  32. * /pub/linux/src/semaphore/semaphore-2.0.24.tar.gz.
  33. *
  34. */
  35. #include <asm/system.h>
  36. #include <asm/atomic.h>
  37. #include <linux/wait.h>
  38. #include <linux/rwsem.h>
  39. struct semaphore {
  40. atomic_t count;
  41. int sleepers;
  42. wait_queue_head_t wait;
  43. };
  44. #define __SEMAPHORE_INITIALIZER(name, n) \
  45. { \
  46. .count = ATOMIC_INIT(n), \
  47. .sleepers = 0, \
  48. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  49. }
  50. #define __MUTEX_INITIALIZER(name) \
  51. __SEMAPHORE_INITIALIZER(name,1)
  52. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  53. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  54. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  55. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  56. static inline void sema_init (struct semaphore *sem, int val)
  57. {
  58. /*
  59. * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  60. *
  61. * i'd rather use the more flexible initialization above, but sadly
  62. * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well.
  63. */
  64. atomic_set(&sem->count, val);
  65. sem->sleepers = 0;
  66. init_waitqueue_head(&sem->wait);
  67. }
  68. static inline void init_MUTEX (struct semaphore *sem)
  69. {
  70. sema_init(sem, 1);
  71. }
  72. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  73. {
  74. sema_init(sem, 0);
  75. }
  76. fastcall void __down_failed(void /* special register calling convention */);
  77. fastcall int __down_failed_interruptible(void /* params in registers */);
  78. fastcall int __down_failed_trylock(void /* params in registers */);
  79. fastcall void __up_wakeup(void /* special register calling convention */);
  80. /*
  81. * This is ugly, but we want the default case to fall through.
  82. * "__down_failed" is a special asm handler that calls the C
  83. * routine that actually waits. See arch/i386/kernel/semaphore.c
  84. */
  85. static inline void down(struct semaphore * sem)
  86. {
  87. might_sleep();
  88. __asm__ __volatile__(
  89. "# atomic down operation\n\t"
  90. LOCK "decl %0\n\t" /* --sem->count */
  91. "js 2f\n"
  92. "1:\n"
  93. LOCK_SECTION_START("")
  94. "2:\tlea %0,%%eax\n\t"
  95. "call __down_failed\n\t"
  96. "jmp 1b\n"
  97. LOCK_SECTION_END
  98. :"=m" (sem->count)
  99. :
  100. :"memory","ax");
  101. }
  102. /*
  103. * Interruptible try to acquire a semaphore. If we obtained
  104. * it, return zero. If we were interrupted, returns -EINTR
  105. */
  106. static inline int down_interruptible(struct semaphore * sem)
  107. {
  108. int result;
  109. might_sleep();
  110. __asm__ __volatile__(
  111. "# atomic interruptible down operation\n\t"
  112. LOCK "decl %1\n\t" /* --sem->count */
  113. "js 2f\n\t"
  114. "xorl %0,%0\n"
  115. "1:\n"
  116. LOCK_SECTION_START("")
  117. "2:\tlea %1,%%eax\n\t"
  118. "call __down_failed_interruptible\n\t"
  119. "jmp 1b\n"
  120. LOCK_SECTION_END
  121. :"=a" (result), "=m" (sem->count)
  122. :
  123. :"memory");
  124. return result;
  125. }
  126. /*
  127. * Non-blockingly attempt to down() a semaphore.
  128. * Returns zero if we acquired it
  129. */
  130. static inline int down_trylock(struct semaphore * sem)
  131. {
  132. int result;
  133. __asm__ __volatile__(
  134. "# atomic interruptible down operation\n\t"
  135. LOCK "decl %1\n\t" /* --sem->count */
  136. "js 2f\n\t"
  137. "xorl %0,%0\n"
  138. "1:\n"
  139. LOCK_SECTION_START("")
  140. "2:\tlea %1,%%eax\n\t"
  141. "call __down_failed_trylock\n\t"
  142. "jmp 1b\n"
  143. LOCK_SECTION_END
  144. :"=a" (result), "=m" (sem->count)
  145. :
  146. :"memory");
  147. return result;
  148. }
  149. /*
  150. * Note! This is subtle. We jump to wake people up only if
  151. * the semaphore was negative (== somebody was waiting on it).
  152. * The default case (no contention) will result in NO
  153. * jumps for both down() and up().
  154. */
  155. static inline void up(struct semaphore * sem)
  156. {
  157. __asm__ __volatile__(
  158. "# atomic up operation\n\t"
  159. LOCK "incl %0\n\t" /* ++sem->count */
  160. "jle 2f\n"
  161. "1:\n"
  162. LOCK_SECTION_START("")
  163. "2:\tlea %0,%%eax\n\t"
  164. "call __up_wakeup\n\t"
  165. "jmp 1b\n"
  166. LOCK_SECTION_END
  167. ".subsection 0\n"
  168. :"=m" (sem->count)
  169. :
  170. :"memory","ax");
  171. }
  172. #endif
  173. #endif