Blame 0116-vscclient-use-glib-thread-primitives-not-qemu.patch

0410ae
From 4a609afa4206d7af9fe2c8dcfbe7850509701aff Mon Sep 17 00:00:00 2001
0410ae
From: Michael Tokarev <mjt@tls.msk.ru>
0410ae
Date: Thu, 8 May 2014 12:30:47 +0400
0410ae
Subject: [PATCH] vscclient: use glib thread primitives not qemu
0410ae
0410ae
Use glib-provided thread primitives in vscclient instead of
0410ae
qemu ones, and do not use qemu sockets in there (open-code
0410ae
call to WSAStartup() for windows to initialize things).
0410ae
0410ae
This way, vscclient becomes more stand-alone, independent on
0410ae
qemu internals.
0410ae
0410ae
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
0410ae
Reviewed-by: Alon Levy <alevy@redhat.com>
0410ae
Tested-by: Alon Levy <alevy@redhat.com>
0410ae
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
0410ae
(cherry picked from commit 2a0c46da967e5dc8cfe73b1b6fe7a1600c04f461)
0410ae
---
0410ae
 libcacard/vscclient.c | 70 +++++++++++++++++++++++++++------------------------
0410ae
 1 file changed, 37 insertions(+), 33 deletions(-)
0410ae
0410ae
diff --git a/libcacard/vscclient.c b/libcacard/vscclient.c
0410ae
index 3477ab3..598206b 100644
0410ae
--- a/libcacard/vscclient.c
0410ae
+++ b/libcacard/vscclient.c
0410ae
@@ -12,12 +12,10 @@
0410ae
 
0410ae
 #ifndef _WIN32
0410ae
 #include <netdb.h>
0410ae
+#define closesocket(x) close(x)
0410ae
 #endif
0410ae
-#include <glib.h>
0410ae
 
0410ae
 #include "qemu-common.h"
0410ae
-#include "qemu/thread.h"
0410ae
-#include "qemu/sockets.h"
0410ae
 
0410ae
 #include "vscard_common.h"
0410ae
 
