This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/depsgraph/intern/depsgraph_registry.cc
Brecht Van Lommel 38688adaad Fix #105818: material preview invalid memory access reported by ASAN
Preview render depsgraphs are put in the depsgraph registry
concurrently with other threads. This was lacking a mutex lock
and a map value that remains unchanged when other elements of
the map are updated.

Pull Request: blender/blender#105839
2023-03-17 11:17:01 +01:00

76 lines
2.0 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2019 Blender Foundation. All rights reserved. */
/** \file
* \ingroup depsgraph
*/
#include <memory>
#include <mutex>
#include "intern/depsgraph_registry.h"
#include "BLI_utildefines.h"
#include "intern/depsgraph.h"
namespace blender::deg {
/* Global registry for dependency graphs associated with a main database.
*
* Threads may add or remove depsgraphs for different mains concurrently
* (for example for preview rendering), but not the same main. */
/* Use pointer for map value to ensure span returned by get_all_registered_graphs
* remains unchanged as other mains are added or removed. */
typedef std::unique_ptr<VectorSet<Depsgraph *>> GraphSetPtr;
struct GraphRegistry {
Map<Main *, GraphSetPtr> map;
std::mutex mutex;
};
static GraphRegistry &get_graph_registry()
{
static GraphRegistry graph_registry;
return graph_registry;
}
void register_graph(Depsgraph *depsgraph)
{
GraphRegistry &graph_registry = get_graph_registry();
Main *bmain = depsgraph->bmain;
std::lock_guard<std::mutex> lock{graph_registry.mutex};
graph_registry.map
.lookup_or_add_cb(bmain, []() { return std::make_unique<VectorSet<Depsgraph *>>(); })
->add_new(depsgraph);
}
void unregister_graph(Depsgraph *depsgraph)
{
Main *bmain = depsgraph->bmain;
GraphRegistry &graph_registry = get_graph_registry();
std::lock_guard<std::mutex> lock{graph_registry.mutex};
GraphSetPtr &graphs = graph_registry.map.lookup(bmain);
graphs->remove(depsgraph);
/* If this was the last depsgraph associated with the main, remove the main entry as well. */
if (graphs->is_empty()) {
graph_registry.map.remove(bmain);
}
}
Span<Depsgraph *> get_all_registered_graphs(Main *bmain)
{
GraphRegistry &graph_registry = get_graph_registry();
std::lock_guard<std::mutex> lock{graph_registry.mutex};
GraphSetPtr *graphs = graph_registry.map.lookup_ptr(bmain);
if (graphs) {
return **graphs;
}
return {};
}
} // namespace blender::deg