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
60{
61public:
63
65
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
169private:
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_
bool Register(const std::string &aLabel, F &&aWork)
Spawn a tracked orphan thread running aWork.
KIGIT_ORPHAN_REGISTRY(const KIGIT_ORPHAN_REGISTRY &)=delete
std::vector< ENTRY > m_entries
KIGIT_ORPHAN_REGISTRY & operator=(const KIGIT_ORPHAN_REGISTRY &)=delete
const wxChar *const traceGit
Flag to enable Git debugging output.
#define APIEXPORT
Macros which export functions from a DLL/DSO.
#define F(x, y, z)
Definition md5_hash.cpp:15
std::shared_ptr< SHARED_STATE > m_state
std::chrono::steady_clock::time_point m_created
wxLogTrace helper definitions.