qemu-coroutine-lock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. bool qemu_co_queue_next(CoQueue *queue)
  57. {
  58. Coroutine *next;
  59. next = QTAILQ_FIRST(&queue->entries);
  60. if (next) {
  61. QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
  62. QTAILQ_INSERT_TAIL(&unlock_bh_queue, next, co_queue_next);
  63. trace_qemu_co_queue_next(next);
  64. qemu_bh_schedule(unlock_bh);
  65. }
  66. return (next != NULL);
  67. }
  68. bool qemu_co_queue_empty(CoQueue *queue)
  69. {
  70. return (QTAILQ_FIRST(&queue->entries) == NULL);
  71. }
  72. void qemu_co_mutex_init(CoMutex *mutex)
  73. {
  74. memset(mutex, 0, sizeof(*mutex));
  75. qemu_co_queue_init(&mutex->queue);
  76. }
  77. void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
  78. {
  79. Coroutine *self = qemu_coroutine_self();
  80. trace_qemu_co_mutex_lock_entry(mutex, self);
  81. while (mutex->locked) {
  82. qemu_co_queue_wait(&mutex->queue);
  83. }
  84. mutex->locked = true;
  85. trace_qemu_co_mutex_lock_return(mutex, self);
  86. }
  87. void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
  88. {
  89. Coroutine *self = qemu_coroutine_self();
  90. trace_qemu_co_mutex_unlock_entry(mutex, self);
  91. assert(mutex->locked == true);
  92. assert(qemu_in_coroutine());
  93. mutex->locked = false;
  94. qemu_co_queue_next(&mutex->queue);
  95. trace_qemu_co_mutex_unlock_return(mutex, self);
  96. }
  97. void qemu_co_rwlock_init(CoRwlock *lock)
  98. {
  99. memset(lock, 0, sizeof(*lock));
  100. qemu_co_queue_init(&lock->queue);
  101. }
  102. void qemu_co_rwlock_rdlock(CoRwlock *lock)
  103. {
  104. while (lock->writer) {
  105. qemu_co_queue_wait(&lock->queue);
  106. }
  107. lock->reader++;
  108. }
  109. void qemu_co_rwlock_unlock(CoRwlock *lock)
  110. {
  111. assert(qemu_in_coroutine());
  112. if (lock->writer) {
  113. lock->writer = false;
  114. while (!qemu_co_queue_empty(&lock->queue)) {
  115. /*
  116. * Wakeup every body. This will include some
  117. * writers too.
  118. */
  119. qemu_co_queue_next(&lock->queue);
  120. }
  121. } else {
  122. lock->reader--;
  123. assert(lock->reader >= 0);
  124. /* Wakeup only one waiting writer */
  125. if (!lock->reader) {
  126. qemu_co_queue_next(&lock->queue);
  127. }
  128. }
  129. }
  130. void qemu_co_rwlock_wrlock(CoRwlock *lock)
  131. {
  132. while (lock->writer || lock->reader) {
  133. qemu_co_queue_wait(&lock->queue);
  134. }
  135. lock->writer = true;
  136. }