Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
// this file contains implementations of the C API
 
#include <cassert>
#include <utility>
 
#include "ort_env.h"
#include "core/session/ort_apis.h"
#include "core/session/environment.h"
#include "core/session/allocator_adapters.h"
#include "core/session/user_logging_sink.h"
#include "core/common/logging/logging.h"
#include "core/framework/provider_shutdown.h"
#include "core/platform/logging/make_platform_default_log_sink.h"
 
// Whether the process is shutting down
std::atomic<bool> g_is_shutting_down(false);
using namespace onnxruntime;
using namespace onnxruntime::logging;
 
#if defined(USE_WEBGPU) && !defined(ORT_USE_EP_API_ADAPTERS)
namespace onnxruntime {
namespace webgpu {
void CleanupWebGpuContexts();
}  // namespace webgpu
}  // namespace onnxruntime
#endif
 
OrtEnv* OrtEnv::p_instance_;
int OrtEnv::ref_count_ = 0;
std::recursive_mutex OrtEnv::m_;
 
OrtEnv::OrtEnv(std::unique_ptr<onnxruntime::Environment> value1)
    : value_(std::move(value1)) {
}
 
OrtEnv::~OrtEnv() {
// We don't support any shared providers in the minimal build yet
#if !defined(ORT_MINIMAL_BUILD)
  UnloadSharedProviders();
#endif
 
#if defined(USE_WEBGPU) && !defined(ORT_USE_EP_API_ADAPTERS)
  // Explicitly destroy the Environment first, which will properly clean up DataTransferManager
  // and call ReleaseImpl on WebGpuDataTransferImpl
  value_.reset();
 
  // Now that Environment is destroyed and all data transfers are cleaned up,
  // we can safely cleanup any remaining WebGPU contexts
  webgpu::CleanupWebGpuContexts();
#endif
}
 
/*static*/
OrtEnvPtr OrtEnv::GetOrCreateInstance(const OrtEnv::LoggingManagerConstructionInfo& lm_info,
                                      onnxruntime::common::Status& status,
                                      const OrtThreadingOptions* tp_options,
                                      const OrtKeyValuePairs* config_entries) {
  std::lock_guard<std::recursive_mutex> lock(m_);
  if (!p_instance_) {
    std::unique_ptr<LoggingManager> lmgr;
    std::string name = lm_info.logid;
 
    std::unique_ptr<ISink> sink = nullptr;
    if (lm_info.logging_function) {
      sink = std::make_unique<UserLoggingSink>(lm_info.logging_function, lm_info.logger_param);
 
    } else {
      sink = MakePlatformDefaultLogSink();
    }
    auto etwOverrideSeverity = logging::OverrideLevelWithEtw(static_cast<Severity>(lm_info.default_warning_level));
    sink = EnhanceSinkWithEtw(std::move(sink), static_cast<Severity>(lm_info.default_warning_level),
                              etwOverrideSeverity);
    lmgr = std::make_unique<LoggingManager>(std::move(sink),
                                            std::min(static_cast<Severity>(lm_info.default_warning_level), etwOverrideSeverity),
                                            false,
                                            LoggingManager::InstanceType::Default,
                                            &name);
 
    const bool create_global_thread_pools = tp_options != nullptr;
    std::unique_ptr<onnxruntime::Environment> env;
    status = onnxruntime::Environment::Create(std::move(lmgr), env, tp_options,
                                              create_global_thread_pools, config_entries);
 
    if (!status.IsOK()) {
      return OrtEnvPtr(nullptr, OrtEnv::Release);
    }
    // Use 'new' to allocate OrtEnv, as it will be managed by p_instance_
    // and deleted in ReleaseEnv or leaked if g_is_process_shutting_down is true.
    p_instance_ = new OrtEnv(std::move(env));
 
    // Take this caller's reference before running any execution provider code below, so that if a provider
    // acquires and releases an OrtEnvPtr the instance isn't destroyed out from under us. Holding it in an OrtEnvPtr
    // also unpublishes and destroys the half-built instance if anything below fails or throws.
    ++ref_count_;
    OrtEnvPtr instance{p_instance_, OrtEnv::Release};
 
#if !defined(ORT_MINIMAL_BUILD)
    // Register statically linked plugin EPs *after* p_instance_ is published. They use the public ORT API, so any
    // OrtEnv API they call while enumerating their devices must be able to find the instance. m_ is recursive so
    // that such a call from this thread doesn't self-deadlock.
    status = p_instance_->GetEnvironment().CreateAndRegisterStaticPluginEps(
        Environment::StaticPluginEpRegistrationToken{});
 
    if (!status.IsOK()) {
      return OrtEnvPtr(nullptr, OrtEnv::Release);
    }
#endif  // !defined(ORT_MINIMAL_BUILD)
 
    return instance;
  }
 
  ++ref_count_;
  return OrtEnvPtr(p_instance_, OrtEnv::Release);
}
 
/*static*/
void OrtEnv::Release(OrtEnv* env_ptr) {
  if (!env_ptr) {
    return;  // nothing to release
  }
 
  OrtEnv* instance_to_delete = nullptr;
 
  {  // Scope for the lock guard
    std::lock_guard<std::recursive_mutex> lock(m_);
    assert(p_instance_ == env_ptr);
 
    --ref_count_;
    if (ref_count_ == 0) {
      if (!g_is_shutting_down.load(std::memory_order_acquire)) {
        instance_to_delete = p_instance_;  // Point to the instance to be deleted.
        p_instance_ = nullptr;             // Set the static instance pointer to nullptr under the lock.
      } else {
#if !defined(ONNXRUNTIME_ENABLE_MEMLEAK_CHECK)
        // Process is shutting down, let it leak.
        // p_instance_ remains as is (though ref_count_ is 0), future CreateEnv calls
        // would increment ref_count_ on this "leaked" instance.
        // This behavior matches the requirement to "just let the memory leak out".
#else
        // we're tracing for memory leaks so we want to avoid as many leaks as possible and the leaks are considered
        // as failures for test apps.
        instance_to_delete = p_instance_;
        p_instance_ = nullptr;
#endif
      }
    }
  }  // Mutex m_ is released here when lock_guard goes out of scope.
 
  // Perform the deletion outside the lock if an instance was marked for deletion.
  // instance_to_delete can be null here, but it's perfectly safe to delete a nullptr
  delete instance_to_delete;
}
 
/*static*/
OrtEnvPtr OrtEnv::TryGetInstance() {
  std::lock_guard<std::recursive_mutex> lock(m_);
 
  if (p_instance_) {
    ++ref_count_;
  }
 
  return OrtEnvPtr(p_instance_, OrtEnv::Release);
}
 
onnxruntime::logging::LoggingManager* OrtEnv::GetLoggingManager() const {
  return value_->GetLoggingManager();
}
 
void OrtEnv::SetLoggingManager(std::unique_ptr<onnxruntime::logging::LoggingManager> logging_manager) {
  value_->SetLoggingManager(std::move(logging_manager));
}