// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include "Env.h" #include #include #include #include #include #include #include "onnxruntime_cxx_api.h" #include #include 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 { public: EpContextDataReadCallback(std::shared_ptr env, facebook::jsi::Runtime& runtime, std::shared_ptr 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 createAndRegister(facebook::jsi::Runtime& runtime, const facebook::jsi::Object& options, const std::shared_ptr& 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); /** * @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 data; bool isFinished() noexcept; void finish(CallStatus status, std::string error, std::vector 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& pending) noexcept; const std::shared_ptr 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 callback_; std::vector> pending_; }; } // namespace onnxruntimejsi