lh2debug commented on code in PR #3140:
URL: https://github.com/apache/brpc/pull/3140#discussion_r2958511946


##########
src/brpc/span.cpp:
##########
@@ -37,9 +37,96 @@
 
 #define BRPC_SPAN_INFO_SEP "\1"
 
-
 namespace brpc {
 
+// Callback for creating a new bthread span when creating a new bthread.
+// This is called by bthread layer when BTHREAD_INHERIT_SPAN flag is set.
+// Returns a heap-allocated weak_ptr<Span>* as void*, or NULL if span creation 
fails.
+void* CreateBthreadSpanAsVoid() {
+    const int64_t received_us = butil::cpuwide_time_us();
+    const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+    std::shared_ptr<Span> span = Span::CreateBthreadSpan("Bthread", 
base_realtime);
+
+    if (!span) {
+        return NULL;
+    }
+    return new std::weak_ptr<Span>(span);
+}
+
+void DestroyRpczParentSpan(void* ptr) {
+    if (ptr) {
+        delete static_cast<std::weak_ptr<Span>*>(ptr);
+    }
+}
+
+void EndBthreadSpan() {
+    std::shared_ptr<Span> span = GetTlsParentSpan();
+    if (span) {
+        bthread_id_t id = {bthread_self()};
+        span->set_ending_cid(id);
+    }
+
+    ClearTlsParentSpan();
+}
+
+void SetTlsParentSpan(std::shared_ptr<Span> span) {
+    using namespace bthread;
+    LocalStorage ls = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls);
+    if (ls.rpcz_parent_span) {
+        *static_cast<std::weak_ptr<Span>*>(ls.rpcz_parent_span) = span;
+    } else {
+        ls.rpcz_parent_span = new std::weak_ptr<Span>(span);
+        BAIDU_SET_VOLATILE_THREAD_LOCAL(tls_bls, ls);
+    }
+}
+
+std::shared_ptr<Span> GetTlsParentSpan() {
+    using namespace bthread;
+    LocalStorage ls = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls);
+    if (!ls.rpcz_parent_span) {
+        return nullptr;
+    }
+
+    auto* weak_ptr = static_cast<std::weak_ptr<Span>*>(ls.rpcz_parent_span);
+    return weak_ptr->lock();
+}
+
+void ClearTlsParentSpan() {
+    using namespace bthread;
+    LocalStorage ls = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls);
+    if (ls.rpcz_parent_span) {
+        static_cast<std::weak_ptr<Span>*>(ls.rpcz_parent_span)->reset();
+    }
+}
+
+bool HasTlsParentSpan() {
+    using namespace bthread;
+    LocalStorage ls = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls);
+    if (!ls.rpcz_parent_span) {
+        return false;
+    }
+
+    auto* weak_ptr = static_cast<std::weak_ptr<Span>*>(ls.rpcz_parent_span);
+    return !weak_ptr->expired();
+}
+
+
+void SpanDeleter::operator()(Span* r) const {
+    if (r == NULL) {
+        return;
+    }
+
+    // All children will be destroyed automatically along with the list.
+    // The list holds std::shared_ptr<> which will trigger deletion of
+    // children.
+    r->_client_list.clear();
+    r->_info.clear();
+    // Destroy the spinlocks, as the destructor might not be invoked.
+    pthread_spin_destroy(&r->_client_list_spinlock);
+    pthread_spin_destroy(&r->_info_spinlock);

Review Comment:
   A span is initialized only once and destroyed once when its reference count 
is reset to zero (the lock is destroyed, and it is returned to the object 
pool). At this point, the span's lifecycle ends.
   
   Subsequently, only new spans will be retrieved from the object pool for use.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to