Skip to content

Commit

Permalink
Use atomic reference counting for the HID device object
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Sep 15, 2018
1 parent 6a7b0c2 commit e8a0e35
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Empty file modified Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
Empty file modified Xcode/SDL/SDL.xcodeproj/project.pbxproj 100644 → 100755
Empty file.
27 changes: 23 additions & 4 deletions src/hidapi/android/hid.cpp
Expand Up @@ -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()

Expand All @@ -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;

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e8a0e35

Please sign in to comment.