@@ -401,10 +401,8 @@ int HttpOperation::OnProgressCallback(void *clientp,
401401}
402402#endif
403403
404- // The atomic member is only free of layout cost while it stays the size and alignment of the
405- // enum it replaced. The standard does not promise that, so it is checked here rather than
406- // asserted in prose: a toolchain where it does not hold changes an installed type and should say
407- // so at build time.
404+ // A toolchain where std::atomic<SessionState> is wider or more aligned than the enum would
405+ // change the layout of an installed type, so check it at build time rather than assume it.
408406static_assert (sizeof (std::atomic<opentelemetry::ext::http::client::SessionState>) ==
409407 sizeof (opentelemetry::ext::http::client::SessionState),
410408 " std::atomic<SessionState> grew, which changes the layout of HttpOperation" );
@@ -415,14 +413,10 @@ static_assert(
415413
416414namespace
417415{
418- // Which operations the calling thread is currently inside a callback for. A handler is allowed to
419- // call FinishSession() on the request it is being told about, and that call must not wait for a
420- // completion only the thread it is running on can publish.
421- // A stack of scopes linked through the scopes themselves, so entering one allocates nothing. Each
422- // lives on the stack frame that dispatches the callback, which is exactly as long as the entry
423- // needs to be there. A container here would put a heap allocation on every event, and would put
424- // it inside a noexcept constructor, where running out of memory calls std::terminate rather than
425- // reaching whoever asked for the request.
416+ // The operations this thread is inside a callback for. A handler may call FinishSession() on the
417+ // request it is being told about, and that must not wait for a completion only this thread
418+ // publishes. An intrusive stack rather than a container: entering allocates nothing, which a
419+ // noexcept constructor needs, since a failed allocation there would call std::terminate.
426420class CallbackScope
427421{
428422public:
@@ -544,9 +538,8 @@ HttpOperation::~HttpOperation()
544538 case opentelemetry::ext::http::client::SessionState::Connecting:
545539 case opentelemetry::ext::http::client::SessionState::Connected:
546540 case opentelemetry::ext::http::client::SessionState::Sending: {
547- // Not while inside a callback this operation dispatched: a handler that destroys the
548- // operation it is being told about would be waiting for a completion that only the thread
549- // running the handler can publish.
541+ // Not inside a callback this operation dispatched: the handler's own thread is the one
542+ // that publishes the completion it would wait for.
550543 if (async_data_ && async_data_->result_future .valid () &&
551544 !CallbackScope::InsideCallbackFor (this ))
552545 {
@@ -1546,10 +1539,8 @@ CURLcode HttpOperation::SendAsync(Session *session, std::function<void(HttpOpera
15461539 async_data_->result_future = async_data_->result_promise .get_future ();
15471540 async_data_->is_promise_running .store (true , std::memory_order_release);
15481541
1549- // Last, and the only thing that makes this operation reachable from the IO thread: an abort is
1550- // queued through this route, so nothing can bring the operation there before the callback and
1551- // the completion above exist. Cleanup is therefore the only thing that ever fulfils the promise,
1552- // at its tail, once the terminal event and the completion callback have run.
1542+ // Last: this store is what makes the operation reachable from the IO thread, so the callback
1543+ // and the promise above are published first. Cleanup() fulfils the promise, at its tail.
15531544 async_data_->session .store (session, std::memory_order_release);
15541545
15551546 DispatchEvent (opentelemetry::ext::http::client::SessionState::Connecting);
@@ -1625,15 +1616,9 @@ void HttpOperation::Abort()
16251616
16261617void HttpOperation::FinishUnscheduled (const char *reason)
16271618{
1628- // The event first, because the operation holds the handler as a bare pointer and the only
1629- // strong reference to it is the one the completion callback captured. Cleanup() takes that
1630- // callback and lets it go, so an event dispatched afterwards can be talking to a handler
1631- // nothing owns any more. A handler calling FinishSession() from here is inside a callback for
1632- // this operation, so Finish() returns rather than waiting on a promise this thread has not
1633- // published yet, which is what used to make the other order necessary.
1634- //
1635- // DispatchEvent stores the state before it calls the handler, so the cleanup below sees a
1636- // terminal state and does not report a manual cancel for something nobody cancelled.
1619+ // Before Cleanup(), which releases the completion callback holding the only strong reference
1620+ // to the handler. DispatchEvent stores the terminal state first, so the Cleanup() below does
1621+ // not report a cancel as well.
16371622 DispatchEvent (opentelemetry::ext::http::client::SessionState::CreateFailed,
16381623 nullptr != reason ? reason : " " );
16391624
0 commit comments