[idd] common: use CSRWLock throughout the drivers

Make CSRWLock own the native lock and provide scoped guards for shared,
exclusive, early-unlock, and non-blocking use.

Replace direct SRW lock management throughout the IDD, input driver,
and helper while preserving the existing lock scopes.
This commit is contained in:
Geoffrey McRae
2026-08-11 22:06:31 +10:00
parent 2543366f5f
commit 61a49ebaba
37 changed files with 576 additions and 525 deletions

View File

@@ -95,7 +95,7 @@ void CInputPipeServer::DeInit()
m_stopEvent = nullptr;
}
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
m_queueHead = 0;
m_queueCount = 0;
m_mouseMode = MouseMode::NONE;
@@ -269,7 +269,7 @@ bool CInputPipeServer::SendMouseRelative(
payload.mouseRelative.deltaY = deltaY;
payload.mouseRelative.wheel = wheel;
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
const bool pureMotion = wheel == 0 && buttons == m_relativeButtons;
const bool switching = m_mouseMode == MouseMode::ABSOLUTE_INPUT;
bool queued = (m_state.load(std::memory_order_relaxed) & 1) != 0;
@@ -316,7 +316,7 @@ bool CInputPipeServer::SendMouseAbsolute(
payload.mouseAbsolute.y = y;
payload.mouseAbsolute.wheel = wheel;
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
const bool pureMotion = wheel == 0 && buttons == m_absoluteButtons;
const bool switching = m_mouseMode == MouseMode::RELATIVE_INPUT;
bool queued = (m_state.load(std::memory_order_relaxed) & 1) != 0;
@@ -360,7 +360,7 @@ bool CInputPipeServer::SendKeyboard(
payload.keyboard.keys[i] = keys[i];
}
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
const bool queued = (m_state.load(std::memory_order_relaxed) & 1) &&
QueueLocked(LG_INPUT_PIPE_MESSAGE_KEYBOARD, payload, false);
if (!queued)
@@ -373,7 +373,7 @@ bool CInputPipeServer::Reset()
if (!(m_state.load(std::memory_order_acquire) & 1))
return false;
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
bool queued = (m_state.load(std::memory_order_relaxed) & 1) != 0;
if (queued)
queued = QueueResetLocked();
@@ -384,7 +384,7 @@ bool CInputPipeServer::Reset()
bool CInputPipeServer::Pop(QueueItem& item)
{
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
if (!m_queueCount)
return false;
@@ -411,7 +411,7 @@ bool CInputPipeServer::Send(const QueueItem& item)
LARGE_INTEGER start = {};
LARGE_INTEGER end = {};
{
CSRWSharedLock lock(&m_connectionLock);
CSRWSharedLock lock(m_connectionLock);
const uint64_t state = m_state.load(std::memory_order_acquire);
current = (state & 1) && item.state == state;
if (current)
@@ -465,7 +465,7 @@ void CInputPipeServer::LogStatistics()
uint64_t resyncDiscarded;
size_t queueHighWater;
{
CSRWExclusiveLock lock(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
enqueued = m_statEnqueued;
relativeCoalesced = m_statRelativeCoalesced;
absoluteCoalesced = m_statAbsoluteCoalesced;
@@ -526,7 +526,7 @@ void CInputPipeServer::LogStatistics()
void CInputPipeServer::Invalidate(uint64_t state, bool requireMatch)
{
CSRWExclusiveLock connectionLock(&m_connectionLock);
CSRWExclusiveLock connectionLock(m_connectionLock);
uint64_t current = m_state.load(std::memory_order_relaxed);
for (;;)
{
@@ -537,7 +537,7 @@ void CInputPipeServer::Invalidate(uint64_t state, bool requireMatch)
break;
}
CSRWExclusiveLock queueLock(&m_queueLock);
CSRWExclusiveLock queueLock(m_queueLock);
m_queueHead = 0;
m_queueCount = 0;
}
@@ -578,8 +578,8 @@ void CInputPipeServer::Thread()
void CInputPipeServer::OnPipeConnected()
{
CSRWExclusiveLock connectionLock(&m_connectionLock);
CSRWExclusiveLock queueLock(&m_queueLock);
CSRWExclusiveLock connectionLock(m_connectionLock);
CSRWExclusiveLock queueLock(m_queueLock);
uint64_t state = m_state.load(std::memory_order_relaxed);
if (state & 1)

View File

@@ -21,6 +21,7 @@
#pragma once
#include "CPipeEndpoint.h"
#include "CSRWLock.h"
#include "InputPipeProtocol.h"
#include "input/IInputSink.h"
@@ -55,11 +56,11 @@ private:
// Odd states are available. Endpoint changes and resyncs advance the state.
std::atomic<uint64_t> m_state { 0 };
SRWLOCK m_queueLock = SRWLOCK_INIT;
SRWLOCK m_connectionLock = SRWLOCK_INIT;
HANDLE m_stopEvent = nullptr;
HANDLE m_queueEvent = nullptr;
HANDLE m_thread = nullptr;
CSRWLock m_queueLock;
CSRWLock m_connectionLock;
HANDLE m_stopEvent = nullptr;
HANDLE m_queueEvent = nullptr;
HANDLE m_thread = nullptr;
QueueItem m_queue[QUEUE_LENGTH] = {};
size_t m_queueHead = 0;

View File

@@ -20,6 +20,7 @@
#include "ipc/CPipeServer.h"
#include "CDebug.h"
#include "CSRWLock.h"
#include "display/CDeviceContext.h"
CPipeServer g_pipe;
@@ -40,7 +41,7 @@ void CPipeServer::DeInit()
void CPipeServer::OnPipeConnected()
{
AcquireSRWLockExclusive(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
std::vector<LGPipeMsg> queued;
queued.swap(m_queue);
@@ -51,7 +52,6 @@ void CPipeServer::OnPipeConnected()
QueueMsgLocked(queued[i]);
break;
}
ReleaseSRWLockExclusive(&m_queueLock);
}
bool CPipeServer::OnPipeMessage(const void * message, size_t size)
@@ -89,27 +89,24 @@ void CPipeServer::QueueMsgLocked(const LGPipeMsg & msg)
void CPipeServer::WriteMsg(const LGPipeMsg & msg)
{
AcquireSRWLockExclusive(&m_queueLock);
CSRWExclusiveLock lock(m_queueLock);
if (!m_endpoint.Send(&msg, sizeof(msg)))
QueueMsgLocked(msg);
ReleaseSRWLockExclusive(&m_queueLock);
}
void CPipeServer::HandleReloadSettings()
{
DEBUG_INFO("Reloading settings");
AcquireSRWLockShared(&m_deviceContextLock);
CSRWSharedLock lock(m_deviceContextLock);
if (m_deviceContext)
m_deviceContext->ReloadSettings();
ReleaseSRWLockShared(&m_deviceContextLock);
}
void CPipeServer::SetDeviceContext(CDeviceContext * context)
{
AcquireSRWLockExclusive(&m_deviceContextLock);
CSRWExclusiveLock lock(m_deviceContextLock);
m_deviceContext = context;
ReleaseSRWLockExclusive(&m_deviceContextLock);
}
void CPipeServer::SetCursorPos(uint32_t x, uint32_t y)

View File

@@ -26,6 +26,7 @@
#include <vector>
#include "CPipeEndpoint.h"
#include "CSRWLock.h"
#include "PipeMsg.h"
class CDeviceContext;
@@ -34,10 +35,10 @@ class CPipeServer : private IPipeEndpointHandler
{
private:
CPipeEndpoint m_endpoint;
SRWLOCK m_queueLock = SRWLOCK_INIT;
CSRWLock m_queueLock;
std::vector<LGPipeMsg> m_queue;
SRWLOCK m_deviceContextLock = SRWLOCK_INIT;
CSRWLock m_deviceContextLock;
CDeviceContext * m_deviceContext = nullptr;
void WriteMsg(const LGPipeMsg & msg);