0410ae
@@ -54,7 +52,7 @@ print_usage(void) {
0410ae
 
0410ae
 static GIOChannel *channel_socket;
0410ae
 static GByteArray *socket_to_send;
0410ae
-static QemuMutex socket_to_send_lock;
0410ae
+static CompatGMutex socket_to_send_lock;
0410ae
 static guint socket_tag;
0410ae
 
0410ae
 static void
0410ae
@@ -103,7 +101,7 @@ send_msg(
0410ae
 ) {
0410ae
     VSCMsgHeader mhHeader;
0410ae
 
0410ae
-    qemu_mutex_lock(&socket_to_send_lock);
0410ae
+    g_mutex_lock(&socket_to_send_lock);
0410ae
 
0410ae
     if (verbose > 10) {
0410ae
         printf("sending type=%d id=%u, len =%u (0x%x)\n",
0410ae
@@ -117,18 +115,18 @@ send_msg(
0410ae
     g_byte_array_append(socket_to_send, (guint8 *)msg, length);
0410ae
     g_idle_add(socket_prepare_sending, NULL);
0410ae
 
0410ae
-    qemu_mutex_unlock(&socket_to_send_lock);
0410ae
+    g_mutex_unlock(&socket_to_send_lock);
0410ae
 
0410ae
     return 0;
0410ae
 }
0410ae
 
0410ae
 static VReader *pending_reader;
0410ae
-static QemuMutex pending_reader_lock;
0410ae
-static QemuCond pending_reader_condition;
0410ae
+static CompatGMutex pending_reader_lock;
0410ae
+static CompatGCond pending_reader_condition;
0410ae
 
0410ae
 #define MAX_ATR_LEN 40
0410ae
-static void *
0410ae
-event_thread(void *arg)
0410ae
+static gpointer
0410ae
+event_thread(gpointer arg)
0410ae
 {
0410ae
     unsigned char atr[MAX_ATR_LEN];
0410ae
     int atr_len = MAX_ATR_LEN;
0410ae
@@ -149,20 +147,20 @@ event_thread(void *arg)
0410ae
             /* ignore events from readers qemu has rejected */
0410ae
             /* if qemu is still deciding on this reader, wait to see if need to
0410ae
              * forward this event */
0410ae
-            qemu_mutex_lock(&pending_reader_lock);
0410ae
+            g_mutex_lock(&pending_reader_lock);
0410ae
             if (!pending_reader || (pending_reader != event->reader)) {
0410ae
                 /* wasn't for a pending reader, this reader has already been
0410ae
                  * rejected by qemu */
0410ae
-                qemu_mutex_unlock(&pending_reader_lock);
0410ae
+                g_mutex_unlock(&pending_reader_lock);
0410ae
                 vevent_delete(event);
0410ae
                 continue;
0410ae
             }
0410ae
             /* this reader hasn't been told its status from qemu yet, wait for
0410ae
              * that status */
0410ae
             while (pending_reader != NULL) {
0410ae
-                qemu_cond_wait(&pending_reader_condition, &pending_reader_lock);
0410ae
+                g_cond_wait(&pending_reader_condition, &pending_reader_lock);
0410ae
             }
0410ae
-            qemu_mutex_unlock(&pending_reader_lock);
0410ae
+            g_mutex_unlock(&pending_reader_lock);
0410ae
             /* now recheck the id */
0410ae
             reader_id = vreader_get_id(event->reader);
0410ae
             if (reader_id == VSCARD_UNDEFINED_READER_ID) {
0410ae
@@ -178,12 +176,12 @@ event_thread(void *arg)
0410ae
             /* wait until qemu has responded to our first reader insert
0410ae
              * before we send a second. That way we won't confuse the responses
0410ae
              * */
0410ae
-            qemu_mutex_lock(&pending_reader_lock);
0410ae
+            g_mutex_lock(&pending_reader_lock);
0410ae
             while (pending_reader != NULL) {
0410ae
-                qemu_cond_wait(&pending_reader_condition, &pending_reader_lock);
0410ae
+                g_cond_wait(&pending_reader_condition, &pending_reader_lock);
0410ae
             }
0410ae
             pending_reader = vreader_reference(event->reader);
0410ae
-            qemu_mutex_unlock(&pending_reader_lock);
0410ae
+            g_mutex_unlock(&pending_reader_lock);
0410ae
             reader_name = vreader_get_name(event->reader);
0410ae
             if (verbose > 10) {
0410ae
                 printf(" READER INSERT: %s\n", reader_name);
0410ae
@@ -246,7 +244,6 @@ on_host_init(VSCMsgHeader *mhHeader, VSCMsgInit *incoming)
0410ae
     int num_capabilities =
0410ae
         1 + ((mhHeader->length - sizeof(VSCMsgInit)) / sizeof(uint32_t));
0410ae
     int i;
0410ae
-    QemuThread thread_id;
0410ae
 
0410ae
     incoming->version = ntohl(incoming->version);
0410ae
     if (incoming->version != VSCARD_VERSION) {
0410ae
@@ -269,7 +266,7 @@ on_host_init(VSCMsgHeader *mhHeader, VSCMsgInit *incoming)
0410ae
     send_msg(VSC_ReaderRemove, VSCARD_MINIMAL_READER_ID, NULL, 0);
0410ae
     /* launch the event_thread. This will trigger reader adds for all the
0410ae
      * existing readers */
0410ae
-    qemu_thread_create(&thread_id, "vsc/event", event_thread, NULL, 0);
0410ae
+    g_thread_new("vsc/event", event_thread, NULL);
0410ae
     return 0;
0410ae
 }
0410ae
 
0410ae
@@ -379,26 +376,26 @@ do_socket_read(GIOChannel *source,
0410ae
         case VSC_Error:
0410ae
             error_msg = (VSCMsgError *) pbSendBuffer;
0410ae
             if (error_msg->code == VSC_SUCCESS) {
0410ae
-                qemu_mutex_lock(&pending_reader_lock);
0410ae
+                g_mutex_lock(&pending_reader_lock);
0410ae
                 if (pending_reader) {
0410ae
                     vreader_set_id(pending_reader, mhHeader.reader_id);
0410ae
                     vreader_free(pending_reader);
0410ae
                     pending_reader = NULL;
0410ae
-                    qemu_cond_signal(&pending_reader_condition);
0410ae
+                    g_cond_signal(&pending_reader_condition);
0410ae
                 }
0410ae
-                qemu_mutex_unlock(&pending_reader_lock);
0410ae
+                g_mutex_unlock(&pending_reader_lock);
0410ae
                 break;
0410ae
             }
0410ae
             printf("warning: qemu refused to add reader\n");
0410ae
             if (error_msg->code == VSC_CANNOT_ADD_MORE_READERS) {
0410ae
                 /* clear pending reader, qemu can't handle any more */
0410ae
-                qemu_mutex_lock(&pending_reader_lock);
0410ae
+                g_mutex_lock(&pending_reader_lock);
0410ae
                 if (pending_reader) {
0410ae
                     pending_reader = NULL;
0410ae
                     /* make sure the event loop doesn't hang */
0410ae
-                    qemu_cond_signal(&pending_reader_condition);
0410ae
+                    g_cond_signal(&pending_reader_condition);
0410ae
                 }
0410ae
-                qemu_mutex_unlock(&pending_reader_lock);
0410ae
+                g_mutex_unlock(&pending_reader_lock);
0410ae
             }
0410ae
             break;
0410ae
         case VSC_Init:
0410ae
@@ -602,7 +599,7 @@ connect_to_qemu(
0410ae
     struct addrinfo *server;
0410ae
     int ret, sock;
0410ae
 
0410ae
-    sock = qemu_socket(AF_INET, SOCK_STREAM, 0);
0410ae
+    sock = socket(AF_INET, SOCK_STREAM, 0);
0410ae
     if (sock < 0) {
0410ae
         /* Error */
0410ae
         fprintf(stderr, "Error opening socket!\n");
0410ae
@@ -655,8 +652,20 @@ main(
0410ae
     int cert_count = 0;
0410ae
     int c, sock;
0410ae
 
0410ae
-    if (socket_init() != 0)
0410ae
+#ifdef _WIN32
0410ae
+    WSADATA Data;
0410ae
+
0410ae
+    if (WSAStartup(MAKEWORD(2, 2), &Data) != 0) {
0410ae
+        c = WSAGetLastError();
0410ae
+        fprintf(stderr, "WSAStartup: %d\n", c);
0410ae
         return 1;
0410ae
+    }
0410ae
+#endif
0410ae
+#if !GLIB_CHECK_VERSION(2, 31, 0)
0410ae
+    if (!g_thread_supported()) {
0410ae
+         g_thread_init(NULL);
0410ae
+    }
0410ae
+#endif
0410ae
 
0410ae
     while ((c = getopt(argc, argv, "c:e:pd:")) != -1) {
0410ae
         switch (c) {
0410ae
@@ -723,13 +732,8 @@ main(
0410ae
     }
0410ae
 
0410ae
     socket_to_send = g_byte_array_new();
0410ae
-    qemu_mutex_init(&socket_to_send_lock);
0410ae
-    qemu_mutex_init(&pending_reader_lock);
0410ae
-    qemu_cond_init(&pending_reader_condition);
0410ae
-
0410ae
     vcard_emul_init(command_line_options);
0410ae
-
0410ae
-    loop = g_main_loop_new(NULL, true);
0410ae
+    loop = g_main_loop_new(NULL, TRUE);
0410ae
 
0410ae
     printf("> ");
0410ae
     fflush(stdout);