KiCad PCB EDA Suite
Loading...
Searching...
No Matches
priority_thread_pool_task.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 2
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/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 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#pragma once
25
26#include <thread_pool.h>
27
28
32template <typename ContainerT>
34{
35public:
36 using ItemT = typename ContainerT::value_type;
37
39 m_reporter( nullptr ),
40 m_highestPriority( BS::pr::high ),
41 m_reporterInterval( std::chrono::milliseconds( 250 ) )
42 {
43 }
44
45 void SetReporter( PROGRESS_REPORTER* aReporter ) { m_reporter = aReporter; }
46
52 void Execute( ContainerT& aItems )
53 {
55 std::vector<std::future<size_t>> returns;
56
57 // Compute priority keys paired to item indices
58 using IndexedPriority = std::pair<size_t, int>;
59 std::vector<IndexedPriority> indexedKeys( aItems.size() );
60 for( size_t i = 0; i < aItems.size(); ++i )
61 {
62 indexedKeys[i] = { i, computePriorityKey( aItems[i] ) };
63 }
64
65 // Sort by descending priority key
66 std::sort( indexedKeys.begin(), indexedKeys.end(),
67 []( const IndexedPriority& a, const IndexedPriority& b )
68 {
69 return a.second > b.second;
70 } );
71
72 // Dispatch largest first
73 const size_t numItems = aItems.size();
74 for( size_t priorityRank = 0; priorityRank < numItems; ++priorityRank )
75 {
76 const size_t itemIndex = indexedKeys[priorityRank].first;
77 ItemT& item = aItems[itemIndex];
78
79 // Earlier ranking -> higher key -> should be higher priority
80 const size_t priority = ( ( numItems - priorityRank - 1 ) * m_highestPriority ) / numItems;
81
82 returns.emplace_back( tp.submit_task(
83 [this, &item]
84 {
85 return task( item );
86 },
87 priority ) );
88 }
89
90 for( const std::future<size_t>& ret : returns )
91 {
92 std::future_status status = ret.wait_for( m_reporterInterval );
93
94 while( status != std::future_status::ready )
95 {
96 if( m_reporter )
97 m_reporter->KeepRefreshing();
98
99 status = ret.wait_for( m_reporterInterval );
100 }
101 }
102 }
103
104protected:
106
107private:
122 virtual int computePriorityKey( const ItemT& aItem ) const = 0;
123
127 virtual size_t task( ItemT& item ) = 0;
128
129 BS::priority_t m_highestPriority;
130 std::chrono::milliseconds m_reporterInterval;
131};
void Execute(ContainerT &aItems)
Call this to execute the task on all items in aItems, using the thread pool and dispatching the tasks...
typename ContainerT::value_type ItemT
std::chrono::milliseconds m_reporterInterval
virtual size_t task(ItemT &item)=0
Process one item in the thread pool.
void SetReporter(PROGRESS_REPORTER *aReporter)
virtual int computePriorityKey(const ItemT &aItem) const =0
Implement this to compute a priority key for an item.
A progress reporter interface for use in multi-threaded environments.
Definition singleton.h:27
STL namespace.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:31