Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 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 | |
| 25 | #include "qemu-common.h" |
Kevin Wolf | 9a1e948 | 2009-10-22 17:54:38 +0200 | [diff] [blame] | 26 | #include "qemu-aio.h" |
Paolo Bonzini | 44a9b35 | 2011-09-12 16:44:30 +0200 | [diff] [blame] | 27 | #include "main-loop.h" |
Kevin Wolf | 9a1e948 | 2009-10-22 17:54:38 +0200 | [diff] [blame] | 28 | |
Kevin Wolf | 384acbf | 2011-07-15 16:36:40 +0200 | [diff] [blame] | 29 | /* Anchor of the list of Bottom Halves belonging to the context */ |
| 30 | static struct QEMUBH *first_bh; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 31 | |
| 32 | /***********************************************************/ |
| 33 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 34 | |
| 35 | struct QEMUBH { |
| 36 | QEMUBHFunc *cb; |
| 37 | void *opaque; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 38 | QEMUBH *next; |
Stefan Weil | 9b47b17 | 2012-04-29 19:08:45 +0200 | [diff] [blame] | 39 | bool scheduled; |
| 40 | bool idle; |
| 41 | bool deleted; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 44 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 45 | { |
| 46 | QEMUBH *bh; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 47 | bh = g_malloc0(sizeof(QEMUBH)); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 48 | bh->cb = cb; |
| 49 | bh->opaque = opaque; |
Kevin Wolf | 384acbf | 2011-07-15 16:36:40 +0200 | [diff] [blame] | 50 | bh->next = first_bh; |
| 51 | first_bh = bh; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 52 | return bh; |
| 53 | } |
| 54 | |
| 55 | int qemu_bh_poll(void) |
| 56 | { |
Kevin Wolf | 7887f62 | 2011-06-07 17:51:21 +0200 | [diff] [blame] | 57 | QEMUBH *bh, **bhp, *next; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 58 | int ret; |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 59 | static int nesting = 0; |
| 60 | |
| 61 | nesting++; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 62 | |
| 63 | ret = 0; |
Kevin Wolf | 384acbf | 2011-07-15 16:36:40 +0200 | [diff] [blame] | 64 | for (bh = first_bh; bh; bh = next) { |
Kevin Wolf | 7887f62 | 2011-06-07 17:51:21 +0200 | [diff] [blame] | 65 | next = bh->next; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 66 | if (!bh->deleted && bh->scheduled) { |
| 67 | bh->scheduled = 0; |
| 68 | if (!bh->idle) |
| 69 | ret = 1; |
| 70 | bh->idle = 0; |
| 71 | bh->cb(bh->opaque); |
| 72 | } |
| 73 | } |
| 74 | |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 75 | nesting--; |
| 76 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 77 | /* remove deleted bhs */ |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 78 | if (!nesting) { |
| 79 | bhp = &first_bh; |
| 80 | while (*bhp) { |
| 81 | bh = *bhp; |
| 82 | if (bh->deleted) { |
| 83 | *bhp = bh->next; |
| 84 | g_free(bh); |
| 85 | } else { |
| 86 | bhp = &bh->next; |
| 87 | } |
| 88 | } |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | void qemu_bh_schedule_idle(QEMUBH *bh) |
| 95 | { |
| 96 | if (bh->scheduled) |
| 97 | return; |
| 98 | bh->scheduled = 1; |
| 99 | bh->idle = 1; |
| 100 | } |
| 101 | |
| 102 | void qemu_bh_schedule(QEMUBH *bh) |
| 103 | { |
| 104 | if (bh->scheduled) |
| 105 | return; |
| 106 | bh->scheduled = 1; |
| 107 | bh->idle = 0; |
| 108 | /* stop the currently executing CPU to execute the BH ASAP */ |
| 109 | qemu_notify_event(); |
| 110 | } |
| 111 | |
| 112 | void qemu_bh_cancel(QEMUBH *bh) |
| 113 | { |
| 114 | bh->scheduled = 0; |
| 115 | } |
| 116 | |
| 117 | void qemu_bh_delete(QEMUBH *bh) |
| 118 | { |
| 119 | bh->scheduled = 0; |
| 120 | bh->deleted = 1; |
| 121 | } |
| 122 | |
Stefano Stabellini | 7c7db75 | 2012-04-13 19:35:04 +0100 | [diff] [blame] | 123 | void qemu_bh_update_timeout(uint32_t *timeout) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 124 | { |
| 125 | QEMUBH *bh; |
| 126 | |
Kevin Wolf | 384acbf | 2011-07-15 16:36:40 +0200 | [diff] [blame] | 127 | for (bh = first_bh; bh; bh = bh->next) { |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 128 | if (!bh->deleted && bh->scheduled) { |
| 129 | if (bh->idle) { |
| 130 | /* idle bottom halves will be polled at least |
| 131 | * every 10ms */ |
| 132 | *timeout = MIN(10, *timeout); |
| 133 | } else { |
| 134 | /* non-idle bottom halves will be executed |
| 135 | * immediately */ |
| 136 | *timeout = 0; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |