[idd] transport: add multi-instance lifecycle manager

This commit is contained in:
Geoffrey McRae
2026-08-12 16:19:22 +10:00
parent 6b8b361101
commit 1ebf80fbe8
9 changed files with 452 additions and 11 deletions

View File

@@ -60,6 +60,7 @@
<ClCompile Include="postprocess\effect\CDownsampleEffect.cpp" /> <ClCompile Include="postprocess\effect\CDownsampleEffect.cpp" />
<ClCompile Include="postprocess\effect\CHDR16to10Effect.cpp" /> <ClCompile Include="postprocess\effect\CHDR16to10Effect.cpp" />
<ClCompile Include="postprocess\effect\CRGB24Effect.cpp" /> <ClCompile Include="postprocess\effect\CRGB24Effect.cpp" />
<ClCompile Include="transport\CTransportManager.cpp" />
<ClCompile Include="transport\TransportFactory.cpp" /> <ClCompile Include="transport\TransportFactory.cpp" />
<ClCompile Include="transport\lgmp\CIVSHMEM.cpp" /> <ClCompile Include="transport\lgmp\CIVSHMEM.cpp" />
<ClCompile Include="transport\lgmp\CRecovery.cpp" /> <ClCompile Include="transport\lgmp\CRecovery.cpp" />
@@ -106,6 +107,7 @@
<ClInclude Include="postprocess\effect\CDownsampleEffect.h" /> <ClInclude Include="postprocess\effect\CDownsampleEffect.h" />
<ClInclude Include="postprocess\effect\CHDR16to10Effect.h" /> <ClInclude Include="postprocess\effect\CHDR16to10Effect.h" />
<ClInclude Include="postprocess\effect\CRGB24Effect.h" /> <ClInclude Include="postprocess\effect\CRGB24Effect.h" />
<ClInclude Include="transport\CTransportManager.h" />
<ClInclude Include="transport\DirectFrameBufferMemory.h" /> <ClInclude Include="transport\DirectFrameBufferMemory.h" />
<ClInclude Include="transport\FrameMemoryLimits.h" /> <ClInclude Include="transport\FrameMemoryLimits.h" />
<ClInclude Include="transport\IControlTransport.h" /> <ClInclude Include="transport\IControlTransport.h" />

View File

@@ -154,6 +154,9 @@
<ClInclude Include="postprocess\effect\CRGB24Effect.h"> <ClInclude Include="postprocess\effect\CRGB24Effect.h">
<Filter>Post-processing\Effects</Filter> <Filter>Post-processing\Effects</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="transport\CTransportManager.h">
<Filter>Transport</Filter>
</ClInclude>
<ClInclude Include="transport\DirectFrameBufferMemory.h"> <ClInclude Include="transport\DirectFrameBufferMemory.h">
<Filter>Transport</Filter> <Filter>Transport</Filter>
</ClInclude> </ClInclude>
@@ -291,6 +294,9 @@
<ClCompile Include="postprocess\effect\CRGB24Effect.cpp"> <ClCompile Include="postprocess\effect\CRGB24Effect.cpp">
<Filter>Post-processing\Effects</Filter> <Filter>Post-processing\Effects</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="transport\CTransportManager.cpp">
<Filter>Transport</Filter>
</ClCompile>
<ClCompile Include="transport\TransportFactory.cpp"> <ClCompile Include="transport\TransportFactory.cpp">
<Filter>Transport</Filter> <Filter>Transport</Filter>
</ClCompile> </ClCompile>

View File

@@ -64,8 +64,8 @@ CDeviceContext::~CDeviceContext()
m_transportTimer = nullptr; m_transportTimer = nullptr;
} }
if (m_transport && m_transport->Input()) if (m_transport)
m_transport->Input()->Stop(); m_transport->Stop();
} }
void CDeviceContext::QueryIddCxCapabilities() void CDeviceContext::QueryIddCxCapabilities()

View File

