build: Merge events and threads into single platform abstraction

The split between events and threads abstractions is unnecessary.
Simplify the library by merging the two into a "platform" abstraction.
The only meaningful change is that Cygwin builds will no longer use the
POSIX threads abstraction but will instead use the native Windows one.

The downside to doing this is that the dpfp_threaded example program
will no longer be available on Cygwin builds. This should be fine, and
future work will make dpfp_threaded available for all forms of Windows
build systems.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
diff --git a/Xcode/config.h b/Xcode/config.h
index 3385553..cbb7d26 100644
--- a/Xcode/config.h
+++ b/Xcode/config.h
@@ -8,9 +8,6 @@
 /* Define to 1 to enable message logging. */
 #define ENABLE_LOGGING 1
 
-/* Define to 1 if using the POSIX events abstraction. */
-#define EVENTS_POSIX 1
-
 /* On 10.12 and later, use newly available clock_*() functions */
 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
 /* Define to 1 if you have the `clock_gettime' function. */
@@ -29,8 +26,8 @@
 /* Define to 1 if you have the <sys/time.h> header file. */
 #define HAVE_SYS_TIME_H 1
 
-/* Define to 1 if using POSIX threads. */
-#define THREADS_POSIX 1
+/* Define to 1 if compiling for a POSIX platform. */
+#define PLATFORM_POSIX 1
 
 /* Enable GNU extensions. */
 #define _GNU_SOURCE 1
diff --git a/android/config.h b/android/config.h
index 4a4b995..12172f6 100644
--- a/android/config.h
+++ b/android/config.h
@@ -26,9 +26,6 @@
 /* Define to 1 to enable message logging. */
 #define ENABLE_LOGGING 1
 
-/* Define to 1 if using the POSIX events abstraction. */
-#define EVENTS_POSIX 1
-
 /* Define to 1 if you have the <asm/types.h> header file. */
 #define HAVE_ASM_TYPES_H 1
 
@@ -44,8 +41,8 @@
 /* Define to 1 if you have the <sys/time.h> header file. */
 #define HAVE_SYS_TIME_H 1
 
-/* Define to 1 if using POSIX threads. */
-#define THREADS_POSIX 1
+/* Define to 1 if compiling for a POSIX platform. */
+#define PLATFORM_POSIX 1
 
 /* Define to 1 to output logging messages to the systemwide log. */
 #define USE_SYSTEM_LOGGING_FACILITY 1
diff --git a/configure.ac b/configure.ac
index 5984380..a5df4df 100644
--- a/configure.ac
+++ b/configure.ac
@@ -75,14 +75,12 @@
 *-darwin*)
 	AC_MSG_RESULT([Darwin/Mac OS X])
 	backend=darwin
-	events=posix
-	threads=posix
+	platform=posix
 	;;
 *-haiku*)
 	AC_MSG_RESULT([Haiku])
 	backend=haiku
-	events=posix
-	threads=posix
+	platform=posix
 	;;
 *-linux* | *-uclinux*)
 	dnl on Android Linux, some functions are in different places
@@ -96,63 +94,49 @@
 		;;
 	esac
 	backend=linux
-	events=posix
-	threads=posix
+	platform=posix
 	;;
 *-netbsd*)
 	AC_MSG_RESULT([NetBSD])
 	backend=netbsd
-	events=posix
-	threads=posix
+	platform=posix
 	;;
 *-openbsd*)
 	AC_MSG_RESULT([OpenBSD])
 	backend=openbsd
-	events=posix
-	threads=posix
+	platform=posix
 	;;
 *-solaris*)
 	AC_MSG_RESULT([SunOS])
 	backend=sunos
-	events=posix
-	threads=posix
+	platform=posix
 	;;
 *-cygwin*)
 	AC_MSG_RESULT([Windows (using Cygwin)])
 	backend=windows
-	events=windows
-	threads=posix
+	platform=windows
+	EXTRA_CFLAGS="-mwin32"
 	;;
 *-mingw* | *msys*)
 	AC_MSG_RESULT([Windows])
 	backend=windows
-	events=windows
-	threads=windows
+	platform=windows
 	test "x$enable_shared" = xyes && create_import_lib=yes
