Kagome
Polkadot Runtime Engine in C++17
runtime_instances_pool.cpp
Go to the documentation of this file.
1 
7 
10 #include "runtime/module.hpp"
14 
15 namespace kagome::runtime {
22  public:
23  BorrowedInstance(std::weak_ptr<RuntimeInstancesPool> pool,
24  const RuntimeInstancesPool::RootHash &state,
25  std::shared_ptr<ModuleInstance> instance)
26  : pool_{std::move(pool)},
27  state_{state},
28  instance_{std::move(instance)} {}
30  if (auto pool = pool_.lock()) {
31  pool->release(state_, std::move(instance_));
32  }
33  }
34 
35  const common::Hash256 &getCodeHash() const override {
36  return instance_->getCodeHash();
37  }
38 
39  outcome::result<PtrSize> callExportFunction(
40  std::string_view name, common::BufferView encoded_args) const override {
41  return instance_->callExportFunction(name, encoded_args);
42  }
43 
44  outcome::result<std::optional<WasmValue>> getGlobal(
45  std::string_view name) const override {
46  return instance_->getGlobal(name);
47  }
48 
49  void forDataSegment(DataSegmentProcessor const &callback) const override {
50  return instance_->forDataSegment(callback);
51  }
52 
53  InstanceEnvironment const &getEnvironment() const override {
54  return instance_->getEnvironment();
55  }
56 
57  outcome::result<void> resetEnvironment() override {
58  return instance_->resetEnvironment();
59  }
60 
61  private:
62  std::weak_ptr<RuntimeInstancesPool> pool_;
64  std::shared_ptr<ModuleInstance> instance_;
65  };
66 
67  outcome::result<std::shared_ptr<ModuleInstance>>
69  const RuntimeInstancesPool::RootHash &state) {
70  std::scoped_lock guard{mt_};
71  auto &pool = pools_[state];
72 
73  if (not pool.empty()) {
74  auto top = std::move(pool.top());
75  pool.pop();
76  return std::make_shared<BorrowedInstance>(
77  weak_from_this(), state, std::move(top));
78  }
79 
80  auto opt_module = modules_.get(state);
81  BOOST_ASSERT(opt_module.has_value());
82  auto module = opt_module.value();
83  OUTCOME_TRY(instance, module.get()->instantiate());
84 
85  return std::make_shared<BorrowedInstance>(
86  weak_from_this(), state, std::move(instance));
87  }
88 
90  const RuntimeInstancesPool::RootHash &state,
91  std::shared_ptr<ModuleInstance> &&instance) {
92  std::lock_guard guard{mt_};
93  auto &pool = pools_[state];
94 
95  pool.emplace(std::move(instance));
96  }
97 
98  std::optional<std::shared_ptr<Module>> RuntimeInstancesPool::getModule(
99  const RuntimeInstancesPool::RootHash &state) {
100  std::lock_guard guard{mt_};
101  return modules_.get(state);
102  }
103 
105  const RuntimeInstancesPool::RootHash &state,
106  std::shared_ptr<Module> module) {
107  std::lock_guard guard{mt_};
108  modules_.put(state, std::move(module));
109  }
110 
111 } // namespace kagome::runtime
std::function< void(SegmentOffset, SegmentData)> DataSegmentProcessor
outcome::result< PtrSize > callExportFunction(std::string_view name, common::BufferView encoded_args) const override
InstanceEnvironment const & getEnvironment() const override
BorrowedInstance(std::weak_ptr< RuntimeInstancesPool > pool, const RuntimeInstancesPool::RootHash &state, std::shared_ptr< ModuleInstance > instance)
std::optional< std::shared_ptr< Module > > getModule(const RootHash &state)
Get the module for state from internal cache.
RuntimeInstancesPool::RootHash state_
void putModule(const RootHash &state, std::shared_ptr< Module > module)
Puts new module into internal cache.
outcome::result< std::shared_ptr< ModuleInstance > > tryAcquire(const RootHash &state)
Instantiate new or reuse existing ModuleInstance for the provided state.
Wrapper type over sptr<ModuleInstance>. Allows to return instance back to the ModuleInstancePool upon...
std::shared_ptr< ModuleInstance > instance_
void forDataSegment(DataSegmentProcessor const &callback) const override
std::weak_ptr< RuntimeInstancesPool > pool_
outcome::result< void > resetEnvironment() override
const common::Hash256 & getCodeHash() const override
void release(const RootHash &state, std::shared_ptr< ModuleInstance > &&instance)
Releases the module instance (returns it to the pool)
outcome::result< std::optional< WasmValue > > getGlobal(std::string_view name) const override