Blame 0004-hw-qxl-render-drop-cursor-locks-replace-with-pipe.patch

Justin M. Forbes da9298
>From 1a33e5f2fa6de800047517a6f3251dc7191c97b5 Mon Sep 17 00:00:00 2001
Justin M. Forbes da9298
From: Alon Levy <alevy@redhat.com>
Justin M. Forbes da9298
Date: Wed, 16 Mar 2011 16:02:16 +0100
Justin M. Forbes da9298
Subject: [PATCH 4/4] hw/qxl-render: drop cursor locks, replace with pipe
Justin M. Forbes da9298
Justin M. Forbes da9298
Switching locking protection of ds->cursor_set/cursor_move to moving
Justin M. Forbes da9298
every call to these functions into the iothread and using the ssd->pipe
Justin M. Forbes da9298
to transfer that, adding QXL_SERVER_CURSOR_SET, QXL_SERVER_CURSOR_MOVE.
Justin M. Forbes da9298
Justin M. Forbes da9298
This is tested with both -vnc :0 -spice and -sdl -spice.
Justin M. Forbes da9298
---
Justin M. Forbes da9298
 hw/qxl-render.c    |   25 +++++++++-----
Justin M. Forbes da9298
 hw/qxl.c           |   90 ++++++++++++++++++++++++++++++++++++++++++++++++++++
Justin M. Forbes da9298
 hw/qxl.h           |    4 ++
Justin M. Forbes da9298
 ui/spice-display.h |   13 +++++++-
Justin M. Forbes da9298
 4 files changed, 122 insertions(+), 10 deletions(-)
Justin M. Forbes da9298
Justin M. Forbes da9298
diff --git a/hw/qxl-render.c b/hw/qxl-render.c
Justin M. Forbes da9298
index 58965e0..6759edb 100644
Justin M. Forbes da9298
--- a/hw/qxl-render.c
Justin M. Forbes da9298
+++ b/hw/qxl-render.c
Justin M. Forbes da9298
@@ -178,7 +178,6 @@ fail:
Justin M. Forbes da9298
     return NULL;
Justin M. Forbes da9298
 }
Justin M. Forbes da9298
 
Justin M. Forbes da9298
-
Justin M. Forbes da9298
 /* called from spice server thread context only */
Justin M. Forbes da9298
 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
Justin M. Forbes da9298
 {
Justin M. Forbes da9298
@@ -209,18 +208,26 @@ void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
Justin M. Forbes da9298
         if (c == NULL) {
Justin M. Forbes da9298
             c = cursor_builtin_left_ptr();
Justin M. Forbes da9298
         }
Justin M. Forbes da9298
-        qemu_mutex_lock_iothread();
Justin M. Forbes da9298
-        qxl->ssd.ds->cursor_define(c);
Justin M. Forbes da9298
-        qxl->ssd.ds->mouse_set(x, y, 1);
Justin M. Forbes da9298
-        qemu_mutex_unlock_iothread();
Justin M. Forbes da9298
-        cursor_put(c);
Justin M. Forbes da9298
+        qxl_server_request_cursor_set(qxl, c, x, y);
Justin M. Forbes da9298
         break;
Justin M. Forbes da9298
     case QXL_CURSOR_MOVE:
Justin M. Forbes da9298
         x = cmd->u.position.x;
Justin M. Forbes da9298
         y = cmd->u.position.y;
Justin M. Forbes da9298
-        qemu_mutex_lock_iothread();
Justin M. Forbes da9298
-        qxl->ssd.ds->mouse_set(x, y, 1);
Justin M. Forbes da9298
-        qemu_mutex_unlock_iothread();
Justin M. Forbes da9298
+        qxl_server_request_cursor_move(qxl, x, y);
Justin M. Forbes da9298
         break;
Justin M. Forbes da9298
     }
