Skip to content

Commit 5df9698

Browse files
addaleaxMylesBorins
authored andcommitted
src: better encapsulate native immediate list
Refactor for clarity and reusability. Make it more obvious that the list is a FIFO queue. Backport-PR-URL: #32301 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 2625244 commit 5df9698

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

src/env-inl.h

+35-8
Original file line numberDiff line numberDiff line change
@@ -732,18 +732,45 @@ inline void IsolateData::set_options(
732732
options_ = std::move(options);
733733
}
734734

735-
template <typename Fn>
736-
void Environment::CreateImmediate(Fn&& cb, bool ref) {
737-
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
738-
std::move(cb), ref);
739-
NativeImmediateCallback* prev_tail = native_immediate_callbacks_tail_;
735+
std::unique_ptr<Environment::NativeImmediateCallback>
736+
Environment::NativeImmediateQueue::Shift() {
737+
std::unique_ptr<Environment::NativeImmediateCallback> ret = std::move(head_);
738+
if (ret) {
739+
head_ = ret->get_next();
740+
if (!head_)
741+
tail_ = nullptr; // The queue is now empty.
742+
}
743+
return ret;
744+
}
745+
746+
void Environment::NativeImmediateQueue::Push(
747+
std::unique_ptr<Environment::NativeImmediateCallback> cb) {
748+
NativeImmediateCallback* prev_tail = tail_;
740749

741-
native_immediate_callbacks_tail_ = callback.get();
750+
tail_ = cb.get();
742751
if (prev_tail != nullptr)
743-
prev_tail->set_next(std::move(callback));
752+
prev_tail->set_next(std::move(cb));
744753
else
745-
native_immediate_callbacks_head_ = std::move(callback);
754+
head_ = std::move(cb);
755+
}
756+
757+
void Environment::NativeImmediateQueue::ConcatMove(
758+
NativeImmediateQueue&& other) {
759+
size_ += other.size_;
760+
if (tail_ != nullptr)
761+
tail_->set_next(std::move(other.head_));
762+
else
763+
head_ = std::move(other.head_);
764+
tail_ = other.tail_;
765+
other.tail_ = nullptr;
766+
other.size_ = 0;
767+
}
746768

769+
template <typename Fn>
770+
void Environment::CreateImmediate(Fn&& cb, bool ref) {
771+
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
772+
std::move(cb), ref);
773+
native_immediates_.Push(std::move(callback));
747774
immediate_info()->count_inc(1);
748775
}
749776

src/env.cc

+6-9
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,14 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) {
665665
"RunAndClearNativeImmediates", this);
666666
size_t ref_count = 0;
667667
size_t count = 0;
668-
std::unique_ptr<NativeImmediateCallback> head;
669-
head.swap(native_immediate_callbacks_head_);
670-
native_immediate_callbacks_tail_ = nullptr;
668+
669+
NativeImmediateQueue queue;
670+
queue.ConcatMove(std::move(native_immediates_));
671671

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

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

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

src/env.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,20 @@ class Environment : public MemoryRetainer {
14251425
Fn callback_;
14261426
};
14271427

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

14311443
void RunAndClearNativeImmediates(bool only_refed = false);
14321444
static void CheckImmediate(uv_check_t* handle);

0 commit comments

Comments
 (0)