-	EXTRA_CFLAGS="-fno-omit-frame-pointer"
+	EXTRA_CFLAGS="-mwin32 -fno-omit-frame-pointer"
 	;;
 *)
 	AC_MSG_RESULT([Null])
 	AC_MSG_WARN([The host being compiled for is not supported.])
 	AC_MSG_WARN([The library may compile but will not function in any useful manner.])
-	backend="null"
-	events=posix
-	threads="posix"
+	backend=null
+	platform=posix
 	;;
 esac
 
-if test "x$events" = xposix; then
-	AC_DEFINE([EVENTS_POSIX], [1], [Define to 1 if using the POSIX events abstraction.])
+if test "x$platform" = xposix; then
+	AC_DEFINE([PLATFORM_POSIX], [1], [Define to 1 if compiling for a POSIX platform.])
 	AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
 	AC_CHECK_FUNCS([pipe2])
-elif test "x$events" = xwindows; then
-	AC_DEFINE([EVENTS_WINDOWS], [1], [Define to 1 if using the Windows events abstraction.])
-else
-	AC_MSG_ERROR([Unknown events abstraction])
-fi
-
-if test "x$threads" = xposix; then
-	AC_DEFINE([THREADS_POSIX], [1], [Define to 1 if using POSIX threads.])
 	dnl Some compilers do not support the '-pthread' option so check for it here
 	saved_CFLAGS="${CFLAGS}"
 	CFLAGS="-Wall -Werror -pthread"
@@ -167,10 +151,10 @@
 	AC_SEARCH_LIBS([pthread_create], [pthread],
 		[test "x$ac_cv_search_pthread_create" != "xnone required" && AC_SUBST(THREAD_LIBS, [-lpthread])],
 		[], [])
-elif test "x$threads" = xwindows; then
-	AC_DEFINE([THREADS_WINDOWS], [1], [Define to 1 if using Windows threads.])
+elif test "x$platform" = xwindows; then
+	AC_DEFINE([PLATFORM_WINDOWS], [1], [Define to 1 if compiling for a Windows platform.])
 else
-	AC_MSG_ERROR([Unknown threads implementation])
+	AC_MSG_ERROR([Unknown platform])
 fi
 
 case $backend in
@@ -203,9 +187,6 @@
 windows)
 	AC_CHECK_TYPES([struct timespec], [], [], [[#include <time.h>]])
 	AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Define to the oldest supported Windows version.])
-	dnl Cygwin and MSYS compilers do not define _WIN32 as MinGW and MSVC do
-	dnl simplify checks for Windows compilation by ensuring it is always defined
-	EXTRA_CPPFLAGS="-D_WIN32"
 	LT_LDFLAGS="${LT_LDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
 	;;
 *)
@@ -352,8 +333,6 @@
 AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != xno])
 AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno])
 AM_CONDITIONAL([CREATE_IMPORT_LIB], [test "x$create_import_lib" = xyes])
-AM_CONDITIONAL([EVENTS_POSIX], [test "x$events" = xposix])
-AM_CONDITIONAL([EVENTS_WINDOWS], [test "x$events" = xwindows])
 AM_CONDITIONAL([HAVE_SIGACTION], [test "x$have_sigaction" = xyes])
 AM_CONDITIONAL([OS_DARWIN], [test "x$backend" = xdarwin])
 AM_CONDITIONAL([OS_HAIKU], [test "x$backend" = xhaiku])
@@ -363,8 +342,8 @@
 AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
 AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
 AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
-AM_CONDITIONAL([THREADS_POSIX], [test "x$threads" = xposix])
-AM_CONDITIONAL([THREADS_WINDOWS], [test "x$threads" = xwindows])
+AM_CONDITIONAL([PLATFORM_POSIX], [test "x$platform" = xposix])
+AM_CONDITIONAL([PLATFORM_WINDOWS], [test "x$platform" = xwindows])
 AM_CONDITIONAL([USE_UDEV], [test "x$use_udev" = xyes])
 
 dnl The -Wcast-function-type warning causes a flurry of warnings when compiling
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 2658490..db757b0 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -8,7 +8,7 @@
 
 if HAVE_SIGACTION
 noinst_PROGRAMS += dpfp
