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/blenlib/intern/cache_mutex.cc
Jacques Lucke c73ae711bf BLI: new basic CacheMutex
This patch introduces a new `CacheMutex` which makes it easy to implement
lazily computed caches in e.g. `Curves`. For more details see `BLI_cache_mutex.hh`.

Differential Revision: https://developer.blender.org/D16419
2022-11-08 15:50:49 +01:00

26 lines
629 B
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_cache_mutex.hh"
#include "BLI_task.hh"
namespace blender {
void CacheMutex::ensure(const FunctionRef<void()> compute_cache)
{
if (cache_valid_.load(std::memory_order_acquire)) {
return;
}
std::scoped_lock lock{mutex_};
/* Double checked lock. */
if (cache_valid_.load(std::memory_order_relaxed)) {
return;
}
/* Use task isolation because a mutex is locked and the cache computation might use
* multi-threading. */
threading::isolate_task(compute_cache);
cache_valid_.store(true, std::memory_order_release);
}
} // namespace blender