Smarter std::thread ownership for HID clients

This commit is contained in:
Jack Andersen 2016-10-10 15:20:39 -10:00
parent 51a097da6b
commit 1877c546ac
2 changed files with 50 additions and 50 deletions

View File

@ -18,12 +18,12 @@ class HIDDeviceIOKit : public IHIDDevice
uint8_t m_usbIntfInPipe = 0; uint8_t m_usbIntfInPipe = 0;
uint8_t m_usbIntfOutPipe = 0; uint8_t m_usbIntfOutPipe = 0;
bool m_runningTransferLoop = false; bool m_runningTransferLoop = false;
const std::string& m_devPath; const std::string& m_devPath;
std::mutex m_initMutex; std::mutex m_initMutex;
std::condition_variable m_initCond; std::condition_variable m_initCond;
std::thread* m_thread; std::thread m_thread;
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length) bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
{ {
if (m_usbIntf) if (m_usbIntf)
@ -33,7 +33,7 @@ class HIDDeviceIOKit : public IHIDDevice
} }
return false; return false;
} }
size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length) size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length)
{ {
if (m_usbIntf) if (m_usbIntf)
@ -46,7 +46,7 @@ class HIDDeviceIOKit : public IHIDDevice
} }
return 0; return 0;
} }
static void _threadProcUSBLL(HIDDeviceIOKit* device) static void _threadProcUSBLL(HIDDeviceIOKit* device)
{ {
char thrName[128]; char thrName[128];
@ -54,7 +54,7 @@ class HIDDeviceIOKit : public IHIDDevice
pthread_setname_np(thrName); pthread_setname_np(thrName);
char errStr[256]; char errStr[256];
std::unique_lock<std::mutex> lk(device->m_initMutex); std::unique_lock<std::mutex> lk(device->m_initMutex);
/* Get the HID element's parent (USB interrupt transfer-interface) */ /* Get the HID element's parent (USB interrupt transfer-interface) */
io_iterator_t devIter; io_iterator_t devIter;
io_registry_entry_t devEntry = IORegistryEntryFromPath(kIOMasterPortDefault, device->m_devPath.c_str()); io_registry_entry_t devEntry = IORegistryEntryFromPath(kIOMasterPortDefault, device->m_devPath.c_str());
@ -77,7 +77,7 @@ class HIDDeviceIOKit : public IHIDDevice
device->m_initCond.notify_one(); device->m_initCond.notify_one();
return; return;
} }
/* IOKit Plugin COM interface (WTF Apple???) */ /* IOKit Plugin COM interface (WTF Apple???) */
IOCFPlugInInterface **iodev; IOCFPlugInInterface **iodev;
SInt32 score; SInt32 score;
@ -97,7 +97,7 @@ class HIDDeviceIOKit : public IHIDDevice
device->m_initCond.notify_one(); device->m_initCond.notify_one();
return; return;
} }
/* USB interface function-pointer table */ /* USB interface function-pointer table */
IOUSBInterfaceInterface** intf = NULL; IOUSBInterfaceInterface** intf = NULL;
err = (*iodev)->QueryInterface(iodev, err = (*iodev)->QueryInterface(iodev,
@ -113,7 +113,7 @@ class HIDDeviceIOKit : public IHIDDevice
IODestroyPlugInInterface(iodev); IODestroyPlugInInterface(iodev);
return; return;
} }
/* Obtain exclusive lock on device */ /* Obtain exclusive lock on device */
device->m_usbIntf = intf; device->m_usbIntf = intf;
err = (*intf)->USBInterfaceOpen(intf); err = (*intf)->USBInterfaceOpen(intf);
@ -137,7 +137,7 @@ class HIDDeviceIOKit : public IHIDDevice
IODestroyPlugInInterface(iodev); IODestroyPlugInInterface(iodev);
return; return;
} }
/* Determine pipe indices for interrupt I/O */ /* Determine pipe indices for interrupt I/O */
UInt8 numEndpoints = 0; UInt8 numEndpoints = 0;
err = (*intf)->GetNumEndpoints(intf, &numEndpoints); err = (*intf)->GetNumEndpoints(intf, &numEndpoints);
@ -154,12 +154,12 @@ class HIDDeviceIOKit : public IHIDDevice
device->m_usbIntfOutPipe = num; device->m_usbIntfOutPipe = num;
} }
} }
/* Return control to main thread */ /* Return control to main thread */
device->m_runningTransferLoop = true; device->m_runningTransferLoop = true;
lk.unlock(); lk.unlock();
device->m_initCond.notify_one(); device->m_initCond.notify_one();
/* Start transfer loop */ /* Start transfer loop */
device->m_devImp.initialCycle(); device->m_devImp.initialCycle();
while (device->m_runningTransferLoop) while (device->m_runningTransferLoop)
@ -171,54 +171,54 @@ class HIDDeviceIOKit : public IHIDDevice
(*intf)->Release(intf); (*intf)->Release(intf);
IODestroyPlugInInterface(iodev); IODestroyPlugInInterface(iodev);
device->m_usbIntf = NULL; device->m_usbIntf = NULL;
} }
static void _threadProcBTLL(HIDDeviceIOKit* device) static void _threadProcBTLL(HIDDeviceIOKit* device)
{ {
std::unique_lock<std::mutex> lk(device->m_initMutex); std::unique_lock<std::mutex> lk(device->m_initMutex);
/* Return control to main thread */ /* Return control to main thread */
device->m_runningTransferLoop = true; device->m_runningTransferLoop = true;
lk.unlock(); lk.unlock();
device->m_initCond.notify_one(); device->m_initCond.notify_one();
/* Start transfer loop */ /* Start transfer loop */
device->m_devImp.initialCycle(); device->m_devImp.initialCycle();
while (device->m_runningTransferLoop) while (device->m_runningTransferLoop)
device->m_devImp.transferCycle(); device->m_devImp.transferCycle();
device->m_devImp.finalCycle(); device->m_devImp.finalCycle();
} }
static void _threadProcHID(HIDDeviceIOKit* device) static void _threadProcHID(HIDDeviceIOKit* device)
{ {
std::unique_lock<std::mutex> lk(device->m_initMutex); std::unique_lock<std::mutex> lk(device->m_initMutex);
/* Return control to main thread */ /* Return control to main thread */
device->m_runningTransferLoop = true; device->m_runningTransferLoop = true;
lk.unlock(); lk.unlock();
device->m_initCond.notify_one(); device->m_initCond.notify_one();
/* Start transfer loop */ /* Start transfer loop */
device->m_devImp.initialCycle(); device->m_devImp.initialCycle();
while (device->m_runningTransferLoop) while (device->m_runningTransferLoop)
device->m_devImp.transferCycle(); device->m_devImp.transferCycle();
device->m_devImp.finalCycle(); device->m_devImp.finalCycle();
} }
void _deviceDisconnected() void _deviceDisconnected()
{ {
m_runningTransferLoop = false; m_runningTransferLoop = false;
} }
bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message) bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
{ {
return false; return false;
} }
public: public:
HIDDeviceIOKit(DeviceToken& token, DeviceBase& devImp) HIDDeviceIOKit(DeviceToken& token, DeviceBase& devImp)
: m_token(token), : m_token(token),
m_devImp(devImp), m_devImp(devImp),
@ -228,11 +228,11 @@ public:
std::unique_lock<std::mutex> lk(m_initMutex); std::unique_lock<std::mutex> lk(m_initMutex);
DeviceToken::DeviceType dType = token.getDeviceType(); DeviceToken::DeviceType dType = token.getDeviceType();
if (dType == DeviceToken::DeviceType::USB) if (dType == DeviceToken::DeviceType::USB)
m_thread = new std::thread(_threadProcUSBLL, this); m_thread = std::thread(_threadProcUSBLL, this);
else if (dType == DeviceToken::DeviceType::Bluetooth) else if (dType == DeviceToken::DeviceType::Bluetooth)
m_thread = new std::thread(_threadProcBTLL, this); m_thread = std::thread(_threadProcBTLL, this);
else if (dType == DeviceToken::DeviceType::GenericHID) else if (dType == DeviceToken::DeviceType::GenericHID)
m_thread = new std::thread(_threadProcHID, this); m_thread = std::thread(_threadProcHID, this);
else else
{ {
fprintf(stderr, "invalid token supplied to device constructor\n"); fprintf(stderr, "invalid token supplied to device constructor\n");
@ -240,14 +240,14 @@ public:
} }
m_initCond.wait(lk); m_initCond.wait(lk);
} }
~HIDDeviceIOKit() ~HIDDeviceIOKit()
{ {
m_runningTransferLoop = false; m_runningTransferLoop = false;
m_thread->join(); if (m_thread.joinable())
delete m_thread; m_thread.join();
} }
}; };

