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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _ASM_PARISC_SEMAPHORE_H
  2. #define _ASM_PARISC_SEMAPHORE_H
  3. /*
  4.  * SMP- and interrupt-safe semaphores.
  5.  *
  6.  * (C) Copyright 1996 Linus Torvalds
  7.  *
  8.  * PA-RISC version by Matthew Wilcox
  9.  *
  10.  */
  11. #include <linux/spinlock.h>
  12. #include <linux/wait.h>
  13. #include <linux/rwsem.h>
  14. #include <asm/system.h>
  15. /*
  16.  * The `count' is initialised to the number of people who are allowed to
  17.  * take the lock.  (Normally we want a mutex, so this is `1').  if
  18.  * `count' is positive, the lock can be taken.  if it's 0, no-one is
  19.  * waiting on it.  if it's -1, at least one task is waiting.
  20.  */
  21. struct semaphore {
  22. spinlock_t sentry;
  23. int count;
  24. wait_queue_head_t wait;
  25. #if WAITQUEUE_DEBUG
  26. long __magic;
  27. #endif
  28. };
  29. #if WAITQUEUE_DEBUG
  30. # define __SEM_DEBUG_INIT(name) 
  31. , (long)&(name).__magic
  32. #else
  33. # define __SEM_DEBUG_INIT(name)
  34. #endif
  35. #define __SEMAPHORE_INITIALIZER(name,count) 
  36. { SPIN_LOCK_UNLOCKED, count, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) 
  37. __SEM_DEBUG_INIT(name) }
  38. #define __MUTEX_INITIALIZER(name) 
  39. __SEMAPHORE_INITIALIZER(name,1)
  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. extern 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. static inline int sem_getcount(struct semaphore *sem)
  57. {
  58. return sem->count;
  59. }
  60. asmlinkage void __down(struct semaphore * sem);
  61. asmlinkage int  __down_interruptible(struct semaphore * sem);
  62. asmlinkage void __up(struct semaphore * sem);
  63. /* Semaphores can be `tried' from irq context.  So we have to disable
  64.  * interrupts while we're messing with the semaphore.  Sorry.
  65.  */
  66. extern __inline__ void down(struct semaphore * sem)
  67. {
  68. #if WAITQUEUE_DEBUG
  69. CHECK_MAGIC(sem->__magic);
  70. #endif
  71. spin_lock_irq(&sem->sentry);
  72. if (sem->count > 0) {
  73. sem->count--;
  74. } else {
  75. __down(sem);
  76. }
  77. spin_unlock_irq(&sem->sentry);
  78. }
  79. extern __inline__ int down_interruptible(struct semaphore * sem)
  80. {
  81. int ret = 0;
  82. #if WAITQUEUE_DEBUG
  83. CHECK_MAGIC(sem->__magic);
  84. #endif
  85. spin_lock_irq(&sem->sentry);
  86. if (sem->count > 0) {
  87. sem->count--;
  88. } else {
  89. ret = __down_interruptible(sem);
  90. }
  91. spin_unlock_irq(&sem->sentry);
  92. return ret;
  93. }
  94. /*
  95.  * down_trylock returns 0 on success, 1 if we failed to get the lock.
  96.  * May not sleep, but must preserve irq state
  97.  */
  98. extern __inline__ int down_trylock(struct semaphore * sem)
  99. {
  100. int flags, count;
  101. #if WAITQUEUE_DEBUG
  102. CHECK_MAGIC(sem->__magic);
  103. #endif
  104. spin_lock_irqsave(&sem->sentry, flags);
  105. count = sem->count - 1;
  106. if (count >= 0)
  107. sem->count = count;
  108. spin_unlock_irqrestore(&sem->sentry, flags);
  109. return (count < 0);
  110. }
  111. /*
  112.  * Note! This is subtle. We jump to wake people up only if
  113.  * the semaphore was negative (== somebody was waiting on it).
  114.  */
  115. extern __inline__ void up(struct semaphore * sem)
  116. {
  117. int flags;
  118. #if WAITQUEUE_DEBUG
  119. CHECK_MAGIC(sem->__magic);
  120. #endif
  121. spin_lock_irqsave(&sem->sentry, flags);
  122. if (sem->count < 0) {
  123. __up(sem);
  124. } else {
  125. sem->count++;
  126. }
  127. spin_unlock_irqrestore(&sem->sentry, flags);
  128. }
  129. #endif /* _ASM_PARISC_SEMAPHORE_H */