mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 13:20:27 -06:00
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.
This commit is contained in:
@ -4,9 +4,11 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <libusb.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include "Common/Flag.h"
|
||||
#include "Common/LibusbContext.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/Thread.h"
|
||||
#include "Core/ConfigManager.h"
|
||||
@ -50,7 +52,7 @@ static Common::Flag s_adapter_detect_thread_running;
|
||||
static std::function<void(void)> s_detect_callback;
|
||||
|
||||
static bool s_libusb_driver_not_supported = false;
|
||||
static libusb_context* s_libusb_context = nullptr;
|
||||
static std::shared_ptr<libusb_context> s_libusb_context;
|
||||
#if defined(__FreeBSD__) && __FreeBSD__ >= 11
|
||||
static bool s_libusb_hotplug_enabled = true;
|
||||
#else
|
||||
@ -116,8 +118,8 @@ static void ScanThreadFunc()
|
||||
if (s_libusb_hotplug_enabled)
|
||||
{
|
||||
if (libusb_hotplug_register_callback(
|
||||
s_libusb_context, (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
|
||||
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
|
||||
s_libusb_context.get(), (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
|
||||
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
|
||||
LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback,
|
||||
nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS)
|
||||
s_libusb_hotplug_enabled = false;
|
||||
@ -131,7 +133,7 @@ static void ScanThreadFunc()
|
||||
if (s_libusb_hotplug_enabled)
|
||||
{
|
||||
static timeval tv = {0, 500000};
|
||||
libusb_handle_events_timeout(s_libusb_context, &tv);
|
||||
libusb_handle_events_timeout(s_libusb_context.get(), &tv);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -168,11 +170,9 @@ void Init()
|
||||
|
||||
s_libusb_driver_not_supported = false;
|
||||
|
||||
int ret = libusb_init(&s_libusb_context);
|
||||
|
||||
if (ret)
|
||||
s_libusb_context = LibusbContext::Get();
|
||||
if (!s_libusb_context)
|
||||
{
|
||||
ERROR_LOG(SERIALINTERFACE, "libusb_init failed with error: %d", ret);
|
||||
s_libusb_driver_not_supported = true;
|
||||
Shutdown();
|
||||
}
|
||||
@ -203,7 +203,7 @@ void StopScanThread()
|
||||
static void Setup()
|
||||
{
|
||||
libusb_device** list;
|
||||
ssize_t cnt = libusb_get_device_list(s_libusb_context, &list);
|
||||
ssize_t cnt = libusb_get_device_list(s_libusb_context.get(), &list);
|
||||
|
||||
for (int i = 0; i < MAX_SI_CHANNELS; i++)
|
||||
{
|
||||
@ -335,16 +335,11 @@ void Shutdown()
|
||||
StopScanThread();
|
||||
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
||||
if (s_libusb_hotplug_enabled)
|
||||
libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle);
|
||||
libusb_hotplug_deregister_callback(s_libusb_context.get(), s_hotplug_handle);
|
||||
#endif
|
||||
Reset();
|
||||
|
||||
if (s_libusb_context)
|
||||
{
|
||||
libusb_exit(s_libusb_context);
|
||||
s_libusb_context = nullptr;
|
||||
}
|
||||
|
||||
s_libusb_context.reset();
|
||||
s_libusb_driver_not_supported = false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user