Skip to content

Commit 9e4977f

Browse files
committed
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 <gpunathi@in.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 5005c3c commit 9e4977f

3 files changed

Lines changed: 55 additions & 19 deletions

File tree

src/env-inl.h

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

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

757-
native_immediate_callbacks_tail_ = callback.get();
766+
tail_ = cb.get();
758767
if (prev_tail != nullptr)
759-
prev_tail->set_next(std::move(callback));
768+
prev_tail->set_next(std::move(cb));
760769
else
761-
native_immediate_callbacks_head_ = std::move(callback);
770+
head_ = std::move(cb);
771+
}
772+
773+
void Environment::NativeImmediateQueue::ConcatMove(
774+
NativeImmediateQueue&& other) {
775+
size_ += other.size_;
776+
if (tail_ != nullptr)
777+
tail_->set_next(std::move(other.head_));
778+
else
779+
head_ = std::move(other.head_);
780+
tail_ = other.tail_;
781+
other.tail_ = nullptr;
782+
other.size_ = 0;
783+
}
762784

785+
template <typename Fn>
786+
void Environment::CreateImmediate(Fn&& cb, bool ref) {
787+
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
788+
std::move(cb), ref);
789+
native_immediates_.Push(std::move(callback));
763790
immediate_info()->count_inc(1);
764791
}
765792

src/env.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,14 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
663663
"RunAndClearNativeImmediates", this);
664664
size_t ref_count = 0;
665665
size_t count = 0;
666-
std::unique_ptr<NativeImmediateCallback> head;
667-
head.swap(native_immediate_callbacks_head_);
668-
native_immediate_callbacks_tail_ = nullptr;
666+
667+
NativeImmediateQueue queue;
668+
queue.ConcatMove(std::move(native_immediates_));
669669

670670
auto drain_list = [&]() {
671671
TryCatchScope try_catch(this);
672-
for (; head; head = head->get_next()) {
673-
DebugSealHandleScope seal_handle_scope(isolate());
672+
DebugSealHandleScope seal_handle_scope(isolate());
673+
while (std::unique_ptr<NativeImmediateCallback> head = queue.Shift()) {
674674
count++;
675675
if (head->is_refed())
676676
ref_count++;
@@ -682,15 +682,12 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
682682
if (!try_catch.HasTerminated() && can_call_into_js())
683683
errors::TriggerUncaughtException(isolate(), try_catch);
684684

685-
// We are done with the current callback. Move one iteration along,
686-
// as if we had completed successfully.
687-
head = head->get_next();
688685
return true;
689686
}
690687
}
691688
return false;
692689
};
693-
while (head && drain_list()) {}
690+
while (queue.size() > 0 && drain_list()) {}
694691

695692
DCHECK_GE(immediate_info()->count(), count);
696693
immediate_info()->count_dec(count);

src/env.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,8 +1430,20 @@ class Environment : public MemoryRetainer {
14301430
Fn callback_;
14311431
};
14321432

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

14361448
void RunAndClearNativeImmediates(bool only_refed = false);
14371449
static void CheckImmediate(uv_check_t* handle);

0 commit comments

Comments
 (0)