Skip to contentSkip to conversation

Threaded vnc server

RFC PATCH Superseded Corentin Chary 4 participants 9 messages May 29, 2010 – Jun 3, 2010 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Threaded vnc server

Hi,
This series add a threaded VNC server and should be applied on top on my previous patch set (adding tight encoding). The first two patchs add some functions to qemu-thread. The last is the threaded VNC server and the changelog explains how it works.

refs: http://xf.iksaif.net/blog/index.php?post/2010/05/28/QEMU%3A-Threaded-VNC-Server-results

Thanks

Corentin Chary (3):
qemu-thread: add qemu_mutex/cond_destroy and qemu_mutex_exit
qemu-thread: add cleanup_push() and cleanup_pop()
vnc: threaded VNC server

 Makefile        |    4 +
 Makefile.objs   |    7 +-
 configure       |   13 ++
 qemu-thread.c   |   22 ++++
 qemu-thread.h   |    8 ++
 vnc-jobs-sync.c |   70 ++++++++++++
 vnc-jobs.c      |  328 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 vnc.c           |  161 +++++++++++++++++++++++----
 vnc.h           |   73 ++++++++++++
 9 files changed, 663 insertions(+), 23 deletions(-)
 create mode 100644 vnc-jobs-sync.c
 create mode 100644 vnc-jobs.c
#1 lore raw

PATCH 1/3 qemu-thread: add qemu_mutex/cond_destroy and qemu_mutex_exit

Add some missing functions in qemu-thread. Currently qemu-thread is only used for io-thread but it will used by the vnc server soon and we need those functions instead of calling pthread directly.

---
 qemu-thread.c |   22 ++++++++++++++++++++++
 qemu-thread.h |    4 ++++
 2 files changed, 26 insertions(+), 0 deletions(-)
qemu-thread.c +22 -0
index 3923db7..afc9933 100644
@@ -34,6 +34,15 @@ void qemu_mutex_init(QemuMutex *mutex)
         error_exit(err, __func__);
 }
 
+void qemu_mutex_destroy(QemuMutex *mutex)
+{
+    int err;
+
+    err = pthread_mutex_destroy(&mutex->lock);
+    if (err)
+        error_exit(err, __func__);
+}
+
 void qemu_mutex_lock(QemuMutex *mutex)
 {
     int err;
@@ -90,6 +99,15 @@ void qemu_cond_init(QemuCond *cond)
         error_exit(err, __func__);
 }
 
+void qemu_cond_destroy(QemuCond *cond)
+{
+    int err;
+
+    err = pthread_cond_destroy(&cond->cond);
+    if (err)
+        error_exit(err, __func__);
+}
+
 void qemu_cond_signal(QemuCond *cond)
 {
     int err;
@@ -161,3 +179,7 @@ int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
    return pthread_equal(thread1->thread, thread2->thread);
 }
 
+void qemu_thread_exit(void *retval)
+{
+    pthread_exit(retval);
+}
qemu-thread.h +4 -0
index 5ef4a3a..19bb30c 100644
@@ -20,12 +20,14 @@ typedef struct QemuCond QemuCond;
 typedef struct QemuThread QemuThread;
 
 void qemu_mutex_init(QemuMutex *mutex);
+void qemu_mutex_destroy(QemuMutex *mutex);
 void qemu_mutex_lock(QemuMutex *mutex);
 int qemu_mutex_trylock(QemuMutex *mutex);
 int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs);
 void qemu_mutex_unlock(QemuMutex *mutex);
 
 void qemu_cond_init(QemuCond *cond);
+void qemu_cond_destroy(QemuCond *cond);
 void qemu_cond_signal(QemuCond *cond);
 void qemu_cond_broadcast(QemuCond *cond);
 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
