Blender V5.0
cpu_processor_cache.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <mutex>
10
11#include "BLI_mutex.hh"
12
13#include "OCIO_cpu_processor.hh"
14
15namespace blender::ocio {
16
18 /* TODO(sergey): Figure out how this can be made per-cache.
19 *
20 * The issue here is that the cache might be part of an object which is used in a Vector(). Or,
21 * even simpler: Vector<CPUProcessorCache>.
22 *
23 * If the mutex is per-object then this doesn't work as the mutex deletes the move constructor.
24 */
25 static inline Mutex mutex_;
26
27 mutable bool processor_created_ = false;
28 mutable std::unique_ptr<const CPUProcessor> cpu_processor_;
29
30 public:
31 CPUProcessorCache() = default;
32 CPUProcessorCache(const CPUProcessorCache &other) = delete;
33 CPUProcessorCache(CPUProcessorCache &&other) noexcept = default;
34
35 ~CPUProcessorCache() = default;
36
39
46 const std::function<std::unique_ptr<CPUProcessor>()> &create_processor) const
47 {
48 std::lock_guard lock(mutex_);
49
50 if (!processor_created_) {
51 cpu_processor_ = create_processor();
52 processor_created_ = true;
53 }
54
55 return cpu_processor_.get();
56 }
57};
58
59} // namespace blender::ocio
volatile int lock
CPUProcessorCache & operator=(CPUProcessorCache &&other)=default
CPUProcessorCache & operator=(const CPUProcessorCache &other)=delete
CPUProcessorCache(CPUProcessorCache &&other) noexcept=default
const CPUProcessor * get(const std::function< std::unique_ptr< CPUProcessor >()> &create_processor) const
CPUProcessorCache(const CPUProcessorCache &other)=delete
std::mutex Mutex
Definition BLI_mutex.hh:47