rwlock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /**
  17. * A dummy implementation of a rw_lock using spinlocks ...
  18. */
  19. #include "rwlock.h"
  20. static void _starpu_take_busy_lock(struct _starpu_rw_lock *lock)
  21. {
  22. uint32_t prev;
  23. do
  24. {
  25. prev = STARPU_TEST_AND_SET(&lock->busy, 1);
  26. }
  27. while (prev);
  28. }
  29. static void _starpu_release_busy_lock(struct _starpu_rw_lock *lock)
  30. {
  31. STARPU_RELEASE(&lock->busy);
  32. }
  33. void _starpu_init_rw_lock(struct _starpu_rw_lock *lock)
  34. {
  35. STARPU_ASSERT(lock);
  36. lock->writer = 0;
  37. lock->readercnt = 0;
  38. lock->busy = 0;
  39. }
  40. int _starpu_take_rw_lock_write_try(struct _starpu_rw_lock *lock)
  41. {
  42. _starpu_take_busy_lock(lock);
  43. if (lock->readercnt > 0 || lock->writer)
  44. {
  45. /* fail to take the lock */
  46. _starpu_release_busy_lock(lock);
  47. return -1;
  48. }
  49. else
  50. {
  51. STARPU_ASSERT(lock->readercnt == 0);
  52. STARPU_ASSERT(lock->writer == 0);
  53. /* no one was either writing nor reading */
  54. lock->writer = 1;
  55. _starpu_release_busy_lock(lock);
  56. return 0;
  57. }
  58. }
  59. int _starpu_take_rw_lock_read_try(struct _starpu_rw_lock *lock)
  60. {
  61. _starpu_take_busy_lock(lock);
  62. if (lock->writer)
  63. {
  64. /* there is a writer ... */
  65. _starpu_release_busy_lock(lock);
  66. return -1;
  67. }
  68. else
  69. {
  70. STARPU_ASSERT(lock->writer == 0);
  71. /* no one is writing */
  72. /* XXX check wrap arounds ... */
  73. lock->readercnt++;
  74. _starpu_release_busy_lock(lock);
  75. return 0;
  76. }
  77. }
  78. void _starpu_take_rw_lock_write(struct _starpu_rw_lock *lock)
  79. {
  80. do
  81. {
  82. _starpu_take_busy_lock(lock);
  83. if (lock->readercnt > 0 || lock->writer)
  84. {
  85. /* fail to take the lock */
  86. _starpu_release_busy_lock(lock);
  87. }
  88. else
  89. {
  90. STARPU_ASSERT(lock->readercnt == 0);
  91. STARPU_ASSERT(lock->writer == 0);
  92. /* no one was either writing nor reading */
  93. lock->writer = 1;
  94. _starpu_release_busy_lock(lock);
  95. return;
  96. }
  97. }
  98. while (1);
  99. }
  100. void _starpu_take_rw_lock_read(struct _starpu_rw_lock *lock)
  101. {
  102. do
  103. {
  104. _starpu_take_busy_lock(lock);
  105. if (lock->writer)
  106. {
  107. /* there is a writer ... */
  108. _starpu_release_busy_lock(lock);
  109. }
  110. else
  111. {
  112. STARPU_ASSERT(lock->writer == 0);
  113. /* no one is writing */
  114. /* XXX check wrap arounds ... */
  115. lock->readercnt++;
  116. _starpu_release_busy_lock(lock);
  117. return;
  118. }
  119. }
  120. while (1);
  121. }
  122. void _starpu_release_rw_lock(struct _starpu_rw_lock *lock)
  123. {
  124. _starpu_take_busy_lock(lock);
  125. /* either writer or reader (exactly one !) */
  126. if (lock->writer)
  127. {
  128. STARPU_ASSERT(lock->readercnt == 0);
  129. lock->writer = 0;
  130. }
  131. else
  132. {
  133. /* reading mode */
  134. STARPU_ASSERT(lock->writer == 0);
  135. lock->readercnt--;
  136. }
  137. _starpu_release_busy_lock(lock);
  138. }