@@ -24,11 +24,15 @@ InputGate::~InputGate() noexcept {
2424 KJ_ASSERT (waiters.empty ());
2525}
2626
27- InputGate::Waiter::Waiter (
28- kj::PromiseFulfiller<Lock>& fulfiller, InputGate& gate, bool isChildWaiter)
27+ InputGate::Waiter::Waiter (kj::PromiseFulfiller<Lock>& fulfiller,
28+ InputGate& gate,
29+ bool isChildWaiter,
30+ SpanParent parentSpan)
2931 : fulfiller(fulfiller),
3032 gate (&gate),
31- isChildWaiter(isChildWaiter) {
33+ isChildWaiter(isChildWaiter),
34+ waitSpan(parentSpan.newChild(" input_gate_lock_wait" _kjc)),
35+ lockSpanParent(kj::mv(parentSpan)) {
3236 gate.hooks .inputGateWaiterAdded ();
3337 if (isChildWaiter) {
3438 gate.waitingChildren .add (*this );
@@ -47,13 +51,13 @@ InputGate::Waiter::~Waiter() noexcept(false) {
4751 }
4852}
4953
50- kj::Promise<InputGate::Lock> InputGate::wait () {
54+ kj::Promise<InputGate::Lock> InputGate::wait (SpanParent parentSpan ) {
5155 KJ_IF_SOME (e, brokenState.tryGet <kj::Exception>()) {
5256 return kj::cp (e);
5357 } else if (lockCount == 0 ) {
54- return Lock (*this );
58+ return Lock (*this , kj::mv (parentSpan) );
5559 } else {
56- return kj::newAdaptedPromise<Lock, Waiter>(*this , false );
60+ return kj::newAdaptedPromise<Lock, Waiter>(*this , false , kj::mv (parentSpan) );
5761 }
5862}
5963
@@ -65,10 +69,11 @@ kj::Promise<void> InputGate::onBroken() {
6569 }
6670}
6771
68- InputGate::Lock::Lock (InputGate& gate)
72+ InputGate::Lock::Lock (InputGate& gate, SpanParent parentSpan )
6973 : gate(&gate),
7074 cs (gate.isCriticalSection ? kj::Maybe(kj::addRef(static_cast <CriticalSection&>(gate)))
71- : kj::none) {
75+ : kj::none),
76+ lockSpan(parentSpan.newChild(" input_gate_lock_hold" _kjc)) {
7277 InputGate* gateToLock = &gate;
7378
7479 KJ_IF_SOME (c, cs) {
@@ -107,11 +112,11 @@ void InputGate::releaseLock() {
107112 if (!waitingChildren.empty ()) {
108113 auto & waiter = waitingChildren.front ();
109114 waitingChildren.remove (waiter);
110- waiter.fulfiller .fulfill (Lock (*this ));
115+ waiter.fulfiller .fulfill (Lock (*this , kj::mv (waiter. lockSpanParent ) ));
111116 } else if (!waiters.empty ()) {
112117 auto & waiter = waiters.front ();
113118 waiters.remove (waiter);
114- waiter.fulfiller .fulfill (Lock (*this ));
119+ waiter.fulfiller .fulfill (Lock (*this , kj::mv (waiter. lockSpanParent ) ));
115120 }
116121 }
117122}
@@ -167,7 +172,7 @@ InputGate::CriticalSection::~CriticalSection() noexcept(false) {
167172 }
168173}
169174
170- kj::Promise<InputGate::Lock> InputGate::CriticalSection::wait () {
175+ kj::Promise<InputGate::Lock> InputGate::CriticalSection::wait (SpanParent parentSpan ) {
171176 for (;;) {
172177 switch (state) {
173178 case NOT_STARTED: {
@@ -183,11 +188,12 @@ kj::Promise<InputGate::Lock> InputGate::CriticalSection::wait() {
183188 // Add ourselves to this parent's child waiter list.
184189 if (target.lockCount == 0 ) {
185190 state = RUNNING;
186- parentLock = Lock (target);
191+ parentLock = Lock (target, parentSpan. addRef () );
187192 continue ;
188193 } else {
189194 try {
190- auto lock = co_await kj::newAdaptedPromise<Lock, Waiter>(target, true );
195+ auto lock =
196+ co_await kj::newAdaptedPromise<Lock, Waiter>(target, true , parentSpan.addRef ());
191197 state = RUNNING;
192198 parentLock = kj::mv (lock);
193199 continue ;
@@ -206,18 +212,18 @@ kj::Promise<InputGate::Lock> InputGate::CriticalSection::wait() {
206212 KJ_FAIL_REQUIRE (" CriticalSection::wait() should be called once initially" );
207213 case RUNNING:
208214 // CriticalSection is active, so defer to InputGate implementation.
209- co_return co_await InputGate::wait ();
215+ co_return co_await InputGate::wait (kj::mv (parentSpan) );
210216 case REPARENTED:
211217 // Once the CriticalSection has declared itself done, then any straggler tasks it initiated
212218 // are adopted by the parent.
213219 // WARNING: Don't use parentAsInputGate() here as that'll bypass the override of wait() if
214220 // the parent is a CriticalSection itself.
215221 KJ_SWITCH_ONEOF (parent) {
216222 KJ_CASE_ONEOF (p, InputGate*) {
217- co_return co_await p->wait ();
223+ co_return co_await p->wait (kj::mv (parentSpan) );
218224 }
219225 KJ_CASE_ONEOF (c, kj::Own<CriticalSection>) {
220- co_return co_await c->wait ();
226+ co_return co_await c->wait (kj::mv (parentSpan) );
221227 }
222228 }
223229 KJ_UNREACHABLE;
0 commit comments