-if THREADS_POSIX
+if PLATFORM_POSIX
 noinst_PROGRAMS += dpfp_threaded
 dpfp_threaded_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
 dpfp_threaded_LDADD = $(LDADD) $(THREAD_LIBS)
diff --git a/libusb/Makefile.am b/libusb/Makefile.am
index 72cd502..c78006e 100644
--- a/libusb/Makefile.am
+++ b/libusb/Makefile.am
@@ -5,22 +5,15 @@
 
 lib_LTLIBRARIES = libusb-1.0.la
 
-POSIX_EVENTS_SRC = os/events_posix.h os/events_posix.c
-WINDOWS_EVENTS_SRC = os/events_windows.h os/events_windows.c
+POSIX_PLATFORM_SRC = os/events_posix.h os/events_posix.c \
+		     os/threads_posix.h os/threads_posix.c
+WINDOWS_PLATFORM_SRC = os/events_windows.h os/events_windows.c \
+		       os/threads_windows.h os/threads_windows.c
 
-if EVENTS_POSIX
-EVENTS_SRC = $(POSIX_EVENTS_SRC)
+if PLATFORM_POSIX
+PLATFORM_SRC = $(POSIX_PLATFORM_SRC)
 else
-EVENTS_SRC = $(WINDOWS_EVENTS_SRC)
-endif
-
-POSIX_THREADS_SRC = os/threads_posix.h os/threads_posix.c
-WINDOWS_THREADS_SRC = os/threads_windows.h os/threads_windows.c
-
-if THREADS_POSIX
-THREADS_SRC = $(POSIX_THREADS_SRC)
-else
-THREADS_SRC = $(WINDOWS_THREADS_SRC)
+PLATFORM_SRC = $(WINDOWS_PLATFORM_SRC)
 endif
 
 OS_DARWIN_SRC = os/darwin_usb.h os/darwin_usb.c
@@ -90,6 +83,6 @@
 libusb_1_0_la_LDFLAGS = $(LT_LDFLAGS)
 libusb_1_0_la_SOURCES = libusbi.h version.h version_nano.h \
 	core.c descriptor.c hotplug.h hotplug.c io.c strerror.c sync.c \
-	$(EVENTS_SRC) $(THREADS_SRC) $(OS_SRC)
+	$(PLATFORM_SRC) $(OS_SRC)
 
 pkginclude_HEADERS = libusb.h
diff --git a/libusb/io.c b/libusb/io.c
index 9db322b..ebad383 100644
--- a/libusb/io.c
+++ b/libusb/io.c
@@ -2628,7 +2628,7 @@
 	libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
 	void *user_data)
 {
-#if !defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(PLATFORM_WINDOWS)
 	ctx = usbi_get_context(ctx);
 	ctx->fd_added_cb = added_cb;
 	ctx->fd_removed_cb = removed_cb;
@@ -2676,7 +2676,7 @@
 	usbi_event_source_notification(ctx);
 	usbi_mutex_unlock(&ctx->event_data_lock);
 
-#if !defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(PLATFORM_WINDOWS)
 	if (ctx->fd_added_cb)
 		ctx->fd_added_cb(os_handle, poll_events, ctx->fd_cb_user_data);
 #endif
@@ -2710,7 +2710,7 @@
 	usbi_event_source_notification(ctx);
 	usbi_mutex_unlock(&ctx->event_data_lock);
 
-#if !defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(PLATFORM_WINDOWS)
 	if (ctx->fd_removed_cb)
 		ctx->fd_removed_cb(os_handle, ctx->fd_cb_user_data);
 #endif
@@ -2735,7 +2735,7 @@
 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
 	libusb_context *ctx)
 {
-#if !defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(PLATFORM_WINDOWS)
 	struct libusb_pollfd **ret = NULL;
 	struct usbi_event_source *ievent_source;
 	size_t i;
@@ -2782,7 +2782,7 @@
  */
 void API_EXPORTED libusb_free_pollfds(const struct libusb_pollfd **pollfds)
 {
-#if !defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(PLATFORM_WINDOWS)
 	free((void *)pollfds);
 #else
 	UNUSED(pollfds);
diff --git a/libusb/libusbi.h b/libusb/libusbi.h
index da45121..e8a0f35 100644
--- a/libusb/libusbi.h
+++ b/libusb/libusbi.h
@@ -74,17 +74,12 @@
 #define PTR_ALIGN(v) \
 	(((v) + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1))
 
-/* Internal abstraction for event handling */
-#if defined(EVENTS_POSIX)
+/* Internal abstractions for event handling and thread synchronization */
+#if defined(PLATFORM_POSIX)
 #include "os/events_posix.h"
-#elif defined(EVENTS_WINDOWS)
-#include "os/events_windows.h"
-#endif
-
-/* Internal abstraction for thread synchronization */
-#if defined(THREADS_POSIX)
 #include "os/threads_posix.h"
-#elif defined(THREADS_WINDOWS)
+#elif defined(PLATFORM_WINDOWS)
+#include "os/events_windows.h"
 #include "os/threads_windows.h"
 #endif
 
@@ -248,7 +243,7 @@
 		}							\
 	} while (0)
 
