spinlock.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * arch/s390/lib/spinlock.c
  3. * Out of line spinlock code.
  4. *
  5. * Copyright (C) IBM Corp. 2004, 2006
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. */
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/init.h>
  12. #include <asm/io.h>
  13. int spin_retry = 1000;
  14. /**
  15. * spin_retry= parameter
  16. */
  17. static int __init spin_retry_setup(char *str)
  18. {
  19. spin_retry = simple_strtoul(str, &str, 0);
  20. return 1;
  21. }
  22. __setup("spin_retry=", spin_retry_setup);
  23. static inline void
  24. _diag44(void)
  25. {
  26. #ifdef CONFIG_64BIT
  27. if (MACHINE_HAS_DIAG44)
  28. #endif
  29. asm volatile("diag 0,0,0x44");
  30. }
  31. void
  32. _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc)
  33. {
  34. int count = spin_retry;
  35. while (1) {
  36. if (count-- <= 0) {
  37. _diag44();
  38. count = spin_retry;
  39. }
  40. if (__raw_spin_is_locked(lp))
  41. continue;
  42. if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
  43. return;
  44. }
  45. }
  46. EXPORT_SYMBOL(_raw_spin_lock_wait);
  47. int
  48. _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc)
  49. {
  50. int count = spin_retry;
  51. while (count-- > 0) {
  52. if (__raw_spin_is_locked(lp))
  53. continue;
  54. if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(_raw_spin_trylock_retry);
  60. void
  61. _raw_read_lock_wait(raw_rwlock_t *rw)
  62. {
  63. unsigned int old;
  64. int count = spin_retry;
  65. while (1) {
  66. if (count-- <= 0) {
  67. _diag44();
  68. count = spin_retry;
  69. }
  70. if (!__raw_read_can_lock(rw))
  71. continue;
  72. old = rw->lock & 0x7fffffffU;
  73. if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
  74. return;
  75. }
  76. }
  77. EXPORT_SYMBOL(_raw_read_lock_wait);
  78. int
  79. _raw_read_trylock_retry(raw_rwlock_t *rw)
  80. {
  81. unsigned int old;
  82. int count = spin_retry;
  83. while (count-- > 0) {
  84. if (!__raw_read_can_lock(rw))
  85. continue;
  86. old = rw->lock & 0x7fffffffU;
  87. if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(_raw_read_trylock_retry);
  93. void
  94. _raw_write_lock_wait(raw_rwlock_t *rw)
  95. {
  96. int count = spin_retry;
  97. while (1) {
  98. if (count-- <= 0) {
  99. _diag44();
  100. count = spin_retry;
  101. }
  102. if (!__raw_write_can_lock(rw))
  103. continue;
  104. if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
  105. return;
  106. }
  107. }
  108. EXPORT_SYMBOL(_raw_write_lock_wait);
  109. int
  110. _raw_write_trylock_retry(raw_rwlock_t *rw)
  111. {
  112. int count = spin_retry;
  113. while (count-- > 0) {
  114. if (!__raw_write_can_lock(rw))
  115. continue;
  116. if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. EXPORT_SYMBOL(_raw_write_trylock_retry);