mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-31 01:59:52 -06:00
Revert "Use a single libusb context"
This reverts commit c8a6dc6c23
.
libusb on Windows isn't really safe to use from different threads
with a single context.
This commit is contained in:
@ -15,7 +15,6 @@
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/ChunkFile.h"
|
||||
#include "Common/LibusbContext.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/MsgHandler.h"
|
||||
#include "Common/Network.h"
|
||||
@ -60,7 +59,11 @@ namespace Device
|
||||
BluetoothReal::BluetoothReal(u32 device_id, const std::string& device_name)
|
||||
: BluetoothBase(device_id, device_name)
|
||||
{
|
||||
m_libusb_context = LibusbContext::Get();
|
||||
const int ret = libusb_init(&m_libusb_context);
|
||||
if (ret < 0)
|
||||
{
|
||||
PanicAlertT("Couldn't initialise libusb for Bluetooth passthrough: %s", libusb_error_name(ret));
|
||||
}
|
||||
LoadLinkKeys();
|
||||
}
|
||||
|
||||
@ -77,6 +80,7 @@ BluetoothReal::~BluetoothReal()
|
||||
libusb_unref_device(m_device);
|
||||
}
|
||||
|
||||
libusb_exit(m_libusb_context);
|
||||
SaveLinkKeys();
|
||||
}
|
||||
|
||||
@ -86,7 +90,7 @@ ReturnCode BluetoothReal::Open(const OpenRequest& request)
|
||||
return IPC_EACCES;
|
||||
|
||||
libusb_device** list;
|
||||
const ssize_t cnt = libusb_get_device_list(m_libusb_context.get(), &list);
|
||||
const ssize_t cnt = libusb_get_device_list(m_libusb_context, &list);
|
||||
if (cnt < 0)
|
||||
{
|
||||
ERROR_LOG(IOS_WIIMOTE, "Couldn't get device list: %s",
|
||||
@ -607,7 +611,7 @@ void BluetoothReal::TransferThread()
|
||||
Common::SetCurrentThreadName("BT USB Thread");
|
||||
while (m_thread_running.IsSet())
|
||||
{
|
||||
libusb_handle_events_completed(m_libusb_context.get(), nullptr);
|
||||
libusb_handle_events_completed(m_libusb_context, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ private:
|
||||
|
||||
libusb_device* m_device = nullptr;
|
||||
libusb_device_handle* m_handle = nullptr;
|
||||
std::shared_ptr<libusb_context> m_libusb_context;
|
||||
libusb_context* m_libusb_context = nullptr;
|
||||
|
||||
Common::Flag m_thread_running;
|
||||
std::thread m_thread;
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/ChunkFile.h"
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/LibusbContext.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/Thread.h"
|
||||
#include "Core/ConfigManager.h"
|
||||
@ -30,6 +29,18 @@ namespace Device
|
||||
{
|
||||
USBHost::USBHost(u32 device_id, const std::string& device_name) : Device(device_id, device_name)
|
||||
{
|
||||
#ifdef __LIBUSB__
|
||||
const int ret = libusb_init(&m_libusb_context);
|
||||
_dbg_assert_msg_(IOS_USB, ret == 0, "Failed to init libusb for USB passthrough.");
|
||||
#endif
|
||||
}
|
||||
|
||||
USBHost::~USBHost()
|
||||
{
|
||||
#ifdef __LIBUSB__
|
||||
if (m_libusb_context)
|
||||
libusb_exit(m_libusb_context);
|
||||
#endif
|
||||
}
|
||||
|
||||
ReturnCode USBHost::Open(const OpenRequest& request)
|
||||
@ -116,11 +127,10 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
|
||||
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||
return true;
|
||||
|
||||
auto libusb_context = LibusbContext::Get();
|
||||
if (libusb_context)
|
||||
if (m_libusb_context)
|
||||
{
|
||||
libusb_device** list;
|
||||
const ssize_t count = libusb_get_device_list(libusb_context.get(), &list);
|
||||
const ssize_t count = libusb_get_device_list(m_libusb_context, &list);
|
||||
if (count < 0)
|
||||
{
|
||||
WARN_LOG(IOS_USB, "Failed to get device list: %s",
|
||||
@ -200,12 +210,11 @@ void USBHost::StartThreads()
|
||||
}
|
||||
|
||||
#ifdef __LIBUSB__
|
||||
if (!m_event_thread_running.IsSet())
|
||||
if (!m_event_thread_running.IsSet() && m_libusb_context)
|
||||
{
|
||||
m_event_thread_running.Set();
|
||||
m_event_thread = std::thread([this] {
|
||||
Common::SetCurrentThreadName("USB Passthrough Thread");
|
||||
std::shared_ptr<libusb_context> context;
|
||||
while (m_event_thread_running.IsSet())
|
||||
{
|
||||
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||
@ -214,15 +223,8 @@ void USBHost::StartThreads()
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!context)
|
||||
context = LibusbContext::Get();
|
||||
|
||||
// If we failed to get a libusb context, stop the thread.
|
||||
if (!context)
|
||||
return;
|
||||
|
||||
static timeval tv = {0, 50000};
|
||||
libusb_handle_events_timeout_completed(context.get(), &tv, nullptr);
|
||||
libusb_handle_events_timeout_completed(m_libusb_context, &tv, nullptr);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class USBHost : public Device
|
||||
{
|
||||
public:
|
||||
USBHost(u32 device_id, const std::string& device_name);
|
||||
virtual ~USBHost() = default;
|
||||
virtual ~USBHost();
|
||||
|
||||
ReturnCode Open(const OpenRequest& request) override;
|
||||
|
||||
@ -71,6 +71,8 @@ private:
|
||||
void DispatchHooks(const DeviceChangeHooks& hooks);
|
||||
|
||||
#ifdef __LIBUSB__
|
||||
libusb_context* m_libusb_context = nullptr;
|
||||
|
||||
// Event thread for libusb
|
||||
Common::Flag m_event_thread_running;
|
||||
std::thread m_event_thread;
|
||||
|
Reference in New Issue
Block a user