Showing the changes to src/vm/vm.cpp (1 of 14 files). Show all
Unified · Split
| @@ -126,53 +126,63 @@ std::shared_ptr<runtime::vm_module_t> vm_t::load_module(const char *path) · 125 unmodified lines | |||
| 126 | if (path == nullptr) | 126 | if (path == nullptr) |
| 127 | throw vm_user_exception{ "Invalid module path" }; | 127 | throw vm_user_exception{ "Invalid module path" }; |
| 128 | 128 | ||
| 129 | std::lock_guard<std::mutex> lock{ _modules_lock }; | 129 | auto new_module_iter = loaded_modules_t::const_iterator{}; |
| 130 | 130 | auto new_module = std::shared_ptr<vm_module_t>{}; | |
| 131 | // Check if the module is already loaded. | ||
| 132 | auto iter = std::begin(_modules); | ||
| 133 | while (iter != std::end(_modules)) | ||
| 134 | { | 131 | { |
| 135 | if (0 == ::strcmp(iter->origin->str(), path)) | 132 | std::lock_guard<std::mutex> lock{ _modules_lock }; |
| 136 | { | ||
| 137 | auto module = iter->module.lock(); | ||
| 138 | 133 | ||
| 139 | // If an entry exists but is null, the module was loaded before. Read the module again and return. | 134 | // Check if the module is already loaded. |
| 140 | if (module == nullptr) | 135 | auto iter = std::begin(_modules); |
| 136 | while (iter != std::end(_modules)) | ||
| 137 | { | ||
| 138 | if (0 == ::strcmp(iter->origin->str(), path)) | ||
| 141 | { | 139 | { |
| 142 | module = std::shared_ptr<vm_module_t>{ std::move(read_module(path)) }; | 140 | auto module = iter->module.lock(); |
| 143 | iter->module = module; | 141 | |
| 142 | // If an entry exists but is null, the module was loaded before. Read the module again and return. | ||
| 143 | if (module == nullptr) | ||
| 144 | { | ||
| 145 | module = std::shared_ptr<vm_module_t>{ std::move(read_module(path)) }; | ||
| 146 | iter->module = module; | ||
| 144 | 147 | ||
| 145 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "reload: vm module: >>%s<<\n", path); | 148 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "reload: vm module: >>%s<<\n", path); |
| 149 | } | ||
| 150 | |||
| 151 | return module; | ||
| 146 | } | 152 | } |
| 147 | 153 | ||
| 148 | return module; | 154 | ++iter; |
| 149 | } | 155 | } |
| 150 | 156 | ||
| 151 | ++iter; | 157 | if (path[0] == BUILTIN_MODULE_PREFIX_CHAR) |
| 152 | } | 158 | { |
| 159 | new_module = std::move(builtin::get_builtin_module(path)); | ||
| 160 | } | ||
| 161 | else | ||
| 162 | { | ||
| 163 | // Read in the module | ||
| 164 | new_module = std::move(read_module(path)); | ||
| 165 | } | ||
| 153 | 166 | ||
| 154 | auto new_module = std::shared_ptr<vm_module_t>{}; | 167 | auto path_local = std::make_unique<vm_string_t>(std::strlen(path), reinterpret_cast<const uint8_t *>(path)); |
| 155 | if (path[0] == BUILTIN_MODULE_PREFIX_CHAR) | 168 | |
| 156 | { | 169 | auto load_id_next = std::size_t{ 0 }; |
| 157 | new_module = std::move(builtin::get_builtin_module(path)); | 170 | if (!_modules.empty()) |
| 158 | } | 171 | load_id_next = (_modules.front().load_id + 1); |
| 159 | else | 172 | |
| 160 | { | 173 | _modules.push_front(std::move(loaded_vm_module_t{ load_id_next, std::move(path_local), new_module })); |
| 161 | // Read in the module | 174 | new_module_iter = _modules.begin(); |
| 162 | new_module = std::move(read_module(path)); | ||
| 163 | } | 175 | } |
| 164 | 176 | ||
| 165 | auto path_local = std::make_unique<vm_string_t>(std::strlen(path), reinterpret_cast<const uint8_t *>(path)); | ||
| 166 | if (_tool_dispatch != nullptr) | 177 | if (_tool_dispatch != nullptr) |
| 167 | { | 178 | { |
| 168 | std::lock_guard<std::mutex> lock{ _tool_dispatch_lock }; | 179 | std::lock_guard<std::mutex> lock{ _tool_dispatch_lock }; |
| 169 | if (_tool_dispatch != nullptr) | 180 | if (_tool_dispatch != nullptr) |
| 170 | _tool_dispatch->on_module_load(new_module, *path_local); | 181 | _tool_dispatch->on_module_vm_load(new_module, *new_module_iter->origin); |
| 171 | } | 182 | } |
| 172 | 183 | ||
| 173 | _modules.push_front(std::move(loaded_vm_module_t{ std::move(path_local), new_module })); | 184 | if (debug::is_component_tracing_enabled<debug::component_trace_t::module>()) |
| 174 | 185 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "load: vm module: >>%s<<\n", path); | |
| 175 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "load: vm module: >>%s<<\n", path); | ||
| 176 | 186 | ||
| 177 | return new_module; | 187 | return new_module; |
| 178 | } | 188 | } |
| @@ -184,7 +194,10 @@ void vm_t::enum_loaded_modules(loaded_vm_module_callback_t callback) const · 5 unmodified lines | |||
| 184 | 194 | ||
| 185 | std::lock_guard<std::mutex> lock{ _modules_lock }; | 195 | std::lock_guard<std::mutex> lock{ _modules_lock }; |
| 186 | for (auto &m : _modules) | 196 | for (auto &m : _modules) |
| 187 | callback(m); | 197 | { |
| 198 | if (!callback(m)) | ||
| 199 | return; | ||
| 200 | } | ||
| 188 | } | 201 | } |
| 189 | 202 | ||
| 190 | runtime::vm_scheduler_control_t &vm_t::get_scheduler_control() const | 203 | runtime::vm_scheduler_control_t &vm_t::get_scheduler_control() const |