@@ -37,4 +39,6 @@ void qemu_thread_create(QemuThread *thread,
 void qemu_thread_signal(QemuThread *thread, int sig);
 void qemu_thread_self(QemuThread *thread);
 int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
+void qemu_thread_exit(void *retval);
+
 #endif
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/3 qemu-thread: add cleanup_push() and cleanup_pop()

In reply to Corentin Chary
Quoted 1 line

From pthread man:

These functions manipulate the calling thread's stack of thread-cancellation clean-up handlers. A clean-up handler is a function that is automatically executed when a thread is canceled [...] it might, for example, unlock a mutex so that it becomes available to other threads in the process.

These two functions are implemented using macros because there is no other way to do that (pthread man, again):

On Linux, the pthread_cleanup_push() and pthread_cleanup_pop() functions are implemented as macros that expand to text containing '{' and '}', respectively. This means that variables declared within the scope of paired calls to these functions will only be visible within that scope.

---
 qemu-thread.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
qemu-thread.h +4 -0
index 19bb30c..e5006bb 100644
@@ -41,4 +41,8 @@ void qemu_thread_self(QemuThread *thread);
 int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
 void qemu_thread_exit(void *retval);
 
+#define qemu_thread_cleanup_pop(execute) pthread_cleanup_pop(execute)
+#define qemu_thread_cleanup_push(routine, arg)  \
+    pthread_cleanup_push(routine, arg)
+
 #endif
Signature · 1 line
1.7.1
#3 lore raw

Re: [Qemu-devel] [PATCH 2/3] qemu-thread: add cleanup_push() and cleanup_pop()

Quoted 7 lines

From pthread man:

These functions manipulate the calling thread's stack of thread-cancellation clean-up handlers. A clean-up handler is a function that is automatically executed when a thread is canceled [...] it might, for example, unlock a mutex so that it becomes available to other threads in the process.

Do we really need to use thread cancellation?
It's one of those features that makes me extremely nervous. Especially in C code where people generally aren't expecting exceptions to be thrown.

Paul

#4 lore raw

[Qemu-devel] Re: [PATCH 2/3] qemu-thread: add cleanup_push() and cleanup_pop()

In reply to Corentin Chary
Quoted 10 lines · Corentin Chary
---
  qemu-thread.h |    4 ++++
  1 files changed, 4 insertions(+), 0 deletions(-)
qemu-thread.h +0 -0
index 19bb30c..e5006bb 100644

qemu-thread.h void qemu_thread_self(QemuThread *thread); · line 46

Quoted 6 lines
qemu-thread.h +3 -0
@@ -41,4 +41,8 @@ void qemu_thread_self(QemuThread *thread);
  int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
  void qemu_thread_exit(void *retval);
 
+#define qemu_thread_cleanup_pop(execute) pthread_cleanup_pop(execute)
+#define qemu_thread_cleanup_push(routine, arg)  \
+    pthread_cleanup_push(routine, arg)

I agree with Paul that this isn't necessary. Also you're not using pthread_exit. Probably stale from a previous version of the patch?

Paolo

#5 lore raw

Re: [Qemu-devel] Re: [PATCH 2/3] qemu-thread: add cleanup_push() and cleanup_pop()

Quoted 15 lines · Paolo Bonzini
Quoted 14 lines · Corentin Chary

---
 qemu-thread.h |    4 ++++

 1 files changed, 4 insertions(+), 0 deletions(-)
qemu-thread.h +0 -0
index 19bb30c..e5006bb 100644
@@ -41,4 +41,8 @@ void qemu_thread_self(QemuThread *thread);
  int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
  void qemu_thread_exit(void *retval);
 
Quoted 6 lines

qemu-thread.h void qemu_thread_self(QemuThread *thread); · line 46

Quoted 3 lines
qemu-thread.h +3 -0
+#define qemu_thread_cleanup_pop(execute) pthread_cleanup_pop(execute)
+#define qemu_thread_cleanup_push(routine, arg)  \
+    pthread_cleanup_push(routine, arg)

I agree with Paul that this isn't necessary.  Also you're not using pthread_exit.  Probably stale from a previous version of the patch?

Right pthread_exit() is missing.

Anyway, I don't use thread cancellation in the threaded vnc server (I send a signal to the condition with an empty queue to stop the thread), so we can skip this patch.

Signature · 2 lines
Corentin Chary
http://xf.iksaif.net
#6 lore raw

PATCH 3/3 vnc: threaded VNC server

In reply to Corentin Chary

Implement a threaded VNC server using the producer-consumer model. The main thread will push encoding jobs (a list a rectangles to update) in a queue, and the VNC worker thread will consume that queue and send framebuffer updates to the output buffer.

There is three levels of locking:
- jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
- VncState global lock: mainly used for framebuffer updates to avoid screen corruption if the framebuffer is updated while the worker threaded is doing something.
- VncState::output lock: used to make sure the output buffer is not corrupted if two threads try to write on it at the same time

While the VNC worker thread is working, the VncState global lock is hold to avoid screen corruptions (this block vnc_refresh() for a short time) but the output lock is not hold because the thread work on its own output buffer. When the encoding job is done, the worker thread will hold the output lock and copy its output buffer in vs->output.

The threaded VNC server can be enabled with ./configure --enable-vnc-thread.

If you don't want it, just use ./configure --disable-vnc-thread and a syncrhonous queue of job will be used (which as exactly the same behavior as the old queue). If you disable the VNC thread, all thread related code will not be built and there will be no overhead.

---
 Makefile        |    4 +
 Makefile.objs   |    7 +-
 configure       |   13 ++
 vnc-jobs-sync.c |   70 ++++++++++++
 vnc-jobs.c      |  328 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 vnc.c           |  161 +++++++++++++++++++++++----
 vnc.h           |   73 ++++++++++++
 7 files changed, 633 insertions(+), 23 deletions(-)
 create mode 100644 vnc-jobs-sync.c
 create mode 100644 vnc-jobs.c
Makefile +4 -0
index b264e38..d870900 100644
@@ -126,6 +126,10 @@ vnc-encoding-hextile.o: vnc-encoding-hextile.c vnc.h
 
 vnc-encoding-tight.o: vnc-encoding-tight.c vnc.h vnc-encoding-tight.h
 
+vnc-jobs.o: vnc-jobs.c vnc.h
+
+vnc-jobs-sync.o: vnc-jobs-sync.c vnc.h
+
 curses.o: curses.c keymaps.h curses_keys.h
 
 bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
Makefile.objs +6 -1
index 070ee09..6534214 100644
@@ -108,8 +108,13 @@ common-obj-y += vnc-encoding-tight.o
 common-obj-y += iov.o
 common-obj-$(CONFIG_VNC_TLS) += vnc-tls.o vnc-auth-vencrypt.o
 common-obj-$(CONFIG_VNC_SASL) += vnc-auth-sasl.o
+ifdef CONFIG_VNC_THREAD
+common-obj-y += vnc-jobs.o
+else
+common-obj-y += vnc-jobs-sync.o
+endif
 common-obj-$(CONFIG_COCOA) += cocoa.o
-common-obj-$(CONFIG_IOTHREAD) += qemu-thread.o
+common-obj-$(CONFIG_THREAD) += qemu-thread.o
 common-obj-y += notify.o event_notifier.o
 common-obj-y += qemu-timer.o
 
configure +13 -0
index 3cd2c5f..e886b67 100755
@@ -263,6 +263,7 @@ uuid=""
 vde=""
 vnc_tls=""
 vnc_sasl=""
+vnc_thread=""
 xen=""
 linux_aio=""
 vhost_net=""
@@ -547,6 +548,10 @@ for opt do
   ;;
   --enable-vnc-sasl) vnc_sasl="yes"
   ;;
