KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_orphan_registry.h
Go to the documentation of this file.
1
/*
2
* This program source code file is part of KiCad, a free EDA CAD application.
3
*
4
* Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 3
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, you may find one here:
18
* http://www.gnu.org/licenses/gpl-3.0.html
19
* or you may search the http://www.gnu.org website for the version 3 license,
20
* or you may write to the Free Software Foundation, Inc.,
21
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22
*/
23
24
#ifndef KIGIT_ORPHAN_REGISTRY_H_
25
#define KIGIT_ORPHAN_REGISTRY_H_
26
27
#include <
import_export.h
>
28
29
#include <atomic>
30
#include <chrono>
31
#include <condition_variable>
32
#include <memory>
33
#include <mutex>
34
#include <string>
35
#include <thread>
36
#include <utility>
37
#include <vector>
38
39
#include <wx/log.h>
40
41
#include <
trace_helpers.h
>
42
59
class
APIEXPORT
KIGIT_ORPHAN_REGISTRY
60
{
61
public
:
62
KIGIT_ORPHAN_REGISTRY
();
63
64
~KIGIT_ORPHAN_REGISTRY
();
65
66
KIGIT_ORPHAN_REGISTRY
(
const
KIGIT_ORPHAN_REGISTRY
& ) =
delete
;
67
KIGIT_ORPHAN_REGISTRY
&
operator=
(
const
KIGIT_ORPHAN_REGISTRY
& ) =
delete
;
68
89
template
<
typename
F>
90
bool
Register
(
const
std::string& aLabel,
F
&& aWork )
91
{
92
auto
state = std::make_shared<SHARED_STATE>();
93
94
std::lock_guard<std::mutex> lock(
m_mutex
);
95
96
if
(
m_shuttingDown
)
97
return
false
;
98
99
reapLocked
();
100
101
// Reserve the slot before starting the thread so we don't leak a
102
// joinable std::thread if vector growth throws; emplace the entry
103
// with the state already wired up and then assign m_thread.
104
105
m_entries
.emplace_back();
106
ENTRY
& entry =
m_entries
.back();
107
entry.
m_label
= aLabel;
108
entry.
m_created
= std::chrono::steady_clock::now();
109
entry.
m_state
= state;
110
111
try
112
{
113
entry.
m_thread
= std::thread(
114
[state,
115
work = std::forward<F>( aWork ),
116
label = aLabel]()
mutable
117
{
118
try
119
{
120
work();
121
}
122
catch
( ... )
123
{
124
wxLogTrace(
traceGit
,
125
"Orphan git thread [%s] threw an exception"
,
126
label.c_str() );
127
}
128
129
{
130
std::lock_guard<std::mutex> doneLock( state->m_doneMutex );
131
state->m_done.store(
true
, std::memory_order_release );
132
}
133
134
state->m_doneCv.notify_all();
135
} );
136
}
137
catch
( ... )
138
{
139
m_entries
.pop_back();
140
throw
;
141
}
142
143
wxLogTrace(
traceGit
,
"Registered orphan git thread [%s]"
, aLabel.c_str() );
144
return
true
;
145
}
146
152
size_t
TrackedCount()
const
;
153
167
size_t
JoinAll( std::chrono::milliseconds aTimeout );
168
169
private
:
170
struct
SHARED_STATE
171
{
172
std::atomic<bool>
m_done
{
false
};
173
std::mutex
m_doneMutex
;
174
std::condition_variable
m_doneCv
;
175
};
176
177
struct
ENTRY
178
{
179
std::thread
m_thread
;
180
std::string
m_label
;
181
std::chrono::steady_clock::time_point
m_created
;
182
std::shared_ptr<SHARED_STATE>
m_state
;
183
};
184
185
// Remove finished entries by joining their threads. Must be called
186
// with m_mutex held.
187
void
reapLocked
();
188
189
mutable
std::mutex
m_mutex
;
190
std::vector<ENTRY>
m_entries
;
191
bool
m_shuttingDown
=
false
;
192
};
193
194
#endif
// KIGIT_ORPHAN_REGISTRY_H_
KIGIT_ORPHAN_REGISTRY::Register
bool Register(const std::string &aLabel, F &&aWork)
Spawn a tracked orphan thread running aWork.
Definition
kigit_orphan_registry.h:90
KIGIT_ORPHAN_REGISTRY::KIGIT_ORPHAN_REGISTRY
KIGIT_ORPHAN_REGISTRY(const KIGIT_ORPHAN_REGISTRY &)=delete
KIGIT_ORPHAN_REGISTRY::m_mutex
std::mutex m_mutex
Definition
kigit_orphan_registry.h:189
KIGIT_ORPHAN_REGISTRY::m_shuttingDown
bool m_shuttingDown
Definition
kigit_orphan_registry.h:191
KIGIT_ORPHAN_REGISTRY::m_entries
std::vector< ENTRY > m_entries
Definition
kigit_orphan_registry.h:190
KIGIT_ORPHAN_REGISTRY::reapLocked
void reapLocked()
Definition
kigit_orphan_registry.cpp:119
KIGIT_ORPHAN_REGISTRY::operator=
KIGIT_ORPHAN_REGISTRY & operator=(const KIGIT_ORPHAN_REGISTRY &)=delete
KIGIT_ORPHAN_REGISTRY::KIGIT_ORPHAN_REGISTRY
KIGIT_ORPHAN_REGISTRY()
traceGit
const wxChar *const traceGit
Flag to enable Git debugging output.
Definition
trace_helpers.cpp:67
import_export.h
APIEXPORT
#define APIEXPORT
Macros which export functions from a DLL/DSO.
Definition
import_export.h:42
F
#define F(x, y, z)
Definition
md5_hash.cpp:15
KIGIT_ORPHAN_REGISTRY::ENTRY
Definition
kigit_orphan_registry.h:178
KIGIT_ORPHAN_REGISTRY::ENTRY::m_state
std::shared_ptr< SHARED_STATE > m_state
Definition
kigit_orphan_registry.h:182
KIGIT_ORPHAN_REGISTRY::ENTRY::m_created
std::chrono::steady_clock::time_point m_created
Definition
kigit_orphan_registry.h:181
KIGIT_ORPHAN_REGISTRY::ENTRY::m_label
std::string m_label
Definition
kigit_orphan_registry.h:180
KIGIT_ORPHAN_REGISTRY::ENTRY::m_thread
std::thread m_thread
Definition
kigit_orphan_registry.h:179
KIGIT_ORPHAN_REGISTRY::SHARED_STATE
Definition
kigit_orphan_registry.h:171
KIGIT_ORPHAN_REGISTRY::SHARED_STATE::m_doneMutex
std::mutex m_doneMutex
Definition
kigit_orphan_registry.h:173
KIGIT_ORPHAN_REGISTRY::SHARED_STATE::m_doneCv
std::condition_variable m_doneCv
Definition
kigit_orphan_registry.h:174
KIGIT_ORPHAN_REGISTRY::SHARED_STATE::m_done
std::atomic< bool > m_done
Definition
kigit_orphan_registry.h:172
trace_helpers.h
wxLogTrace helper definitions.
src
common
git
kigit_orphan_registry.h
Generated on Sun Apr 26 2026 00:06:42 for KiCad PCB EDA Suite by
1.13.2