Исходный код СУБД 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
127
128
129
130
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include "Env.h"
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <jsi/jsi.h>
#include <memory>
#include <mutex>
#include "onnxruntime_cxx_api.h"
#include <string>
#include <vector>
 
namespace onnxruntimejsi {
 
/**
 * @brief Bridges OrtReadNamedBufferFunc to the `epContextDataRead.callback` JavaScript function.
 *
 * ONNX Runtime may call the read function from any thread while a session is being created and
 * does not serialize calls made by different EP instances, so every invocation is serialized here
 * and marshalled to the JS thread unless the caller demonstrably runs on it already.
 *
 * An instance owns a strong reference to the JS function for the whole session lifetime and must
 * outlive the Ort::Session it was registered with.
 */
class EpContextDataReadCallback
    : public EnvTeardownListener,
      public std::enable_shared_from_this<EpContextDataReadCallback> {
 public:
  EpContextDataReadCallback(std::shared_ptr<Env> env,
                            facebook::jsi::Runtime& runtime,
                            std::shared_ptr<facebook::jsi::Function> callback,
                            size_t maxDataSize);
 
  ~EpContextDataReadCallback() override;
 
  EpContextDataReadCallback(const EpContextDataReadCallback&) = delete;
  EpContextDataReadCallback& operator=(const EpContextDataReadCallback&) = delete;
  EpContextDataReadCallback(EpContextDataReadCallback&&) = delete;
  EpContextDataReadCallback& operator=(EpContextDataReadCallback&&) = delete;
 
  /**
   * @brief Parse `epContextDataRead` and register it with `sessionOptions`.
   *
   * Must be called on the JS thread. Returns nullptr when the option is absent, undefined or null,
   * and throws facebook::jsi::JSError when it is present but malformed.
   *
   * Registration is transactional: the state is returned to the caller only after ONNX Runtime
   * accepted it. The caller must keep the returned state alive for at least as long as the session
   * created from `sessionOptions`.
   */
  static std::shared_ptr<EpContextDataReadCallback>
  createAndRegister(facebook::jsi::Runtime& runtime,
                    const facebook::jsi::Object& options,
                    const std::shared_ptr<Env>& env,
                    Ort::SessionOptions& sessionOptions);
 
  static facebook::jsi::Value testCallbackBridge(
      facebook::jsi::Runtime& runtime, const facebook::jsi::Value* arguments,
      size_t count, const std::shared_ptr<Env>& env);
 
  /**
   * @brief OrtReadNamedBufferFunc entry point. No exception ever crosses the C ABI.
   */
  static OrtStatus* ORT_API_CALL read(void* state, const char* name,
                                      OrtAllocator* allocator, void** buffer,
                                      size_t* dataSize) noexcept;
 
  /**
   * @brief Stop serving reads and wake every thread blocked on the JS thread.
   *
   * Callable from any thread and more than once. Subsequent reads fail instead of falling back to
   * any other data source.
   */
  void invalidate() noexcept;
 
  void onEnvTeardown() noexcept override { invalidate(); }
 
  size_t maxDataSize() const noexcept { return maxDataSize_; }
 
 private:
  enum class CallStatus {
    Ok,
    // Bad callback result: wrong type, unreadable buffer, or oversized payload.
    InvalidArgument,
    // The callback threw, or the bindings were torn down mid-call.
    Failed,
  };
 
  struct PendingCall {
    std::mutex mutex;
    std::condition_variable cv;
    bool finished = false;
    CallStatus status = CallStatus::Failed;
    std::string error;
    std::vector<uint8_t> data;
 
    bool isFinished() noexcept;
    void finish(CallStatus status, std::string error,
                std::vector<uint8_t> data = {}) noexcept;
    void wait();
  };
 
  OrtStatus* readImpl(const std::string& name, OrtAllocator* allocator,
                      void** buffer, size_t* dataSize);
 
  // Runs on the JS thread. Fills `pending` with the callback outcome.
  void invokeOnJsThread(const std::string& name, PendingCall& pending) noexcept;
 
  void unregisterPending(const std::shared_ptr<PendingCall>& pending) noexcept;
 
  const std::shared_ptr<Env> env_;
  const size_t maxDataSize_;
 
  // Serializes read callbacks so the JS function never observes overlapping calls.
  std::mutex callMutex_;
 
  // Guards the members below. Never held while blocking, so invalidate() cannot deadlock against
  // a read that is waiting on the JS thread.
  std::mutex stateMutex_;
  bool valid_ = true;
  facebook::jsi::Runtime* runtime_;
  std::shared_ptr<facebook::jsi::Function> callback_;
  std::vector<std::shared_ptr<PendingCall>> pending_;
};
 
}  // namespace onnxruntimejsi