vnc: threaded VNC server
A message that is not in the archive (sent off-list or not yet received): 1152486922.2330781275561618453.JavaMail.root@zmail07.collab.prod.int.phx2.redhat.com
Quoted 11 lines
Quoted 7 lines
Review
Quoted 5 lines
(quoted diff) +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.
You're obviously right.
Quoted 11 lines
Quoted 9 lines
Review
Quoted 6 lines
+ qemu_mutex_unlock(&job->vs->output_mutex);
(quoted diff) +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.
They are, however: 1) if you access them outside a mutex you have to think about whether you need memory barriers and whether there are races; 2) since you already own the mutex and you're just keeping it a bit longer, it costs basically nothing. BTW, with the same reasoning you could avoid taking the mutex altogether in vnc_abort_display_jobs (but I think it's better to keep it).
Also, I took a look at the code again and I noticed this:
Quoted 3 lines
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ if (job->vs->csock == -1) { + goto disconnected; + }
The "goto" is jumping over the unlocking of &job->vs->mutex. You only want a "break;" I think.
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->exitat the beginning ofvnc_jobs_join, and refuse new jobs if it is set.
vnc_disconnect_finishcan only be called by the main thread, I don't see how this could be racy, any hint ?
I was thinking of someone queuing jobs between the end of vnc_jobs_join and the time the vnc_lock is taken. But indeed jobs cannot be queued at this time because vnc_refresh can only be called from the same thread. So this is correct.
Quoted 6 lines
Quoted 6 lines
Also, if anything waits on the same vs in
vnc_refreshwhile you own it invnc_disconnect_finish, as soon as you unlock they'll have a dangling pointer. (After you unlock the mutex the OS wakes the thread, but thenpthread_mutex_lockhas to check again that no one got the lock in the meanwhile; soQTAILQ_FOREACH_SAFEis not protecting you). Probably it's better to use a single lock onvd->clientsinstead of one lock per VncState.
Same here. No race because everything happens in the main thread.
Paolo