Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU VNC display driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> |
| 5 | * Copyright (C) 2006 Fabrice Bellard |
| 6 | * Copyright (C) 2009 Red Hat, Inc |
| 7 | * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 29 | #include "qemu/osdep.h" |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 30 | #include "vnc.h" |
| 31 | #include "vnc-jobs.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/sockets.h" |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 33 | #include "qemu/main-loop.h" |
Markus Armbruster | a0b1a66 | 2015-03-17 18:16:21 +0100 | [diff] [blame] | 34 | #include "block/aio.h" |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 35 | #include "trace.h" |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Locking: |
| 39 | * |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 40 | * There are three levels of locking: |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 41 | * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?) |
| 42 | * - VncDisplay global lock: mainly used for framebuffer updates to avoid |
| 43 | * screen corruption if the framebuffer is updated |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 44 | * while the worker is doing something. |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 45 | * - VncState::output lock: used to make sure the output buffer is not corrupted |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 46 | * if two threads try to write on it at the same time |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 47 | * |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 48 | * While the VNC worker thread is working, the VncDisplay global lock is held |
| 49 | * to avoid screen corruption (this does not block vnc_refresh() because it |
| 50 | * uses trylock()) but the output lock is not held because the thread works on |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 51 | * its own output buffer. |
| 52 | * When the encoding job is done, the worker thread will hold the output lock |
| 53 | * and copy its output buffer in vs->output. |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 54 | */ |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 55 | |
| 56 | struct VncJobQueue { |
| 57 | QemuCond cond; |
| 58 | QemuMutex mutex; |
| 59 | QemuThread thread; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 60 | bool exit; |
| 61 | QTAILQ_HEAD(, VncJob) jobs; |
| 62 | }; |
| 63 | |
| 64 | typedef struct VncJobQueue VncJobQueue; |
| 65 | |
| 66 | /* |
| 67 | * We use a single global queue, but most of the functions are |
Peter Maydell | 11f6697 | 2012-10-18 17:40:53 +0100 | [diff] [blame] | 68 | * already reentrant, so we can easily add more than one encoding thread |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 69 | */ |
| 70 | static VncJobQueue *queue; |
| 71 | |
| 72 | static void vnc_lock_queue(VncJobQueue *queue) |
| 73 | { |
| 74 | qemu_mutex_lock(&queue->mutex); |
| 75 | } |
| 76 | |
| 77 | static void vnc_unlock_queue(VncJobQueue *queue) |
| 78 | { |
| 79 | qemu_mutex_unlock(&queue->mutex); |
| 80 | } |
| 81 | |
| 82 | VncJob *vnc_job_new(VncState *vs) |
| 83 | { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 84 | VncJob *job = g_new0(VncJob, 1); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 85 | |
Gerd Hoffmann | f31f9c1 | 2018-05-07 12:22:54 +0200 | [diff] [blame] | 86 | assert(vs->magic == VNC_MAGIC); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 87 | job->vs = vs; |
| 88 | vnc_lock_queue(queue); |
| 89 | QLIST_INIT(&job->rectangles); |
| 90 | vnc_unlock_queue(queue); |
| 91 | return job; |
| 92 | } |
| 93 | |
| 94 | int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h) |
| 95 | { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 96 | VncRectEntry *entry = g_new0(VncRectEntry, 1); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 97 | |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 98 | trace_vnc_job_add_rect(job->vs, job, x, y, w, h); |
| 99 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 100 | entry->rect.x = x; |
| 101 | entry->rect.y = y; |
| 102 | entry->rect.w = w; |
| 103 | entry->rect.h = h; |
| 104 | |
| 105 | vnc_lock_queue(queue); |
| 106 | QLIST_INSERT_HEAD(&job->rectangles, entry, next); |
| 107 | vnc_unlock_queue(queue); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | void vnc_job_push(VncJob *job) |
| 112 | { |
| 113 | vnc_lock_queue(queue); |
| 114 | if (queue->exit || QLIST_EMPTY(&job->rectangles)) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 115 | g_free(job); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 116 | } else { |
| 117 | QTAILQ_INSERT_TAIL(&queue->jobs, job, next); |
| 118 | qemu_cond_broadcast(&queue->cond); |
| 119 | } |
| 120 | vnc_unlock_queue(queue); |
| 121 | } |
| 122 | |
| 123 | static bool vnc_has_job_locked(VncState *vs) |
| 124 | { |
| 125 | VncJob *job; |
| 126 | |
| 127 | QTAILQ_FOREACH(job, &queue->jobs, next) { |
| 128 | if (job->vs == vs || !vs) { |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 135 | void vnc_jobs_join(VncState *vs) |
| 136 | { |
| 137 | vnc_lock_queue(queue); |
| 138 | while (vnc_has_job_locked(vs)) { |
| 139 | qemu_cond_wait(&queue->cond, &queue->mutex); |
| 140 | } |
| 141 | vnc_unlock_queue(queue); |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 142 | vnc_jobs_consume_buffer(vs); |
| 143 | } |
| 144 | |
| 145 | void vnc_jobs_consume_buffer(VncState *vs) |
| 146 | { |
| 147 | bool flush; |
| 148 | |
| 149 | vnc_lock_output(vs); |
| 150 | if (vs->jobs_buffer.offset) { |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 151 | if (vs->ioc != NULL && buffer_empty(&vs->output)) { |
| 152 | if (vs->ioc_tag) { |
| 153 | g_source_remove(vs->ioc_tag); |
| 154 | } |
Klim Kireev | d49b87f | 2018-02-07 12:48:44 +0300 | [diff] [blame] | 155 | if (vs->disconnecting == FALSE) { |
| 156 | vs->ioc_tag = qio_channel_add_watch( |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 157 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT, |
| 158 | vnc_client_io, vs, NULL); |
Klim Kireev | d49b87f | 2018-02-07 12:48:44 +0300 | [diff] [blame] | 159 | } |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 160 | } |
| 161 | buffer_move(&vs->output, &vs->jobs_buffer); |
Daniel P. Berrange | ada8d2e | 2017-12-18 19:12:25 +0000 | [diff] [blame] | 162 | |
| 163 | if (vs->job_update == VNC_STATE_UPDATE_FORCE) { |
| 164 | vs->force_update_offset = vs->output.offset; |
| 165 | } |
| 166 | vs->job_update = VNC_STATE_UPDATE_NONE; |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 167 | } |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 168 | flush = vs->ioc != NULL && vs->abort != true; |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 169 | vnc_unlock_output(vs); |
| 170 | |
| 171 | if (flush) { |
| 172 | vnc_flush(vs); |
| 173 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Copy data for local use |
| 178 | */ |
| 179 | static void vnc_async_encoding_start(VncState *orig, VncState *local) |
| 180 | { |
Gerd Hoffmann | 2e0c90a | 2015-10-30 12:10:10 +0100 | [diff] [blame] | 181 | buffer_init(&local->output, "vnc-worker-output"); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 182 | local->sioc = NULL; /* Don't do any network work on this thread */ |
| 183 | local->ioc = NULL; /* Don't do any network work on this thread */ |
Gerd Hoffmann | 2e0c90a | 2015-10-30 12:10:10 +0100 | [diff] [blame] | 184 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 185 | local->vnc_encoding = orig->vnc_encoding; |
| 186 | local->features = orig->features; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 187 | local->vd = orig->vd; |
Corentin Chary | 7d964c9 | 2011-02-04 09:05:56 +0100 | [diff] [blame] | 188 | local->lossy_rect = orig->lossy_rect; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 189 | local->write_pixels = orig->write_pixels; |
Gerd Hoffmann | 9f64916 | 2012-10-10 13:29:43 +0200 | [diff] [blame] | 190 | local->client_pf = orig->client_pf; |
| 191 | local->client_be = orig->client_be; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 192 | local->tight = orig->tight; |
| 193 | local->zlib = orig->zlib; |
| 194 | local->hextile = orig->hextile; |
Corentin Chary | 148954f | 2011-02-04 09:06:01 +0100 | [diff] [blame] | 195 | local->zrle = orig->zrle; |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 196 | local->client_width = orig->client_width; |
| 197 | local->client_height = orig->client_height; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void vnc_async_encoding_end(VncState *orig, VncState *local) |
| 201 | { |
Peter Wu | 0ae0b06 | 2018-08-08 00:18:30 +0200 | [diff] [blame] | 202 | buffer_free(&local->output); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 203 | orig->tight = local->tight; |
| 204 | orig->zlib = local->zlib; |
| 205 | orig->hextile = local->hextile; |
Corentin Chary | 148954f | 2011-02-04 09:06:01 +0100 | [diff] [blame] | 206 | orig->zrle = local->zrle; |
Corentin Chary | 7d964c9 | 2011-02-04 09:05:56 +0100 | [diff] [blame] | 207 | orig->lossy_rect = local->lossy_rect; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 210 | static bool vnc_worker_clamp_rect(VncState *vs, VncJob *job, VncRect *rect) |
| 211 | { |
| 212 | trace_vnc_job_clamp_rect(vs, job, rect->x, rect->y, rect->w, rect->h); |
| 213 | |
| 214 | if (rect->x >= vs->client_width) { |
| 215 | goto discard; |
| 216 | } |
| 217 | rect->w = MIN(vs->client_width - rect->x, rect->w); |
| 218 | if (rect->w == 0) { |
| 219 | goto discard; |
| 220 | } |
| 221 | |
| 222 | if (rect->y >= vs->client_height) { |
| 223 | goto discard; |
| 224 | } |
| 225 | rect->h = MIN(vs->client_height - rect->y, rect->h); |
| 226 | if (rect->h == 0) { |
| 227 | goto discard; |
| 228 | } |
| 229 | |
| 230 | trace_vnc_job_clamped_rect(vs, job, rect->x, rect->y, rect->w, rect->h); |
| 231 | return true; |
| 232 | |
| 233 | discard: |
| 234 | trace_vnc_job_discard_rect(vs, job, rect->x, rect->y, rect->w, rect->h); |
| 235 | return false; |
| 236 | } |
| 237 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 238 | static int vnc_worker_thread_loop(VncJobQueue *queue) |
| 239 | { |
| 240 | VncJob *job; |
| 241 | VncRectEntry *entry, *tmp; |
Gerd Hoffmann | 2e0c90a | 2015-10-30 12:10:10 +0100 | [diff] [blame] | 242 | VncState vs = {}; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 243 | int n_rectangles; |
| 244 | int saved_offset; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 245 | |
| 246 | vnc_lock_queue(queue); |
| 247 | while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) { |
| 248 | qemu_cond_wait(&queue->cond, &queue->mutex); |
| 249 | } |
| 250 | /* Here job can only be NULL if queue->exit is true */ |
| 251 | job = QTAILQ_FIRST(&queue->jobs); |
| 252 | vnc_unlock_queue(queue); |
| 253 | |
| 254 | if (queue->exit) { |
| 255 | return -1; |
| 256 | } |
| 257 | |
Anastasia Belova | bdfca8a | 2023-06-09 12:23:06 +0300 | [diff] [blame] | 258 | assert(job->vs->magic == VNC_MAGIC); |
| 259 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 260 | vnc_lock_output(job->vs); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 261 | if (job->vs->ioc == NULL || job->vs->abort == true) { |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 262 | vnc_unlock_output(job->vs); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 263 | goto disconnected; |
| 264 | } |
Peter Lieven | c3d6899 | 2015-10-30 12:10:11 +0100 | [diff] [blame] | 265 | if (buffer_empty(&job->vs->output)) { |
| 266 | /* |
| 267 | * Looks like a NOP as it obviously moves no data. But it |
| 268 | * moves the empty buffer, so we don't have to malloc a new |
| 269 | * one for vs.output |
| 270 | */ |
| 271 | buffer_move_empty(&vs.output, &job->vs->output); |
| 272 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 273 | vnc_unlock_output(job->vs); |
| 274 | |
| 275 | /* Make a local copy of vs and switch output buffers */ |
| 276 | vnc_async_encoding_start(job->vs, &vs); |
Gerd Hoffmann | f31f9c1 | 2018-05-07 12:22:54 +0200 | [diff] [blame] | 277 | vs.magic = VNC_MAGIC; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 278 | |
| 279 | /* Start sending rectangles */ |
| 280 | n_rectangles = 0; |
| 281 | vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); |
| 282 | vnc_write_u8(&vs, 0); |
| 283 | saved_offset = vs.output.offset; |
| 284 | vnc_write_u16(&vs, 0); |
| 285 | |
| 286 | vnc_lock_display(job->vs->vd); |
| 287 | QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) { |
| 288 | int n; |
| 289 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 290 | if (job->vs->ioc == NULL) { |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 291 | vnc_unlock_display(job->vs->vd); |
Gonglei (Arei) | e3c1adf | 2014-01-23 13:30:57 +0000 | [diff] [blame] | 292 | /* Copy persistent encoding data */ |
| 293 | vnc_async_encoding_end(job->vs, &vs); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 294 | goto disconnected; |
| 295 | } |
| 296 | |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 297 | if (vnc_worker_clamp_rect(&vs, job, &entry->rect)) { |
| 298 | n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y, |
| 299 | entry->rect.w, entry->rect.h); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 300 | |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 301 | if (n >= 0) { |
| 302 | n_rectangles += n; |
| 303 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 304 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 305 | g_free(entry); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 306 | } |
Daniel P. Berrangé | 55b4004 | 2021-03-11 18:29:55 +0000 | [diff] [blame] | 307 | trace_vnc_job_nrects(&vs, job, n_rectangles); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 308 | vnc_unlock_display(job->vs->vd); |
| 309 | |
| 310 | /* Put n_rectangles at the beginning of the message */ |
| 311 | vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF; |
| 312 | vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF; |
| 313 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 314 | vnc_lock_output(job->vs); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 315 | if (job->vs->ioc != NULL) { |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 316 | buffer_move(&job->vs->jobs_buffer, &vs.output); |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 317 | /* Copy persistent encoding data */ |
| 318 | vnc_async_encoding_end(job->vs, &vs); |
| 319 | |
Peter Wu | 0ae0b06 | 2018-08-08 00:18:30 +0200 | [diff] [blame] | 320 | qemu_bh_schedule(job->vs->bh); |
Gonglei (Arei) | e3c1adf | 2014-01-23 13:30:57 +0000 | [diff] [blame] | 321 | } else { |
Gerd Hoffmann | d903401 | 2015-10-30 12:10:04 +0100 | [diff] [blame] | 322 | buffer_reset(&vs.output); |
Gonglei (Arei) | e3c1adf | 2014-01-23 13:30:57 +0000 | [diff] [blame] | 323 | /* Copy persistent encoding data */ |
| 324 | vnc_async_encoding_end(job->vs, &vs); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 325 | } |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 326 | vnc_unlock_output(job->vs); |
| 327 | |
Corentin Chary | 175b2a6 | 2012-03-14 07:58:47 +0100 | [diff] [blame] | 328 | disconnected: |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 329 | vnc_lock_queue(queue); |
| 330 | QTAILQ_REMOVE(&queue->jobs, job, next); |
| 331 | vnc_unlock_queue(queue); |
| 332 | qemu_cond_broadcast(&queue->cond); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 333 | g_free(job); |
Gerd Hoffmann | f31f9c1 | 2018-05-07 12:22:54 +0200 | [diff] [blame] | 334 | vs.magic = 0; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static VncJobQueue *vnc_queue_init(void) |
| 339 | { |
Markus Armbruster | fedf0d3 | 2015-11-03 17:12:03 +0100 | [diff] [blame] | 340 | VncJobQueue *queue = g_new0(VncJobQueue, 1); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 341 | |
| 342 | qemu_cond_init(&queue->cond); |
| 343 | qemu_mutex_init(&queue->mutex); |
| 344 | QTAILQ_INIT(&queue->jobs); |
| 345 | return queue; |
| 346 | } |
| 347 | |
| 348 | static void vnc_queue_clear(VncJobQueue *q) |
| 349 | { |
| 350 | qemu_cond_destroy(&queue->cond); |
| 351 | qemu_mutex_destroy(&queue->mutex); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 352 | g_free(q); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 353 | queue = NULL; /* Unset global queue */ |
| 354 | } |
| 355 | |
| 356 | static void *vnc_worker_thread(void *arg) |
| 357 | { |
| 358 | VncJobQueue *queue = arg; |
| 359 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 360 | qemu_thread_get_self(&queue->thread); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 361 | |
| 362 | while (!vnc_worker_thread_loop(queue)) ; |
| 363 | vnc_queue_clear(queue); |
| 364 | return NULL; |
| 365 | } |
| 366 | |
Blue Swirl | 71a8cde | 2012-10-28 11:04:48 +0000 | [diff] [blame] | 367 | static bool vnc_worker_thread_running(void) |
| 368 | { |
| 369 | return queue; /* Check global queue */ |
| 370 | } |
| 371 | |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 372 | void vnc_start_worker_thread(void) |
| 373 | { |
| 374 | VncJobQueue *q; |
| 375 | |
| 376 | if (vnc_worker_thread_running()) |
Bin Meng | c1dadb8 | 2022-10-24 15:28:02 +0800 | [diff] [blame] | 377 | return; |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 378 | |
| 379 | q = vnc_queue_init(); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 380 | qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q, |
| 381 | QEMU_THREAD_DETACHED); |
Corentin Chary | bd023f9 | 2010-07-07 20:58:02 +0200 | [diff] [blame] | 382 | queue = q; /* Set global queue */ |
| 383 | } |