2
0

async.c 3.3 KB

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