KiCad PCB EDA Suite
Loading...
Searching...
No Matches
KIGIT_ORPHAN_REGISTRY Class Reference

Registry of background git cleanup threads that outlive the owning project or dialog. More...

#include <kigit_orphan_registry.h>

Classes

struct  ENTRY
 
struct  SHARED_STATE
 

Public Member Functions

 KIGIT_ORPHAN_REGISTRY ()
 
 ~KIGIT_ORPHAN_REGISTRY ()
 
 KIGIT_ORPHAN_REGISTRY (const KIGIT_ORPHAN_REGISTRY &)=delete
 
KIGIT_ORPHAN_REGISTRYoperator= (const KIGIT_ORPHAN_REGISTRY &)=delete
 
template<typename F>
bool Register (const std::string &aLabel, F &&aWork)
 Spawn a tracked orphan thread running aWork.
 
size_t TrackedCount () const
 Return the number of tracked orphan threads that have not yet finished executing.
 
size_t JoinAll (std::chrono::milliseconds aTimeout)
 Join all registered orphan threads, giving them up to aTimeout to finish cooperatively.
 

Private Member Functions

void reapLocked ()
 

Private Attributes

std::mutex m_mutex
 
std::vector< ENTRYm_entries
 
bool m_shuttingDown = false
 

Detailed Description

Registry of background git cleanup threads that outlive the owning project or dialog.

When a user abandons a slow git close (for example, a fetch stuck under a blocking recv) we cannot wait for the libgit2 worker to notice cancellation before the UI returns, but we also cannot call git_libgit2_shutdown() while that worker is still inside libgit2. The registry lets us track such "orphan" threads and join them with a bounded timeout during application shutdown.

Each entry is reference-counted via std::shared_ptr so that a detached worker never touches freed registry state after the registry has given up on it: the worker and the registry each hold their own shared reference to a small state block.

Definition at line 59 of file kigit_orphan_registry.h.

Constructor & Destructor Documentation

◆ KIGIT_ORPHAN_REGISTRY() [1/2]

KIGIT_ORPHAN_REGISTRY::KIGIT_ORPHAN_REGISTRY ( )
default

◆ ~KIGIT_ORPHAN_REGISTRY()

KIGIT_ORPHAN_REGISTRY::~KIGIT_ORPHAN_REGISTRY ( )

Definition at line 30 of file kigit_orphan_registry.cpp.

References m_entries, and m_mutex.

◆ KIGIT_ORPHAN_REGISTRY() [2/2]

KIGIT_ORPHAN_REGISTRY::KIGIT_ORPHAN_REGISTRY ( const KIGIT_ORPHAN_REGISTRY & )
delete

Member Function Documentation

◆ JoinAll()

size_t KIGIT_ORPHAN_REGISTRY::JoinAll ( std::chrono::milliseconds aTimeout)

Join all registered orphan threads, giving them up to aTimeout to finish cooperatively.

After the timeout the remaining threads are detached so the caller can proceed with teardown; the shared per-entry state outlives the registry until the worker itself drops its reference, so detached workers are safe to keep running.

Once JoinAll() has been called the registry enters a shutting-down state and Register() returns false; this prevents a late caller from queueing a new orphan after the join window has already closed.

Returns
Number of threads that failed to join within the timeout.

Definition at line 65 of file kigit_orphan_registry.cpp.

References m_entries, m_mutex, m_shuttingDown, and traceGit.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().

◆ operator=()

KIGIT_ORPHAN_REGISTRY & KIGIT_ORPHAN_REGISTRY::operator= ( const KIGIT_ORPHAN_REGISTRY & )
delete

◆ reapLocked()

void KIGIT_ORPHAN_REGISTRY::reapLocked ( )
private

Definition at line 119 of file kigit_orphan_registry.cpp.

References m_entries.

Referenced by Register().

◆ Register()

template<typename F>
bool KIGIT_ORPHAN_REGISTRY::Register ( const std::string & aLabel,
F && aWork )
inline

Spawn a tracked orphan thread running aWork.

The thread is owned by the registry and will be joined during JoinAll() or on destruction.

The callable is accepted by forwarding reference so move-only work objects (for example lambdas capturing a std::unique_ptr) are supported without forcing shared ownership on the caller.

If the registry has already begun shutdown via JoinAll() the call returns false and aWork is not invoked; the caller is expected to run the cleanup synchronously or drop it.

Parameters
aLabelHuman-readable tag used in trace logs, e.g. "abandon close /home/user/proj.kicad_pro".
aWorkWork function executed on the new thread. It should honour any cooperative cancellation flag supplied by the caller before performing blocking libgit2 calls.
Returns
true if the thread was registered, false if the registry is shutting down.

Definition at line 90 of file kigit_orphan_registry.h.

References F, KIGIT_ORPHAN_REGISTRY::ENTRY::m_created, m_entries, KIGIT_ORPHAN_REGISTRY::ENTRY::m_label, m_mutex, m_shuttingDown, KIGIT_ORPHAN_REGISTRY::ENTRY::m_state, KIGIT_ORPHAN_REGISTRY::ENTRY::m_thread, reapLocked(), and traceGit.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), and PROJECT_TREE_PANE::EmptyTreePrj().

◆ TrackedCount()

size_t KIGIT_ORPHAN_REGISTRY::TrackedCount ( ) const

Return the number of tracked orphan threads that have not yet finished executing.

Threads that JoinAll() detached after a timeout are no longer tracked and do not contribute to this count.

Definition at line 50 of file kigit_orphan_registry.cpp.

References m_entries, and m_mutex.

Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().

Member Data Documentation

◆ m_entries

std::vector<ENTRY> KIGIT_ORPHAN_REGISTRY::m_entries
private

◆ m_mutex

std::mutex KIGIT_ORPHAN_REGISTRY::m_mutex
mutableprivate

Definition at line 189 of file kigit_orphan_registry.h.

Referenced by JoinAll(), Register(), TrackedCount(), and ~KIGIT_ORPHAN_REGISTRY().

◆ m_shuttingDown

bool KIGIT_ORPHAN_REGISTRY::m_shuttingDown = false
private

Definition at line 191 of file kigit_orphan_registry.h.

Referenced by JoinAll(), and Register().


The documentation for this class was generated from the following files: