Skip to content

Commit

Permalink
fixup! src: make Environment::interrupt_data_ atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Mar 28, 2020
1 parent d9689db commit aeb354d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,14 @@ void Environment::RequestInterruptFromV8() {
// The Isolate may outlive the Environment, so some logic to handle the
// situation in which the Environment is destroyed before the handler runs
// is required.

// We allocate a new pointer to a pointer to this Environment instance, and
// try to set it as interrupt_data_. If interrupt_data_ was already set, then
// callbacks are already scheduled to run and we can delete our own pointer
// and just return. If it was nullptr previously, the Environment** is stored;
// ~Environment sets the Environment* contained in it to nullptr, so that
// the callback can check whether ~Environment has already run and it is thus
// not safe to access the Environment instance itself.
Environment** interrupt_data = new Environment*(this);
Environment** dummy = nullptr;
if (!interrupt_data_.compare_exchange_strong(dummy, interrupt_data)) {
Expand Down

0 comments on commit aeb354d

Please sign in to comment.