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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _PPC64_SEMAPHORE_H
  2. #define _PPC64_SEMAPHORE_H
  3. /*
  4.  * Remove spinlock-based RW semaphores; RW semaphore definitions are
  5.  * now in rwsem.h and we use the generic lib/rwsem.c implementation.
  6.  * Rework semaphores to use atomic_dec_if_positive.
  7.  * -- Paul Mackerras (paulus@samba.org)
  8.  */
  9. #ifdef __KERNEL__
  10. #include <asm/atomic.h>
  11. #include <asm/system.h>
  12. #include <linux/wait.h>
  13. #include <linux/rwsem.h>
  14. struct semaphore {
  15. /*
  16.  * Note that any negative value of count is equivalent to 0,
  17.  * but additionally indicates that some process(es) might be
  18.  * sleeping on `wait'.
  19.  */
  20. atomic_t count;
  21. wait_queue_head_t wait;
  22. #if WAITQUEUE_DEBUG
  23. long __magic;
  24. #endif
  25. };
  26. #if WAITQUEUE_DEBUG
  27. # define __SEM_DEBUG_INIT(name) 
  28. , (long)&(name).__magic
  29. #else
  30. # define __SEM_DEBUG_INIT(name)
  31. #endif
  32. #define __SEMAPHORE_INITIALIZER(name, count) 
  33. { ATOMIC_INIT(count), 
  34.   __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) 
  35.   __SEM_DEBUG_INIT(name) }
  36. #define __MUTEX_INITIALIZER(name) 
  37. __SEMAPHORE_INITIALIZER(name, 1)
  38. #define __DECLARE_SEMAPHORE_GENERIC(name, count) 
  39. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  40. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  41. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
  42. static inline void sema_init (struct semaphore *sem, int val)
  43. {
  44. atomic_set(&sem->count, val);
  45. init_waitqueue_head(&sem->wait);
  46. #if WAITQUEUE_DEBUG
  47. sem->__magic = (long)&sem->__magic;
  48. #endif
  49. }
  50. static inline void init_MUTEX (struct semaphore *sem)
  51. {
  52. sema_init(sem, 1);
  53. }
  54. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  55. {
  56. sema_init(sem, 0);
  57. }
  58. extern void __down(struct semaphore * sem);
  59. extern int  __down_interruptible(struct semaphore * sem);
  60. extern void __up(struct semaphore * sem);
  61. static inline void down(struct semaphore * sem)
  62. {
  63. #if WAITQUEUE_DEBUG
  64. CHECK_MAGIC(sem->__magic);
  65. #endif
  66. /*
  67.  * Try to get the semaphore, take the slow path if we fail.
  68.  */
  69. if (atomic_dec_return(&sem->count) < 0)
  70. __down(sem);
  71. smp_wmb();
  72. }
  73. static inline int down_interruptible(struct semaphore * sem)
  74. {
  75. int ret = 0;
  76. #if WAITQUEUE_DEBUG
  77. CHECK_MAGIC(sem->__magic);
  78. #endif
  79. if (atomic_dec_return(&sem->count) < 0)
  80. ret = __down_interruptible(sem);
  81. smp_wmb();
  82. return ret;
  83. }
  84. static inline int down_trylock(struct semaphore * sem)
  85. {
  86. int ret;
  87. #if WAITQUEUE_DEBUG
  88. CHECK_MAGIC(sem->__magic);
  89. #endif
  90. ret = atomic_dec_if_positive(&sem->count) < 0;
  91. smp_wmb();
  92. return ret;
  93. }
  94. static inline void up(struct semaphore * sem)
  95. {
  96. #if WAITQUEUE_DEBUG
  97. CHECK_MAGIC(sem->__magic);
  98. #endif
  99. smp_wmb();
  100. if (atomic_inc_return(&sem->count) <= 0)
  101. __up(sem);
  102. }
  103. static inline int sem_getcount(struct semaphore *sem)
  104. {
  105. return atomic_read(&sem->count);
  106. }
  107. #endif /* __KERNEL__ */
  108. #endif /* !(_PPC64_SEMAPHORE_H) */