Justin M. Forbes da9298
 }
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+/* called from iothread only (via qxl.c:pipe_read) */
Justin M. Forbes da9298
+void qxl_render_cursor_set(SimpleSpiceDisplay *ssd, QEMUCursor *c, int x, int y)
Justin M. Forbes da9298
+{
Justin M. Forbes da9298
+    ssd->ds->cursor_define(c);
Justin M. Forbes da9298
+    ssd->ds->mouse_set(x, y, 1);
Justin M. Forbes da9298
+    cursor_put(c);
Justin M. Forbes da9298
+}
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+/* called from iothread only (via qxl.c:pipe_read) */
Justin M. Forbes da9298
+void qxl_render_cursor_move(SimpleSpiceDisplay *ssd, int x, int y)
Justin M. Forbes da9298
+{
Justin M. Forbes da9298
+    ssd->ds->mouse_set(x, y, 1);
Justin M. Forbes da9298
+}
Justin M. Forbes da9298
diff --git a/hw/qxl.c b/hw/qxl.c
Justin M. Forbes da9298
index cf3c938..4c27deb 100644
Justin M. Forbes da9298
--- a/hw/qxl.c
Justin M. Forbes da9298
+++ b/hw/qxl.c
Justin M. Forbes da9298
@@ -117,6 +117,27 @@ static QXLMode qxl_modes[] = {
Justin M. Forbes da9298
 #endif
Justin M. Forbes da9298
 };
Justin M. Forbes da9298
 
Justin M. Forbes da9298
+typedef struct __attribute__ ((__packed__)) {
Justin M. Forbes da9298
+    QEMUCursor *c;
Justin M. Forbes da9298
+    int x;
Justin M. Forbes da9298
+    int y;
Justin M. Forbes da9298
+} QXLServerCursorSet;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+typedef struct __attribute__ ((__packed__)) {
Justin M. Forbes da9298
+    int x;
Justin M. Forbes da9298
+    int y;
Justin M. Forbes da9298
+} QXLServerCursorMove;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+typedef struct __attribute__ ((__packed__)) {
Justin M. Forbes da9298
+    unsigned char req;
Justin M. Forbes da9298
+    QXLServerCursorMove data;
Justin M. Forbes da9298
+} QXLServerCursorMoveRequest;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+typedef struct __attribute__ ((__packed__)) {
Justin M. Forbes da9298
+    unsigned char req;
Justin M. Forbes da9298
+    QXLServerCursorSet data;
Justin M. Forbes da9298
+} QXLServerCursorSetRequest;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
 static PCIQXLDevice *qxl0;
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 static void qxl_send_events(PCIQXLDevice *d, uint32_t events);
Justin M. Forbes da9298
@@ -337,6 +358,33 @@ static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
Justin M. Forbes da9298
 }
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 /* called from spice server thread context only */
Justin M. Forbes da9298
+void qxl_server_request_cursor_set(PCIQXLDevice *qxl, QEMUCursor *c, int x, int y)
Justin M. Forbes da9298
+{
Justin M. Forbes da9298
+    QXLServerCursorSetRequest req;
Justin M. Forbes da9298
+    int r;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+    req.req = QXL_SERVER_CURSOR_SET;
Justin M. Forbes da9298
+    req.data.c = c;
Justin M. Forbes da9298
+    req.data.x = x;
Justin M. Forbes da9298
+    req.data.y = y;
Justin M. Forbes da9298
+    r = write(qxl->ssd.pipe[1], &req, sizeof(req));
Justin M. Forbes da9298
+    assert(r == sizeof(req));
Justin M. Forbes da9298
+}
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+/* called from spice server thread context only */
Justin M. Forbes da9298
+void qxl_server_request_cursor_move(PCIQXLDevice *qxl, int x, int y)
Justin M. Forbes da9298
+{
Justin M. Forbes da9298
+    QXLServerCursorMoveRequest req;
Justin M. Forbes da9298
+    int r;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+    req.req = QXL_SERVER_CURSOR_MOVE;
Justin M. Forbes da9298
+    req.data.x = x;
Justin M. Forbes da9298
+    req.data.y = y;
Justin M. Forbes da9298
+    r = write(qxl->ssd.pipe[1], &req, sizeof(req));
Justin M. Forbes da9298
+    assert(r == sizeof(req));
Justin M. Forbes da9298
+}
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+/* called from spice server thread context only */
Justin M. Forbes da9298
 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