+  --disable-vnc-thread) vnc_thread="no"
+  ;;
+  --enable-vnc-thread) vnc_thread="yes"
+  ;;
   --disable-slirp) slirp="no"
   ;;
   --disable-uuid) uuid="no"
@@ -779,6 +784,8 @@ echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
 echo "  --enable-vnc-tls         enable TLS encryption for VNC server"
 echo "  --disable-vnc-sasl       disable SASL encryption for VNC server"
 echo "  --enable-vnc-sasl        enable SASL encryption for VNC server"
+echo "  --disable-vnc-thread     disable threaded VNC server"
+echo "  --enable-vnc-thread      enable threaded VNC server"
 echo "  --disable-curses         disable curses output"
 echo "  --enable-curses          enable curses output"
 echo "  --disable-curl           disable curl connectivity"
@@ -2019,6 +2026,7 @@ echo "Block whitelist   $block_drv_whitelist"
 echo "Mixer emulation   $mixemu"
 echo "VNC TLS support   $vnc_tls"
 echo "VNC SASL support  $vnc_sasl"
+echo "VNC thread        $vnc_thread"
 if test -n "$sparc_cpu"; then
     echo "Target Sparc Arch $sparc_cpu"
 fi
@@ -2158,6 +2166,10 @@ if test "$vnc_sasl" = "yes" ; then
   echo "CONFIG_VNC_SASL=y" >> $config_host_mak
   echo "VNC_SASL_CFLAGS=$vnc_sasl_cflags" >> $config_host_mak
 fi