@@ -0,0 +1,308 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "transport/CTransportManager.h"
#include "CDebug.h"
#include <Windows.h>
#include <new>
static const uint64_t RETRY_DELAY_MS = 500;
CTransportManager::~CTransportManager()
{
Stop();
}
bool CTransportManager::Add(BackendId id, const char * name, bool required,
bool primary, CreateFn create)
{
if (!id || !name || !create || (primary && m_primary))
return false;
for (const auto& current : m_entries)
if (current->id == id)
return false;
std::unique_ptr<Entry> entry(new (std::nothrow) Entry);
if (!entry)
return false;
entry->id = id;
entry->name = name;
entry->required = required;
entry->primary = primary;
entry->create = create;
Entry * raw = entry.get();
m_entries.push_back(std::move(entry));
if (primary)
m_primary = raw;
return true;
}
ITransport::OpenResult CTransportManager::OpenEntry(Entry& entry)
{
if (!entry.transport)
entry.transport = entry.create();
if (!entry.transport)
{
entry.state = State::FAILED;
return OpenResult::FAILURE;
}
const OpenResult result = entry.transport->Open();
switch (result)
{
case OpenResult::SUCCESS:
entry.state = State::OPEN;
break;
case OpenResult::RETRY:
ScheduleRetry(entry);
break;
case OpenResult::FAILURE:
entry.state = State::FAILED;
break;
}
return result;
}
bool CTransportManager::InitializeEntry(Entry& entry)
{
if (entry.state == State::INITIALIZED || entry.state == State::READY)
return true;
if (entry.state != State::OPEN || !entry.transport->Initialize())
{
entry.state = State::FAILED;
return false;
}
entry.state = State::INITIALIZED;
return true;
}
bool CTransportManager::SetupEntry(Entry& entry)
{
if (entry.state == State::READY)
return true;
if (entry.state != State::INITIALIZED ||
!entry.transport->Setup(m_alignment))
{
entry.state = State::FAILED;
return false;
}
entry.state = State::READY;
return true;
}
void CTransportManager::ScheduleRetry(Entry& entry)
{
entry.state = State::RETRY;
entry.retryAt = GetTickCount64() + RETRY_DELAY_MS;
}
ITransport::OpenResult CTransportManager::Open()
{
if (!m_primary)
return OpenResult::FAILURE;
OpenResult aggregate = OpenResult::SUCCESS;
for (const auto& current : m_entries)
{
Entry& entry = *current;
if (entry.state == State::OPEN ||
entry.state == State::INITIALIZED || entry.state == State::READY)
continue;
const OpenResult result = OpenEntry(entry);
if (!entry.required || result == OpenResult::SUCCESS)
continue;
if (result == OpenResult::FAILURE)
return OpenResult::FAILURE;
aggregate = OpenResult::RETRY;
}
return aggregate;
}
bool CTransportManager::Initialize()
{
for (const auto& current : m_entries)
{
Entry& entry = *current;
if (entry.state != State::OPEN)
continue;
if (!InitializeEntry(entry))
{
if (entry.required)
return false;
ScheduleRetry(entry);
}
}
m_initialized = true;
return m_primary &&
(m_primary->state == State::INITIALIZED ||
m_primary->state == State::READY);
}
bool CTransportManager::Setup(size_t alignment)
{
if (!m_initialized || !m_primary)
return false;
m_alignment = alignment;
if (!SetupEntry(*m_primary))
return false;
m_setup = true;
return true;
}
void CTransportManager::RetryEntry(Entry& entry, uint64_t now)
{
if (entry.state != State::RETRY || now < entry.retryAt)
return;
if (entry.primary && m_exposed)
return;
if (entry.transport)
entry.transport->Stop();
entry.transport.reset();
++entry.epoch;
if (!entry.epoch)
++entry.epoch;
if (OpenEntry(entry) != OpenResult::SUCCESS)
return;
if (m_initialized && !InitializeEntry(entry))
{
ScheduleRetry(entry);
return;
}
if (m_setup && entry.primary && !SetupEntry(entry))
ScheduleRetry(entry);
}
void CTransportManager::HandleProcessResult(
Entry& entry, ProcessResult result)
{
if (result == ProcessResult::OK)
return;
if (entry.primary && m_exposed)
{
DEBUG_WARN("Transport %s requested a restart while its frame interfaces "
"are active", entry.name);
return;
}
if (result == ProcessResult::RETRY || !entry.required)
{
ScheduleRetry(entry);
return;
}
entry.state = State::FAILED;
}
ITransport::ProcessResult CTransportManager::Process(
ITransportEvents& events)
{
const uint64_t now = GetTickCount64();
for (const auto& current : m_entries)
{
Entry& entry = *current;
RetryEntry(entry, now);
if (entry.state != State::INITIALIZED && entry.state != State::READY)
continue;
const ProcessResult result = entry.transport->Process(events);
HandleProcessResult(entry, result);
}
return ProcessResult::OK;
}
void CTransportManager::Stop()
{
for (auto current = m_entries.rbegin(); current != m_entries.rend();
++current)
{
Entry& entry = **current;
if (entry.transport && entry.state != State::STOPPED)
entry.transport->Stop();
entry.state = State::STOPPED;
}
}
void CTransportManager::SyncRecovery()
{
for (const auto& current : m_entries)
if (current->transport &&
(current->state == State::INITIALIZED ||
current->state == State::READY))
current->transport->SyncRecovery();
}
void CTransportManager::RecoveryStatus(uint64_t session, uint32_t serial,
bool active, Recovery state, uint32_t error)
{
for (const auto& current : m_entries)
if (current->transport &&
(current->state == State::INITIALIZED ||
current->state == State::READY))
current->transport->RecoveryStatus(
session, serial, active, state, error);
}
ITransport& CTransportManager::Primary()
{
m_exposed = true;
return *m_primary->transport;
}
FrameMemoryLimits CTransportManager::GetMemoryLimits() const
{
return m_primary->transport->GetMemoryLimits();
}
DirectFrameBufferMemory CTransportManager::GetDirectMemory() const
{
return m_primary->transport->GetDirectMemory();
}
IFrameTransport& CTransportManager::Frames()
{
return Primary().Frames();
}
IControlTransport& CTransportManager::Control()
{
return Primary().Control();
}
IInputTransport * CTransportManager::Input()
{
return Primary().Input();
}