Justin M. Forbes da9298
 {
Justin M. Forbes da9298
     PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
Justin M. Forbes da9298
@@ -1055,12 +1103,37 @@ static void qxl_map(PCIDevice *pci, int region_num,
Justin M. Forbes da9298
     }
Justin M. Forbes da9298
 }
Justin M. Forbes da9298
 
Justin M. Forbes da9298
+static void read_bytes(int fd, void *buf, int len_requested)
Justin M. Forbes da9298
+{
Justin M. Forbes da9298
+    int len;
Justin M. Forbes da9298
+    int total_len = 0;
Justin M. Forbes da9298
+
Justin M. Forbes da9298
+    do {
Justin M. Forbes da9298
+        len = read(fd, buf, len_requested - total_len);
Justin M. Forbes da9298
+        if (len < 0) {
Justin M. Forbes da9298
+            if (errno == EINTR || errno == EAGAIN) {
Justin M. Forbes da9298
+                continue;
Justin M. Forbes da9298
+            }
Justin M. Forbes da9298
+            perror("qxl: pipe_read: read failed");
Justin M. Forbes da9298
+            /* will abort once it's out of the while loop */
Justin M. Forbes da9298
+            break;
Justin M. Forbes da9298
+        }
Justin M. Forbes da9298
+        total_len += len;
Justin M. Forbes da9298
+        buf = (uint8_t *)buf + len;
Justin M. Forbes da9298
+    } while (total_len < len_requested);
Justin M. Forbes da9298
+    assert(total_len == len_requested);
Justin M. Forbes da9298
+}
Justin M. Forbes da9298
+
Justin M. Forbes da9298
 static void pipe_read(void *opaque)
Justin M. Forbes da9298
 {
Justin M. Forbes da9298
     SimpleSpiceDisplay *ssd = opaque;
Justin M. Forbes da9298
     unsigned char cmd;
Justin M. Forbes da9298
     int len, set_irq = 0;
Justin M. Forbes da9298
     int create_update = 0;
Justin M. Forbes da9298
+    int cursor_set = 0;
Justin M. Forbes da9298
+    int cursor_move = 0;
Justin M. Forbes da9298
+    QXLServerCursorSet cursor_set_data;
Justin M. Forbes da9298
+    QXLServerCursorMove cursor_move_data;
Justin M. Forbes da9298
 
Justin M. Forbes da9298
     while (1) {
Justin M. Forbes da9298
         cmd = 0;
Justin M. Forbes da9298
@@ -1082,6 +1155,17 @@ static void pipe_read(void *opaque)
Justin M. Forbes da9298
         case QXL_SERVER_CREATE_UPDATE:
Justin M. Forbes da9298
             create_update = 1;
Justin M. Forbes da9298
             break;
Justin M. Forbes da9298
+        case QXL_SERVER_CURSOR_SET:
Justin M. Forbes da9298
+            if (cursor_set == 1) {
Justin M. Forbes da9298
+                cursor_put(cursor_set_data.c);
Justin M. Forbes da9298
+            }
Justin M. Forbes da9298
+            cursor_set = 1;
Justin M. Forbes da9298
+            read_bytes(ssd->pipe[0], &cursor_set_data, sizeof(cursor_set_data));
Justin M. Forbes da9298
+            break;
Justin M. Forbes da9298
+        case QXL_SERVER_CURSOR_MOVE:
Justin M. Forbes da9298
+            cursor_move = 1;
Justin M. Forbes da9298
+            read_bytes(ssd->pipe[0], &cursor_move_data, sizeof(cursor_move_data));
Justin M. Forbes da9298
+            break;
Justin M. Forbes da9298
         default:
Justin M. Forbes da9298
             fprintf(stderr, "%s: unknown cmd %u\n", __FUNCTION__, cmd);
Justin M. Forbes da9298
             abort();
Justin M. Forbes da9298
@@ -1099,6 +1183,12 @@ static void pipe_read(void *opaque)
Justin M. Forbes da9298
     if (set_irq) {
Justin M. Forbes da9298
         qxl_set_irq(container_of(ssd, PCIQXLDevice, ssd));
Justin M. Forbes da9298
     }
Justin M. Forbes da9298
+    if (cursor_move) {
Justin M. Forbes da9298
+        qxl_render_cursor_move(ssd, cursor_move_data.x, cursor_move_data.y);
Justin M. Forbes da9298
+    }
Justin M. Forbes da9298
+    if (cursor_set) {
Justin M. Forbes da9298
+        qxl_render_cursor_set(ssd, cursor_set_data.c, cursor_set_data.x, cursor_set_data.y);
Justin M. Forbes da9298
+    }
Justin M. Forbes da9298
 }
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
Justin M. Forbes da9298
diff --git a/hw/qxl.h b/hw/qxl.h
Justin M. Forbes da9298
index 701245f..f4f99ec 100644
Justin M. Forbes da9298
--- a/hw/qxl.h
Justin M. Forbes da9298
+++ b/hw/qxl.h
Justin M. Forbes da9298
@@ -93,6 +93,8 @@ typedef struct PCIQXLDevice {
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 /* qxl.c */
Justin M. Forbes da9298
 void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL phys, int group_id);
Justin M. Forbes da9298
+void qxl_server_request_cursor_set(PCIQXLDevice *qxl, QEMUCursor *c, int x, int y);
Justin M. Forbes da9298
+void qxl_server_request_cursor_move(PCIQXLDevice *qxl, int x, int y);
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 /* qxl-logger.c */
Justin M. Forbes da9298
 void qxl_log_cmd_cursor(PCIQXLDevice *qxl, QXLCursorCmd *cmd, int group_id);
Justin M. Forbes da9298
@@ -102,3 +104,5 @@ void qxl_log_command(PCIQXLDevice *qxl, const char *ring, QXLCommandExt *ext);
Justin M. Forbes da9298
 void qxl_render_resize(PCIQXLDevice *qxl);
Justin M. Forbes da9298
 void qxl_render_update(PCIQXLDevice *qxl);
Justin M. Forbes da9298
 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext);
