Skip to contentSkip to conversation

w32: Reduce dependency on Windows API

PATCH In review Stefan Weil 4 participants 13 messages Mar 7, 2014 – Mar 11, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover w32: Reduce dependency on Windows API

The first 4 patches reduce the number of files which depend on windows.h by about 90 percent.

This reduces the compilation time, allows removing some hacks and avoids name space pollution.

Patch 5 is optional and new here.

Changes in v2:

* Change name of new include file include/qemu/winapi.h (suggested by Paolo Bonzini)

* Don't replace Win32 data types in block/raw-aio.h (suggested by Kevin Wolf)

* Replace Win32 data types in central header files by new QEMU data types instead of basic C data types (suggested by Peter Maydell and Stefan Hajnoczi)

[PATCH v2 1/5] w32: Add and use intermediate include file for windows.h [PATCH v2 2/5] w32: Move inline function from header file to C source [PATCH v2 3/5] w32: Reduce dependencies in sysemu/os-win32.h [PATCH v2 4/5] w32: Replace Windows specific data types in common [PATCH v2 5/5] block: Review include statements for winioctl.h

#1 lore raw

PATCH v2 1/5 w32: Add and use intermediate include file for windows.h

Including windows.h from the new file include/qemu/winapi.h allows better tracking of the files which depend on the Windows API.

1864 *.o files depend on windows.h in a typical build, only 88 *.o files don't.

The windows.h specific macro WIN32_LEAN_AND_MEAN is now defined in the new file and no longer part of the QEMU_CFLAGS. A hack in ui/sdl.c can be removed after this change.

WINVER is still needed for all compilations with MinGW, so it cannot be defined in the new file. Replace its numeric value by a symbolic value to show which API is requested.

Cc Gerd Hoffmann <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Anthony Liguori <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
---
 audio/audio_win_int.c         |    2 +-
 audio/dsoundaudio.c           |    2 +-
 audio/winwaveaudio.c          |    2 +-
 block.c                       |    2 +-
 block/raw-win32.c             |    2 +-
 block/win32-aio.c             |    2 +-
 configure                     |    2 +-
 include/qemu/event_notifier.h |    2 +-
 include/qemu/sockets.h        |    2 +-
 include/qemu/thread-win32.h   |    2 +-
 include/qemu/winapi.h         |   22 ++++++++++++++++++++++
 include/sysemu/os-win32.h     |    2 +-
 net/tap-win32.c               |    2 +-
 os-win32.c                    |    2 +-
 qga/channel-win32.c           |    2 +-
 qga/commands-win32.c          |    2 +-
 qga/service-win32.c           |    2 +-
 qga/service-win32.h           |    2 +-
 qga/vss-win32.c               |    2 +-
 qga/vss-win32/vss-common.h    |    2 +-
 slirp/slirp.h                 |    2 +-
 translate-all.c               |    2 +-
 ui/sdl.c                      |    3 ---
 util/oslib-win32.c            |    2 +-
 24 files changed, 44 insertions(+), 25 deletions(-)
 create mode 100644 include/qemu/winapi.h
audio/audio_win_int.c +1 -1
index e132405..0e9f2a4 100644
@@ -3,7 +3,7 @@
 #include "qemu-common.h"
 
 #define AUDIO_CAP "win-int"
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <mmsystem.h>
 
 #include "audio.h"
audio/dsoundaudio.c +1 -1
index e2d89fd..cf779bf 100644
@@ -32,7 +32,7 @@
 #define AUDIO_CAP "dsound"
 #include "audio_int.h"
 
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <mmsystem.h>
 #include <objbase.h>
 #include <dsound.h>
audio/winwaveaudio.c +1 -1
index 8dbd145..f11f5ba 100644
@@ -7,7 +7,7 @@
 #define AUDIO_CAP "winwave"
 #include "audio_int.h"
 
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <mmsystem.h>
 
 #include "audio_win_int.h"
block.c +1 -1
index 38bbdf3..409cbd8 100644
@@ -47,7 +47,7 @@
 #endif
 
 #ifdef _WIN32
