KiCad PCB EDA Suite
Loading...
Searching...
No Matches
conn_tasks.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <thread_pool.h>
23#include <algorithm>
24#include <future>
25#include <utility>
26#include <vector>
27
28namespace SCH_CONNECTIVITY
29{
30// Own submitted work until all references captured by workers can safely leave scope.
32{
33public:
34 explicit TASK_GROUP( size_t aCount ) { m_tasks.reserve( aCount ); }
35 TASK_GROUP( const TASK_GROUP& ) = delete;
36 TASK_GROUP& operator=( const TASK_GROUP& ) = delete;
37
39 {
40 for( auto& task : m_tasks )
41 {
42 if( task.valid() )
43 task.wait();
44 }
45 }
46
47 template <typename FUNCTION>
48 void Submit( thread_pool& aPool, FUNCTION&& aFunction )
49 {
50 if( m_tasks.size() == m_tasks.capacity() )
51 m_tasks.reserve( std::max<size_t>( 1, m_tasks.capacity() * 2 ) );
52
53 m_tasks.push_back( aPool.submit_task( std::forward<FUNCTION>( aFunction ) ) );
54 }
55
56 void Get()
57 {
58 for( auto& task : m_tasks )
59 task.get();
60 }
61
62private:
63 std::vector<std::future<void>> m_tasks;
64};
65
71template <typename FUNCTION>
72void ParallelFor( size_t aCount, FUNCTION&& aFunction, thread_pool& aPool = GetKiCadThreadPool() )
73{
74 if( aCount <= 1 )
75 {
76 if( aCount == 1 )
77 aFunction( 0 );
78
79 return;
80 }
81
82 const size_t workers = std::min<size_t>( aCount, aPool.get_thread_count() );
83 TASK_GROUP tasks( workers );
84
85 for( size_t worker = 0; worker < workers; ++worker )
86 {
87 tasks.Submit( aPool, [&, worker]
88 {
89 for( size_t ordinal = worker; ordinal < aCount; ordinal += workers )
90 aFunction( ordinal );
91 } );
92 }
93
94 tasks.Get();
95}
96} // namespace SCH_CONNECTIVITY
std::vector< std::future< void > > m_tasks
Definition conn_tasks.h:63
void Submit(thread_pool &aPool, FUNCTION &&aFunction)
Definition conn_tasks.h:48
TASK_GROUP(const TASK_GROUP &)=delete
TASK_GROUP & operator=(const TASK_GROUP &)=delete
Value keys and the key session of the schematic connectivity engine.
void ParallelFor(size_t aCount, FUNCTION &&aFunction, thread_pool &aPool=GetKiCadThreadPool())
Independent ordinal writes only; preparation and cache commits stay on the caller thread.
Definition conn_tasks.h:72
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27