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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/include/asm-arm/semaphore.h
  3.  */
  4. #ifndef __ASM_ARM_SEMAPHORE_H
  5. #define __ASM_ARM_SEMAPHORE_H
  6. #include <linux/linkage.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/wait.h>
  9. #include <linux/rwsem.h>
  10. #include <asm/atomic.h>
  11. #include <asm/proc/locks.h>
  12. struct semaphore {
  13. atomic_t count;
  14. int sleepers;
  15. wait_queue_head_t wait;
  16. #if WAITQUEUE_DEBUG
  17. long __magic;
  18. #endif
  19. };
  20. #if WAITQUEUE_DEBUG
  21. # define __SEM_DEBUG_INIT(name) 
  22. , (long)&(name).__magic
  23. #else
  24. # define __SEM_DEBUG_INIT(name)
  25. #endif
  26. #define __SEMAPHORE_INIT(name,count)
  27. { ATOMIC_INIT(count), 0,
  28.   __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)
  29.   __SEM_DEBUG_INIT(name) }
  30. #define __MUTEX_INITIALIZER(name) 
  31. __SEMAPHORE_INIT(name,1)
  32. #define __DECLARE_SEMAPHORE_GENERIC(name,count)
  33. struct semaphore name = __SEMAPHORE_INIT(name,count)
  34. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  35. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  36. static inline void sema_init(struct semaphore *sem, int val)
  37. {
  38. atomic_set(&sem->count, val);
  39. sem->sleepers = 0;
  40. init_waitqueue_head(&sem->wait);
  41. #if WAITQUEUE_DEBUG
  42. sem->__magic = (long)&sem->__magic;
  43. #endif
  44. }
  45. static inline void init_MUTEX(struct semaphore *sem)
  46. {
  47. sema_init(sem, 1);
  48. }
  49. static inline void init_MUTEX_LOCKED(struct semaphore *sem)
  50. {
  51. sema_init(sem, 0);
  52. }
  53. /*
  54.  * special register calling convention
  55.  */
  56. asmlinkage void __down_failed(void);
  57. asmlinkage int  __down_interruptible_failed(void);
  58. asmlinkage int  __down_trylock_failed(void);
  59. asmlinkage void __up_wakeup(void);
  60. extern void __down(struct semaphore * sem);
  61. extern int  __down_interruptible(struct semaphore * sem);
  62. extern int  __down_trylock(struct semaphore * sem);
  63. extern void __up(struct semaphore * sem);
  64. /*
  65.  * This is ugly, but we want the default case to fall through.
  66.  * "__down" is the actual routine that waits...
  67.  */
  68. static inline void down(struct semaphore * sem)
  69. {
  70. #if WAITQUEUE_DEBUG
  71. CHECK_MAGIC(sem->__magic);
  72. #endif
  73. __down_op(sem, __down_failed);
  74. }
  75. /*
  76.  * This is ugly, but we want the default case to fall through.
  77.  * "__down_interruptible" is the actual routine that waits...
  78.  */
  79. static inline int down_interruptible (struct semaphore * sem)
  80. {
  81. #if WAITQUEUE_DEBUG
  82. CHECK_MAGIC(sem->__magic);
  83. #endif
  84. return __down_op_ret(sem, __down_interruptible_failed);
  85. }
  86. static inline int down_trylock(struct semaphore *sem)
  87. {
  88. #if WAITQUEUE_DEBUG
  89. CHECK_MAGIC(sem->__magic);
  90. #endif
  91. return __down_op_ret(sem, __down_trylock_failed);
  92. }
  93. /*
  94.  * Note! This is subtle. We jump to wake people up only if
  95.  * the semaphore was negative (== somebody was waiting on it).
  96.  * The default case (no contention) will result in NO
  97.  * jumps for both down() and up().
  98.  */
  99. static inline void up(struct semaphore * sem)
  100. {
  101. #if WAITQUEUE_DEBUG
  102. CHECK_MAGIC(sem->__magic);
  103. #endif
  104. __up_op(sem, __up_wakeup);
  105. }
  106. static inline int sem_getcount(struct semaphore *sem)
  107. {
  108. return atomic_read(&sem->count);
  109. }
  110. #endif