Blender V4.3
depsgraph_registry.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <memory>
10#include <mutex>
11
13
14#include "BLI_utildefines.h"
15
16#include "intern/depsgraph.hh"
17
18namespace blender::deg {
19
20/* Global registry for dependency graphs associated with a main database.
21 *
22 * Threads may add or remove depsgraphs for different mains concurrently
23 * (for example for preview rendering), but not the same main. */
24
25/* Use pointer for map value to ensure span returned by get_all_registered_graphs
26 * remains unchanged as other mains are added or removed. */
27using GraphSetPtr = std::unique_ptr<VectorSet<Depsgraph *>>;
32
34{
35 static GraphRegistry graph_registry;
36 return graph_registry;
37}
38
40{
41 GraphRegistry &graph_registry = get_graph_registry();
42 Main *bmain = depsgraph->bmain;
43
44 std::lock_guard<std::mutex> lock{graph_registry.mutex};
45 graph_registry.map
46 .lookup_or_add_cb(bmain, []() { return std::make_unique<VectorSet<Depsgraph *>>(); })
47 ->add_new(depsgraph);
48}
49
51{
52 Main *bmain = depsgraph->bmain;
53 GraphRegistry &graph_registry = get_graph_registry();
54
55 std::lock_guard<std::mutex> lock{graph_registry.mutex};
56 GraphSetPtr &graphs = graph_registry.map.lookup(bmain);
57 graphs->remove(depsgraph);
58
59 /* If this was the last depsgraph associated with the main, remove the main entry as well. */
60 if (graphs->is_empty()) {
61 graph_registry.map.remove(bmain);
62 }
63}
64
66{
67 GraphRegistry &graph_registry = get_graph_registry();
68 std::lock_guard<std::mutex> lock{graph_registry.mutex};
69 GraphSetPtr *graphs = graph_registry.map.lookup_ptr(bmain);
70 if (graphs) {
71 return **graphs;
72 }
73 return {};
74}
75
76} // namespace blender::deg
volatile int lock
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:484
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:582
bool remove(const Key &key)
Definition BLI_map.hh:344
const Depsgraph * depsgraph
Span< Depsgraph * > get_all_registered_graphs(Main *bmain)
std::unique_ptr< VectorSet< Depsgraph * > > GraphSetPtr
static GraphRegistry & get_graph_registry()
void unregister_graph(Depsgraph *depsgraph)
void register_graph(Depsgraph *depsgraph)
Map< Main *, GraphSetPtr > map