View File

@@ -0,0 +1,98 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include "transport/ITransport.h"
#include <memory>
#include <vector>
class CTransportManager final : public ITransport
{
public:
using CreateFn = std::unique_ptr<ITransport> (*)();
private:
enum class State
{
CLOSED,
OPEN,
INITIALIZED,
READY,
RETRY,
FAILED,
STOPPED,
};
struct Entry
{
BackendId id;
const char * name;
bool required;
bool primary;
CreateFn create;
std::unique_ptr<ITransport> transport;
State state = State::CLOSED;
uint32_t epoch = 1;
uint64_t retryAt = 0;
};
std::vector<std::unique_ptr<Entry>> m_entries;
Entry * m_primary = nullptr;
bool m_initialized = false;
bool m_setup = false;
bool m_exposed = false;
size_t m_alignment = 0;
OpenResult OpenEntry(Entry& entry);
bool InitializeEntry(Entry& entry);
bool SetupEntry(Entry& entry);
void RetryEntry(Entry& entry, uint64_t now);
void HandleProcessResult(Entry& entry, ProcessResult result);
void ScheduleRetry(Entry& entry);
ITransport& Primary();
public:
CTransportManager() = default;
~CTransportManager() override;
CTransportManager(const CTransportManager&) = delete;
CTransportManager& operator=(const CTransportManager&) = delete;
bool Add(BackendId id, const char * name, bool required, bool primary,
CreateFn create);
OpenResult Open() override;
bool Initialize() override;
bool Setup(size_t alignment) override;
ProcessResult Process(ITransportEvents& events) override;
void Stop() override;
void SyncRecovery() override;
void RecoveryStatus(uint64_t session, uint32_t serial, bool active,
Recovery state, uint32_t error) override;
FrameMemoryLimits GetMemoryLimits() const override;
DirectFrameBufferMemory GetDirectMemory() const override;
IFrameTransport& Frames() override;
IControlTransport& Control() override;
IInputTransport * Input() override;
};

View File

