Blender V5.0
BLI_cache_mutex.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
64
65#include <atomic>
66
67#include "BLI_function_ref.hh"
68#include "BLI_mutex.hh"
69
70namespace blender {
71
73 private:
74 Mutex mutex_;
75 std::atomic<bool> cache_valid_ = false;
76
77 public:
85 void ensure(const FunctionRef<void()> compute_cache)
86 {
87 /* Handle fast case when the cache is up-to-date. */
88 if (cache_valid_.load(std::memory_order_acquire)) {
89 return;
90 }
91 this->ensure_impl(compute_cache);
92 }
93
97 void tag_dirty()
98 {
99 cache_valid_.store(false);
100 }
101
105 bool is_dirty() const
106 {
107 return !this->is_cached();
108 }
109
113 bool is_cached() const
114 {
115 return cache_valid_.load(std::memory_order_relaxed);
116 }
117
118 private:
119 void ensure_impl(FunctionRef<void()> compute_cache);
120};
121
122} // namespace blender
void ensure(const FunctionRef< void()> compute_cache)
std::mutex Mutex
Definition BLI_mutex.hh:47