[Commits] SDL: Use atomic reference counting for the HID device object

libsdl.org revision control commits-owner at libsdl.org
Fri Sep 14 18:32:23 PDT 2018


details:   https://hg.libsdl.org/SDL/rev/6e9555603dc5
changeset: 12186:6e9555603dc5
user:      Sam Lantinga <slouken at libsdl.org>
date:      Fri Sep 14 18:31:01 2018 -0700
description:
Use atomic reference counting for the HID device object

diffstat:

 src/hidapi/android/hid.cpp |  27 +++++++++++++++++++++++----
 1 files changed, 23 insertions(+), 4 deletions(-)

diffs (73 lines):

diff -r b6aa21d1a522 -r 6e9555603dc5 src/hidapi/android/hid.cpp
--- a/src/hidapi/android/hid.cpp	Fri Sep 14 12:41:29 2018 -0700
+++ b/src/hidapi/android/hid.cpp	Fri Sep 14 18:31:01 2018 -0700
@@ -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 @@
 
 	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 @@
 
 	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 @@
 	}
 
 private:
+	pthread_mutex_t m_refCountLock = PTHREAD_MUTEX_INITIALIZER;
 	int m_nRefCount = 0;
 	int m_nId = 0;
 	hid_device_info *m_pInfo = nullptr;


More information about the commits mailing list