Fix a deadlock

This commit is contained in:
Lior Halphon 2020-05-01 19:31:04 +03:00
parent 69fb2ad0a3
commit c492022ae6

View File

@ -35,6 +35,8 @@ static bool axesEmulateButtons = false;
static bool axes2DEmulateButtons = false; static bool axes2DEmulateButtons = false;
static bool hatsEmulateButtons = false; static bool hatsEmulateButtons = false;
static NSLock *globalPWMThreadLock;
@interface JOYController () @interface JOYController ()
+ (void)controllerAdded:(IOHIDDeviceRef) device; + (void)controllerAdded:(IOHIDDeviceRef) device;
+ (void)controllerRemoved:(IOHIDDeviceRef) device; + (void)controllerRemoved:(IOHIDDeviceRef) device;
@ -111,9 +113,9 @@ typedef struct __attribute__((packed)) {
JOYElement *_connectedElement; JOYElement *_connectedElement;
NSMutableDictionary<NSValue *, JOYElement *> *_iokitToJOY; NSMutableDictionary<NSValue *, JOYElement *> *_iokitToJOY;
NSString *_serialSuffix; NSString *_serialSuffix;
bool _isSwitch; // Does thie controller use the Switch protocol? bool _isSwitch; // Does this controller use the Switch protocol?
JOYSwitchPacket _lastSwitchPacket; JOYSwitchPacket _lastSwitchPacket;
NSCondition *_rumblePWMThreadLock; NSLock *_rumblePWMThreadLock;
volatile double _rumblePWMRatio; volatile double _rumblePWMRatio;
bool _physicallyConnected; bool _physicallyConnected;
bool _logicallyConnected; bool _logicallyConnected;
@ -149,7 +151,7 @@ typedef struct __attribute__((packed)) {
_hatEmulatedButtons = [NSMutableDictionary dictionary]; _hatEmulatedButtons = [NSMutableDictionary dictionary];
_multiElements = [NSMutableDictionary dictionary]; _multiElements = [NSMutableDictionary dictionary];
_iokitToJOY = [NSMutableDictionary dictionary]; _iokitToJOY = [NSMutableDictionary dictionary];
_rumblePWMThreadLock = [[NSCondition alloc] init]; _rumblePWMThreadLock = [[NSLock alloc] init];
//NSMutableArray *axes3d = [NSMutableArray array]; //NSMutableArray *axes3d = [NSMutableArray array];
@ -606,13 +608,6 @@ typedef struct __attribute__((packed)) {
- (void)pwmThread - (void)pwmThread
{ {
/* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller
uses rumble. At least make sure any sibling controllers don't have their PWM thread running. */
for (JOYController *controller in [JOYController allControllers]) {
if (controller != self && controller->_device == _device) {
[controller _forceStopPWMThread];
}
}
unsigned rumbleCounter = 0; unsigned rumbleCounter = 0;
while (self.connected && !_forceStopPWMThread) { while (self.connected && !_forceStopPWMThread) {
if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) {
@ -678,9 +673,20 @@ typedef struct __attribute__((packed)) {
_rumblePWMRatio = amp; _rumblePWMRatio = amp;
if (!_rumblePWMThreadRunning) { // PWM thread not running, start it. if (!_rumblePWMThreadRunning) { // PWM thread not running, start it.
if (amp != 0) { if (amp != 0) {
/* TODO: The PWM thread does not handle correctly the case of having a multi-port controller where more
than one controller uses rumble. At least make sure any sibling controllers don't have their
PWM thread running. */
[globalPWMThreadLock lock];
for (JOYController *controller in [JOYController allControllers]) {
if (controller != self && controller->_device == _device) {
[controller _forceStopPWMThread];
}
}
_rumblePWMRatio = amp; _rumblePWMRatio = amp;
_rumblePWMThreadRunning = true; _rumblePWMThreadRunning = true;
[self performSelectorInBackground:@selector(pwmThread) withObject:nil]; [self performSelectorInBackground:@selector(pwmThread) withObject:nil];
[globalPWMThreadLock unlock];
} }
} }
[_rumblePWMThreadLock unlock]; [_rumblePWMThreadLock unlock];
@ -765,6 +771,7 @@ typedef struct __attribute__((packed)) {
controllers = [NSMutableDictionary dictionary]; controllers = [NSMutableDictionary dictionary];
exposedControllers = [NSMutableArray array]; exposedControllers = [NSMutableArray array];
globalPWMThreadLock = [[NSLock alloc] init];
NSArray *array = @[ NSArray *array = @[
CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick), CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick),
CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad), CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad),