Use atomic reference counting for the HID device object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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 37b159b..c9150d1 100644
--- a/src/hidapi/android/hid.cpp
+++ b/src/hidapi/android/hid.cpp
@@ -9,6 +9,7 @@
#include <jni.h>
#include <android/log.h>
#include <pthread.h>
+#include <stdlib.h>
#include <errno.h> // For ETIMEDOUT and ECONNRESET
#include <stdlib.h> // 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 @@ public:
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 @@ public:
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 @@ public:
}
private:
+ pthread_mutex_t m_refCountLock = PTHREAD_MUTEX_INITIALIZER;
int m_nRefCount = 0;
int m_nId = 0;
hid_device_info *m_pInfo = nullptr;