[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

@@ -178,7 +178,7 @@ bool CDisplayConfiguration::LoadModes(const FrameMemoryLimits& limits)
if (!hasPreferred)
newModes.front().preferred = true;
CSRWExclusiveLock lock(&m_modeLock);
CSRWExclusiveLock lock(m_modeLock);
m_modes = std::move(newModes);
return true;
}
@@ -193,7 +193,7 @@ bool CDisplayConfiguration::ReloadSettings(
{
bool modesLoaded = false;
{
CSRWExclusiveLock reloadLock(&m_reloadLock);
CSRWExclusiveLock reloadLock(m_reloadLock);
bool settingsUpdated = true;
CSettings::DisplayMode extraMode = {};
@@ -252,7 +252,7 @@ CDisplayConfiguration::SetResolution(
mode.preferred = true;
{
CSRWExclusiveLock reloadLock(&m_reloadLock);
CSRWExclusiveLock reloadLock(m_reloadLock);
if (!m_settings.SetExtraMode(mode))
result.status = ResolutionStatus::SETTINGS_FAILED;
else if (!LoadModes(limits))
@@ -271,7 +271,7 @@ CDisplayConfiguration::SetResolution(
void CDisplayConfiguration::InitializeEdid(bool hdr)
{
CSRWExclusiveLock lock(&m_modeLock);
CSRWExclusiveLock lock(m_modeLock);
if (m_edid.Size())
return;
@@ -281,7 +281,7 @@ void CDisplayConfiguration::InitializeEdid(bool hdr)
void CDisplayConfiguration::RebuildEdid(bool hdr)
{
CSRWExclusiveLock lock(&m_modeLock);
CSRWExclusiveLock lock(m_modeLock);
m_edid.Build(hdr);
m_hdrEnabled = hdr;
}
@@ -290,7 +290,7 @@ CDisplayConfiguration::Description
CDisplayConfiguration::GetDescription() const
{
Description result;
CSRWSharedLock lock(&m_modeLock);
CSRWSharedLock lock(m_modeLock);
result.modeCount = m_modes.size();
if (m_edid.Size())
result.edid.assign(m_edid.Data(), m_edid.Data() + m_edid.Size());
@@ -300,7 +300,7 @@ CDisplayConfiguration::GetDescription() const
CSettings::DisplayModes CDisplayConfiguration::SnapshotModes(
bool * hdrEnabled) const
{
CSRWSharedLock lock(&m_modeLock);
CSRWSharedLock lock(m_modeLock);
if (hdrEnabled)
*hdrEnabled = m_hdrEnabled;
return m_modes;

View File

@@ -20,6 +20,8 @@
#pragma once
#include "CSRWLock.h"
#include "config/CSettings.h"
#include "display/CEdid.h"
#include "display/IddCxCompat.h"
@@ -59,8 +61,8 @@ private:
// Registry-backed changes are serialized before publishing a replacement
// mode list. Readers only hold m_modeLock long enough to take a snapshot.
SRWLOCK m_reloadLock = SRWLOCK_INIT;
mutable SRWLOCK m_modeLock = SRWLOCK_INIT;
CSRWLock m_reloadLock;
mutable CSRWLock m_modeLock;
CSettings::DisplayModes m_modes;
CEdid m_edid;

View File

@@ -62,10 +62,10 @@ NTSTATUS CMonitorContext::AssignSwapChain(
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
}
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (!IsAssignmentCurrent(assignmentGeneration))
{
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
DEBUG_INFO("Swap chain assignment canceled before processor startup");
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
}
@@ -82,13 +82,12 @@ NTSTATUS CMonitorContext::AssignSwapChain(
{
auto processor = std::move(m_swapChain);
dx11Device = std::move(m_dx11Device);
ReleaseSRWLockExclusive(&m_lock);
lock.Unlock();
processor.reset();
dx11Device.reset();
m_devContext->OnSwapChainReleased();
return STATUS_GRAPHICS_INDIRECT_DISPLAY_ABANDON_SWAPCHAIN;
}
ReleaseSRWLockExclusive(&m_lock);
return STATUS_SUCCESS;
}
@@ -105,10 +104,11 @@ void CMonitorContext::DetachSwapChain()
std::unique_ptr<CSwapChainProcessor> processor;
std::shared_ptr<CD3D11Device> dx11Device;
AcquireSRWLockExclusive(&m_lock);
processor = std::move(m_swapChain);
dx11Device = std::move(m_dx11Device);
ReleaseSRWLockExclusive(&m_lock);
{
CSRWExclusiveLock lock(m_lock);
processor = std::move(m_swapChain);
dx11Device = std::move(m_dx11Device);
}
const bool hadSwapChain = !!processor;
processor.reset();

View File

@@ -20,6 +20,8 @@
#pragma once
#include "CSRWLock.h"
#include <Windows.h>
#include <wdf.h>
#include <IddCx.h>
@@ -40,7 +42,7 @@ private:
// Guards the swap chain and device pointers. Assign and unassign can run
// concurrently (an unassign triggered by the worker's WdfObjectDelete can
// race the next assign), and shared_ptr copy/reset is not thread safe.
SRWLOCK m_lock = SRWLOCK_INIT;
CSRWLock m_lock;
// IddCx can issue a replacement assignment before an earlier assignment
// has finished creating its devices. Serialize those expensive setup paths

View File

@@ -30,9 +30,11 @@ void CMonitorManager::Create(UINT connectorIndex, IDDCX_ADAPTER adapter,
// We support a single monitor; never create a second one if one already
// exists (a replug must clear m_monitor via departure first).
AcquireSRWLockExclusive(&m_lock);
const bool haveMonitor = m_monitor != WDF_NO_HANDLE;
ReleaseSRWLockExclusive(&m_lock);
bool haveMonitor;
{
CSRWExclusiveLock lock(m_lock);
haveMonitor = m_monitor != WDF_NO_HANDLE;
}
if (haveMonitor)
{
DEBUG_WARN("FinishInit skipped: a monitor already exists");
@@ -76,9 +78,10 @@ void CMonitorManager::Create(UINT connectorIndex, IDDCX_ADAPTER adapter,
DEBUG_INFO("Monitor object created (%p)", createOut.MonitorObject);
AcquireSRWLockExclusive(&m_lock);
m_monitor = createOut.MonitorObject;
ReleaseSRWLockExclusive(&m_lock);
{
CSRWExclusiveLock lock(m_lock);
m_monitor = createOut.MonitorObject;
}
auto * wrapper = WdfObjectGet_CMonitorContextWrapper(m_monitor);
wrapper->context = new CMonitorContext(m_monitor, owner);
@@ -96,56 +99,65 @@ void CMonitorManager::Create(UINT connectorIndex, IDDCX_ADAPTER adapter,
CMonitorManager::ReplugAction CMonitorManager::Replug()
{
AcquireSRWLockExclusive(&m_lock);
if (m_replugMonitor || (m_swapChainAssigned && !m_swapChainReady))
IDDCX_MONITOR monitor;
{
// Coalesce changes received while a swap chain is being initialized, the
// old one is draining, or its replacement is being initialized.
m_replugPending = true;
ReleaseSRWLockExclusive(&m_lock);
return ReplugAction::NONE;
CSRWExclusiveLock lock(m_lock);
if (m_replugMonitor || (m_swapChainAssigned && !m_swapChainReady))
{
// Coalesce changes received while a swap chain is being initialized,
// the old one is draining, or its replacement is being initialized.
m_replugPending = true;
return ReplugAction::NONE;
}
monitor = m_monitor;
if (monitor == WDF_NO_HANDLE)
{
m_replugMonitor = true;
m_monitorDeparted = true;
}
else
{
// Clear the handle before departing so nothing calls an IddCx monitor
// API on a departing/destroyed handle. Create publishes the new one.
m_replugMonitor = true;
m_monitorDeparted = false;
m_waitForSwapChainRelease = m_swapChainAssigned;
m_monitor = nullptr;
}
}
IDDCX_MONITOR monitor = m_monitor;
if (monitor == WDF_NO_HANDLE)
{
m_replugMonitor = true;
m_monitorDeparted = true;
ReleaseSRWLockExclusive(&m_lock);
// Either no monitor yet, or one is already pending; build it now and
// cancel any queued rebuild so we do not create two.
m_createQueued.store(0);
return ReplugAction::CREATE;
}
// Clear the handle before departing so nothing calls an IddCx monitor API
// on a departing/destroyed handle. Create publishes the new one.
m_replugMonitor = true;
m_monitorDeparted = false;
m_waitForSwapChainRelease = m_swapChainAssigned;
m_monitor = nullptr;
ReleaseSRWLockExclusive(&m_lock);
DEBUG_TRACE("ReplugMonitor");
NTSTATUS status = IddCxMonitorDeparture(monitor);
if (!NT_SUCCESS(status))
{
AcquireSRWLockExclusive(&m_lock);
m_replugMonitor = false;
m_replugPending = false;
m_monitorDeparted = false;
m_waitForSwapChainRelease = false;
m_monitor = monitor;
ReleaseSRWLockExclusive(&m_lock);
{
CSRWExclusiveLock lock(m_lock);
m_replugMonitor = false;
m_replugPending = false;
m_monitorDeparted = false;
m_waitForSwapChainRelease = false;
m_monitor = monitor;
}
DEBUG_ERROR("IddCxMonitorDeparture Failed (0x%08x)", status);
return ReplugAction::NONE;
}
AcquireSRWLockExclusive(&m_lock);
m_monitorDeparted = true;
const bool rebuild = !m_waitForSwapChainRelease;
ReleaseSRWLockExclusive(&m_lock);
bool rebuild;
{
CSRWExclusiveLock departedLock(m_lock);
m_monitorDeparted = true;
rebuild = !m_waitForSwapChainRelease;
}
// If there was no swap chain there will be no unassign callback to queue
// the rebuild. Otherwise OnSwapChainReleased does so after teardown drains.
@@ -157,41 +169,39 @@ CMonitorManager::ReplugAction CMonitorManager::Replug()
void CMonitorManager::RequestMode(const CSettings::DisplayMode& mode)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
m_setMode = mode;
m_doSetMode = true;
ReleaseSRWLockExclusive(&m_lock);
}
void CMonitorManager::OnDestroyed(IDDCX_MONITOR monitor)
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
if (m_monitor == monitor)
m_monitor = nullptr;
ReleaseSRWLockExclusive(&m_lock);
}
void CMonitorManager::OnSwapChainAssigned()
{
AcquireSRWLockExclusive(&m_lock);
CSRWExclusiveLock lock(m_lock);
m_swapChainAssigned = true;
m_swapChainReady = false;
ReleaseSRWLockExclusive(&m_lock);
}
void CMonitorManager::OnSwapChainReleased()
{
bool rebuild = false;
AcquireSRWLockExclusive(&m_lock);
m_swapChainAssigned = false;
m_swapChainReady = false;
if (m_replugMonitor && m_waitForSwapChainRelease)
{
m_waitForSwapChainRelease = false;
rebuild = m_monitorDeparted;
CSRWExclusiveLock lock(m_lock);
m_swapChainAssigned = false;
m_swapChainReady = false;
if (m_replugMonitor && m_waitForSwapChainRelease)
{
m_waitForSwapChainRelease = false;
rebuild = m_monitorDeparted;
}
}
ReleaseSRWLockExclusive(&m_lock);
if (rebuild)
m_createQueued.store(1);
@@ -202,33 +212,34 @@ CMonitorManager::ReadyAction CMonitorManager::OnSwapChainReady()
ReadyAction action = {};
bool replug = false;
AcquireSRWLockExclusive(&m_lock);
m_swapChainReady = true;
if (m_replugMonitor)
{
m_replugMonitor = false;
m_monitorDeparted = false;
if (m_replugPending)
CSRWExclusiveLock lock(m_lock);
m_swapChainReady = true;
if (m_replugMonitor)
{
m_replugMonitor = false;
m_monitorDeparted = false;
if (m_replugPending)
{
m_replugPending = false;
replug = true;
}
}
else if (m_replugPending)
{
m_replugPending = false;
replug = true;
}
}
else if (m_replugPending)
{
m_replugPending = false;
replug = true;
}
// Do not consume the requested mode on an intermediate replacement swap
// chain. The last coalesced replug must be the one that applies it.
if (!replug && m_doSetMode)
{
action.mode = m_setMode;
m_doSetMode = false;
action.setMode = true;
// Do not consume the requested mode on an intermediate replacement swap
// chain. The last coalesced replug must be the one that applies it.
if (!replug && m_doSetMode)
{
action.mode = m_setMode;
m_doSetMode = false;
action.setMode = true;
}
}
ReleaseSRWLockExclusive(&m_lock);
action.replug = replug;

View File

@@ -20,6 +20,7 @@
#pragma once
#include "CSRWLock.h"
#include <Windows.h>
#include <wdf.h>
#include <IddCx.h>
@@ -58,7 +59,7 @@ private:
// Guards the monitor/replug/swap-chain state. These values are touched by
// IddCx callback threads, the swap-chain thread, and the transport timer.
SRWLOCK m_lock = SRWLOCK_INIT;
CSRWLock m_lock;
bool m_replugMonitor = false;
bool m_replugPending = false;