ngscopeclient 0.1-dev+51fbda87c
FilterGraphExecutor.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
37#ifndef FilterGraphExecutor_h
38#define FilterGraphExecutor_h
39
40#include <condition_variable>
41#include <atomic>
42
48{
49public:
50 FilterGraphExecutor(size_t numThreads = 8);
52
53 void RunBlocking(const std::set<FlowGraphNode*>& nodes);
54
56
58 std::map<FlowGraphNode*, int64_t> GetRunTimes()
59 {
60 std::lock_guard<std::mutex> lock(m_perfStatsMutex);
62 }
63
64protected:
65 static void ExecutorThread(FilterGraphExecutor* pThis, size_t i);
66 void DoExecutorThread(size_t i);
67
68 void UpdateRunnable();
69
71 std::mutex m_mutex;
72
74 std::set<FlowGraphNode*> m_incompleteNodes;
75
77 std::set<FlowGraphNode*> m_runnableNodes;
78
80 std::set<FlowGraphNode*> m_runningNodes;
81
83 std::vector<std::unique_ptr<std::thread>> m_threads;
84
86 std::condition_variable m_workerCvar;
87
90
92 std::condition_variable m_completionCvar;
93
96
99
102
104 std::map<FlowGraphNode*, int64_t> m_lastExecutionTime;
105
107 std::map<FlowGraphNode*, int64_t> m_currentExecutionTime;
108
111};
112
113#endif
Execution manager / scheduler for the filter graph.
Definition: FilterGraphExecutor.h:48
FlowGraphNode * GetNextRunnableNode()
Returns the next filter available to run, blocking if none are ready.
Definition: FilterGraphExecutor.cpp:123
std::vector< std::unique_ptr< std::thread > > m_threads
Set of thread contexts.
Definition: FilterGraphExecutor.h:83
std::map< FlowGraphNode *, int64_t > m_currentExecutionTime
Performance statistics from current execution.
Definition: FilterGraphExecutor.h:107
std::condition_variable m_completionCvar
Condition variable for waking up main thread when work is complete.
Definition: FilterGraphExecutor.h:92
std::mutex m_perfStatsMutex
Mutex for updating performance statistics.
Definition: FilterGraphExecutor.h:110
std::set< FlowGraphNode * > m_incompleteNodes
Nodes that have not yet been updated.
Definition: FilterGraphExecutor.h:74
std::map< FlowGraphNode *, int64_t > GetRunTimes()
Get the run times of the most recent filter graph evaluation.
Definition: FilterGraphExecutor.h:58
std::map< FlowGraphNode *, int64_t > m_lastExecutionTime
Performance statistics from previous execution.
Definition: FilterGraphExecutor.h:104
void UpdateRunnable()
Searches m_incompleteNodes for any that are unblocked, and adds them to m_runnableNodes.
Definition: FilterGraphExecutor.cpp:160
std::set< FlowGraphNode * > m_runningNodes
Nodes that are actively being run.
Definition: FilterGraphExecutor.h:80
std::mutex m_completionCvarMutex
Mutex for access to m_completionCvar.
Definition: FilterGraphExecutor.h:95
std::mutex m_mutex
Mutex for access to shared state.
Definition: FilterGraphExecutor.h:71
bool m_terminating
Shutdown flag.
Definition: FilterGraphExecutor.h:101
std::mutex m_workerCvarMutex
Mutex for access to m_workerCvar.
Definition: FilterGraphExecutor.h:89
bool m_allWorkersComplete
Indicates that all worker threads have finished executing this pass.
Definition: FilterGraphExecutor.h:98
std::set< FlowGraphNode * > m_runnableNodes
Nodes that have no dependencies and are eligible to run now.
Definition: FilterGraphExecutor.h:77
static void ExecutorThread(FilterGraphExecutor *pThis, size_t i)
Thread function to handle filter graph execution.
Definition: FilterGraphExecutor.cpp:198
std::condition_variable m_workerCvar
Condition variable for waking up worker threads when work arrives.
Definition: FilterGraphExecutor.h:86
void RunBlocking(const std::set< FlowGraphNode * > &nodes)
Evaluates the filter graph, blocking until execution has completed.
Definition: FilterGraphExecutor.cpp:68
Abstract base class for a node in the signal flow graph.
Definition: FlowGraphNode.h:54