From e8a0e35e24a4259e5d68a0f24f0994e20a81a457 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 14 Sep 2018 18:31:01 -0700 Subject: [PATCH] Use atomic reference counting for the HID device object --- Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj | 0 Xcode/SDL/SDL.xcodeproj/project.pbxproj | 0 src/hidapi/android/hid.cpp | 27 ++++++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) mode change 100644 => 100755 Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj mode change 100644 => 100755 Xcode/SDL/SDL.xcodeproj/project.pbxproj diff --git a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj old mode 100644 new mode 100755 diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj old mode 100644 new mode 100755 diff --git a/src/hidapi/android/hid.cpp b/src/hidapi/android/hid.cpp index 37b159b12a834..c9150d1e46332 100644 --- a/src/hidapi/android/hid.cpp +++ b/src/hidapi/android/hid.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include // For ETIMEDOUT and ECONNRESET #include // For malloc() and free() @@ -27,6 +28,7 @@ #define HID_DEVICE_MANAGER_JAVA_INTERFACE(function) CONCAT1(SDL_JAVA_PREFIX, HIDDeviceManager, function) #include "../hidapi/hidapi.h" + typedef uint32_t uint32; typedef uint64_t uint64; @@ -364,12 +366,20 @@ class CHIDDevice int IncrementRefCount() { - return ++m_nRefCount; + int nValue; + pthread_mutex_lock( &m_refCountLock ); + nValue = ++m_nRefCount; + pthread_mutex_unlock( &m_refCountLock ); + return nValue; } int DecrementRefCount() { - return --m_nRefCount; + int nValue; + pthread_mutex_lock( &m_refCountLock ); + nValue = --m_nRefCount; + pthread_mutex_unlock( &m_refCountLock ); + return nValue; } int GetId() @@ -394,12 +404,20 @@ class CHIDDevice int IncrementDeviceRefCount() { - return ++m_nDeviceRefCount; + int nValue; + pthread_mutex_lock( &m_refCountLock ); + nValue = ++m_nDeviceRefCount; + pthread_mutex_unlock( &m_refCountLock ); + return nValue; } int DecrementDeviceRefCount() { - return --m_nDeviceRefCount; + int nValue; + pthread_mutex_lock( &m_refCountLock ); + nValue = --m_nDeviceRefCount; + pthread_mutex_unlock( &m_refCountLock ); + return nValue; } bool BOpen() @@ -644,6 +662,7 @@ class CHIDDevice } private: + pthread_mutex_t m_refCountLock = PTHREAD_MUTEX_INITIALIZER; int m_nRefCount = 0; int m_nId = 0; hid_device_info *m_pInfo = nullptr;