+if test "$vnc_thread" = "yes" ; then
+  echo "CONFIG_VNC_THREAD=y" >> $config_host_mak
+  echo "CONFIG_THREAD=y" >> $config_host_mak
+fi
 if test "$fnmatch" = "yes" ; then
   echo "CONFIG_FNMATCH=y" >> $config_host_mak
 fi
@@ -2234,6 +2246,7 @@ if test "$xen" = "yes" ; then
 fi
 if test "$io_thread" = "yes" ; then
   echo "CONFIG_IOTHREAD=y" >> $config_host_mak
+  echo "CONFIG_THREAD=y" >> $config_host_mak
 fi
 if test "$linux_aio" = "yes" ; then
   echo "CONFIG_LINUX_AIO=y" >> $config_host_mak
vnc-jobs-sync.c new file +70 -0

70 lines not on this page · raw message

vnc-jobs.c new file +328 -0

328 lines not on this page · raw message

vnc.c +139 -22

385 lines not on this page · raw message

vnc.h +73 -0

111 lines not on this page · raw message

Signature · 1 line
1.7.1
#7 lore raw

[Qemu-devel] Re: [PATCH 3/3] vnc: threaded VNC server

Quoted 13 lines · Corentin Chary

Implement a threaded VNC server using the producer-consumer model. The main thread will push encoding jobs (a list a rectangles to update) in a queue, and the VNC worker thread will consume that queue and send framebuffer updates to the output buffer.

There is three levels of locking:
- jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
- VncState global lock: mainly used for framebuffer updates to avoid screen corruption if the framebuffer is updated while the worker threaded is doing something.
- VncState::output lock: used to make sure the output buffer is not corrupted if two threads try to write on it at the same time

Quoted 5 lines

While the VNC worker thread is working, the VncState global lock is hold to avoid screen corruptions (this block vnc_refresh() for a short time) but the output lock is not hold because the thread work on its own output buffer. When the encoding job is done, the worker thread will hold the output lock and copy its output buffer in vs->output.

This belong in a comment in the code, not in the commit message (or in both).

vnc-jobs.c line 88

Quoted 5 lines
vnc-jobs.c new file +5 -0
+void vnc_job_push(VncJob *job)
+{
+    vnc_lock_queue(queue);
+    if (QLIST_EMPTY(&job->rectangles)) {
+        qemu_free(job);

No need to lock if you get into the "then" block.

Quoted 6 lines
vnc-jobs.c new file +6 -0
+    } else {
+        QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
+        qemu_cond_broadcast(&queue->cond);
+    }
+    vnc_unlock_queue(queue);
+}

...

