qemu-coroutine-lock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/osdep.h"
  25. #include "qemu-common.h"
  26. #include "qemu/coroutine.h"
  27. #include "qemu/coroutine_int.h"
  28. #include "qemu/queue.h"
  29. #include "trace.h"
  30. void qemu_co_queue_init(CoQueue *queue)
  31. {
  32. QTAILQ_INIT(&queue->entries);
  33. }
  34. void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
  35. {
  36. Coroutine *self = qemu_coroutine_self();
  37. QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
  38. qemu_coroutine_yield();
  39. assert(qemu_in_coroutine());
  40. }
  41. /**
  42. * qemu_co_queue_run_restart:
  43. *
  44. * Enter each coroutine that was previously marked for restart by
  45. * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
  46. * invoked by the core coroutine code when the current coroutine yields or
  47. * terminates.
  48. */
  49. void qemu_co_queue_run_restart(Coroutine *co)
  50. {
  51. Coroutine *next;
  52. trace_qemu_co_queue_run_restart(co);
  53. while ((next = QTAILQ_FIRST(&co->co_queue_wakeup))) {
  54. QTAILQ_REMOVE(&co->co_queue_wakeup, next, co_queue_next);
  55. qemu_coroutine_enter(next, NULL);
  56. }
  57. }
  58. static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
  59. {
  60. Coroutine *self = qemu_coroutine_self();
  61. Coroutine *next;
  62. if (QTAILQ_EMPTY(&queue->entries)) {
  63. return false;
  64. }
  65. while ((next = QTAILQ_FIRST(&queue->entries)) != NULL) {
  66. QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
  67. QTAILQ_INSERT_TAIL(&self->co_queue_wakeup, next, co_queue_next);
  68. trace_qemu_co_queue_next(next);
  69. if (single) {
  70. break;
  71. }
  72. }
  73. return true;
  74. }
  75. bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
  76. {
  77. assert(qemu_in_coroutine());
  78. return qemu_co_queue_do_restart(queue, true);
  79. }
  80. void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
  81. {
  82. assert(qemu_in_coroutine());
  83. qemu_co_queue_do_restart(queue, false);
  84. }
  85. bool qemu_co_enter_next(CoQueue *queue)
  86. {
  87. Coroutine *next;
  88. next = QTAILQ_FIRST(&queue->entries);
  89. if (!next) {
  90. return false;
  91. }
  92. QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
  93. qemu_coroutine_enter(next, NULL);
  94. return true;
  95. }
  96. bool qemu_co_queue_empty(CoQueue *queue)
  97. {
  98. return QTAILQ_FIRST(&queue->entries) == NULL;
  99. }
  100. void qemu_co_mutex_init(CoMutex *mutex)
  101. {
  102. memset(mutex, 0, sizeof(*mutex));
  103. qemu_co_queue_init(&mutex->queue);
  104. }
  105. void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
  106. {
  107. Coroutine *self = qemu_coroutine_self();
  108. trace_qemu_co_mutex_lock_entry(mutex, self);
  109. while (mutex->locked) {
  110. qemu_co_queue_wait(&mutex->queue);
  111. }
  112. mutex->locked = true;
  113. trace_qemu_co_mutex_lock_return(mutex, self);
  114. }
  115. void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
  116. {
  117. Coroutine *self = qemu_coroutine_self();
  118. trace_qemu_co_mutex_unlock_entry(mutex, self);
  119. assert(mutex->locked == true);
  120. assert(qemu_in_coroutine());
  121. mutex->locked = false;
  122. qemu_co_queue_next(&mutex->queue);
  123. trace_qemu_co_mutex_unlock_return(mutex, self);
  124. }
  125. void qemu_co_rwlock_init(CoRwlock *lock)
  126. {
  127. memset(lock, 0, sizeof(*lock));
  128. qemu_co_queue_init(&lock->queue);
  129. }
  130. void qemu_co_rwlock_rdlock(CoRwlock *lock)
  131. {
  132. while (lock->writer) {
  133. qemu_co_queue_wait(&lock->queue);
  134. }
  135. lock->reader++;
  136. }
  137. void qemu_co_rwlock_unlock(CoRwlock *lock)
  138. {
  139. assert(qemu_in_coroutine());
  140. if (lock->writer) {
  141. lock->writer = false;
  142. qemu_co_queue_restart_all(&lock->queue);
  143. } else {
  144. lock->reader--;
  145. assert(lock->reader >= 0);
  146. /* Wakeup only one waiting writer */
  147. if (!lock->reader) {
  148. qemu_co_queue_next(&lock->queue);
  149. }
  150. }
  151. }
  152. void qemu_co_rwlock_wrlock(CoRwlock *lock)
  153. {
  154. while (lock->writer || lock->reader) {
  155. qemu_co_queue_wait(&lock->queue);
  156. }
  157. lock->writer = true;
  158. }