@@ -280,11 +280,7 @@ WasmBase::WasmBase(std::unique_ptr<WasmVm> wasm_vm, std::string_view vm_id,
280
280
}
281
281
}
282
282
283
- WasmBase::~WasmBase () {
284
- root_contexts_.clear ();
285
- pending_done_.clear ();
286
- pending_delete_.clear ();
287
- }
283
+ WasmBase::~WasmBase () {}
288
284
289
285
bool WasmBase::initialize (const std::string &code, bool allow_precompiled) {
290
286
if (!wasm_vm_) {
@@ -323,19 +319,22 @@ bool WasmBase::initialize(const std::string &code, bool allow_precompiled) {
323
319
return !isFailed ();
324
320
}
325
321
326
- ContextBase *WasmBase::getRootContext (const std::shared_ptr<PluginBase> &plugin,
327
- bool allow_closed) {
328
- auto it = root_contexts_.find (plugin->key ());
329
- if (it != root_contexts_.end ()) {
330
- return it->second .get ();
322
+ ContextBase *WasmBase::getRootContext (std::string_view root_id) {
323
+ auto it = root_contexts_.find (std::string (root_id));
324
+ if (it == root_contexts_.end ()) {
325
+ return nullptr ;
331
326
}
332
- if (allow_closed) {
333
- it = pending_done_.find (plugin->key ());
334
- if (it != pending_done_.end ()) {
335
- return it->second .get ();
336
- }
327
+ return it->second .get ();
328
+ }
329
+
330
+ ContextBase *WasmBase::getOrCreateRootContext (const std::shared_ptr<PluginBase> &plugin) {
331
+ auto root_context = getRootContext (plugin->root_id_ );
332
+ if (!root_context) {
333
+ auto context = std::unique_ptr<ContextBase>(createRootContext (plugin));
334
+ root_context = context.get ();
335
+ root_contexts_[plugin->root_id_ ] = std::move (context);
337
336
}
338
- return nullptr ;
337
+ return root_context ;
339
338
}
340
339
341
340
void WasmBase::startVm (ContextBase *root_context) {
@@ -353,14 +352,15 @@ bool WasmBase::configure(ContextBase *root_context, std::shared_ptr<PluginBase>
353
352
}
354
353
355
354
ContextBase *WasmBase::start (std::shared_ptr<PluginBase> plugin) {
356
- auto it = root_contexts_.find (plugin->key ());
355
+ auto root_id = plugin->root_id_ ;
356
+ auto it = root_contexts_.find (root_id);
357
357
if (it != root_contexts_.end ()) {
358
358
it->second ->onStart (plugin);
359
359
return it->second .get ();
360
360
}
361
361
auto context = std::unique_ptr<ContextBase>(createRootContext (plugin));
362
362
auto context_ptr = context.get ();
363
- root_contexts_[plugin-> key () ] = std::move (context);
363
+ root_contexts_[root_id ] = std::move (context);
364
364
if (!context_ptr->onStart (plugin)) {
365
365
return nullptr ;
366
366
}
@@ -377,49 +377,38 @@ uint32_t WasmBase::allocContextId() {
377
377
}
378
378
}
379
379
380
- void WasmBase::startShutdown (const std::shared_ptr<PluginBase> &plugin) {
381
- auto it = root_contexts_.find (plugin->key ());
382
- if (it != root_contexts_.end ()) {
383
- if (it->second ->onDone ()) {
384
- it->second ->onDelete ();
385
- } else {
386
- pending_done_[it->first ] = std::move (it->second );
387
- }
388
- root_contexts_.erase (it);
389
- }
390
- }
391
-
392
380
void WasmBase::startShutdown () {
393
- auto it = root_contexts_.begin ();
394
- while (it != root_contexts_.end ()) {
395
- if (it->second ->onDone ()) {
396
- it->second ->onDelete ();
397
- } else {
398
- pending_done_[it->first ] = std::move (it->second );
381
+ bool all_done = true ;
382
+ for (auto &p : root_contexts_) {
383
+ if (!p.second ->onDone ()) {
384
+ all_done = false ;
385
+ pending_done_.insert (p.second .get ());
399
386
}
400
- it = root_contexts_.erase (it);
387
+ }
388
+ if (!all_done) {
389
+ shutdown_handle_ = std::make_unique<ShutdownHandle>(shared_from_this ());
390
+ } else {
391
+ finishShutdown ();
401
392
}
402
393
}
403
394
404
395
WasmResult WasmBase::done (ContextBase *root_context) {
405
- auto it = pending_done_.find (root_context-> plugin_ -> key () );
396
+ auto it = pending_done_.find (root_context);
406
397
if (it == pending_done_.end ()) {
407
398
return WasmResult::NotFound;
408
399
}
409
- pending_delete_.insert (std::move (it->second ));
410
400
pending_done_.erase (it);
411
- // Defer the delete so that onDelete is not called from within the done() handler.
412
- shutdown_handle_ = std::make_unique<ShutdownHandle>(shared_from_this ());
413
- addAfterVmCallAction (
414
- [shutdown_handle = shutdown_handle_.release ()]() { delete shutdown_handle; });
401
+ if (pending_done_.empty () && shutdown_handle_) {
402
+ // Defer the delete so that onDelete is not called from within the done() handler.
403
+ addAfterVmCallAction (
404
+ [shutdown_handle = shutdown_handle_.release ()]() { delete shutdown_handle; });
405
+ }
415
406
return WasmResult::Ok;
416
407
}
417
408
418
409
void WasmBase::finishShutdown () {
419
- auto it = pending_delete_.begin ();
420
- while (it != pending_delete_.end ()) {
421
- (*it)->onDelete ();
422
- it = pending_delete_.erase (it);
410
+ for (auto &p : root_contexts_) {
411
+ p.second ->onDelete ();
423
412
}
424
413
}
425
414
@@ -531,18 +520,11 @@ getOrCreateThreadLocalWasm(std::shared_ptr<WasmHandleBase> base_wasm,
531
520
WasmHandleCloneFactory clone_factory) {
532
521
auto wasm_handle = getThreadLocalWasm (base_wasm->wasm ()->vm_key ());
533
522
if (wasm_handle) {
534
- auto root_context = wasm_handle->wasm ()->getRootContext (plugin, false );
535
- if (!root_context) {
536
- root_context = wasm_handle->wasm ()->start (plugin);
537
- if (!root_context) {
538
- base_wasm->wasm ()->fail (FailState::StartFailed, " Failed to start thread-local Wasm" );
539
- return nullptr ;
540
- }
541
- if (!wasm_handle->wasm ()->configure (root_context, plugin)) {
542
- base_wasm->wasm ()->fail (FailState::ConfigureFailed,
543
- " Failed to configure thread-local Wasm plugin" );
544
- return nullptr ;
545
- }
523
+ auto root_context = wasm_handle->wasm ()->getOrCreateRootContext (plugin);
524
+ if (!wasm_handle->wasm ()->configure (root_context, plugin)) {
525
+ base_wasm->wasm ()->fail (FailState::ConfigureFailed,
526
+ " Failed to configure thread-local Wasm code" );
527
+ return nullptr ;
546
528
}
547
529
return wasm_handle;
548
530
}
0 commit comments