-#include <windows.h>
+#include "qemu/winapi.h"
 #endif
 
 struct BdrvDirtyBitmap {
block/raw-win32.c +1 -1
index ae1c8e6..95b27a5 100644
@@ -23,13 +23,13 @@
  */
 #include "qemu-common.h"
 #include "qemu/timer.h"
+#include "qemu/winapi.h"        /* HANDLE (in raw-aio.h) */
 #include "block/block_int.h"
 #include "qemu/module.h"
 #include "raw-aio.h"
 #include "trace.h"
 #include "block/thread-pool.h"
 #include "qemu/iov.h"
-#include <windows.h>
 #include <winioctl.h>
 
 #define FTYPE_FILE 0
block/win32-aio.c +1 -1
index 5d1d199..dbcc6bc 100644
@@ -23,13 +23,13 @@
  */
 #include "qemu-common.h"
 #include "qemu/timer.h"
+#include "qemu/winapi.h"        /* HANDLE (in raw-aio.h) */
 #include "block/block_int.h"
 #include "qemu/module.h"
 #include "block/aio.h"
 #include "raw-aio.h"
 #include "qemu/event_notifier.h"
 #include "qemu/iov.h"
-#include <windows.h>
 #include <winioctl.h>
 
 #define FTYPE_FILE 0
configure +1 -1
index 8689435..daf6f69 100755
@@ -682,7 +682,7 @@ fi
 if test "$mingw32" = "yes" ; then
   EXESUF=".exe"
   DSOSUF=".dll"
-  QEMU_CFLAGS="-DWIN32_LEAN_AND_MEAN -DWINVER=0x501 $QEMU_CFLAGS"
+  QEMU_CFLAGS="-DWINVER=_WIN32_WINNT_WINXP $QEMU_CFLAGS"
   # enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later)
   QEMU_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $QEMU_CFLAGS"
   LIBS="-lwinmm -lws2_32 -liphlpapi $LIBS"
include/qemu/event_notifier.h +1 -1
index 88b57af..bdca689 100644
@@ -16,7 +16,7 @@
 #include "qemu-common.h"
 
 #ifdef _WIN32
-#include <windows.h>
+#include "qemu/winapi.h"
 #endif
 
 struct EventNotifier {
include/qemu/sockets.h +1 -1
index 45588d7..a41d453 100644
@@ -3,7 +3,7 @@
 #define QEMU_SOCKET_H
 
 #ifdef _WIN32
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <winsock2.h>
 #include <ws2tcpip.h>
 
include/qemu/thread-win32.h +1 -1
index 3d58081..7ade61a 100644
@@ -1,6 +1,6 @@
 #ifndef __QEMU_THREAD_WIN32_H
 #define __QEMU_THREAD_WIN32_H 1
-#include "windows.h"
+#include "qemu/winapi.h"
 
 struct QemuMutex {
     CRITICAL_SECTION lock;
include/qemu/winapi.h new file +22 -0
new file mode 100644
index 0000000..d4cfaaa
@@ -0,0 +1,22 @@
+/*
+ * QEMU interface to the Windows API
+ *
+ * Copyright (c) 2014 Stefan Weil
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2
+ * or later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_WINAPI_H
+#define QEMU_WINAPI_H
+
+/* Don't include some less frequently used APIs. */
+#define WIN32_LEAN_AND_MEAN
+
+#if !defined(WINVER)
+# error Add -DWINVER=_WIN32_WINNT_WINXP to compiler options
+#endif
+
+#include <windows.h>
+
+#endif /* QEMU_WINAPI_H */
include/sysemu/os-win32.h +1 -1
index bf8523a..1d6494a 100644
@@ -26,7 +26,7 @@
 #ifndef QEMU_OS_WIN32_H
 #define QEMU_OS_WIN32_H
 
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <winsock2.h>
 
 /* Workaround for older versions of MinGW. */
net/tap-win32.c +1 -1
index 8aee611..52e6143 100644
@@ -35,7 +35,7 @@
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 #include <stdio.h>
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <winioctl.h>
 
 //=============
os-win32.c +1 -1
index 5f95caa..f6ec112 100644
@@ -22,7 +22,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <mmsystem.h>
 #include <unistd.h>
 #include <fcntl.h>
qga/channel-win32.c +1 -1
index 0d5e5f5..25c3257 100644
@@ -2,7 +2,7 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <glib.h>
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <errno.h>
 #include <io.h>
 #include "qga/guest-agent-core.h"
qga/commands-win32.c +1 -1
index 0ee07b6..48cc819 100644
@@ -11,8 +11,8 @@
  * See the COPYING file in the top-level directory.
  */
 
+#include "qemu/winapi.h" /* OpenProcessToken, ... */
 #include <glib.h>
-#include <wtypes.h>
 #include <powrprof.h>
 #include "qga/guest-agent-core.h"
 #include "qga/vss-win32.h"
qga/service-win32.c +1 -1
index aef41f0..b22ae67 100644
@@ -13,7 +13,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <glib.h>
-#include <windows.h>
+#include "qemu/winapi.h"
 #include "qga/service-win32.h"
 
 static int printf_win_error(const char *text)
qga/service-win32.h +1 -1
index 3b9e870..bd401f2 100644
@@ -13,7 +13,7 @@
 #ifndef QGA_SERVICE_H
 #define QGA_SERVICE_H
 
-#include <windows.h>
+#include "qemu/winapi.h"
 
 #define QGA_SERVICE_DISPLAY_NAME "QEMU Guest Agent"
 #define QGA_SERVICE_NAME         "qemu-ga"
qga/vss-win32.c +1 -1
index 24c4288..c6970d2 100644
@@ -11,7 +11,7 @@
  */
 
 #include <stdio.h>
-#include <windows.h>
+#include "qemu/winapi.h"
 #include "qga/guest-agent-core.h"
 #include "qga/vss-win32.h"
 #include "qga/vss-win32/requester.h"
qga/vss-win32/vss-common.h +1 -1
index ce14e14..9768c1e 100644
@@ -15,7 +15,7 @@
 
 #define __MIDL_user_allocate_free_DEFINED__
 #include "config-host.h"
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <shlwapi.h>
 
 /* Reduce warnings to include vss.h */
slirp/slirp.h +1 -1
index e4a1bd4..bb25ab9 100644
@@ -9,7 +9,7 @@
 
 typedef char *caddr_t;
 
-# include <windows.h>
+# include "qemu/winapi.h"
 # include <winsock2.h>
 # include <ws2tcpip.h>
 # include <sys/timeb.h>
translate-all.c +1 -1
index 1ac0246..c124c07 100644
@@ -17,7 +17,7 @@
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 #ifdef _WIN32
-#include <windows.h>
+#include "qemu/winapi.h"
 #else
 #include <sys/types.h>
 #include <sys/mman.h>
ui/sdl.c +0 -3
index c1a16be..efae4a7 100644
@@ -22,9 +22,6 @@
  * THE SOFTWARE.
  */
 
-/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
-#undef WIN32_LEAN_AND_MEAN
-
 #include <SDL.h>
 
 #if SDL_MAJOR_VERSION == 1
util/oslib-win32.c +1 -1
index 93f7d35..3fbe72a 100644
@@ -25,7 +25,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include <windows.h>
+#include "qemu/winapi.h"
 #include <glib.h>
 #include <stdlib.h>
 #include "config-host.h"
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] w32: Add and use intermediate include file for windows.h

Quoted 13 lines · Stefan Weil

Including windows.h from the new file include/qemu/winapi.h allows better tracking of the files which depend on the Windows API.

1864 *.o files depend on windows.h in a typical build, only 88 *.o files don't.

The windows.h specific macro WIN32_LEAN_AND_MEAN is now defined in the new file and no longer part of the QEMU_CFLAGS. A hack in ui/sdl.c can be removed after this change.

WINVER is still needed for all compilations with MinGW, so it cannot be defined in the new file. Replace its numeric value by a symbolic value to show which API is requested.

[...]

block/raw-win32.c line 26

Quoted 9 lines
block/raw-win32.c +1 -0
index ae1c8e6..95b27a5 100644
@@ -23,13 +23,13 @@
  */
 #include "qemu-common.h"
 #include "qemu/timer.h"
+#include "qemu/winapi.h"        /* HANDLE (in raw-aio.h) */

Such comments get out of date real fast. I treat them as noise.

If raw-aio.h needs stuff from winapi.h, why doesn't raw-aio.h include it?

Quoted 10 lines
block/raw-win32.c +0 -1
 #include "block/block_int.h"
 #include "qemu/module.h"
 #include "raw-aio.h"
 #include "trace.h"
 #include "block/thread-pool.h"
 #include "qemu/iov.h"
-#include <windows.h>
 #include <winioctl.h>
 
 #define FTYPE_FILE 0

[...]

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] w32: Add and use intermediate include file for windows.h

Am 10.03.2014 09:56, schrieb Markus Armbruster:

Quoted 16 lines
Quoted 13 lines · Stefan Weil

Including windows.h from the new file include/qemu/winapi.h allows better tracking of the files which depend on the Windows API.

1864 *.o files depend on windows.h in a typical build, only 88 *.o files don't.

The windows.h specific macro WIN32_LEAN_AND_MEAN is now defined in the new file and no longer part of the QEMU_CFLAGS. A hack in ui/sdl.c can be removed after this change.

WINVER is still needed for all compilations with MinGW, so it cannot be defined in the new file. Replace its numeric value by a symbolic value to show which API is requested.

[...]

Quoted 11 lines

block/raw-win32.c line 26

Quoted 9 lines
block/raw-win32.c +1 -0
index ae1c8e6..95b27a5 100644
@@ -23,13 +23,13 @@
  */
 #include "qemu-common.h"
 #include "qemu/timer.h"
+#include "qemu/winapi.h"        /* HANDLE (in raw-aio.h) */

Such comments get out of date real fast. I treat them as noise.

I agree that dependencies change over time. That's the main reason for this kind of comments: it is easy to check whether the comment still applies. If not, chances are good that the include statement is no longer needed and can be removed together with the comment. How can human reviewers see quickly why an include statement is needed when there is no comment? Usually they can see that only for the most common cases, and for all others they need help from the compiler.

Quoted 3 lines

If raw-aio.h needs stuff from winapi.h, why doesn't raw-aio.h include it?

It could, of course, but I assume that there is no special reason why it does not. A lot of QEMU sources don't include everything which they need explicitly. I thought about moving the include statement from block/raw-win32.c and block/win32-aio.c to raw-aio.h, but decided to minimize my changes and keep the current structure.

Stefan

#4 lore raw

PATCH v2 2/5 w32: Move inline function from header file to C source

In reply to Stefan Weil

A lot of files depend on qemu/timer.h. We don't want that all these files depend on windows.h, too.

---
 include/qemu/timer.h     |    8 +-------
 util/qemu-timer-common.c |   11 ++++++++++-
 2 files changed, 11 insertions(+), 8 deletions(-)
include/qemu/timer.h +1 -7
index 7f9a074..19316b7 100644
@@ -712,14 +712,8 @@ static inline int64_t get_clock_realtime(void)
    also used by simpletrace backend and tracepoints would cause
    an infinite recursion! */
 #ifdef _WIN32
-extern int64_t clock_freq;
 
-static inline int64_t get_clock(void)
-{
-    LARGE_INTEGER ti;
-    QueryPerformanceCounter(&ti);
-    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
-}
+int64_t get_clock(void);
 
 #else
 
util/qemu-timer-common.c +10 -1
index 95e0847..22b0109 100644
@@ -28,7 +28,16 @@
 
 #ifdef _WIN32
 
-int64_t clock_freq;
+#include "qemu/winapi.h" /* QueryPerformanceCounter, ... */
+
+static int64_t clock_freq;
+
+int64_t get_clock(void)
+{
+    LARGE_INTEGER ti;
+    QueryPerformanceCounter(&ti);
+    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
+}
 
 static void __attribute__((constructor)) init_get_clock(void)
 {
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v2 3/5 w32: Reduce dependencies in sysemu/os-win32.h

In reply to Stefan Weil

Most *.o files depend on that file, but many of them don't need windows.h or winsock2.h. sysemu/os-win32.h only needs some definitions from winerror.h.

After that change, all files which depend on windows.h or winsock2.h and which no longer get it indirectly have to be fixed. Use qemu/sockets.h to get winsock2.h. Add comments to all those new include statements.

The modification in ui/vnc-enc-tight.c is needed temporarily and will be removed again in the following patch.

Cc Anthony Liguori <[email protected]>
Cc Max Filippov <[email protected]>
---
 coroutine-win32.c           |    1 +
 include/net/eth.h           |    2 +-
 include/sysemu/os-win32.h   |    3 +--
 target-xtensa/xtensa-semi.c |    1 +
 ui/vnc-enc-tight.c          |    1 +
 5 files changed, 5 insertions(+), 3 deletions(-)
coroutine-win32.c +1 -0
index edc1f72..4678d17 100644
@@ -24,6 +24,7 @@
 
 #include "qemu-common.h"
 #include "block/coroutine_int.h"
+#include "qemu/winapi.h"   /* CreateFiber, ... */
 
 typedef struct
 {
include/net/eth.h +1 -1
index b3273b8..f5a369f 100644
@@ -26,10 +26,10 @@
 #ifndef QEMU_ETH_H
 #define QEMU_ETH_H
 
-#include <sys/types.h>
 #include <string.h>
 #include "qemu/bswap.h"
 #include "qemu/iov.h"
+#include "qemu/sockets.h" /* u_short */
 
 #define ETH_ALEN 6
 
include/sysemu/os-win32.h +1 -2
index 1d6494a..d625612 100644
@@ -26,8 +26,7 @@
 #ifndef QEMU_OS_WIN32_H
 #define QEMU_OS_WIN32_H
 
-#include "qemu/winapi.h"
-#include <winsock2.h>
+#include <winerror.h> /* WSAECONNREFUSED, ... */
 
 /* Workaround for older versions of MinGW. */
 #ifndef ECONNREFUSED
target-xtensa/xtensa-semi.c +1 -0
index 424253d..ad06f99 100644
@@ -32,6 +32,7 @@
 #include "cpu.h"
 #include "helper.h"
 #include "qemu/log.h"
+#include "qemu/sockets.h" /* select */
 
 enum {
     TARGET_SYS_exit = 1,
ui/vnc-enc-tight.c +1 -0
index e6966ae..94bb002 100644
@@ -31,6 +31,7 @@
 /* This needs to be before jpeglib.h line because of conflict with
    INT32 definitions between jmorecfg.h (included by jpeglib.h) and
    Win32 basetsd.h (included by windows.h). */
+#include "qemu/winapi.h" /* TODO: workaround, remove */
 #include "qemu-common.h"
 
 #ifdef CONFIG_VNC_PNG
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v2 4/5 w32: Replace Windows specific data types in common header files

In reply to Stefan Weil

These header files are used by most QEMU source files. If they depend on windows.h, all those source files do so, too.

All Windows specific data types which are replaced use identical definitions for the 32 and 64 bit Windows APIs. HANDLE, LONG and CRITICAL_SECTION are replaced by the compatible types WinHandle, WinLong and WinCriticalSection.

Add an explicit dependency on qemu/winapi.h for some files which need it. These sources use the Windows API (see comment after include statement) and no longer get windows.h indirectly from other header files.

A workaround which was added in the previous patch is no longer needed.

Now 175 *.o files remain which still depend on windows.h.

Cc Anthony Liguori <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Andreas Färber <[email protected]>
---
 cpus.c                        |    4 +++-
 hw/intc/apic.c                |    3 ++-
 include/qemu/event_notifier.h |    8 ++------
 include/qemu/main-loop.h      |    4 ++--
 include/qemu/thread-win32.h   |   29 ++++++++++++++++++++---------
 include/qom/cpu.h             |    2 +-
 include/sysemu/os-win32.h     |    7 +++++++
 ui/vnc-enc-tight.c            |    5 -----
 util/event_notifier-win32.c   |    1 +
 util/qemu-thread-win32.c      |   17 +++++++++--------
 10 files changed, 47 insertions(+), 33 deletions(-)
cpus.c +3 -1
index 945d85b..a96eb16 100644
@@ -39,7 +39,9 @@
 #include "qemu/bitmap.h"
 #include "qemu/seqlock.h"
 
-#ifndef _WIN32
+#ifdef _WIN32
+#include "qemu/winapi.h" /* SuspendThread, ... */
+#else
 #include "qemu/compatfd.h"
 #endif
 
hw/intc/apic.c +2 -1
index 361ae90..6bb2d78 100644
@@ -16,12 +16,13 @@
  * 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 "qemu/thread.h"
+
 #include "hw/i386/apic_internal.h"
 #include "hw/i386/apic.h"
 #include "hw/i386/ioapic.h"
 #include "hw/pci/msi.h"
 #include "qemu/host-utils.h"
+#include "qemu/thread.h"
 #include "trace.h"
 #include "hw/i386/pc.h"
 #include "hw/i386/apic-msidef.h"
include/qemu/event_notifier.h +2 -6
index bdca689..c5cf381 100644
@@ -15,13 +15,9 @@
 
 #include "qemu-common.h"
 
-#ifdef _WIN32
-#include "qemu/winapi.h"
-#endif
-
 struct EventNotifier {
 #ifdef _WIN32
-    HANDLE event;
+    WinHandle event;
 #else
     int rfd;
     int wfd;
@@ -40,7 +36,7 @@ int event_notifier_set_handler(EventNotifier *, EventNotifierHandler *);
 void event_notifier_init_fd(EventNotifier *, int fd);
 int event_notifier_get_fd(EventNotifier *);
 #else
-HANDLE event_notifier_get_handle(EventNotifier *);
+WinHandle event_notifier_get_handle(EventNotifier *);
 #endif
 
 #endif
include/qemu/main-loop.h +2 -2
index 6f0200a..aefdc94 100644
@@ -152,7 +152,7 @@ typedef void WaitObjectFunc(void *opaque);
  * @func: A function to be called when @handle is in a signaled state.
  * @opaque: A pointer-size value that is passed to @func.
  */
-int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
+int qemu_add_wait_object(WinHandle handle, WaitObjectFunc *func, void *opaque);
 
 /**
  * qemu_del_wait_object: Unregister a callback for a Windows handle
@@ -163,7 +163,7 @@ int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
  * @func: The function that was passed to qemu_add_wait_object.
  * @opaque: A pointer-size value that was passed to qemu_add_wait_object.
  */
-void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
+void qemu_del_wait_object(WinHandle handle, WaitObjectFunc *func, void *opaque);
 #endif
 
 /* async I/O support */
include/qemu/thread-win32.h +20 -9
index 7ade61a..b8b8e61 100644
@@ -1,24 +1,35 @@
 #ifndef __QEMU_THREAD_WIN32_H
 #define __QEMU_THREAD_WIN32_H 1
-#include "qemu/winapi.h"
+
+/* WinCriticalSection is a substitute for CRITICAL_SECTION and
+ * introduced here to avoid dependencies on windows.h. */
+
+typedef struct {
+    WinHandle DebugInfo;
+    WinLong LockCount;
+    WinLong RecursionCount;
+    WinHandle OwningThread;
+    WinHandle LockSemaphore;
+    WinULong *SpinCount;
+} WinCriticalSection;
 
 struct QemuMutex {
-    CRITICAL_SECTION lock;
-    LONG owner;
+    WinCriticalSection lock;
+    WinLong owner;
 };
 
 struct QemuCond {
-    LONG waiters, target;
-    HANDLE sema;
-    HANDLE continue_event;
+    WinLong waiters, target;
+    WinHandle sema;
+    WinHandle continue_event;
 };
 
 struct QemuSemaphore {
-    HANDLE sema;
+    WinHandle sema;
 };
 
 struct QemuEvent {
-    HANDLE event;
+    WinHandle event;
 };
 
 typedef struct QemuThreadData QemuThreadData;
@@ -28,6 +39,6 @@ struct QemuThread {
 };
 
 /* Only valid for joinable threads.  */
-HANDLE qemu_thread_get_handle(QemuThread *thread);
+WinHandle qemu_thread_get_handle(QemuThread *thread);
 
 #endif
include/qom/cpu.h +1 -1
index d734be8..6626681 100644
@@ -171,7 +171,7 @@ struct CPUState {
 
     struct QemuThread *thread;
 #ifdef _WIN32
-    HANDLE hThread;
+    WinHandle hThread;
 #endif
     int thread_id;
     uint32_t host_tid;
include/sysemu/os-win32.h +7 -0
index d625612..a23d6aa 100644
@@ -54,6 +54,13 @@
 # define EWOULDBLOCK  WSAEWOULDBLOCK
 #endif
 
+/* Define substitutes for the Win API types HANDLE, LONG, ULONG.
+ * These types are used to avoid dependencies on windows.h. */
+
+typedef void * WinHandle;
+typedef long WinLong;
+typedef unsigned long WinULong;
+
 #if defined(_WIN64)
 /* On w64, setjmp is implemented by _setjmp which needs a second parameter.
  * If this parameter is NULL, longjump does no stack unwinding.
ui/vnc-enc-tight.c +0 -5
index 94bb002..2e6fb88 100644
@@ -27,11 +27,6 @@
  */
 
 #include "config-host.h"
-
-/* This needs to be before jpeglib.h line because of conflict with
-   INT32 definitions between jmorecfg.h (included by jpeglib.h) and
-   Win32 basetsd.h (included by windows.h). */
-#include "qemu/winapi.h" /* TODO: workaround, remove */
 #include "qemu-common.h"
 
 #ifdef CONFIG_VNC_PNG
util/event_notifier-win32.c +1 -0
index 6dbb530..9f3f61d 100644
@@ -13,6 +13,7 @@
 #include "qemu-common.h"
 #include "qemu/event_notifier.h"
 #include "qemu/main-loop.h"
+#include "qemu/winapi.h" /* CreateEvent, ... */
 
 int event_notifier_init(EventNotifier *e, int active)
 {
util/qemu-thread-win32.c +9 -8
index 27a5217..9de0136 100644
@@ -10,11 +10,10 @@
  * See the COPYING file in the top-level directory.
  *
  */
+
+#include "qemu/winapi.h" /* LocalFree, ... */
 #include "qemu-common.h"
 #include "qemu/thread.h"
-#include <process.h>
-#include <assert.h>
-#include <limits.h>
 
 static void error_exit(int err, const char *msg)
 {
@@ -30,18 +29,20 @@ static void error_exit(int err, const char *msg)
 void qemu_mutex_init(QemuMutex *mutex)
 {
     mutex->owner = 0;
-    InitializeCriticalSection(&mutex->lock);
+    /* mutex->lock uses a data type which must fit CRITICAL_SECTION. */
+    g_assert(sizeof(mutex->lock) == sizeof(CRITICAL_SECTION));
+    InitializeCriticalSection((CRITICAL_SECTION *)&mutex->lock);
 }
 
 void qemu_mutex_destroy(QemuMutex *mutex)
 {
     assert(mutex->owner == 0);
-    DeleteCriticalSection(&mutex->lock);
+    DeleteCriticalSection((CRITICAL_SECTION *)&mutex->lock);
 }
 
 void qemu_mutex_lock(QemuMutex *mutex)
 {
-    EnterCriticalSection(&mutex->lock);
+    EnterCriticalSection((CRITICAL_SECTION *)&mutex->lock);
 
     /* Win32 CRITICAL_SECTIONs are recursive.  Assert that we're not
      * using them as such.
@@ -54,7 +55,7 @@ int qemu_mutex_trylock(QemuMutex *mutex)
 {
     int owned;
 
-    owned = TryEnterCriticalSection(&mutex->lock);
+    owned = TryEnterCriticalSection((CRITICAL_SECTION *)&mutex->lock);
     if (owned) {
         assert(mutex->owner == 0);
         mutex->owner = GetCurrentThreadId();
@@ -66,7 +67,7 @@ void qemu_mutex_unlock(QemuMutex *mutex)
 {
     assert(mutex->owner == GetCurrentThreadId());
     mutex->owner = 0;
-    LeaveCriticalSection(&mutex->lock);
+    LeaveCriticalSection((CRITICAL_SECTION *)&mutex->lock);
 }
 
 void qemu_cond_init(QemuCond *cond)
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] w32: Replace Windows specific data types in common header files

Quoted 10 lines · Stefan Weil
include/qemu/thread-win32.h +2 -1
index 7ade61a..b8b8e61 100644
@@ -1,24 +1,35 @@
 #ifndef __QEMU_THREAD_WIN32_H
 #define __QEMU_THREAD_WIN32_H 1
-#include "qemu/winapi.h"
+
+/* WinCriticalSection is a substitute for CRITICAL_SECTION and

include/qemu/thread-win32.h line 14

Quoted 10 lines
include/qemu/thread-win32.h +10 -0
+ * introduced here to avoid dependencies on windows.h. */
+
+typedef struct {
+    WinHandle DebugInfo;
+    WinLong LockCount;
+    WinLong RecursionCount;
+    WinHandle OwningThread;
+    WinHandle LockSemaphore;
+    WinULong *SpinCount;
+} WinCriticalSection;

This is taking it a bit far. Avoiding includes for the scalar types seems okay but duplicating struct definitions makes me wonder how far we'll go to reduce compile times. (Plus wouldn't we have the same kind of copyright/license issues that mingw and other projects need to be very careful about when reimplementing Windows headers?)

I guess the problem is that qemu-thread.h is included in a lot of places and you wish to avoid including <windows.h>. Still, I would leave this one out.

Stefan

#8 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] w32: Replace Windows specific data types in common header files

Am 10.03.2014 16:17, schrieb Stefan Hajnoczi:

Quoted 19 lines · Stefan Weil
Quoted 18 lines
include/qemu/thread-win32.h +10 -1
index 7ade61a..b8b8e61 100644
@@ -1,24 +1,35 @@
 #ifndef __QEMU_THREAD_WIN32_H
 #define __QEMU_THREAD_WIN32_H 1
-#include "qemu/winapi.h"
+
+/* WinCriticalSection is a substitute for CRITICAL_SECTION and
+ * introduced here to avoid dependencies on windows.h. */
+
+typedef struct {
+    WinHandle DebugInfo;
+    WinLong LockCount;
+    WinLong RecursionCount;
+    WinHandle OwningThread;
+    WinHandle LockSemaphore;
Quoted 8 lines

include/qemu/thread-win32.h line 14

Quoted 2 lines
include/qemu/thread-win32.h +2 -0
+    WinULong *SpinCount;
+} WinCriticalSection;

This is taking it a bit far. Avoiding includes for the scalar types seems okay but duplicating struct definitions makes me wonder how far we'll go to reduce compile times. (Plus wouldn't we have the same kind of copyright/license issues that mingw and other projects need to be very careful about when reimplementing Windows headers?)

I don't think that we have a copyright or license issue here because we don't use names which were invented by MS. They have a copyright on win32 (that's why I typically use w32 or wxx), but not on WinXXX AFAIK. Our existing files with names using win32 might be a problem, although I doubt that anybody will complain.

Instead of defining a struct, WinCriticalSection could also be an array which is sufficiently large to store a critical section and which uses the correct alignment. Do you think that would be a better solution?

Quoted 6 lines

I guess the problem is that qemu-thread.h is included in a lot of places and you wish to avoid including <windows.h>. Still, I would leave this one out.

Stefan

As you noticed, the problem is include/qemu/thread.h which includes include/qemu/thread-win32.h to define QemuMutex. Unfortunately, QemuMutex is used by value in include/block/aio.h and in include/exec/cpu-all.h. Most QEMU files depend on these two files. Breaking the dependencies of all those files on windows.h is essential for my patch series. I see only three solutions:

* Leave the code as it is. That implies long compile time for MinGW builds and name space pollution because nearly every compilation needs windows.h. This last point is the reason for two existing hacks and one more hack which is still needed (both Peter and I sent patches for it), and we have a realistic chance to need future hacks from time to time.

* Break the dependency on windows.h by using QEMU data types instead of Windows API data types.

* Break the dependency on windows.h by avoiding the use of certain QEMU data types (especially QemuMutex) by value, because those QEMU data types use Windows data types.

I must admit that I tried that third solution and gave up after a while.

What do you suggest to do? For me, any of the three alternatives is fine. I have no personal use for QEMU on Windows, nor do I need it for my professional work any longer.

Stefan

Reviewed-by
#9 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] w32: Replace Windows specific data types in common header files

Quoted 67 lines · Stefan Weil

Am 10.03.2014 16:17, schrieb Stefan Hajnoczi:

Quoted 19 lines · Stefan Weil
Quoted 18 lines
include/qemu/thread-win32.h +10 -1
index 7ade61a..b8b8e61 100644
@@ -1,24 +1,35 @@
 #ifndef __QEMU_THREAD_WIN32_H
 #define __QEMU_THREAD_WIN32_H 1
-#include "qemu/winapi.h"
+
+/* WinCriticalSection is a substitute for CRITICAL_SECTION and
+ * introduced here to avoid dependencies on windows.h. */
+
+typedef struct {
+    WinHandle DebugInfo;
+    WinLong LockCount;
+    WinLong RecursionCount;
+    WinHandle OwningThread;
+    WinHandle LockSemaphore;
Quoted 8 lines

include/qemu/thread-win32.h line 14

Quoted 2 lines
include/qemu/thread-win32.h +2 -0
+    WinULong *SpinCount;
+} WinCriticalSection;

This is taking it a bit far. Avoiding includes for the scalar types seems okay but duplicating struct definitions makes me wonder how far we'll go to reduce compile times. (Plus wouldn't we have the same kind of copyright/license issues that mingw and other projects need to be very careful about when reimplementing Windows headers?)

I don't think that we have a copyright or license issue here because we don't use names which were invented by MS. They have a copyright on win32 (that's why I typically use w32 or wxx), but not on WinXXX AFAIK. Our existing files with names using win32 might be a problem, although I doubt that anybody will complain.

Instead of defining a struct, WinCriticalSection could also be an array which is sufficiently large to store a critical section and which uses the correct alignment. Do you think that would be a better solution?

Quoted 6 lines

I guess the problem is that qemu-thread.h is included in a lot of places and you wish to avoid including <windows.h>. Still, I would leave this one out.

Stefan

As you noticed, the problem is include/qemu/thread.h which includes include/qemu/thread-win32.h to define QemuMutex. Unfortunately, QemuMutex is used by value in include/block/aio.h and in include/exec/cpu-all.h. Most QEMU files depend on these two files. Breaking the dependencies of all those files on windows.h is essential for my patch series. I see only three solutions:

* Leave the code as it is. That implies long compile time for MinGW builds and name space pollution because nearly every compilation needs windows.h. This last point is the reason for two existing hacks and one more hack which is still needed (both Peter and I sent patches for it), and we have a realistic chance to need future hacks from time to time.

* Break the dependency on windows.h by using QEMU data types instead of Windows API data types.

* Break the dependency on windows.h by avoiding the use of certain QEMU data types (especially QemuMutex) by value, because those QEMU data types use Windows data types.

Quoted 5 lines

I must admit that I tried that third solution and gave up after a while.

What do you suggest to do? For me, any of the three alternatives is fine. I have no personal use for QEMU on Windows, nor do I need it for my professional work any longer.

I don't see a hard reason against merging this series. There are trade-offs but you have thought through the alternatives.

Quoted 75 lines · Stefan Weil

Am 10.03.2014 16:17, schrieb Stefan Hajnoczi:

Quoted 19 lines · Stefan Weil
Quoted 18 lines
include/qemu/thread-win32.h +10 -1
index 7ade61a..b8b8e61 100644
@@ -1,24 +1,35 @@
 #ifndef __QEMU_THREAD_WIN32_H
 #define __QEMU_THREAD_WIN32_H 1
-#include "qemu/winapi.h"
+
+/* WinCriticalSection is a substitute for CRITICAL_SECTION and
+ * introduced here to avoid dependencies on windows.h. */
+
+typedef struct {
+    WinHandle DebugInfo;
+    WinLong LockCount;
+    WinLong RecursionCount;
+    WinHandle OwningThread;
+    WinHandle LockSemaphore;
Quoted 8 lines

include/qemu/thread-win32.h line 14

Quoted 2 lines
include/qemu/thread-win32.h +2 -0
+    WinULong *SpinCount;
+} WinCriticalSection;

This is taking it a bit far. Avoiding includes for the scalar types seems okay but duplicating struct definitions makes me wonder how far we'll go to reduce compile times. (Plus wouldn't we have the same kind of copyright/license issues that mingw and other projects need to be very careful about when reimplementing Windows headers?)

I don't think that we have a copyright or license issue here because we don't use names which were invented by MS. They have a copyright on win32 (that's why I typically use w32 or wxx), but not on WinXXX AFAIK. Our existing files with names using win32 might be a problem, although I doubt that anybody will complain.

Instead of defining a struct, WinCriticalSection could also be an array which is sufficiently large to store a critical section and which uses the correct alignment. Do you think that would be a better solution?

Quoted 6 lines

I guess the problem is that qemu-thread.h is included in a lot of places and you wish to avoid including <windows.h>. Still, I would leave this one out.

Stefan

As you noticed, the problem is include/qemu/thread.h which includes include/qemu/thread-win32.h to define QemuMutex. Unfortunately, QemuMutex is used by value in include/block/aio.h and in include/exec/cpu-all.h. Most QEMU files depend on these two files. Breaking the dependencies of all those files on windows.h is essential for my patch series. I see only three solutions:

* Leave the code as it is. That implies long compile time for MinGW builds and name space pollution because nearly every compilation needs windows.h. This last point is the reason for two existing hacks and one more hack which is still needed (both Peter and I sent patches for it), and we have a realistic chance to need future hacks from time to time.

* Break the dependency on windows.h by using QEMU data types instead of Windows API data types.

* Break the dependency on windows.h by avoiding the use of certain QEMU data types (especially QemuMutex) by value, because those QEMU data types use Windows data types.

I must admit that I tried that third solution and gave up after a while.

What do you suggest to do? For me, any of the three alternatives is fine. I have no personal use for QEMU on Windows, nor do I need it for my professional work any longer.

Stefan

#10 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] w32: Replace Windows specific data types in common header files

In reply to Stefan Weil

Il 10/03/2014 19:34, Stefan Weil ha scritto:

Quoted 20 lines

As you noticed, the problem is include/qemu/thread.h which includes include/qemu/thread-win32.h to define QemuMutex. Unfortunately, QemuMutex is used by value in include/block/aio.h and in include/exec/cpu-all.h. Most QEMU files depend on these two files. Breaking the dependencies of all those files on windows.h is essential for my patch series. I see only three solutions:

* Leave the code as it is. That implies long compile time for MinGW builds and name space pollution because nearly every compilation needs windows.h. This last point is the reason for two existing hacks and one more hack which is still needed (both Peter and I sent patches for it), and we have a realistic chance to need future hacks from time to time.

* Break the dependency on windows.h by using QEMU data types instead of Windows API data types.

* Break the dependency on windows.h by avoiding the use of certain QEMU data types (especially QemuMutex) by value, because those QEMU data types use Windows data types.

Quoted 5 lines

I must admit that I tried that third solution and gave up after a while.

What do you suggest to do? For me, any of the three alternatives is fine. I have no personal use for QEMU on Windows, nor do I need it for my professional work any longer.

Any solution is fine. I don't like particularly this series, but I don't dislike it enough to override your judgement as Win32 maintainer.

I agree with Markus about not liking the comments on the #include lines, though.

Paolo

#11 lore raw

PATCH v2 5/5 block: Review include statements for winioctl.h

In reply to Stefan Weil

block/win32-aio.c does not need it.
Add a comment why it is needed to block/raw-win32.c.

Cc Kevin Wolf <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
---
 block/raw-win32.c |    2 +-
 block/win32-aio.c |    1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
block/raw-win32.c +1 -1
index 95b27a5..2231a30 100644
@@ -30,7 +30,7 @@
 #include "trace.h"
 #include "block/thread-pool.h"
 #include "qemu/iov.h"
-#include <winioctl.h>
+#include <winioctl.h>           /* FSCTL_SET_SPARSE, ... */
 
 #define FTYPE_FILE 0
 #define FTYPE_CD     1
block/win32-aio.c +0 -1
index dbcc6bc..a10c16f 100644
@@ -30,7 +30,6 @@
 #include "raw-aio.h"
 #include "qemu/event_notifier.h"
 #include "qemu/iov.h"
-#include <winioctl.h>
 
 #define FTYPE_FILE 0
 #define FTYPE_CD     1
Signature · 1 line
1.7.10.4
#12 lore raw
In reply to Stefan Weil
Quoted 20 lines · Stefan Weil

The first 4 patches reduce the number of files which depend on windows.h by about 90 percent.

This reduces the compilation time, allows removing some hacks and avoids name space pollution.

Patch 5 is optional and new here.

Changes in v2:

* Change name of new include file include/qemu/winapi.h (suggested by Paolo Bonzini)

* Don't replace Win32 data types in block/raw-aio.h (suggested by Kevin Wolf)

* Replace Win32 data types in central header files by new QEMU data types instead of basic C data types (suggested by Peter Maydell and Stefan Hajnoczi)

Quoted 5 lines

[PATCH v2 1/5] w32: Add and use intermediate include file for windows.h [PATCH v2 2/5] w32: Move inline function from header file to C source [PATCH v2 3/5] w32: Reduce dependencies in sysemu/os-win32.h [PATCH v2 4/5] w32: Replace Windows specific data types in common [PATCH v2 5/5] block: Review include statements for winioctl.h

Personally, I think the code was nicer without adding this indirection to avoid longer compile times. But then you're one of the few people who cares about Windows, so if it makes your life easier that's a good thing.

Stefan