semaphore.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _X86_64_SEMAPHORE_H
  2. #define _X86_64_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 <asm/rwlock.h>
  38. #include <linux/wait.h>
  39. #include <linux/rwsem.h>
  40. #include <linux/stringify.h>
  41. struct semaphore {
  42. atomic_t count;
  43. int sleepers;
  44. wait_queue_head_t wait;
  45. #if WAITQUEUE_DEBUG
  46. long __magic;
  47. #endif
  48. };
  49. #if WAITQUEUE_DEBUG
  50. # define __SEM_DEBUG_INIT(name) 
  51. , (int)&(name).__magic
  52. #else
  53. # define __SEM_DEBUG_INIT(name)
  54. #endif
  55. #define __SEMAPHORE_INITIALIZER(name,count) 
  56. { ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) 
  57. __SEM_DEBUG_INIT(name) }
  58. #define __MUTEX_INITIALIZER(name) 
  59. __SEMAPHORE_INITIALIZER(name,1)
  60. #define __DECLARE_SEMAPHORE_GENERIC(name,count) 
  61. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  62. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  63. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  64. static inline void sema_init (struct semaphore *sem, int val)
  65. {
  66. /*
  67.  * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  68.  *
  69.  * i'd rather use the more flexible initialization above, but sadly
  70.  * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
  71.  */
  72. atomic_set(&sem->count, val);
  73. sem->sleepers = 0;
  74. init_waitqueue_head(&sem->wait);
  75. #if WAITQUEUE_DEBUG
  76. sem->__magic = (int)&sem->__magic;
  77. #endif
  78. }
  79. static inline void init_MUTEX (struct semaphore *sem)
  80. {
  81. sema_init(sem, 1);
  82. }
  83. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  84. {
  85. sema_init(sem, 0);
  86. }
  87. asmlinkage void __down_failed(void /* special register calling convention */);
  88. asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
  89. asmlinkage int  __down_failed_trylock(void  /* params in registers */);
  90. asmlinkage void __up_wakeup(void /* special register calling convention */);
  91. asmlinkage void __down(struct semaphore * sem);
  92. asmlinkage int  __down_interruptible(struct semaphore * sem);
  93. asmlinkage int  __down_trylock(struct semaphore * sem);
  94. asmlinkage void __up(struct semaphore * sem);
  95. /*
  96.  * This is ugly, but we want the default case to fall through.
  97.  * "__down_failed" is a special asm handler that calls the C
  98.  * routine that actually waits. See arch/x86_64/kernel/semaphore.c
  99.  */
  100. static inline void down(struct semaphore * sem)
  101. {
  102. #if WAITQUEUE_DEBUG
  103. CHECK_MAGIC(sem->__magic);
  104. #endif
  105. __asm__ __volatile__(
  106. "# atomic down operationnt"
  107. LOCK "decl %0nt"     /* --sem->count */
  108. "js 2fn"
  109. "1:n"
  110.                 ".subsection 1n" 
  111.                 ".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "n" 
  112.                 "_text_lock_" __stringify(KBUILD_BASENAME) ":n" 
  113.                 ".endifn" 
  114. "2:tcall __down_failednt"
  115. "jmp 1bn"
  116. ".subsection 0"
  117. :"=m" (sem->count)
  118. :"D" (sem)
  119. :"memory");
  120. }
  121. /*
  122.  * Interruptible try to acquire a semaphore.  If we obtained
  123.  * it, return zero.  If we were interrupted, returns -EINTR
  124.  */
  125. static inline int down_interruptible(struct semaphore * sem)
  126. {
  127. int result;
  128. #if WAITQUEUE_DEBUG
  129. CHECK_MAGIC(sem->__magic);
  130. #endif
  131. __asm__ __volatile__(
  132. "# atomic interruptible down operationnt"
  133. LOCK "decl %1nt"     /* --sem->count */
  134. "js 2fnt"
  135. "xorl %0,%0n"
  136. "1:n"
  137.                 ".subsection 1n" 
  138.                ".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "n" 
  139.                 "_text_lock_" __stringify(KBUILD_BASENAME) ":n" 
  140.                 ".endifn" 
  141. "2:tcall __down_failed_interruptiblent"
  142. "jmp 1bn"
  143. ".subsection 0"
  144. :"=a" (result), "=m" (sem->count)
  145. :"D" (sem)
  146. :"memory");
  147. return result;
  148. }
  149. /*
  150.  * Non-blockingly attempt to down() a semaphore.
  151.  * Returns zero if we acquired it
  152.  */
  153. static inline int down_trylock(struct semaphore * sem)
  154. {
  155. int result;
  156. #if WAITQUEUE_DEBUG
  157. CHECK_MAGIC(sem->__magic);
  158. #endif
  159. __asm__ __volatile__(
  160. "# atomic interruptible down operationnt"
  161. LOCK "decl %1nt"     /* --sem->count */
  162. "js 2fnt"
  163. "xorl %0,%0n"
  164. "1:n"
  165.                 ".subsection 1n" 
  166.                 ".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "n" 
  167.                 "_text_lock_" __stringify(KBUILD_BASENAME) ":n" 
  168.                 ".endifn" 
  169. "2:tcall __down_failed_trylocknt"
  170. "jmp 1bn"
  171. ".subsection 0"
  172. :"=a" (result), "=m" (sem->count)
  173. :"D" (sem)
  174. :"memory","cc");
  175. return result;
  176. }
  177. /*
  178.  * Note! This is subtle. We jump to wake people up only if
  179.  * the semaphore was negative (== somebody was waiting on it).
  180.  * The default case (no contention) will result in NO
  181.  * jumps for both down() and up().
  182.  */
  183. static inline void up(struct semaphore * sem)
  184. {
  185. #if WAITQUEUE_DEBUG
  186. CHECK_MAGIC(sem->__magic);
  187. #endif
  188. __asm__ __volatile__(
  189. "# atomic up operationnt"
  190. LOCK "incl %0nt"     /* ++sem->count */
  191. "jle 2fn"
  192. "1:n"
  193.                 ".subsection 1n" 
  194.                 ".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "n" 
  195.                 "_text_lock_" __stringify(KBUILD_BASENAME) ":n" 
  196.                 ".endifn" 
  197. "2:tcall __up_wakeupnt"
  198. "jmp 1bn"
  199. ".subsection 0"
  200. :"=m" (sem->count)
  201. :"D" (sem)
  202. :"memory");
  203. }
  204. static inline int sem_getcount(struct semaphore *sem)
  205. {
  206.        return atomic_read(&sem->count);
  207. }
  208. #endif /* __KERNEL__ */
  209. #endif