diff options
| author | Léo Lam <leo@innovatetechnologi.es> | 2017-01-15 20:50:26 +0100 |
|---|---|---|
| committer | Léo Lam <leo@innovatetechnologi.es> | 2017-02-05 11:36:48 +0100 |
| commit | c8a6dc6c23a5efe99b4bf937003ba3f2f7f8f200 (patch) | |
| tree | 44e1e1fac10808bad10e20a72c18c54777e5f1bf /Source/Core/Common/LibusbContext.cpp | |
| parent | 73e55ccf4472db8c85255dd97ebb3c7596d5201f (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/LibusbContext.cpp')
| -rw-r--r-- | Source/Core/Common/LibusbContext.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
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; +} +} |
