2
0

qemu-coroutine-lock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "qemu-coroutine.h"
  26. #include "qemu-coroutine-int.h"
  27. #include "qemu-queue.h"
  28. #include "main-loop.h"
  29. #include "trace.h"
  30. static QTAILQ_HEAD(, Coroutine) unlock_bh_queue =
  31. QTAILQ_HEAD_INITIALIZER(unlock_bh_queue);
  32. static QEMUBH* unlock_bh;
  33. static void qemu_co_queue_next_bh(void *opaque)
  34. {
  35. Coroutine *next;
  36. trace_qemu_co_queue_next_bh();
  37. while ((next = QTAILQ_FIRST(&unlock_bh_queue))) {
  38. QTAILQ_REMOVE(&unlock_bh_queue, next, co_queue_next);
  39. qemu_coroutine_enter(next, NULL);
  40. }
  41. }
  42. void qemu_co_queue_init(CoQueue *queue)
  43. {
  44. QTAILQ_INIT(&queue->entries);
  45. if (!unlock_bh) {
  46. unlock_bh = qemu_bh_new(qemu_co_queue_next_bh, NULL);
  47. }
  48. }
  49. void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
  50. {
  51. Coroutine *self = qemu_coroutine_self();
  52. QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
  53. qemu_coroutine_yield();
  54. assert(qemu_in_coroutine());
  55. }
  56. void coroutine_fn qemu_co_queue_wait_insert_head(CoQueue *queue)
  57. {
  58. Coroutine *self = qemu_coroutine_self();
  59. QTAILQ_INSERT_HEAD(&queue->entries, self, co_queue_next);
  60. qemu_coroutine_yield();
  61. assert(qemu_in_coroutine());
  62. }
  63. bool qemu_co_queue_next(CoQueue *queue)
  64. {
  65. Coroutine *next;
  66. next = QTAILQ_FIRST(&queue->entries);
  67. if (next) {
  68. QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
  69. QTAILQ_INSERT_TAIL(&unlock_bh_queue, next, co_queue_next);
  70. trace_qemu_co_queue_next(next);
  71. qemu_bh_schedule(unlock_bh);
  72. }
  73. return (next != NULL);
  74. }
  75. void qemu_co_queue_restart_all(CoQueue *queue)
  76. {
  77. while (qemu_co_queue_next(queue)) {
  78. /* Do nothing */
  79. }
  80. }
  81. bool qemu_co_queue_empty(CoQueue *queue)
  82. {
  83. return (QTAILQ_FIRST(&queue->entries) == NULL);
  84. }
  85. void qemu_co_mutex_init(CoMutex *mutex)
  86. {
  87. memset(mutex, 0, sizeof(*mutex));
  88. qemu_co_queue_init(&mutex->queue);
  89. }
  90. void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
  91. {
  92. Coroutine *self = qemu_coroutine_self();
  93. trace_qemu_co_mutex_lock_entry(mutex, self);
  94. while (mutex->locked) {
  95. qemu_co_queue_wait(&mutex->queue);
  96. }
  97. mutex->locked = true;
  98. trace_qemu_co_mutex_lock_return(mutex, self);
  99. }
  100. void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
  101. {
  102. Coroutine *self = qemu_coroutine_self();
  103. trace_qemu_co_mutex_unlock_entry(mutex, self);
  104. assert(mutex->locked == true);
  105. assert(qemu_in_coroutine());
  106. mutex->locked = false;
  107. qemu_co_queue_next(&mutex->queue);
  108. trace_qemu_co_mutex_unlock_return(mutex, self);
  109. }
  110. void qemu_co_rwlock_init(CoRwlock *lock)
  111. {
  112. memset(lock, 0, sizeof(*lock));
  113. qemu_co_queue_init(&lock->queue);
  114. }
  115. void qemu_co_rwlock_rdlock(CoRwlock *lock)
  116. {
  117. while (lock->writer) {
  118. qemu_co_queue_wait(&lock->queue);
  119. }
  120. lock->reader++;
  121. }
  122. void qemu_co_rwlock_unlock(CoRwlock *lock)
  123. {
  124. assert(qemu_in_coroutine());
  125. if (lock->writer) {
  126. lock->writer = false;
  127. qemu_co_queue_restart_all(&lock->queue);
  128. } else {
  129. lock->reader--;
  130. assert(lock->reader >= 0);
  131. /* Wakeup only one waiting writer */
  132. if (!lock->reader) {
  133. qemu_co_queue_next(&lock->queue);
  134. }
  135. }
  136. }
  137. void qemu_co_rwlock_wrlock(CoRwlock *lock)
  138. {
  139. while (lock->writer || lock->reader) {
  140. qemu_co_queue_wait(&lock->queue);
  141. }
  142. lock->writer = true;
  143. }