async.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  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-aio.h"
  26. #include "main-loop.h"
  27. /* Anchor of the list of Bottom Halves belonging to the context */
  28. static struct QEMUBH *first_bh;
  29. /***********************************************************/
  30. /* bottom halves (can be seen as timers which expire ASAP) */
  31. struct QEMUBH {
  32. QEMUBHFunc *cb;
  33. void *opaque;
  34. QEMUBH *next;
  35. bool scheduled;
  36. bool idle;
  37. bool deleted;
  38. };
  39. QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
  40. {
  41. QEMUBH *bh;
  42. bh = g_malloc0(sizeof(QEMUBH));
  43. bh->cb = cb;
  44. bh->opaque = opaque;
  45. bh->next = first_bh;
  46. first_bh = bh;
  47. return bh;
  48. }
  49. int qemu_bh_poll(void)
  50. {
  51. QEMUBH *bh, **bhp, *next;
  52. int ret;
  53. static int nesting = 0;
  54. nesting++;
  55. ret = 0;
  56. for (bh = first_bh; bh; bh = next) {
  57. next = bh->next;
  58. if (!bh->deleted && bh->scheduled) {
  59. bh->scheduled = 0;
  60. if (!bh->idle)
  61. ret = 1;
  62. bh->idle = 0;
  63. bh->cb(bh->opaque);
  64. }
  65. }
  66. nesting--;
  67. /* remove deleted bhs */
  68. if (!nesting) {
  69. bhp = &first_bh;
  70. while (*bhp) {
  71. bh = *bhp;
  72. if (bh->deleted) {
  73. *bhp = bh->next;
  74. g_free(bh);
  75. } else {
  76. bhp = &bh->next;
  77. }
  78. }
  79. }
  80. return ret;
  81. }
  82. void qemu_bh_schedule_idle(QEMUBH *bh)
  83. {
  84. if (bh->scheduled)
  85. return;
  86. bh->scheduled = 1;
  87. bh->idle = 1;
  88. }
  89. void qemu_bh_schedule(QEMUBH *bh)
  90. {
  91. if (bh->scheduled)
  92. return;
  93. bh->scheduled = 1;
  94. bh->idle = 0;
  95. /* stop the currently executing CPU to execute the BH ASAP */
  96. qemu_notify_event();
  97. }
  98. void qemu_bh_cancel(QEMUBH *bh)
  99. {
  100. bh->scheduled = 0;
  101. }
  102. void qemu_bh_delete(QEMUBH *bh)
  103. {
  104. bh->scheduled = 0;
  105. bh->deleted = 1;
  106. }
  107. void qemu_bh_update_timeout(uint32_t *timeout)
  108. {
  109. QEMUBH *bh;
  110. for (bh = first_bh; bh; bh = bh->next) {
  111. if (!bh->deleted && bh->scheduled) {
  112. if (bh->idle) {
  113. /* idle bottom halves will be polled at least
  114. * every 10ms */
  115. *timeout = MIN(10, *timeout);
  116. } else {
  117. /* non-idle bottom halves will be executed
  118. * immediately */
  119. *timeout = 0;
  120. break;
  121. }
  122. }
  123. }
  124. }