Skip to content

Commit

Permalink
Use CriticalSection for shared memory
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jul 27, 2019
1 parent c3e5678 commit e0739ef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 29
#define BUILD_NUMBER 30
18 changes: 11 additions & 7 deletions IDirectInputDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC
return DIERR_INVALIDPARAM;
}

// Verify that only one thread can use the variable at a time
SetCriticalSection(dodThreadFlag);
EnterCriticalSection(&dics);

// Check the size of the array
if (rgdod && pdwInOut && *pdwInOut > pdod.size())
Expand All @@ -140,7 +139,7 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC
}
}

ReleaseCriticalSection(dodThreadFlag);
LeaveCriticalSection(&dics);

return hr;
}
Expand All @@ -152,6 +151,8 @@ HRESULT m_IDirectInputDeviceX::SetDataFormat(LPCDIDATAFORMAT lpdf)
// Fix unsupported flags
if (lpdf && lpdf->dwNumObjs && lpdf->dwObjSize == 16)
{
EnterCriticalSection(&dics);

if (rgodf.size() < lpdf->dwNumObjs)
{
rgodf.resize(lpdf->dwNumObjs);
Expand All @@ -173,7 +174,11 @@ HRESULT m_IDirectInputDeviceX::SetDataFormat(LPCDIDATAFORMAT lpdf)
rgodf[x].dwFlags = lpdf->rgodf[x].dwFlags;
}

return ProxyInterface->SetDataFormat(&df);
HRESULT hr = ProxyInterface->SetDataFormat(&df);

LeaveCriticalSection(&dics);

return hr;
}

return ProxyInterface->SetDataFormat(lpdf);
Expand Down Expand Up @@ -351,8 +356,7 @@ HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJ
return DIERR_INVALIDPARAM;
}

// Verify that only one thread can use the variable at a time
SetCriticalSection(dodThreadFlag);
EnterCriticalSection(&dics);

// Check the size of the array
if (rgdod && pdwInOut && *pdwInOut > pdod.size())
Expand All @@ -374,7 +378,7 @@ HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJ

HRESULT hr = ProxyInterface->SendDeviceData(sizeof(DIDEVICEOBJECTDATA), (rgdod) ? &pdod[0] : nullptr, pdwInOut, fl);

ReleaseCriticalSection(dodThreadFlag);
LeaveCriticalSection(&dics);

return hr;
}
Expand Down
10 changes: 9 additions & 1 deletion IDirectInputDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class m_IDirectInputDeviceX
// For CooperativeLevel
bool CanAquireDevice = false;

// Critical section for shared memory
CRITICAL_SECTION dics;

// For DeviceData
bool dodThreadFlag = false;
std::vector<DIDEVICEOBJECTDATA> pdod;

// For DataFormat
Expand All @@ -27,10 +29,16 @@ class m_IDirectInputDeviceX
StringType = GetStringType(WrapperID);

LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")" << " converting device from v" << Version << " to v8 using " << ((StringType == DEFAULT_CHARSET) ? "UNICODE" : "ANSI"));

// Initialize Critical Section
InitializeCriticalSection(&dics);
}
~m_IDirectInputDeviceX()
{
LOG_LIMIT(3, __FUNCTION__ << "(" << this << ")" << " deleting device!");

// Delete Critical Section
DeleteCriticalSection(&dics);
}

/*** IUnknown methods ***/
Expand Down
17 changes: 0 additions & 17 deletions dinputto8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,3 @@ HRESULT WINAPI DllUnregisterServer()

return m_pDllUnregisterServer();
}

void dinputto8::SetCriticalSection(bool &ThreadSyncFlag)
{
if (ThreadSyncFlag)
{
do {
Sleep(0);
} while (ThreadSyncFlag);
}

ThreadSyncFlag = true;
}

void dinputto8::ReleaseCriticalSection(bool &ThreadSyncFlag)
{
ThreadSyncFlag = false;
}

0 comments on commit e0739ef

Please sign in to comment.