Skip to content

Commit

Permalink
Unregister TimeLimit in Runtime destructor
Browse files Browse the repository at this point in the history
Summary:
Provide a mechanism to register a list of callbacks to call during runtime destruction.
`TimeLimitMonitor` leverages this mechanism to unregister the monitoring.

Reviewed By: davedets

Differential Revision: D16677617

fbshipit-source-id: f2bc1d4fca1507a6a3f4e112fd2490d50045fe2e
  • Loading branch information
Jeffrey Tan authored and facebook-github-bot committed Aug 23, 2019
1 parent cfad9b5 commit 798100d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions include/hermes/VM/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ using VMExperimentFlags = uint32_t;
/// Type used to assign object unique integer identifiers.
using ObjectID = uint32_t;

using DestructionCallback = std::function<void(Runtime *)>;

#define PROP_CACHE_IDS(V) V(RegExpLastIndex, Predefined::lastIndex)

/// Fixed set of ids used by the property cache in Runtime.
Expand Down Expand Up @@ -474,6 +476,12 @@ class Runtime : public HandleRootOwner,
}
#endif

/// Register \p callback which will be called
/// during runtime destruction.
void registerDestructionCallback(DestructionCallback callback) {
destructionCallbacks_.emplace_back(callback);
}

#ifdef HERMES_ENABLE_DEBUGGER
/// Encapsulates useful information about a stack frame, needed by the
/// debugger. It requres extra context and cannot be extracted from a
Expand Down Expand Up @@ -978,6 +986,9 @@ class Runtime : public HandleRootOwner,
/// we are sure it's safe to unregisterRuntime in destructor.
std::shared_ptr<SamplingProfiler> samplingProfiler_;

/// A list of callbacks to call before runtime destruction.
std::vector<DestructionCallback> destructionCallbacks_;

#if defined(HERMES_ENABLE_DEBUGGER) || defined(HERMESVM_TIMELIMIT)
/// An atomic boolean set when an async pause is requested.
/// This may be manipulated from multiple threads.
Expand Down
4 changes: 4 additions & 0 deletions lib/VM/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ Runtime::~Runtime() {
// Calling delete will automatically remove it from the list.
delete &runtimeModuleList_.back();
}

for (auto callback : destructionCallbacks_) {
callback(this);
}
}

/// A helper class used to measure the duration of GC marking different roots.
Expand Down
9 changes: 8 additions & 1 deletion lib/VM/TimeLimitMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,20 @@ void TimeLimitMonitor::watchRuntime(Runtime *runtime, int timeoutInMs) {
std::chrono::milliseconds(timeoutInMs);
timeoutMap_[runtime] = deadline;
}

runtime->registerDestructionCallback(
[this](Runtime *runtime) { this->unwatchRuntime(runtime); });

// There is only one thread anyway.
newRequestCond_.notify_one();
}

void TimeLimitMonitor::unwatchRuntime(Runtime *runtime) {
std::lock_guard<std::mutex> lock(timeoutMapMtx_);
timeoutMap_.erase(runtime);
// unwatchRuntime() may be called multiple times for the same runtime.
if (timeoutMap_.find(runtime) != timeoutMap_.end()) {
timeoutMap_.erase(runtime);
}
}

} // namespace vm
Expand Down

0 comments on commit 798100d

Please sign in to comment.