semaphore.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * include/asm-s390/semaphore.h
  3. *
  4. * S390 version
  5. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. *
  7. * Derived from "include/asm-i386/semaphore.h"
  8. * (C) Copyright 1996 Linus Torvalds
  9. */
  10. #ifndef _S390_SEMAPHORE_H
  11. #define _S390_SEMAPHORE_H
  12. #include <asm/system.h>
  13. #include <asm/atomic.h>
  14. #include <linux/wait.h>
  15. #include <linux/rwsem.h>
  16. struct semaphore {
  17. /*
  18. * Note that any negative value of count is equivalent to 0,
  19. * but additionally indicates that some process(es) might be
  20. * sleeping on `wait'.
  21. */
  22. atomic_t count;
  23. wait_queue_head_t wait;
  24. };
  25. #define __SEMAPHORE_INITIALIZER(name,count) \
  26. { ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) }
  27. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  28. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  29. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  30. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  31. static inline void sema_init (struct semaphore *sem, int val)
  32. {
  33. atomic_set(&sem->count, val);
  34. init_waitqueue_head(&sem->wait);
  35. }
  36. static inline void init_MUTEX (struct semaphore *sem)
  37. {
  38. sema_init(sem, 1);
  39. }
  40. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  41. {
  42. sema_init(sem, 0);
  43. }
  44. asmlinkage void __down(struct semaphore * sem);
  45. asmlinkage int __down_interruptible(struct semaphore * sem);
  46. asmlinkage int __down_trylock(struct semaphore * sem);
  47. asmlinkage void __up(struct semaphore * sem);
  48. static inline void down(struct semaphore * sem)
  49. {
  50. might_sleep();
  51. if (atomic_dec_return(&sem->count) < 0)
  52. __down(sem);
  53. }
  54. static inline int down_interruptible(struct semaphore * sem)
  55. {
  56. int ret = 0;
  57. might_sleep();
  58. if (atomic_dec_return(&sem->count) < 0)
  59. ret = __down_interruptible(sem);
  60. return ret;
  61. }
  62. static inline int down_trylock(struct semaphore * sem)
  63. {
  64. int old_val, new_val;
  65. /*
  66. * This inline assembly atomically implements the equivalent
  67. * to the following C code:
  68. * old_val = sem->count.counter;
  69. * if ((new_val = old_val) > 0)
  70. * sem->count.counter = --new_val;
  71. * In the ppc code this is called atomic_dec_if_positive.
  72. */
  73. __asm__ __volatile__ (
  74. " l %0,0(%3)\n"
  75. "0: ltr %1,%0\n"
  76. " jle 1f\n"
  77. " ahi %1,-1\n"
  78. " cs %0,%1,0(%3)\n"
  79. " jl 0b\n"
  80. "1:"
  81. : "=&d" (old_val), "=&d" (new_val), "=m" (sem->count.counter)
  82. : "a" (&sem->count.counter), "m" (sem->count.counter)
  83. : "cc", "memory" );
  84. return old_val <= 0;
  85. }
  86. static inline void up(struct semaphore * sem)
  87. {
  88. if (atomic_inc_return(&sem->count) <= 0)
  89. __up(sem);
  90. }
  91. #endif