View File

@ -33,12 +33,12 @@ class HIDDeviceUdev final : public IHIDDevice
unsigned m_usbIntfInPipe = 0; unsigned m_usbIntfInPipe = 0;
unsigned m_usbIntfOutPipe = 0; unsigned m_usbIntfOutPipe = 0;
bool m_runningTransferLoop = false; bool m_runningTransferLoop = false;
const std::string& m_devPath; const std::string& m_devPath;
std::mutex m_initMutex; std::mutex m_initMutex;
std::condition_variable m_initCond; std::condition_variable m_initCond;
std::thread* m_thread; std::thread m_thread;
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length) bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
{ {
if (m_devFd) if (m_devFd)
@ -73,7 +73,7 @@ class HIDDeviceUdev final : public IHIDDevice
} }
return 0; return 0;
} }
static void _threadProcUSBLL(HIDDeviceUdev* device) static void _threadProcUSBLL(HIDDeviceUdev* device)
{ {
unsigned i; unsigned i;
@ -131,12 +131,12 @@ class HIDDeviceUdev final : public IHIDDevice
NULL NULL
}; };
ioctl(fd, USBDEVFS_IOCTL, &disconnectReq); ioctl(fd, USBDEVFS_IOCTL, &disconnectReq);
/* Return control to main thread */ /* Return control to main thread */
device->m_runningTransferLoop = true; device->m_runningTransferLoop = true;
lk.unlock(); lk.unlock();
device->m_initCond.notify_one(); device->m_initCond.notify_one();
/* Start transfer loop */ /* Start transfer loop */
device->m_devImp.initialCycle(); device->m_devImp.initialCycle();
while (device->m_runningTransferLoop) while (device->m_runningTransferLoop)
@ -147,7 +147,7 @@ class HIDDeviceUdev final : public IHIDDevice
close(fd); close(fd);
device->m_devFd = 0; device->m_devFd = 0;
udev_device_unref(udevDev); udev_device_unref(udevDev);
} }
static void _threadProcBTLL(HIDDeviceUdev* device) static void _threadProcBTLL(HIDDeviceUdev* device)
@ -168,7 +168,7 @@ class HIDDeviceUdev final : public IHIDDevice
udev_device_unref(udevDev); udev_device_unref(udevDev);
} }
static void _threadProcHID(HIDDeviceUdev* device) static void _threadProcHID(HIDDeviceUdev* device)
{ {
std::unique_lock<std::mutex> lk(device->m_initMutex); std::unique_lock<std::mutex> lk(device->m_initMutex);
@ -187,12 +187,12 @@ class HIDDeviceUdev final : public IHIDDevice
udev_device_unref(udevDev); udev_device_unref(udevDev);
} }
void _deviceDisconnected() void _deviceDisconnected()
{ {
m_runningTransferLoop = false; m_runningTransferLoop = false;
} }
bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message) bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
{ {
if (m_devFd) if (m_devFd)
@ -233,9 +233,9 @@ class HIDDeviceUdev final : public IHIDDevice
} }
return 0; return 0;
} }
public: public:
HIDDeviceUdev(DeviceToken& token, DeviceBase& devImp) HIDDeviceUdev(DeviceToken& token, DeviceBase& devImp)
: m_token(token), : m_token(token),
m_devImp(devImp), m_devImp(devImp),
@ -245,11 +245,11 @@ public:
std::unique_lock<std::mutex> lk(m_initMutex); std::unique_lock<std::mutex> lk(m_initMutex);
DeviceToken::DeviceType dType = token.getDeviceType(); DeviceToken::DeviceType dType = token.getDeviceType();
if (dType == DeviceToken::DeviceType::USB) if (dType == DeviceToken::DeviceType::USB)
m_thread = new std::thread(_threadProcUSBLL, this); m_thread = std::thread(_threadProcUSBLL, this);
else if (dType == DeviceToken::DeviceType::Bluetooth) else if (dType == DeviceToken::DeviceType::Bluetooth)
m_thread = new std::thread(_threadProcBTLL, this); m_thread = std::thread(_threadProcBTLL, this);
else if (dType == DeviceToken::DeviceType::GenericHID) else if (dType == DeviceToken::DeviceType::GenericHID)
m_thread = new std::thread(_threadProcHID, this); m_thread = std::thread(_threadProcHID, this);
else else
{ {
fprintf(stderr, "invalid token supplied to device constructor"); fprintf(stderr, "invalid token supplied to device constructor");
@ -257,14 +257,14 @@ public:
} }
m_initCond.wait(lk); m_initCond.wait(lk);
} }
~HIDDeviceUdev() ~HIDDeviceUdev()
{ {
m_runningTransferLoop = false; m_runningTransferLoop = false;
m_thread->join(); if (m_thread.joinable())
delete m_thread; m_thread.join();
} }
}; };