Use atomic reference counting for the HID device object
authorSam Lantinga <slouken@libsdl.org>
Fri, 14 Sep 2018 18:31:01 -0700
changeset 121866e9555603dc5
parent 12185 b6aa21d1a522
child 12187 e755760381d8
Use atomic reference counting for the HID device object
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
Xcode/SDL/SDL.xcodeproj/project.pbxproj
src/hidapi/android/hid.cpp
     1.1 --- a/src/hidapi/android/hid.cpp	Fri Sep 14 12:41:29 2018 -0700
     1.2 +++ b/src/hidapi/android/hid.cpp	Fri Sep 14 18:31:01 2018 -0700
     1.3 @@ -9,6 +9,7 @@
     1.4  #include <jni.h>
     1.5  #include <android/log.h>
     1.6  #include <pthread.h>
     1.7 +#include <stdlib.h>
     1.8  #include <errno.h>	// For ETIMEDOUT and ECONNRESET
     1.9  #include <stdlib.h> // For malloc() and free()
    1.10  
    1.11 @@ -27,6 +28,7 @@
    1.12  #define HID_DEVICE_MANAGER_JAVA_INTERFACE(function)     CONCAT1(SDL_JAVA_PREFIX, HIDDeviceManager, function)
    1.13  
    1.14  #include "../hidapi/hidapi.h"
    1.15 +
    1.16  typedef uint32_t uint32;
    1.17  typedef uint64_t uint64;
    1.18  
    1.19 @@ -364,12 +366,20 @@
    1.20  
    1.21  	int IncrementRefCount()
    1.22  	{
    1.23 -		return ++m_nRefCount;
    1.24 +		int nValue;
    1.25 +		pthread_mutex_lock( &m_refCountLock );
    1.26 +		nValue = ++m_nRefCount;
    1.27 +		pthread_mutex_unlock( &m_refCountLock );
    1.28 +		return nValue;
    1.29  	}
    1.30  
    1.31  	int DecrementRefCount()
    1.32  	{
    1.33 -		return --m_nRefCount;
    1.34 +		int nValue;
    1.35 +		pthread_mutex_lock( &m_refCountLock );
    1.36 +		nValue = --m_nRefCount;
    1.37 +		pthread_mutex_unlock( &m_refCountLock );
    1.38 +		return nValue;
    1.39  	}
    1.40  
    1.41  	int GetId()
    1.42 @@ -394,12 +404,20 @@
    1.43  
    1.44  	int IncrementDeviceRefCount()
    1.45  	{
    1.46 -		return ++m_nDeviceRefCount;
    1.47 +		int nValue;
    1.48 +		pthread_mutex_lock( &m_refCountLock );
    1.49 +		nValue = ++m_nDeviceRefCount;
    1.50 +		pthread_mutex_unlock( &m_refCountLock );
    1.51 +		return nValue;
    1.52  	}
    1.53  
    1.54  	int DecrementDeviceRefCount()
    1.55  	{
    1.56 -		return --m_nDeviceRefCount;
    1.57 +		int nValue;
    1.58 +		pthread_mutex_lock( &m_refCountLock );
    1.59 +		nValue = --m_nDeviceRefCount;
    1.60 +		pthread_mutex_unlock( &m_refCountLock );
    1.61 +		return nValue;
    1.62  	}
    1.63  
    1.64  	bool BOpen()
    1.65 @@ -644,6 +662,7 @@
    1.66  	}
    1.67  
    1.68  private:
    1.69 +	pthread_mutex_t m_refCountLock = PTHREAD_MUTEX_INITIALIZER;
    1.70  	int m_nRefCount = 0;
    1.71  	int m_nId = 0;
    1.72  	hid_device_info *m_pInfo = nullptr;