Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
afa5499
src: introduce custom smart pointers for `BaseObject`s
addaleax Sep 28, 2019
e1b1267
http2: use custom BaseObject smart pointers
addaleax Sep 28, 2019
95bf451
src: use BaseObjectPtr for keeping channel alive in dns bindings
addaleax Nov 12, 2019
cb196ce
src: remove keep alive option from SetImmediate()
addaleax Nov 12, 2019
92cb961
src: remove HandleWrap instances from list once closed
addaleax Oct 11, 2019
b18531d
src: keep object alive in stream_pipe code
addaleax Nov 26, 2019
ece9faf
src: add more `can_call_into_js()` guards
addaleax Nov 26, 2019
c3fff25
src: no SetImmediate from destructor in stream_pipe code
addaleax Nov 26, 2019
2470035
src: run native immediates during Environment cleanup
addaleax Nov 26, 2019
973f8ef
src: better encapsulate native immediate list
addaleax Dec 16, 2019
5e1bcae
src: exclude C++ SetImmediate() from count
addaleax Jan 15, 2020
f1aeed5
src: add a threadsafe variant of SetImmediate()
addaleax Jan 15, 2020
3f9d1dd
src: remove AsyncRequest
addaleax Jan 15, 2020
b0ce668
src: add interrupts to Environments/Workers
addaleax Jan 16, 2020
4e1a192
src: move MemoryInfo() for worker code to .cc files
addaleax Jan 16, 2020
a9c4fe5
report: add support for Workers
addaleax Jan 16, 2020
9c43d0e
src: simplify native immediate queue running
addaleax Jan 25, 2020
1915b5b
worker: move JoinThread() back into exit callback
addaleax Jan 22, 2020
4d3275b
src: harden running native `SetImmediate()`s slightly
addaleax Jan 22, 2020
d85cb07
src: delete BaseObjectWeakPtr data when pointee is gone
addaleax Mar 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
src: better encapsulate native immediate list
Refactor for clarity and reusability. Make it more obvious that the
list is a FIFO queue.

PR-URL: #31386
Refs: openjs-foundation/summit#240
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed Apr 1, 2020
commit 973f8ef12ea2a7355611ffdaab6d117c23c8ee07
43 changes: 35 additions & 8 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,18 +732,45 @@ inline void IsolateData::set_options(
options_ = std::move(options);
}

template <typename Fn>
void Environment::CreateImmediate(Fn&& cb, bool ref) {
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
std::move(cb), ref);
NativeImmediateCallback* prev_tail = native_immediate_callbacks_tail_;
std::unique_ptr<Environment::NativeImmediateCallback>
Environment::NativeImmediateQueue::Shift() {
std::unique_ptr<Environment::NativeImmediateCallback> ret = std::move(head_);
if (ret) {
head_ = ret->get_next();
if (!head_)
tail_ = nullptr; // The queue is now empty.
}
return ret;
}

void Environment::NativeImmediateQueue::Push(
std::unique_ptr<Environment::NativeImmediateCallback> cb) {
NativeImmediateCallback* prev_tail = tail_;

native_immediate_callbacks_tail_ = callback.get();
tail_ = cb.get();
if (prev_tail != nullptr)
prev_tail->set_next(std::move(callback));
prev_tail->set_next(std::move(cb));
else
native_immediate_callbacks_head_ = std::move(callback);
head_ = std::move(cb);
}

void Environment::NativeImmediateQueue::ConcatMove(
NativeImmediateQueue&& other) {
size_ += other.size_;
if (tail_ != nullptr)
tail_->set_next(std::move(other.head_));
else
head_ = std::move(other.head_);
tail_ = other.tail_;
other.tail_ = nullptr;
other.size_ = 0;
}

template <typename Fn>
void Environment::CreateImmediate(Fn&& cb, bool ref) {
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
std::move(cb), ref);
native_immediates_.Push(std::move(callback));
immediate_info()->count_inc(1);
}

Expand Down
15 changes: 6 additions & 9 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,14 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
"RunAndClearNativeImmediates", this);
size_t ref_count = 0;
size_t count = 0;
std::unique_ptr<NativeImmediateCallback> head;
head.swap(native_immediate_callbacks_head_);
native_immediate_callbacks_tail_ = nullptr;

NativeImmediateQueue queue;
queue.ConcatMove(std::move(native_immediates_));

auto drain_list = [&]() {
TryCatchScope try_catch(this);
for (; head; head = head->get_next()) {
DebugSealHandleScope seal_handle_scope(isolate());
DebugSealHandleScope seal_handle_scope(isolate());
while (std::unique_ptr<NativeImmediateCallback> head = queue.Shift()) {
count++;
if (head->is_refed())
ref_count++;
Expand All @@ -684,15 +684,12 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
if (!try_catch.HasTerminated() && can_call_into_js())
errors::TriggerUncaughtException(isolate(), try_catch);

// We are done with the current callback. Move one iteration along,
// as if we had completed successfully.
head = head->get_next();
return true;
}
}
return false;
};
while (head && drain_list()) {}
while (queue.size() > 0 && drain_list()) {}

DCHECK_GE(immediate_info()->count(), count);
immediate_info()->count_dec(count);
Expand Down
16 changes: 14 additions & 2 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1425,8 +1425,20 @@ class Environment : public MemoryRetainer {
Fn callback_;
};

std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_;
NativeImmediateCallback* native_immediate_callbacks_tail_ = nullptr;
class NativeImmediateQueue {
public:
inline std::unique_ptr<NativeImmediateCallback> Shift();
inline void Push(std::unique_ptr<NativeImmediateCallback> cb);
// ConcatMove adds elements from 'other' to the end of this list, and clears
// 'other' afterwards.
inline void ConcatMove(NativeImmediateQueue&& other);

private:
std::unique_ptr<NativeImmediateCallback> head_;
NativeImmediateCallback* tail_ = nullptr;
};

NativeImmediateQueue native_immediates_;

void RunAndClearNativeImmediates(bool only_refed = false);
static void CheckImmediate(uv_check_t* handle);
Expand Down