monitor: Fix tracepoint crash on JSON syntax error
[qemu/armbru.git] / ui / vnc-jobs.c
blobb0b15d42a8760f92880d2c5be08b2405422e8a1a
1 /*
2 * QEMU VNC display driver
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>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
29 #include "qemu/osdep.h"
30 #include "vnc.h"
31 #include "vnc-jobs.h"
32 #include "qemu/sockets.h"
33 #include "qemu/main-loop.h"
34 #include "block/aio.h"
37 * Locking:
39 * There are three levels of locking:
40 * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
41 * - VncDisplay global lock: mainly used for framebuffer updates to avoid
42 * screen corruption if the framebuffer is updated
43 * while the worker is doing something.
44 * - VncState::output lock: used to make sure the output buffer is not corrupted
45 * if two threads try to write on it at the same time
47 * While the VNC worker thread is working, the VncDisplay global lock is held
48 * to avoid screen corruption (this does not block vnc_refresh() because it
49 * uses trylock()) but the output lock is not held because the thread works on
50 * its own output buffer.
51 * When the encoding job is done, the worker thread will hold the output lock
52 * and copy its output buffer in vs->output.
55 struct VncJobQueue {
56 QemuCond cond;
57 QemuMutex mutex;
58 QemuThread thread;
59 bool exit;
60 QTAILQ_HEAD(, VncJob) jobs;
63 typedef struct VncJobQueue VncJobQueue;
66 * We use a single global queue, but most of the functions are
67 * already reentrant, so we can easily add more than one encoding thread
69 static VncJobQueue *queue;
71 static void vnc_lock_queue(VncJobQueue *queue)
73 qemu_mutex_lock(&queue->mutex);
76 static void vnc_unlock_queue(VncJobQueue *queue)
78 qemu_mutex_unlock(&queue->mutex);
81 VncJob *vnc_job_new(VncState *vs)
83 VncJob *job = g_new0(VncJob, 1);
85 assert(vs->magic == VNC_MAGIC);
86 job->vs = vs;
87 vnc_lock_queue(queue);
88 QLIST_INIT(&job->rectangles);
89 vnc_unlock_queue(queue);
90 return job;
93 int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
95 VncRectEntry *entry = g_new0(VncRectEntry, 1);
97 entry->rect.x = x;
98 entry->rect.y = y;
99 entry->rect.w = w;
100 entry->rect.h = h;
102 vnc_lock_queue(queue);
103 QLIST_INSERT_HEAD(&job->rectangles, entry, next);
104 vnc_unlock_queue(queue);
105 return 1;
108 void vnc_job_push(VncJob *job)
110 vnc_lock_queue(queue);
111 if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
112 g_free(job);
113 } else {
114 QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
115 qemu_cond_broadcast(&queue->cond);
117 vnc_unlock_queue(queue);
120 static bool vnc_has_job_locked(VncState *vs)
122 VncJob *job;
124 QTAILQ_FOREACH(job, &queue->jobs, next) {
125 if (job->vs == vs || !vs) {
126 return true;
129 return false;
132 void vnc_jobs_join(VncState *vs)
134 vnc_lock_queue(queue);
135 while (vnc_has_job_locked(vs)) {
136 qemu_cond_wait(&queue->cond, &queue->mutex);
138 vnc_unlock_queue(queue);
139 vnc_jobs_consume_buffer(vs);
142 void vnc_jobs_consume_buffer(VncState *vs)
144 bool flush;
146 vnc_lock_output(vs);
147 if (vs->jobs_buffer.offset) {
148 if (vs->ioc != NULL && buffer_empty(&vs->output)) {
149 if (vs->ioc_tag) {
150 g_source_remove(vs->ioc_tag);
152 if (vs->disconnecting == FALSE) {
153 vs->ioc_tag = qio_channel_add_watch(
154 vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
157 buffer_move(&vs->output, &vs->jobs_buffer);
159 if (vs->job_update == VNC_STATE_UPDATE_FORCE) {
160 vs->force_update_offset = vs->output.offset;
162 vs->job_update = VNC_STATE_UPDATE_NONE;
164 flush = vs->ioc != NULL && vs->abort != true;
165 vnc_unlock_output(vs);
167 if (flush) {
168 vnc_flush(vs);
173 * Copy data for local use
175 static void vnc_async_encoding_start(VncState *orig, VncState *local)
177 buffer_init(&local->output, "vnc-worker-output");
178 local->sioc = NULL; /* Don't do any network work on this thread */
179 local->ioc = NULL; /* Don't do any network work on this thread */
181 local->vnc_encoding = orig->vnc_encoding;
182 local->features = orig->features;
183 local->vd = orig->vd;
184 local->lossy_rect = orig->lossy_rect;
185 local->write_pixels = orig->write_pixels;
186 local->client_pf = orig->client_pf;
187 local->client_be = orig->client_be;
188 local->tight = orig->tight;
189 local->zlib = orig->zlib;
190 local->hextile = orig->hextile;
191 local->zrle = orig->zrle;
194 static void vnc_async_encoding_end(VncState *orig, VncState *local)
196 orig->tight = local->tight;
197 orig->zlib = local->zlib;
198 orig->hextile = local->hextile;
199 orig->zrle = local->zrle;
200 orig->lossy_rect = local->lossy_rect;
203 static int vnc_worker_thread_loop(VncJobQueue *queue)
205 VncJob *job;
206 VncRectEntry *entry, *tmp;
207 VncState vs = {};
208 int n_rectangles;
209 int saved_offset;
211 vnc_lock_queue(queue);
212 while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
213 qemu_cond_wait(&queue->cond, &queue->mutex);
215 /* Here job can only be NULL if queue->exit is true */
216 job = QTAILQ_FIRST(&queue->jobs);
217 vnc_unlock_queue(queue);
218 assert(job->vs->magic == VNC_MAGIC);
220 if (queue->exit) {
221 return -1;
224 vnc_lock_output(job->vs);
225 if (job->vs->ioc == NULL || job->vs->abort == true) {
226 vnc_unlock_output(job->vs);
227 goto disconnected;
229 if (buffer_empty(&job->vs->output)) {
231 * Looks like a NOP as it obviously moves no data. But it
232 * moves the empty buffer, so we don't have to malloc a new
233 * one for vs.output
235 buffer_move_empty(&vs.output, &job->vs->output);
237 vnc_unlock_output(job->vs);
239 /* Make a local copy of vs and switch output buffers */
240 vnc_async_encoding_start(job->vs, &vs);
241 vs.magic = VNC_MAGIC;
243 /* Start sending rectangles */
244 n_rectangles = 0;
245 vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
246 vnc_write_u8(&vs, 0);
247 saved_offset = vs.output.offset;
248 vnc_write_u16(&vs, 0);
250 vnc_lock_display(job->vs->vd);
251 QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
252 int n;
254 if (job->vs->ioc == NULL) {
255 vnc_unlock_display(job->vs->vd);
256 /* Copy persistent encoding data */
257 vnc_async_encoding_end(job->vs, &vs);
258 goto disconnected;
261 n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
262 entry->rect.w, entry->rect.h);
264 if (n >= 0) {
265 n_rectangles += n;
267 g_free(entry);
269 vnc_unlock_display(job->vs->vd);
271 /* Put n_rectangles at the beginning of the message */
272 vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
273 vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
275 vnc_lock_output(job->vs);
276 if (job->vs->ioc != NULL) {
277 buffer_move(&job->vs->jobs_buffer, &vs.output);
278 /* Copy persistent encoding data */
279 vnc_async_encoding_end(job->vs, &vs);
281 qemu_bh_schedule(job->vs->bh);
282 } else {
283 buffer_reset(&vs.output);
284 /* Copy persistent encoding data */
285 vnc_async_encoding_end(job->vs, &vs);
287 vnc_unlock_output(job->vs);
289 disconnected:
290 vnc_lock_queue(queue);
291 QTAILQ_REMOVE(&queue->jobs, job, next);
292 vnc_unlock_queue(queue);
293 qemu_cond_broadcast(&queue->cond);
294 g_free(job);
295 vs.magic = 0;
296 return 0;
299 static VncJobQueue *vnc_queue_init(void)
301 VncJobQueue *queue = g_new0(VncJobQueue, 1);
303 qemu_cond_init(&queue->cond);
304 qemu_mutex_init(&queue->mutex);
305 QTAILQ_INIT(&queue->jobs);
306 return queue;
309 static void vnc_queue_clear(VncJobQueue *q)
311 qemu_cond_destroy(&queue->cond);
312 qemu_mutex_destroy(&queue->mutex);
313 g_free(q);
314 queue = NULL; /* Unset global queue */
317 static void *vnc_worker_thread(void *arg)
319 VncJobQueue *queue = arg;
321 qemu_thread_get_self(&queue->thread);
323 while (!vnc_worker_thread_loop(queue)) ;
324 vnc_queue_clear(queue);
325 return NULL;
328 static bool vnc_worker_thread_running(void)
330 return queue; /* Check global queue */
333 void vnc_start_worker_thread(void)
335 VncJobQueue *q;
337 if (vnc_worker_thread_running())
338 return ;
340 q = vnc_queue_init();
341 qemu_thread_create(&q->thread, "vnc_worker", vnc_worker_thread, q,
342 QEMU_THREAD_DETACHED);
343 queue = q; /* Set global queue */