semaphore.h 2.5 KB

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