Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
#pragma once
 
#include <ReactCommon/CallInvoker.h>
#include <algorithm>
#include <functional>
#include <jsi/jsi.h>
#include <memory>
#include <mutex>
#include "onnxruntime_cxx_api.h"
#include <thread>
#include <vector>
 
namespace onnxruntimejsi {
 
/**
 * @brief Notified once the host tears the JSI bindings down.
 *
 * Teardown is not guaranteed to happen on the JS thread, so listeners must not touch the runtime.
 */
class EnvTeardownListener {
 public:
  virtual ~EnvTeardownListener() = default;
 
  virtual void onEnvTeardown() noexcept = 0;
};
 
class Env : public std::enable_shared_from_this<Env> {
 public:
  // Constructed from install(), which the host always calls on the JS thread.
  Env(std::shared_ptr<facebook::react::CallInvoker> jsInvoker)
      : jsInvoker_(jsInvoker), jsThreadId_(std::this_thread::get_id()) {}
 
  ~Env() { invalidate(); }
 
  inline void initOrtEnv(OrtLoggingLevel logLevel, const char* logid) {
    if (ortEnv_) {
      return;
    }
    ortEnv_ = std::make_shared<Ort::Env>(logLevel, logid);
  }
 
  // Keep JS values in the runtime, not in an environment retained by native module teardown.
  inline void setTensorConstructor(facebook::jsi::Runtime& runtime,
                                   const facebook::jsi::Object& tensorConstructor) {
    runtime.global()
        .getPropertyAsObject(runtime, "OrtApi")
        .setProperty(runtime, "__tensorConstructor", tensorConstructor);
  }
 
  inline facebook::jsi::Value
  getTensorConstructor(facebook::jsi::Runtime& runtime) const {
    return runtime.global()
        .getPropertyAsObject(runtime, "OrtApi")
        .getProperty(runtime, "__tensorConstructor");
  }
 
  inline Ort::Env& getOrtEnv() const { return *ortEnv_; }
 
  /**
   * @brief Whether the caller runs on the thread that installed the bindings, i.e. the JS thread.
   */
  inline bool isJsThread() const noexcept {
    return std::this_thread::get_id() == jsThreadId_;
  }
 
  /**
   * @brief Queue work on the JS thread.
   *
   * @return false when the bindings were torn down and the work will never run.
   */
  inline bool runOnJsThread(std::function<void()>&& func) {
    std::shared_ptr<facebook::react::CallInvoker> jsInvoker;
    {
      std::lock_guard<std::mutex> lock(mutex_);
      jsInvoker = jsInvoker_;
    }
    if (!jsInvoker) return false;
    jsInvoker->invokeAsync(std::move(func));
    return true;
  }
 
  inline void
  addTeardownListener(const std::weak_ptr<EnvTeardownListener>& listener) {
    std::lock_guard<std::mutex> lock(mutex_);
    teardownListeners_.erase(
        std::remove_if(teardownListeners_.begin(), teardownListeners_.end(),
                       [](const std::weak_ptr<EnvTeardownListener>& entry) {
                         return entry.expired();
                       }),
        teardownListeners_.end());
    teardownListeners_.push_back(listener);
  }
 
  /**
   * @brief Stop dispatching to the JS thread and notify teardown listeners.
   *
   * Callable from any thread and more than once. Never touches the runtime, so it is safe to call
   * from a host teardown hook that does not run on the JS thread.
   */
  inline void invalidate() noexcept {
    std::vector<std::shared_ptr<EnvTeardownListener>> listeners;
    {
      std::lock_guard<std::mutex> lock(mutex_);
      if (!jsInvoker_ && teardownListeners_.empty()) return;
      for (auto& entry : teardownListeners_) {
        if (auto listener = entry.lock()) {
          listeners.push_back(std::move(listener));
        }
      }
      teardownListeners_.clear();
      jsInvoker_.reset();
    }
    for (auto& listener : listeners) {
      listener->onEnvTeardown();
    }
  }
 
 private:
  mutable std::mutex mutex_;
  std::shared_ptr<facebook::react::CallInvoker> jsInvoker_;
  std::vector<std::weak_ptr<EnvTeardownListener>> teardownListeners_;
  const std::thread::id jsThreadId_;
  std::shared_ptr<Ort::Env> ortEnv_;
};
 
}  // namespace onnxruntimejsi