vnc-jobs-async.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include "vnc.h"
  28. #include "vnc-jobs.h"
  29. /*
  30. * Locking:
  31. *
  32. * There is three levels of locking:
  33. * - jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
  34. * - VncDisplay global lock: mainly used for framebuffer updates to avoid
  35. * screen corruption if the framebuffer is updated
  36. * while the worker is doing something.
  37. * - VncState::output lock: used to make sure the output buffer is not corrupted
  38. * if two threads try to write on it at the same time
  39. *
  40. * While the VNC worker thread is working, the VncDisplay global lock is hold
  41. * to avoid screen corruptions (this does not block vnc_refresh() because it
  42. * uses trylock()) but the output lock is not hold because the thread work on
  43. * its own output buffer.
  44. * When the encoding job is done, the worker thread will hold the output lock
  45. * and copy its output buffer in vs->output.
  46. */
  47. struct VncJobQueue {
  48. QemuCond cond;
  49. QemuMutex mutex;
  50. QemuThread thread;
  51. Buffer buffer;
  52. bool exit;
  53. QTAILQ_HEAD(, VncJob) jobs;
  54. };
  55. typedef struct VncJobQueue VncJobQueue;
  56. /*
  57. * We use a single global queue, but most of the functions are
  58. * already reetrant, so we can easilly add more than one encoding thread
  59. */
  60. static VncJobQueue *queue;
  61. static void vnc_lock_queue(VncJobQueue *queue)
  62. {
  63. qemu_mutex_lock(&queue->mutex);
  64. }
  65. static void vnc_unlock_queue(VncJobQueue *queue)
  66. {
  67. qemu_mutex_unlock(&queue->mutex);
  68. }
  69. VncJob *vnc_job_new(VncState *vs)
  70. {
  71. VncJob *job = g_malloc0(sizeof(VncJob));
  72. job->vs = vs;
  73. vnc_lock_queue(queue);
  74. QLIST_INIT(&job->rectangles);
  75. vnc_unlock_queue(queue);
  76. return job;
  77. }
  78. int vnc_job_add_rect(VncJob *job, int x, int y, int w, int h)
  79. {
  80. VncRectEntry *entry = g_malloc0(sizeof(VncRectEntry));
  81. entry->rect.x = x;
  82. entry->rect.y = y;
  83. entry->rect.w = w;
  84. entry->rect.h = h;
  85. vnc_lock_queue(queue);
  86. QLIST_INSERT_HEAD(&job->rectangles, entry, next);
  87. vnc_unlock_queue(queue);
  88. return 1;
  89. }
  90. void vnc_job_push(VncJob *job)
  91. {
  92. vnc_lock_queue(queue);
  93. if (queue->exit || QLIST_EMPTY(&job->rectangles)) {
  94. g_free(job);
  95. } else {
  96. QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
  97. qemu_cond_broadcast(&queue->cond);
  98. }
  99. vnc_unlock_queue(queue);
  100. }
  101. static bool vnc_has_job_locked(VncState *vs)
  102. {
  103. VncJob *job;
  104. QTAILQ_FOREACH(job, &queue->jobs, next) {
  105. if (job->vs == vs || !vs) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. bool vnc_has_job(VncState *vs)
  112. {
  113. bool ret;
  114. vnc_lock_queue(queue);
  115. ret = vnc_has_job_locked(vs);
  116. vnc_unlock_queue(queue);
  117. return ret;
  118. }
  119. void vnc_jobs_clear(VncState *vs)
  120. {
  121. VncJob *job, *tmp;
  122. vnc_lock_queue(queue);
  123. QTAILQ_FOREACH_SAFE(job, &queue->jobs, next, tmp) {
  124. if (job->vs == vs || !vs) {
  125. QTAILQ_REMOVE(&queue->jobs, job, next);
  126. }
  127. }
  128. vnc_unlock_queue(queue);
  129. }
  130. void vnc_jobs_join(VncState *vs)
  131. {
  132. vnc_lock_queue(queue);
  133. while (vnc_has_job_locked(vs)) {
  134. qemu_cond_wait(&queue->cond, &queue->mutex);
  135. }
  136. vnc_unlock_queue(queue);
  137. }
  138. /*
  139. * Copy data for local use
  140. */
  141. static void vnc_async_encoding_start(VncState *orig, VncState *local)
  142. {
  143. local->vnc_encoding = orig->vnc_encoding;
  144. local->features = orig->features;
  145. local->ds = orig->ds;
  146. local->vd = orig->vd;
  147. local->lossy_rect = orig->lossy_rect;
  148. local->write_pixels = orig->write_pixels;
  149. local->clientds = orig->clientds;
  150. local->tight = orig->tight;
  151. local->zlib = orig->zlib;
  152. local->hextile = orig->hextile;
  153. local->zrle = orig->zrle;
  154. local->output = queue->buffer;
  155. local->csock = -1; /* Don't do any network work on this thread */
  156. buffer_reset(&local->output);
  157. }
  158. static void vnc_async_encoding_end(VncState *orig, VncState *local)
  159. {
  160. orig->tight = local->tight;
  161. orig->zlib = local->zlib;
  162. orig->hextile = local->hextile;
  163. orig->zrle = local->zrle;
  164. orig->lossy_rect = local->lossy_rect;
  165. queue->buffer = local->output;
  166. }
  167. static int vnc_worker_thread_loop(VncJobQueue *queue)
  168. {
  169. VncJob *job;
  170. VncRectEntry *entry, *tmp;
  171. VncState vs;
  172. int n_rectangles;
  173. int saved_offset;
  174. bool flush;
  175. vnc_lock_queue(queue);
  176. while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {
  177. qemu_cond_wait(&queue->cond, &queue->mutex);
  178. }
  179. /* Here job can only be NULL if queue->exit is true */
  180. job = QTAILQ_FIRST(&queue->jobs);
  181. vnc_unlock_queue(queue);
  182. if (queue->exit) {
  183. return -1;
  184. }
  185. vnc_lock_output(job->vs);
  186. if (job->vs->csock == -1 || job->vs->abort == true) {
  187. goto disconnected;
  188. }
  189. vnc_unlock_output(job->vs);
  190. /* Make a local copy of vs and switch output buffers */
  191. vnc_async_encoding_start(job->vs, &vs);
  192. /* Start sending rectangles */
  193. n_rectangles = 0;
  194. vnc_write_u8(&vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
  195. vnc_write_u8(&vs, 0);
  196. saved_offset = vs.output.offset;
  197. vnc_write_u16(&vs, 0);
  198. vnc_lock_display(job->vs->vd);
  199. QLIST_FOREACH_SAFE(entry, &job->rectangles, next, tmp) {
  200. int n;
  201. if (job->vs->csock == -1) {
  202. vnc_unlock_display(job->vs->vd);
  203. /* output mutex must be locked before going to
  204. * disconnected:
  205. */
  206. vnc_lock_output(job->vs);
  207. goto disconnected;
  208. }
  209. n = vnc_send_framebuffer_update(&vs, entry->rect.x, entry->rect.y,
  210. entry->rect.w, entry->rect.h);
  211. if (n >= 0) {
  212. n_rectangles += n;
  213. }
  214. g_free(entry);
  215. }
  216. vnc_unlock_display(job->vs->vd);
  217. /* Put n_rectangles at the beginning of the message */
  218. vs.output.buffer[saved_offset] = (n_rectangles >> 8) & 0xFF;
  219. vs.output.buffer[saved_offset + 1] = n_rectangles & 0xFF;
  220. /* Switch back buffers */
  221. vnc_lock_output(job->vs);
  222. if (job->vs->csock == -1) {
  223. goto disconnected;
  224. }
  225. vnc_write(job->vs, vs.output.buffer, vs.output.offset);
  226. disconnected:
  227. /* Copy persistent encoding data */
  228. vnc_async_encoding_end(job->vs, &vs);
  229. flush = (job->vs->csock != -1 && job->vs->abort != true);
  230. vnc_unlock_output(job->vs);
  231. if (flush) {
  232. vnc_flush(job->vs);
  233. }
  234. vnc_lock_queue(queue);
  235. QTAILQ_REMOVE(&queue->jobs, job, next);
  236. vnc_unlock_queue(queue);
  237. qemu_cond_broadcast(&queue->cond);
  238. g_free(job);
  239. return 0;
  240. }
  241. static VncJobQueue *vnc_queue_init(void)
  242. {
  243. VncJobQueue *queue = g_malloc0(sizeof(VncJobQueue));
  244. qemu_cond_init(&queue->cond);
  245. qemu_mutex_init(&queue->mutex);
  246. QTAILQ_INIT(&queue->jobs);
  247. return queue;
  248. }
  249. static void vnc_queue_clear(VncJobQueue *q)
  250. {
  251. qemu_cond_destroy(&queue->cond);
  252. qemu_mutex_destroy(&queue->mutex);
  253. buffer_free(&queue->buffer);
  254. g_free(q);
  255. queue = NULL; /* Unset global queue */
  256. }
  257. static void *vnc_worker_thread(void *arg)
  258. {
  259. VncJobQueue *queue = arg;
  260. qemu_thread_get_self(&queue->thread);
  261. while (!vnc_worker_thread_loop(queue)) ;
  262. vnc_queue_clear(queue);
  263. return NULL;
  264. }
  265. void vnc_start_worker_thread(void)
  266. {
  267. VncJobQueue *q;
  268. if (vnc_worker_thread_running())
  269. return ;
  270. q = vnc_queue_init();
  271. qemu_thread_create(&q->thread, vnc_worker_thread, q);
  272. queue = q; /* Set global queue */
  273. }
  274. bool vnc_worker_thread_running(void)
  275. {
  276. return queue; /* Check global queue */
  277. }
  278. void vnc_stop_worker_thread(void)
  279. {
  280. if (!vnc_worker_thread_running())
  281. return ;
  282. /* Remove all jobs and wake up the thread */
  283. vnc_lock_queue(queue);
  284. queue->exit = true;
  285. vnc_unlock_queue(queue);
  286. vnc_jobs_clear(NULL);
  287. qemu_cond_broadcast(&queue->cond);
  288. }