@@ -44,6 +44,8 @@ public:
class ITransport class ITransport
{ {
public: public:
using BackendId = uint32_t;
enum class OpenResult enum class OpenResult
{ {
SUCCESS, SUCCESS,
@@ -51,6 +53,13 @@ public:
FAILURE, FAILURE,
}; };
enum class ProcessResult
{
OK,
RETRY,
FAILURE,
};
enum class Recovery enum class Recovery
{ {
NORMAL, NORMAL,
@@ -63,7 +72,8 @@ public:
virtual OpenResult Open() = 0; virtual OpenResult Open() = 0;
virtual bool Initialize() = 0; virtual bool Initialize() = 0;
virtual bool Setup(size_t alignment) = 0; virtual bool Setup(size_t alignment) = 0;
virtual void Process(ITransportEvents& events) = 0; virtual ProcessResult Process(ITransportEvents& events) = 0;
virtual void Stop() = 0;
virtual void SyncRecovery() {} virtual void SyncRecovery() {}
virtual void RecoveryStatus( virtual void RecoveryStatus(
uint64_t, uint32_t, bool, Recovery, uint32_t) {} uint64_t, uint32_t, bool, Recovery, uint32_t) {}

View File

@@ -20,11 +20,21 @@
#include "transport/TransportFactory.h" #include "transport/TransportFactory.h"
#include "transport/CTransportManager.h"
#include "transport/lgmp/CLGMPTransport.h" #include "transport/lgmp/CLGMPTransport.h"
#include <new> #include <new>
std::unique_ptr<ITransport> CreateTransport() static std::unique_ptr<ITransport> CreateLGMP()
{ {
return std::unique_ptr<ITransport>(new (std::nothrow) CLGMPTransport()); return std::unique_ptr<ITransport>(new (std::nothrow) CLGMPTransport());
} }
std::unique_ptr<ITransport> CreateTransport()
{
std::unique_ptr<CTransportManager> manager(
new (std::nothrow) CTransportManager());
if (!manager || !manager->Add(1, "LGMP", true, true, CreateLGMP))
return std::unique_ptr<ITransport>();
return std::unique_ptr<ITransport>(manager.release());
}

View File

@@ -93,7 +93,7 @@ bool CLGMPTransport::Setup(size_t alignment)
return true; return true;
} }
void CLGMPTransport::Process(ITransportEvents& events) ITransport::ProcessResult CLGMPTransport::Process(ITransportEvents& events)
{ {
const CRecovery::Request recovery = m_recovery.Process(); const CRecovery::Request recovery = m_recovery.Process();
if (recovery.valid) if (recovery.valid)
@@ -104,7 +104,7 @@ void CLGMPTransport::Process(ITransportEvents& events)
// only the protocol-independent recovery channel. This preserves the old // only the protocol-independent recovery channel. This preserves the old
// transport startup boundary while keeping recovery available immediately. // transport startup boundary while keeping recovery available immediately.
if (!m_ready.load(std::memory_order_acquire)) if (!m_ready.load(std::memory_order_acquire))
return; return ProcessResult::OK;
const LGMP_STATUS processStatus = m_host.Process(); const LGMP_STATUS processStatus = m_host.Process();
if (processStatus != LGMP_OK) if (processStatus != LGMP_OK)
@@ -113,14 +113,12 @@ void CLGMPTransport::Process(ITransportEvents& events)
{ {
DEBUG_WARN( DEBUG_WARN(
"LGMP reported the shared memory has been corrupted, attempting to recover\n"); "LGMP reported the shared memory has been corrupted, attempting to recover\n");
// TODO: reinitialize LGMP. return ProcessResult::RETRY;
return;
} }
DEBUG_ERROR("lgmpHostProcess Failed: %s", DEBUG_ERROR("lgmpHostProcess Failed: %s",
lgmpStatusString(processStatus)); lgmpStatusString(processStatus));
// TODO: shut down LGMP. return ProcessResult::FAILURE;
return;
} }
const uint64_t now = CFrameScheduler::Nanotime(); const uint64_t now = CFrameScheduler::Nanotime();
@@ -191,6 +189,14 @@ void CLGMPTransport::Process(ITransportEvents& events)
if (m_control.HasNewSubscribers()) if (m_control.HasNewSubscribers())
m_control.ResendState(); m_control.ResendState();
return ProcessResult::OK;
}
void CLGMPTransport::Stop()
{
m_ready.store(false, std::memory_order_release);
m_input.Stop();
} }
void CLGMPTransport::SyncRecovery() void CLGMPTransport::SyncRecovery()

View File

@@ -53,7 +53,8 @@ public:
OpenResult Open() override; OpenResult Open() override;
bool Initialize() override; bool Initialize() override;
bool Setup(size_t alignment) override; bool Setup(size_t alignment) override;
void Process(ITransportEvents& events) override; ProcessResult Process(ITransportEvents& events) override;
void Stop() override;
void SyncRecovery() override; void SyncRecovery() override;
void RecoveryStatus( void RecoveryStatus(
uint64_t session, uint32_t serial, bool active, uint64_t session, uint32_t serial, bool active,