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, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <thread_pool.h>
23
24
28template <typename ContainerT>
30{
31public:
32 using ItemT = typename ContainerT::value_type;
33
35 m_reporter( nullptr ),
36 m_highestPriority( BS::pr::high ),
37 m_reporterInterval( std::chrono::milliseconds( 250 ) )
38 {
39 }
40
41 void SetReporter( PROGRESS_REPORTER* aReporter ) { m_reporter = aReporter; }
42
48 void Execute( ContainerT& aItems )
49 {
51 std::vector<std::future<size_t>> returns;
52
53 // Compute priority keys paired to item indices
54 using IndexedPriority = std::pair<size_t, int>;
55 std::vector<IndexedPriority> indexedKeys( aItems.size() );
56 for( size_t i = 0; i < aItems.size(); ++i )
57 {
58 indexedKeys[i] = { i, computePriorityKey( aItems[i] ) };
59 }
60
61 // Sort by descending priority key
62 std::sort( indexedKeys.begin(), indexedKeys.end(),
63 []( const IndexedPriority& a, const IndexedPriority& b )
64 {
65 return a.second > b.second;
66 } );
67
68 // Dispatch largest first
69 const size_t numItems = aItems.size();
70 for( size_t priorityRank = 0; priorityRank < numItems; ++priorityRank )
71 {
72 const size_t itemIndex = indexedKeys[priorityRank].first;
73 ItemT& item = aItems[itemIndex];
74
75 // Earlier ranking -> higher key -> should be higher priority
76 const size_t priority = ( ( numItems - priorityRank - 1 ) * m_highestPriority ) / numItems;
77
78 returns.emplace_back( tp.submit_task(
79 [this, &item]
80 {
81 return task( item );
82 },
83 priority ) );
84 }
85
86 for( const std::future<size_t>& ret : returns )
87 {
88 std::future_status status = ret.wait_for( m_reporterInterval );
89
90 while( status != std::future_status::ready )
91 {
92 if( m_reporter )
93 m_reporter->KeepRefreshing();
94
95 status = ret.wait_for( m_reporterInterval );
96 }
97 }
98 }
99
100protected:
102
103private:
118 virtual int computePriorityKey( const ItemT& aItem ) const = 0;
119
123 virtual size_t task( ItemT& item ) = 0;
124
125 BS::priority_t m_highestPriority;
126 std::chrono::milliseconds m_reporterInterval;
127};
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:27