Quoted 8 lines
vnc-jobs.c new file +8 -0
+static int vnc_worker_thread_loop(VncJobQueue *queue)
+{
+    VncJob *job;
+    VncRectEntry *entry, *tmp;
+    VncState vs;
+    int n_rectangles;
+    int saved_offset;
+

vnc-jobs.c line 205

Quoted 10 lines
vnc-jobs.c new file +10 -0
+    vnc_lock_queue(queue);
+    if (QTAILQ_EMPTY(&queue->jobs)) {
+        qemu_cond_wait(&queue->cond,&queue->mutex);
+    }
+
+    /* If the queue is empty, it's an exit order */
+    if (QTAILQ_EMPTY(&queue->jobs)) {
+        vnc_unlock_queue(queue);
+        return -1;
+    }

This is not safe. It might work with a single consumer, but something like this is better:

   vnc_lock_queue(queue);
   while (!queue->exit && QTAILQ_EMPTY(&queue->jobs)) {
        qemu_cond_wait(&queue->cond,&queue->mutex);
   }
   if (queue->exit) {
       vnc_unlock_queue(queue);
       return -1;
   }

(It occurred to me now that maybe you can reuse ->aborting. Not sure though).

vnc-jobs.c line 263

Quoted 6 lines
vnc-jobs.c new file +6 -0
+    qemu_mutex_unlock(&job->vs->output_mutex);
+
+    if (job->vs->csock != -1 && job->vs->abording != true) {
+        vnc_flush(job->vs);
+    }
+

You're accessing the abort flag outside the mutex here. Also, you are not using vnc_{,un}lock_output.

Quoted 2 lines
vnc-jobs.c new file +2 -0
+    job = QTAILQ_FIRST(&queue->jobs);
+    vnc_unlock_queue(queue);

...

Quoted 8 lines
vnc.c +8 -0
+static void vnc_abord_display_jobs(VncDisplay *vd)
+{
+    VncState *vs;
+
+    QTAILQ_FOREACH(vs, &vd->clients, next) {
+        vnc_lock_output(vs);
+        vs->abording = true;
+        vnc_unlock_output(vs);

vnc.c void buffer_append(Buffer *buffer, const void *data, size_t len) · line 568

Quoted 10 lines
vnc.c +10 -0
+    }
+    QTAILQ_FOREACH(vs, &vd->clients, next) {
+        vnc_jobs_join(vs);
+    }
+    QTAILQ_FOREACH(vs, &vd->clients, next) {
+        vnc_lock_output(vs);
+        vs->abording = false;
+        vnc_unlock_output(vs);
+    }
+}

It's "abort" not "abord". :-)

...

vnc.c static void vnc_disconnect_start(VncState *vs) · line 1000

Quoted 4 lines

static void vnc_disconnect_finish(VncState *vs)
{

vnc.c +2 -0
+    vnc_jobs_join(vs); /* Wait encoding jobs */
+    vnc_lock(vs);

Possibly racy? Maybe you have to set the aforementioned new flag queue->exit at the beginning of vnc_jobs_join, and refuse new jobs if it is set.

Also, if anything waits on the same vs in vnc_refresh while you own it in vnc_disconnect_finish, as soon as you unlock they'll have a dangling pointer. (After you unlock the mutex the OS wakes the thread, but then pthread_mutex_lock has to check again that no one got the lock in the meanwhile; so QTAILQ_FOREACH_SAFE is not protecting you). Probably it's better to use a single lock on vd->clients instead of one lock per VncState.

vnc.c void vnc_client_write(void *opaque) · line 1179

Quoted 10 lines
vnc.c +10 -0
+void vnc_client_write(void *opaque)
+{
+    VncState *vs = opaque;
+
+    vnc_lock_output(vs);
+    if (vs->output.offset) {
+        vnc_client_write_locked(opaque);
+    } else {
+        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+    }

Why the if? The "else" branch is already done by vnc_client_write_plain.

This may be a good time to port qemu-threads to Windows too. IO thread has no hope to work under Windows at least without major hacks (because Windows has no asynchronous interrupts; the only way I can imagine to emulate them is a breakpoint) but threaded VNC should work.

Paolo

#8 lore raw

Re: [Qemu-devel] Re: [PATCH 3/3] vnc: threaded VNC server

Quoted 25 lines · Paolo Bonzini
Quoted 15 lines · Corentin Chary

Implement a threaded VNC server using the producer-consumer model. The main thread will push encoding jobs (a list a rectangles to update) in a queue, and the VNC worker thread will consume that queue and send framebuffer updates to the output buffer.

There is three levels of locking:
- jobs queue lock: for each operation on the queue (push, pop, isEmpty?)
- VncState global lock: mainly used for framebuffer updates to avoid screen corruption if the framebuffer is updated while the worker threaded is doing something.
- VncState::output lock: used to make sure the output buffer is not corrupted
                        if two threads try to write on it at the same time

Quoted 8 lines

While the VNC worker thread is working, the VncState global lock is hold to avoid screen corruptions (this block vnc_refresh() for a short time) but the
output lock is not hold because the thread work on its own output buffer. When
the encoding job is done, the worker thread will hold the output lock and copy
its output buffer in vs->output.

Quoted 2 lines

This belong in a comment in the code, not in the commit message (or in both).

Right

Quoted 7 lines

vnc-jobs.c line 88

Quoted 5 lines
vnc-jobs.c new file +5 -0
+void vnc_job_push(VncJob *job)
+{
+    vnc_lock_queue(queue);
+    if (QLIST_EMPTY(&job->rectangles)) {
+        qemu_free(job);

No need to lock if you get into the "then" block.

I locked it because the main thread can try to push a job while a consumer is removing one, so I can't call QLIST_EMPTY() without locking the queue.

Quoted 31 lines
Quoted 6 lines

+    } else {
+        QTAILQ_INSERT_TAIL(&queue->jobs, job, next);
+        qemu_cond_broadcast(&queue->cond);
+    }
+    vnc_unlock_queue(queue);
+}

...

Quoted 8 lines
vnc-jobs.c new file +8 -0
+static int vnc_worker_thread_loop(VncJobQueue *queue)
+{
+    VncJob *job;
+    VncRectEntry *entry, *tmp;
+    VncState vs;
+    int n_rectangles;
+    int saved_offset;
+

vnc-jobs.c line 205

Quoted 10 lines
vnc-jobs.c new file +10 -0
+    vnc_lock_queue(queue);
+    if (QTAILQ_EMPTY(&queue->jobs)) {
+        qemu_cond_wait(&queue->cond,&queue->mutex);
+    }
+
+    /* If the queue is empty, it's an exit order */
+    if (QTAILQ_EMPTY(&queue->jobs)) {
+        vnc_unlock_queue(queue);
+        return -1;
+    }

This is not safe.  It might work with a single consumer, but something like this is better:

Quoted 8 lines

  vnc_lock_queue(queue);
  while (!queue->exit && QTAILQ_EMPTY(&queue->jobs)) { qemu_cond_wait(&queue->cond,&queue->mutex); }
  if (queue->exit) {
      vnc_unlock_queue(queue);
      return -1;
  }

Right,

Quoted 12 lines

(It occurred to me now that maybe you can reuse ->aborting.  Not sure though).

vnc-jobs.c line 263

Quoted 6 lines

+    qemu_mutex_unlock(&job->vs->output_mutex);

vnc-jobs.c new file +5 -0
+
+    if (job->vs->csock != -1 && job->vs->abording != true) {
+        vnc_flush(job->vs);
+    }
+

You're accessing the abort flag outside the mutex here.  Also, you are not using vnc_{,un}lock_output.

I assumed that bool (int) where atomic .. but you're right I should lock that.

Quoted 17 lines
Quoted 2 lines

+    job = QTAILQ_FIRST(&queue->jobs);
+    vnc_unlock_queue(queue);

...

Quoted 12 lines
vnc.c +12 -0
+static void vnc_abord_display_jobs(VncDisplay *vd)
+{
+    VncState *vs;
+
+    QTAILQ_FOREACH(vs, &vd->clients, next) {
+        vnc_lock_output(vs);
+        vs->abording = true;
+        vnc_unlock_output(vs);
+    }
+    QTAILQ_FOREACH(vs, &vd->clients, next) {
+        vnc_jobs_join(vs);
+    }
Quoted 8 lines
Quoted 6 lines

+    QTAILQ_FOREACH(vs, &vd->clients, next) {
+        vnc_lock_output(vs);
+        vs->abording = false;
+        vnc_unlock_output(vs);
+    }
+}

It's "abort" not "abord". :-)

Ooops ...

Quoted 11 lines

...

Quoted 4 lines

 static void vnc_disconnect_finish(VncState *vs)
 {
+    vnc_jobs_join(vs); /* Wait encoding jobs */
+    vnc_lock(vs);

Possibly racy?  Maybe you have to set the aforementioned new flag queue->exit at the beginning of vnc_jobs_join, and refuse new jobs if it is set.

Quoted 6 lines

Also, if anything waits on the same vs in vnc_refresh while you own it in vnc_disconnect_finish, as soon as you unlock they'll have a dangling pointer.  (After you unlock the mutex the OS wakes the thread, but then pthread_mutex_lock has to check again that no one got the lock in the meanwhile; so QTAILQ_FOREACH_SAFE is not protecting you).  Probably it's better to use a single lock on vd->clients instead of one lock per VncState.

vnc_disconnect_finish can only be called by the main thread, I don't see how this could be racy, any hint ?
I am missing something ?

Quoted 4 lines
Quoted 4 lines
vnc.c +4 -0
+void vnc_client_write(void *opaque)
+{
+    VncState *vs = opaque;
+
Quoted 8 lines
Quoted 6 lines

+    vnc_lock_output(vs);
+    if (vs->output.offset) {
+        vnc_client_write_locked(opaque);
+    } else {
+        qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+    }

Why the if?  The "else" branch is already done by vnc_client_write_plain.

This is because the vnc_write fd handler can be set by the thread, and this can end up calling vnc_client_write_plain
with vs->output.offset == 0 and disconnecting.

Quoted 4 lines

This may be a good time to port qemu-threads to Windows too.  IO thread has no hope to work under Windows at least without major hacks (because Windows has no asynchronous interrupts; the only way I can imagine to emulate them is a breakpoint) but threaded VNC should work.

Right, but I won't do that since I don't have Windows :).

Signature · 2 lines
Corentin Chary
http://xf.iksaif.net