-#if defined(_WIN32)
+#if defined(PLATFORM_WINDOWS)
 #define TIMEVAL_TV_SEC_TYPE	long
 #else
 #define TIMEVAL_TV_SEC_TYPE	time_t
@@ -345,7 +340,7 @@
 	 * take this lock first */
 	usbi_mutex_t flying_transfers_lock;
 
-#if !defined(_WIN32) && !defined(__CYGWIN__)
+#if !defined(PLATFORM_WINDOWS)
 	/* user callbacks for pollfd changes */
 	libusb_pollfd_added_cb fd_added_cb;
 	libusb_pollfd_removed_cb fd_removed_cb;
diff --git a/libusb/os/threads_posix.c b/libusb/os/threads_posix.c
index 71a404f..071e205 100644
--- a/libusb/os/threads_posix.c
+++ b/libusb/os/threads_posix.c
@@ -60,14 +60,10 @@
 
 int usbi_get_tid(void)
 {
-#ifndef _WIN32
 	static _Thread_local int tid;
 
 	if (tid)
 		return tid;
-#else
-	int tid;
-#endif
 
 #if defined(__ANDROID__)
 	tid = gettid();
@@ -94,8 +90,6 @@
 	tid = syscall(SYS_getthrid);
 #elif defined(__sun__)
 	tid = _lwp_self();
-#elif defined(_WIN32)
-	tid = (int)GetCurrentThreadId();
 #else
 	tid = -1;
 #endif
diff --git a/libusb/os/windows_common.h b/libusb/os/windows_common.h
index 3de4f02..9f88956 100644
--- a/libusb/os/windows_common.h
+++ b/libusb/os/windows_common.h
@@ -47,7 +47,7 @@
 #define ULONG_CAST(x)	((unsigned long)(x))
 #endif
 
-#if defined(__CYGWIN__ )
+#if defined(__CYGWIN__)
 #define _stricmp strcasecmp
 #define _strdup strdup
 // _beginthreadex is MSVCRT => unavailable for cygwin. Fallback to using CreateThread
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
index a2be5c2..41b9eb4 100644
--- a/libusb/version_nano.h
+++ b/libusb/version_nano.h
@@ -1 +1 @@
-#define LIBUSB_NANO 11553
+#define LIBUSB_NANO 11554
diff --git a/msvc/config.h b/msvc/config.h
index 2f6eaf0..2e0a01a 100644
--- a/msvc/config.h
+++ b/msvc/config.h
@@ -47,11 +47,8 @@
 /* Define to 1 to enable message logging. */
 #define ENABLE_LOGGING 1
 
-/* Define to 1 if using the Windows events abstraction. */
-#define EVENTS_WINDOWS 1
-
-/* Define to 1 if using Windows threads. */
-#define THREADS_WINDOWS 1
+/* Define to 1 if compiling for a Windows platform. */
+#define PLATFORM_WINDOWS 1
 
 /* Define to 1 to output logging messages to the systemwide log. */
 /* #undef USE_SYSTEM_LOGGING_FACILITY */