aboutsummaryrefslogtreecommitdiff
path: root/src/backend/port/win32/mingwcompat.c
blob: e12aca3d708ffedf30df860885d3d233694c1a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*-------------------------------------------------------------------------
 *
 * mingwcompat.c
 *	  MinGW compatibility functions
 *
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/backend/port/win32/mingwcompat.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#ifndef WIN32_ONLY_COMPILER
/*
 * MingW defines an extern to this struct, but the actual struct isn't present
 * in any library. It's trivial enough that we can safely define it
 * ourselves.
 */
const struct in6_addr in6addr_any = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};


/*
 * This file contains loaders for functions that are missing in the MinGW
 * import libraries. It's only for actual Win32 API functions, so they are
 * all present in proper Win32 compilers.
 */
static HMODULE kernel32 = NULL;

/*
 * Load DLL file just once regardless of how many functions
 * we load/call in it.
 */
static void
LoadKernel32()
{
	if (kernel32 != NULL)
		return;

	kernel32 = LoadLibraryEx("kernel32.dll", NULL, 0);
	if (kernel32 == NULL)
		ereport(FATAL,
				(errmsg_internal("could not load kernel32.dll: %d",
								 (int) GetLastError())));
}


/*
 * Replacement for RegisterWaitForSingleObject(), which lives in
 * kernel32.dll
 */
typedef
BOOL		(WINAPI * __RegisterWaitForSingleObject)
			(PHANDLE, HANDLE, WAITORTIMERCALLBACK, PVOID, ULONG, ULONG);
static __RegisterWaitForSingleObject _RegisterWaitForSingleObject = NULL;

BOOL		WINAPI
RegisterWaitForSingleObject(PHANDLE phNewWaitObject,
							HANDLE hObject,
							WAITORTIMERCALLBACK Callback,
							PVOID Context,
							ULONG dwMilliseconds,
							ULONG dwFlags)
{
	if (_RegisterWaitForSingleObject == NULL)
	{
		LoadKernel32();

		_RegisterWaitForSingleObject = (__RegisterWaitForSingleObject)
			GetProcAddress(kernel32, "RegisterWaitForSingleObject");

		if (_RegisterWaitForSingleObject == NULL)
			ereport(FATAL,
					(errmsg_internal("could not locate RegisterWaitForSingleObject in kernel32.dll: %d",
									 (int) GetLastError())));
	}

	return (_RegisterWaitForSingleObject)
		(phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags);
}

#endif