2
0

qemu-coroutine-lock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * coroutine queues and locks
  3. *
  4. * Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "block/coroutine.h"
  26. #include "block/coroutine_int.h"
  27. #include "qemu/queue.h"
  28. #include "trace.h"
  29. void qemu_co_queue_init(CoQueue *queue)
  30. {
  31. QTAILQ_INIT(&queue->entries);
  32. }
  33. void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
  34. {
  35. Coroutine *self = qemu_coroutine_self();
  36. QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
  37. qemu_coroutine_yield();
  38. assert(qemu_in_coroutine());
  39. }
  40. /**
  41. * qemu_co_queue_run_restart:
  42. *
  43. * Enter each coroutine that was previously marked for restart by
  44. * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
  45. * invoked by the core coroutine code when the current coroutine yields or
  46. * terminates.
  47. */
  48. void qemu_co_queue_run_restart(Coroutine *co)
  49. {
  50. Coroutine *next;
  51. trace_qemu_co_queue_run_restart(co);
  52. while ((next = QTAILQ_FIRST(&co->co_queue_wakeup))) {
  53. QTAILQ_REMOVE(&co->co_queue_wakeup, next, co_queue_next);
  54. qemu_coroutine_enter(next, NULL);
  55. }
  56. }
  57. static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
  58. {
  59. Coroutine *self = qemu_coroutine_self();
  60. Coroutine *next;
  61. if (QTAILQ_EMPTY(&queue->entries)) {
  62. return false;
  63. }
  64. while ((next = QTAILQ_FIRST(&queue->entries)) != NULL) {
  65. QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
  66. QTAILQ_INSERT_TAIL(&self->co_queue_wakeup, next, co_queue_next);
  67. trace_qemu_co_queue_next(next);
  68. if (single) {
  69. break;
  70. }
  71. }
  72. return true;
  73. }
  74. bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
  75. {
  76. assert(qemu_in_coroutine());
  77. return qemu_co_queue_do_restart(queue, true);
  78. }
  79. void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
  80. {
  81. assert(qemu_in_coroutine());
  82. qemu_co_queue_do_restart(queue, false);
  83. }
  84. bool qemu_co_enter_next(CoQueue *queue)
  85. {
  86. Coroutine *next;
  87. next = QTAILQ_FIRST(&queue->entries);
  88. if (!next) {
  89. return false;
  90. }
  91. QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
  92. qemu_coroutine_enter(next, NULL);
  93. return true;
  94. }
  95. bool qemu_co_queue_empty(CoQueue *queue)
  96. {
  97. return (QTAILQ_FIRST(&queue->entries) == NULL);
  98. }
  99. void qemu_co_mutex_init(CoMutex *mutex)
  100. {
  101. memset(mutex, 0, sizeof(*mutex));
  102. qemu_co_queue_init(&mutex->queue);
  103. }
  104. void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
  105. {
  106. Coroutine *self = qemu_coroutine_self();
  107. trace_qemu_co_mutex_lock_entry(mutex, self);
  108. while (mutex->locked) {
  109. qemu_co_queue_wait(&mutex->queue);
  110. }
  111. mutex->locked = true;
  112. trace_qemu_co_mutex_lock_return(mutex, self);
  113. }
  114. void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
  115. {
  116. Coroutine *self = qemu_coroutine_self();
  117. trace_qemu_co_mutex_unlock_entry(mutex, self);
  118. assert(mutex->locked == true);
  119. assert(qemu_in_coroutine());
  120. mutex->locked = false;
  121. qemu_co_queue_next(&mutex->queue);
  122. trace_qemu_co_mutex_unlock_return(mutex, self);
  123. }
  124. void qemu_co_rwlock_init(CoRwlock *lock)
  125. {
  126. memset(lock, 0, sizeof(*lock));
  127. qemu_co_queue_init(&lock->queue);
  128. }
  129. void qemu_co_rwlock_rdlock(CoRwlock *lock)
  130. {
  131. while (lock->writer) {
  132. qemu_co_queue_wait(&lock->queue);
  133. }
  134. lock->reader++;
  135. }
  136. void qemu_co_rwlock_unlock(CoRwlock *lock)
  137. {
  138. assert(qemu_in_coroutine());
  139. if (lock->writer) {
  140. lock->writer = false;
  141. qemu_co_queue_restart_all(&lock->queue);
  142. } else {
  143. lock->reader--;
  144. assert(lock->reader >= 0);
  145. /* Wakeup only one waiting writer */
  146. if (!lock->reader) {
  147. qemu_co_queue_next(&lock->queue);
  148. }
  149. }
  150. }
  151. void qemu_co_rwlock_wrlock(CoRwlock *lock)
  152. {
  153. while (lock->writer || lock->reader) {
  154. qemu_co_queue_wait(&lock->queue);
  155. }
  156. lock->writer = true;
  157. }