Skip to content

Commit 53e566b

Browse files
addaleaxcodebytere
authored andcommitted
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]>
1 parent 7ecf842 commit 53e566b

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

src/env-inl.h

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -745,18 +745,45 @@ inline void IsolateData::set_options(
745745
options_ = std::move(options);
746746
}
747747

748-
template <typename Fn>
749-
void Environment::CreateImmediate(Fn&& cb, bool ref) {
750-
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
751-
std::move(cb), ref);
752-
NativeImmediateCallback* prev_tail = native_immediate_callbacks_tail_;
748+
std::unique_ptr<Environment::NativeImmediateCallback>
749+
Environment::NativeImmediateQueue::Shift() {
750+
std::unique_ptr<Environment::NativeImmediateCallback> ret = std::move(head_);
751+
if (ret) {
752+
head_ = ret->get_next();
753+
if (!head_)
754+
tail_ = nullptr; // The queue is now empty.
755+
}
756+
return ret;
757+
}
758+
759+
void Environment::NativeImmediateQueue::Push(
760+
std::unique_ptr<Environment::NativeImmediateCallback> cb) {
761+
NativeImmediateCallback* prev_tail = tail_;
753762

754-
native_immediate_callbacks_tail_ = callback.get();
763+
tail_ = cb.get();
755764
if (prev_tail != nullptr)
756-
prev_tail->set_next(std::move(callback));
765+
prev_tail->set_next(std::move(cb));
757766
else
758-
native_immediate_callbacks_head_ = std::move(callback);
767+
head_ = std::move(cb);
768+
}
769+
770+
void Environment::NativeImmediateQueue::ConcatMove(
771+
NativeImmediateQueue&& other) {
772+
size_ += other.size_;
773+
if (tail_ != nullptr)
774+
tail_->set_next(std::move(other.head_));
775+
else
776+
head_ = std::move(other.head_);
777+
tail_ = other.tail_;
778+
other.tail_ = nullptr;
779+
other.size_ = 0;
780+
}
759781

782+
template <typename Fn>
783+
void Environment::CreateImmediate(Fn&& cb, bool ref) {
784+
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
785+
std::move(cb), ref);
786+
native_immediates_.Push(std::move(callback));
760787
immediate_info()->count_inc(1);
761788
}
762789

src/env.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,14 +666,14 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
666666
"RunAndClearNativeImmediates", this);
667667
size_t ref_count = 0;
668668
size_t count = 0;
669-
std::unique_ptr<NativeImmediateCallback> head;
670-
head.swap(native_immediate_callbacks_head_);
671-
native_immediate_callbacks_tail_ = nullptr;
669+
670+
NativeImmediateQueue queue;
671+
queue.ConcatMove(std::move(native_immediates_));
672672

673673
auto drain_list = [&]() {
674674
TryCatchScope try_catch(this);
675-
for (; head; head = head->get_next()) {
676-
DebugSealHandleScope seal_handle_scope(isolate());
675+
DebugSealHandleScope seal_handle_scope(isolate());
676+
while (std::unique_ptr<NativeImmediateCallback> head = queue.Shift()) {
677677
count++;
678678
if (head->is_refed())
679679
ref_count++;
@@ -685,15 +685,12 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
685685
if (!try_catch.HasTerminated() && can_call_into_js())
686686
errors::TriggerUncaughtException(isolate(), try_catch);
687687

688-
// We are done with the current callback. Move one iteration along,
689-
// as if we had completed successfully.
690-
head = head->get_next();
691688
return true;
692689
}
693690
}
694691
return false;
695692
};
696-
while (head && drain_list()) {}
693+
while (queue.size() > 0 && drain_list()) {}
697694

698695
DCHECK_GE(immediate_info()->count(), count);
699696
immediate_info()->count_dec(count);

src/env.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,8 +1427,20 @@ class Environment : public MemoryRetainer {
14271427
Fn callback_;
14281428
};
14291429

1430-
std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_;
1431-
NativeImmediateCallback* native_immediate_callbacks_tail_ = nullptr;
1430+
class NativeImmediateQueue {
1431+
public:
1432+
inline std::unique_ptr<NativeImmediateCallback> Shift();
1433+
inline void Push(std::unique_ptr<NativeImmediateCallback> cb);
1434+
// ConcatMove adds elements from 'other' to the end of this list, and clears
1435+
// 'other' afterwards.
1436+
inline void ConcatMove(NativeImmediateQueue&& other);
1437+
1438+
private:
1439+
std::unique_ptr<NativeImmediateCallback> head_;
1440+
NativeImmediateCallback* tail_ = nullptr;
1441+
};
1442+
1443+
NativeImmediateQueue native_immediates_;
14321444

14331445
void RunAndClearNativeImmediates(bool only_refed = false);
14341446
static void CheckImmediate(uv_check_t* handle);

0 commit comments

Comments
 (0)