[Spice-devel] [PATCH spice-server v6 04/19] tests: Provide alarm replacement for Windows
Frediano Ziglio
fziglio at redhat.com
Tue Apr 30 12:24:04 UTC 2019
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
server/tests/Makefile.am | 2 +
server/tests/meson.build | 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 +++++++++++++
7 files changed, 98 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/meson.build b/server/tests/meson.build
index 20548213..31febc6a 100644
--- a/server/tests/meson.build
+++ b/server/tests/meson.build
@@ -11,6 +11,8 @@ test_lib_sources = [
'test-display-base.h',
'test-glib-compat.c',
'test-glib-compat.h',
+ 'win-alarm.c',
+ 'win-alarm.h',
]
test_libs = []
diff --git a/server/tests/test-channel.c b/server/tests/test-channel.c
index 9700e31c..fcea98aa 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 ce37822f..f5698c7d 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.20.1
More information about the Spice-devel
mailing list