semaphore.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 __MUTEX_INITIALIZER(name) \
  28. __SEMAPHORE_INITIALIZER(name,1)
  29. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  30. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  31. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  32. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  33. static inline void sema_init (struct semaphore *sem, int val)
  34. {
  35. *sem = (struct semaphore) __SEMAPHORE_INITIALIZER((*sem),val);
  36. }
  37. static inline void init_MUTEX (struct semaphore *sem)
  38. {
  39. sema_init(sem, 1);
  40. }
  41. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  42. {
  43. sema_init(sem, 0);
  44. }
  45. asmlinkage void __down(struct semaphore * sem);
  46. asmlinkage int __down_interruptible(struct semaphore * sem);
  47. asmlinkage int __down_trylock(struct semaphore * sem);
  48. asmlinkage void __up(struct semaphore * sem);
  49. static inline void down(struct semaphore * sem)
  50. {
  51. might_sleep();
  52. if (atomic_dec_return(&sem->count) < 0)
  53. __down(sem);
  54. }
  55. static inline int down_interruptible(struct semaphore * sem)
  56. {
  57. int ret = 0;
  58. might_sleep();
  59. if (atomic_dec_return(&sem->count) < 0)
  60. ret = __down_interruptible(sem);
  61. return ret;
  62. }
  63. static inline int down_trylock(struct semaphore * sem)
  64. {
  65. int old_val, new_val;
  66. /*
  67. * This inline assembly atomically implements the equivalent
  68. * to the following C code:
  69. * old_val = sem->count.counter;
  70. * if ((new_val = old_val) > 0)
  71. * sem->count.counter = --new_val;
  72. * In the ppc code this is called atomic_dec_if_positive.
  73. */
  74. __asm__ __volatile__ (
  75. " l %0,0(%3)\n"
  76. "0: ltr %1,%0\n"
  77. " jle 1f\n"
  78. " ahi %1,-1\n"
  79. " cs %0,%1,0(%3)\n"
  80. " jl 0b\n"
  81. "1:"
  82. : "=&d" (old_val), "=&d" (new_val), "=m" (sem->count.counter)
  83. : "a" (&sem->count.counter), "m" (sem->count.counter)
  84. : "cc", "memory" );
  85. return old_val <= 0;
  86. }
  87. static inline void up(struct semaphore * sem)
  88. {
  89. if (atomic_inc_return(&sem->count) <= 0)
  90. __up(sem);
  91. }
  92. #endif