[wing] Add utility to get a proper monotonic on windows
- From: Ignacio Casal Quinteiro <icq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [wing] Add utility to get a proper monotonic on windows
- Date: Wed, 29 Mar 2017 15:50:31 +0000 (UTC)
commit cbcd9ad620a6fd6f08c33356d7edfaf59d98e6b9
Author: Ignacio Casal Quinteiro <qignacio amazon com>
Date: Wed Mar 29 10:37:47 2017 +0200
Add utility to get a proper monotonic on windows
Currently glib provides a monotonic that works on Windows XP
but that does not provide a accurate monotonic.
tests/meson.build | 3 ++-
tests/utils.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
wing/meson.build | 5 +++++
wing/wing-init.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
wing/wing-init.h | 25 +++++++++++++++++++++++++
wing/wingutils.c | 40 ++++++++++++++++++++++++++++++++++++++++
wing/wingutils.h | 3 +++
7 files changed, 163 insertions(+), 1 deletions(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index b26d794..007e82b 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,5 +1,6 @@
unit_tests = [
- 'named-pipe'
+ 'named-pipe',
+ 'utils',
]
foreach unit: unit_tests
diff --git a/tests/utils.c b/tests/utils.c
new file mode 100644
index 0000000..57dcc56
--- /dev/null
+++ b/tests/utils.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 NICE s.r.l.
+ *
+ * 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 <wing/wing.h>
+#include <glib.h>
+
+static void
+test_monotonic_time (void)
+{
+ gint64 t1, t2;
+
+ t1 = wing_get_monotonic_time ();
+ t2 = wing_get_monotonic_time ();
+ g_assert_cmpint (t2, >=, t1);
+
+ g_usleep(10000);
+ t2 = wing_get_monotonic_time ();
+ g_assert_cmpint (t2, >, t1 + 10000);
+}
+
+int
+main(int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/utils/monotonic-time", test_monotonic_time);
+
+ return g_test_run ();
+}
diff --git a/wing/meson.build b/wing/meson.build
index 74855aa..c696a68 100644
--- a/wing/meson.build
+++ b/wing/meson.build
@@ -10,7 +10,12 @@ headers = [
'wingutils.h',
]
+private_headers = [
+ 'wing-init.h',
+]
+
sources = [
+ 'wing-init.c',
'wingnamedpipeclient.c',
'wingnamedpipeconnection.c',
'wingnamedpipelistener.c',
diff --git a/wing/wing-init.c b/wing/wing-init.c
new file mode 100644
index 0000000..aa3188a
--- /dev/null
+++ b/wing/wing-init.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 NICE s.r.l.
+ *
+ * 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 "wing-init.h"
+#include <windows.h>
+
+BOOL WINAPI DllMain (HINSTANCE hinstDLL,
+ DWORD fdwReason,
+ LPVOID lpvReserved);
+
+HMODULE wing_dll;
+
+BOOL WINAPI
+DllMain (HINSTANCE hinstDLL,
+ DWORD fdwReason,
+ LPVOID lpvReserved)
+{
+ switch (fdwReason)
+ {
+ case DLL_PROCESS_ATTACH:
+ wing_dll = hinstDLL;
+ wing_init_monotonic_time ();
+ break;
+ default:
+ /* do nothing */
+ ;
+ }
+
+ return TRUE;
+}
diff --git a/wing/wing-init.h b/wing/wing-init.h
new file mode 100644
index 0000000..97e4b47
--- /dev/null
+++ b/wing/wing-init.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright © 2017 NICE s.r.l.
+ *
+ * This program 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 of the licence 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/>.
+ *
+ * Authors: Ignacio Casal Quinteiro <ignacio casal nice-software com>
+ */
+
+#ifndef WING_INIT_H
+#define WING_INIT_H
+
+void wing_init_monotonic_time (void);
+
+#endif /* WING_INIT_H */
diff --git a/wing/wingutils.c b/wing/wingutils.c
index c743386..4d5e994 100644
--- a/wing/wingutils.c
+++ b/wing/wingutils.c
@@ -72,3 +72,43 @@ wing_get_version_number (gint *major,
return TRUE;
}
+
+static gint64 monotonic_ticks_per_sec = 0;
+
+void
+wing_init_monotonic_time (void)
+{
+ LARGE_INTEGER freq;
+
+ if (!QueryPerformanceFrequency (&freq) || freq.QuadPart == 0)
+ {
+ g_warning ("Unable to use QueryPerformanceCounter (%d). Fallback to low resolution timer",
GetLastError ());
+ monotonic_ticks_per_sec = 0;
+ return;
+ }
+
+ monotonic_ticks_per_sec = freq.QuadPart;
+}
+
+gint64
+wing_get_monotonic_time (void)
+{
+ if (G_LIKELY (monotonic_ticks_per_sec != 0))
+ {
+ LARGE_INTEGER ticks;
+
+ if (QueryPerformanceCounter (&ticks))
+ {
+ gint64 time;
+
+ time = ticks.QuadPart * G_USEC_PER_SEC; /* multiply first to avoid loss of precision */
+
+ return time / monotonic_ticks_per_sec;
+ }
+
+ g_warning ("QueryPerformanceCounter Failed (%d). Permanently fallback to low resolution timer",
GetLastError ());
+ monotonic_ticks_per_sec = 0;
+ }
+
+ return g_get_monotonic_time ();
+}
diff --git a/wing/wingutils.h b/wing/wingutils.h
index 390ad20..23220b0 100644
--- a/wing/wingutils.h
+++ b/wing/wingutils.h
@@ -33,6 +33,9 @@ WING_AVAILABLE_IN_ALL
gboolean wing_get_version_number (gint *major,
gint *minor);
+WING_AVAILABLE_IN_ALL
+gint64 wing_get_monotonic_time (void);
+
G_END_DECLS
#endif /* WING_UTILS_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]