/proc/self/cwd/runtime/internal/runtime_env.cc
Line | Count | Source |
1 | | // Copyright 2024 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "runtime/internal/runtime_env.h" |
16 | | |
17 | | #include <atomic> |
18 | | #include <memory> |
19 | | #include <utility> |
20 | | |
21 | | #include "absl/base/nullability.h" |
22 | | #include "absl/synchronization/mutex.h" |
23 | | #include "internal/noop_delete.h" |
24 | | #include "google/protobuf/descriptor.h" |
25 | | #include "google/protobuf/dynamic_message.h" |
26 | | #include "google/protobuf/message.h" |
27 | | |
28 | | namespace cel::runtime_internal { |
29 | | |
30 | 14.5k | RuntimeEnv::KeepAlives::~KeepAlives() { |
31 | 14.5k | while (!deque.empty()) { |
32 | 0 | deque.pop_back(); |
33 | 0 | } |
34 | 14.5k | } |
35 | | |
36 | 9.92k | google::protobuf::MessageFactory* absl_nonnull RuntimeEnv::MutableMessageFactory() const { |
37 | 9.92k | google::protobuf::MessageFactory* absl_nullable shared_message_factory = |
38 | 9.92k | message_factory_ptr.load(std::memory_order_relaxed); |
39 | 9.92k | if (shared_message_factory != nullptr) { |
40 | 9.92k | return shared_message_factory; |
41 | 9.92k | } |
42 | 0 | absl::MutexLock lock(message_factory_mutex); |
43 | 0 | shared_message_factory = message_factory_ptr.load(std::memory_order_relaxed); |
44 | 0 | if (shared_message_factory == nullptr) { |
45 | 0 | if (descriptor_pool.get() == google::protobuf::DescriptorPool::generated_pool()) { |
46 | | // Using the generated descriptor pool, just use the generated message |
47 | | // factory. |
48 | 0 | message_factory = std::shared_ptr<google::protobuf::MessageFactory>( |
49 | 0 | google::protobuf::MessageFactory::generated_factory(), |
50 | 0 | internal::NoopDeleteFor<google::protobuf::MessageFactory>()); |
51 | 0 | } else { |
52 | 0 | auto dynamic_message_factory = |
53 | 0 | std::make_shared<google::protobuf::DynamicMessageFactory>(); |
54 | | // Ensure we do not delegate to the generated factory, if the default |
55 | | // every changes. We prefer being hermetic. |
56 | 0 | dynamic_message_factory->SetDelegateToGeneratedFactory(false); |
57 | 0 | message_factory = std::move(dynamic_message_factory); |
58 | 0 | } |
59 | 0 | shared_message_factory = message_factory.get(); |
60 | 0 | message_factory_ptr.store(shared_message_factory, |
61 | 0 | std::memory_order_seq_cst); |
62 | 0 | } |
63 | 0 | return shared_message_factory; |
64 | 9.92k | } |
65 | | |
66 | 0 | void RuntimeEnv::KeepAlive(std::shared_ptr<const void> keep_alive) { |
67 | 0 | if (keep_alive == nullptr) { |
68 | 0 | return; |
69 | 0 | } |
70 | 0 | keep_alives.deque.push_back(std::move(keep_alive)); |
71 | 0 | } |
72 | | |
73 | | } // namespace cel::runtime_internal |