summaryrefslogtreecommitdiff
path: root/Source/Core/Common
diff options
context:
space:
mode:
authorLéo Lam <leo@innovatetechnologi.es>2017-01-15 20:50:26 +0100
committerLéo Lam <leo@innovatetechnologi.es>2017-02-05 11:36:48 +0100
commitc8a6dc6c23a5efe99b4bf937003ba3f2f7f8f200 (patch)
tree44e1e1fac10808bad10e20a72c18c54777e5f1bf /Source/Core/Common
parent73e55ccf4472db8c85255dd97ebb3c7596d5201f (diff)
Use a single libusb context
libusb on Windows is limited to only a single context. Trying to open more than one can cause device enumerations to fail randomly. libusb is thread-safe and we don't use the manual polling support (with `poll()`) so this should be safe.
Diffstat (limited to 'Source/Core/Common')
-rw-r--r--Source/Core/Common/CMakeLists.txt5
-rw-r--r--Source/Core/Common/Common.vcxproj4
-rw-r--r--Source/Core/Common/Common.vcxproj.filters2
-rw-r--r--Source/Core/Common/LibusbContext.cpp46
-rw-r--r--Source/Core/Common/LibusbContext.h15
5 files changed, 72 insertions, 0 deletions
diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt
index 2a5ff1d4d8..5f910a1670 100644
--- a/Source/Core/Common/CMakeLists.txt
+++ b/Source/Core/Common/CMakeLists.txt
@@ -34,6 +34,11 @@ set(SRCS Analytics.cpp
Crypto/ec.cpp
Logging/LogManager.cpp)
+if(LIBUSB_FOUND)
+ set(LIBS ${LIBS} ${LIBUSB_LIBRARIES})
+ set(SRCS ${SRCS} LibusbContext.cpp)
+endif(LIBUSB_FOUND)
+
if(ANDROID)
set(SRCS ${SRCS}
Logging/ConsoleListenerDroid.cpp)
diff --git a/Source/Core/Common/Common.vcxproj b/Source/Core/Common/Common.vcxproj
index 52f408e776..831dcad4a7 100644
--- a/Source/Core/Common/Common.vcxproj
+++ b/Source/Core/Common/Common.vcxproj
@@ -113,6 +113,7 @@
<ClInclude Include="Hash.h" />
<ClInclude Include="IniFile.h" />
<ClInclude Include="JitRegister.h" />
+ <ClInclude Include="LibusbContext.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="MD5.h" />
@@ -159,6 +160,9 @@
<ClCompile Include="Hash.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="JitRegister.cpp" />
+ <ClCompile Include="LibusbContext.cpp">
+ <DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
+ </ClCompile>
<ClCompile Include="Logging\ConsoleListenerWin.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="MD5.cpp" />
diff --git a/Source/Core/Common/Common.vcxproj.filters b/Source/Core/Common/Common.vcxproj.filters
index 7731482020..3c9b952742 100644
--- a/Source/Core/Common/Common.vcxproj.filters
+++ b/Source/Core/Common/Common.vcxproj.filters
@@ -45,6 +45,7 @@
<ClInclude Include="FPURoundMode.h" />
<ClInclude Include="Hash.h" />
<ClInclude Include="IniFile.h" />
+ <ClInclude Include="LibusbContext.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="MemArena.h" />
@@ -236,6 +237,7 @@
<ClCompile Include="FileUtil.cpp" />
<ClCompile Include="Hash.cpp" />
<ClCompile Include="IniFile.cpp" />
+ <ClCompile Include="LibusbContext.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="MemArena.cpp" />
<ClCompile Include="MemoryUtil.cpp" />
diff --git a/Source/Core/Common/LibusbContext.cpp b/Source/Core/Common/LibusbContext.cpp
new file mode 100644
index 0000000000..a2ed128200
--- /dev/null
+++ b/Source/Core/Common/LibusbContext.cpp
@@ -0,0 +1,46 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <libusb.h>
+#include <memory>
+#include <mutex>
+
+#include "Common/LibusbContext.h"
+#include "Common/MsgHandler.h"
+
+namespace LibusbContext
+{
+static std::shared_ptr<libusb_context> s_libusb_context;
+static std::once_flag s_context_initialized;
+
+static libusb_context* Create()
+{
+ libusb_context* context;
+ const int ret = libusb_init(&context);
+ if (ret < LIBUSB_SUCCESS)
+ {
+ bool is_windows = false;
+#ifdef _WIN32
+ is_windows = true;
+#endif
+ if (is_windows && ret == LIBUSB_ERROR_NOT_FOUND)
+ PanicAlertT("Failed to initialize libusb because usbdk is not installed.");
+ else
+ PanicAlertT("Failed to initialize libusb: %s", libusb_error_name(ret));
+ return nullptr;
+ }
+ return context;
+}
+
+std::shared_ptr<libusb_context> Get()
+{
+ std::call_once(s_context_initialized, []() {
+ s_libusb_context.reset(Create(), [](auto* context) {
+ if (context != nullptr)
+ libusb_exit(context);
+ });
+ });
+ return s_libusb_context;
+}
+}
diff --git a/Source/Core/Common/LibusbContext.h b/Source/Core/Common/LibusbContext.h
new file mode 100644
index 0000000000..99af12ba4c
--- /dev/null
+++ b/Source/Core/Common/LibusbContext.h
@@ -0,0 +1,15 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <memory>
+
+struct libusb_context;
+
+namespace LibusbContext
+{
+// libusb on Windows is limited to only a single context. Trying to open more
+// than one can cause issues with device enumerations.
+// libusb is thread-safe so this context can be safely used from different threads.
+std::shared_ptr<libusb_context> Get();
+}