[Spice-devel] [PATCH spice-server 23/33] tests: Provide alarm replacement for Windows

Frediano Ziglio fziglio at redhat.com
Fri Dec 21 12:03:11 UTC 2018


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/tests/Makefile.am          |  2 +
 server/tests/test-channel.c       |  1 +
 server/tests/test-loop.c          |  1 +
 server/tests/test-stream-device.c |  1 +
 server/tests/win-alarm.c          | 65 +++++++++++++++++++++++++++++++
 server/tests/win-alarm.h          | 26 +++++++++++++
 6 files changed, 96 insertions(+)
 create mode 100644 server/tests/win-alarm.c
 create mode 100644 server/tests/win-alarm.h

diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
index d7f7af9b..7668739f 100644
--- a/server/tests/Makefile.am
+++ b/server/tests/Makefile.am
@@ -35,6 +35,8 @@ libtest_a_SOURCES =				\
 	test-display-base.h			\
 	test-glib-compat.c			\
 	test-glib-compat.h			\
+	win-alarm.c				\
+	win-alarm.h				\
 	$(NULL)
 
 LDADD =								\
diff --git a/server/tests/test-channel.c b/server/tests/test-channel.c
index fef295a3..583cb522 100644
--- a/server/tests/test-channel.c
+++ b/server/tests/test-channel.c
@@ -28,6 +28,7 @@
 #include "red-client.h"
 #include "cursor-channel.h"
 #include "net-utils.h"
+#include "win-alarm.h"
 
 /*
  * Declare a RedTestChannel to be used for the test
diff --git a/server/tests/test-loop.c b/server/tests/test-loop.c
index 1e3b39e5..82af80ab 100644
--- a/server/tests/test-loop.c
+++ b/server/tests/test-loop.c
@@ -31,6 +31,7 @@
 #include <spice/macros.h>
 #include <common/log.h>
 #include "basic-event-loop.h"
+#include "win-alarm.h"
 
 static SpiceCoreInterface *core = NULL;
 static GMainLoop *loop = NULL;
diff --git a/server/tests/test-stream-device.c b/server/tests/test-stream-device.c
index f1707d2f..e1868789 100644
--- a/server/tests/test-stream-device.c
+++ b/server/tests/test-stream-device.c
@@ -33,6 +33,7 @@
 #include "test-glib-compat.h"
 #include "stream-channel.h"
 #include "reds.h"
+#include "win-alarm.h"
 
 static SpiceCharDeviceInstance vmc_instance;
 
diff --git a/server/tests/win-alarm.c b/server/tests/win-alarm.c
new file mode 100644
index 00000000..225d0709
--- /dev/null
+++ b/server/tests/win-alarm.c
@@ -0,0 +1,65 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2018 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <config.h>
+#include <glib.h>
+
+#ifdef _WIN32
+#include <windows.h>
+#include "win-alarm.h"
+
+static HANDLE alarm_cond = NULL;
+
+static DWORD WINAPI alarm_thread_proc(LPVOID arg)
+{
+    unsigned int timeout = (uintptr_t) arg;
+    switch (WaitForSingleObject(alarm_cond, timeout * 1000)) {
+    case WAIT_OBJECT_0:
+        return 0;
+    }
+    abort();
+    return 0;
+}
+
+void alarm(unsigned int timeout)
+{
+    static HANDLE thread = NULL;
+
+    // create an event to stop the alarm thread
+    if (alarm_cond == NULL) {
+        alarm_cond = CreateEvent(NULL, TRUE, FALSE, NULL);
+        g_assert(alarm_cond != NULL);
+    }
+
+    // stop old alarm
+    if (thread) {
+        SetEvent(alarm_cond);
+        g_assert(WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0);
+        CloseHandle(thread);
+        thread = NULL;
+    }
+
+    if (timeout) {
+        ResetEvent(alarm_cond);
+
+        // start alarm thread
+        thread = CreateThread(NULL, 0, alarm_thread_proc, (LPVOID) (uintptr_t) timeout, 0, NULL);
+        g_assert(thread);
+    }
+}
+#endif
diff --git a/server/tests/win-alarm.h b/server/tests/win-alarm.h
new file mode 100644
index 00000000..a7233a8f
--- /dev/null
+++ b/server/tests/win-alarm.h
@@ -0,0 +1,26 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2018 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef TEST_WIN_ALARM_H
+#define TEST_WIN_ALARM_H
+
+#ifdef _WIN32
+void test_alarm(unsigned int timeout);
+#define alarm test_alarm
+#endif
+
+#endif // TEST_WIN_ALARM_H
-- 
2.17.2



More information about the Spice-devel mailing list