Justin M. Forbes da9298
+void qxl_render_cursor_set(SimpleSpiceDisplay *ssd, QEMUCursor *c, int x, int y);
Justin M. Forbes da9298
+void qxl_render_cursor_move(SimpleSpiceDisplay *ssd, int x, int y);
Justin M. Forbes da9298
diff --git a/ui/spice-display.h b/ui/spice-display.h
Justin M. Forbes da9298
index 2be6a34..bbfd689 100644
Justin M. Forbes da9298
--- a/ui/spice-display.h
Justin M. Forbes da9298
+++ b/ui/spice-display.h
Justin M. Forbes da9298
@@ -31,11 +31,22 @@
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 #define NUM_SURFACES 1024
Justin M. Forbes da9298
 
Justin M. Forbes da9298
-/* For commands/requests from server thread to iothread */
Justin M. Forbes da9298
+/*
Justin M. Forbes da9298
+ * Commands/requests from server thread to iothread.
Justin M. Forbes da9298
+ * Note that CREATE_UPDATE is used both with qxl and without it (spice-display)
Justin M. Forbes da9298
+ * the others are only used with the qxl device.
Justin M. Forbes da9298
+ *
Justin M. Forbes da9298
+ * SET_IRQ - just the request is sent (1 byte)
Justin M. Forbes da9298
+ * CREATE_UPDATE - jus the request is sent (1 byte)
Justin M. Forbes da9298
+ * CURSOR_SET - send QXLServerRequestCursorSet
Justin M. Forbes da9298
+ * CURSOR_MOVE - send QXLServerRequestCursorMove
Justin M. Forbes da9298
+ */
Justin M. Forbes da9298
 #define QXL_EMPTY_UPDATE ((void *)-1)
Justin M. Forbes da9298
 enum {
Justin M. Forbes da9298
     QXL_SERVER_SET_IRQ = 1,
Justin M. Forbes da9298
     QXL_SERVER_CREATE_UPDATE,
Justin M. Forbes da9298
+    QXL_SERVER_CURSOR_SET,
Justin M. Forbes da9298
+    QXL_SERVER_CURSOR_MOVE
Justin M. Forbes da9298
 };
Justin M. Forbes da9298
 
Justin M. Forbes da9298
 struct SimpleSpiceUpdate;
Justin M. Forbes da9298
-- 
Justin M. Forbes da9298
1.7.3.2
Justin M. Forbes da9298