Run clang-format on the whole tree
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.cpp
index 7a3cbdea0ebde00f0613ccff1cb42553d2d6d798..9e8693333ebda098f02a8943c71016c2b8b2f9a2 100644 (file)
@@ -9,21 +9,21 @@
 
 #define _LGPL_SOURCE
 
-#include <common/common.h>
-#include <common/utils.h>
-#include <urcu/rculist.h>
+#include "ctf-trace.hpp"
+#include "lttng-relayd.hpp"
+#include "stream.hpp"
+
+#include <common/common.hpp>
+#include <common/utils.hpp>
 
-#include "ctf-trace.h"
-#include "lttng-relayd.h"
-#include "stream.h"
+#include <urcu/rculist.h>
 
 static uint64_t last_relay_ctf_trace_id;
 static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
 {
-       struct ctf_trace *trace =
-               caa_container_of(rcu_head, struct ctf_trace, rcu_node);
+       struct ctf_trace *trace = lttng::utils::container_of(rcu_head, &ctf_trace::rcu_node);
 
        free(trace);
 }
@@ -41,6 +41,8 @@ static void ctf_trace_destroy(struct ctf_trace *trace)
         * control side.
         */
        LTTNG_ASSERT(cds_list_empty(&trace->stream_list));
+       ASSERT_RCU_READ_LOCKED();
+
        session_put(trace->session);
        trace->session = NULL;
        free(trace->path);
@@ -50,8 +52,7 @@ static void ctf_trace_destroy(struct ctf_trace *trace)
 
 static void ctf_trace_release(struct urcu_ref *ref)
 {
-       struct ctf_trace *trace =
-               caa_container_of(ref, struct ctf_trace, ref);
+       struct ctf_trace *trace = lttng::utils::container_of(ref, &ctf_trace::ref);
        int ret;
        struct lttng_ht_iter iter;
 
@@ -62,11 +63,26 @@ static void ctf_trace_release(struct urcu_ref *ref)
 }
 
 /*
- * Should be called with RCU read-side lock held.
+ * The caller must either:
+ * - hold the RCU read side lock, or
+ * - guarantee the existence of the object by already holding a reference to
+ *   the object.
  */
 bool ctf_trace_get(struct ctf_trace *trace)
 {
-       return urcu_ref_get_unless_zero(&trace->ref);
+       const bool ref = urcu_ref_get_unless_zero(&trace->ref);
+
+       if (!ref) {
+               /*
+                * The ref count is already zero. It means the object is being
+                * torn down concurently.
+                * This is only acceptable if we hold the RCU read-side lock,
+                * else it's a logic error.
+                */
+               ASSERT_RCU_READ_LOCKED();
+       }
+
+       return ref;
 }
 
 /*
@@ -75,12 +91,11 @@ bool ctf_trace_get(struct ctf_trace *trace)
  * create and refcounting. Whenever all the streams belonging to a trace
  * put their reference, its refcount drops to 0.
  */
-static struct ctf_trace *ctf_trace_create(struct relay_session *session,
-               const char *subpath)
+static struct ctf_trace *ctf_trace_create(struct relay_session *session, const char *subpath)
 {
        struct ctf_trace *trace;
 
-       trace = (ctf_trace *) zmalloc(sizeof(*trace));
+       trace = zmalloc<ctf_trace>();
        if (!trace) {
                PERROR("Failed to allocate ctf_trace");
                goto end;
@@ -109,9 +124,11 @@ static struct ctf_trace *ctf_trace_create(struct relay_session *session,
        pthread_mutex_init(&trace->stream_list_lock, NULL);
        lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
 
-       DBG("Created ctf_trace %" PRIu64 "of session \"%s\" from host \"%s\" with path: %s",
-                       trace->id, session->session_name, session->hostname,
-                       subpath);
+       DBG("Created ctf_trace %" PRIu64 " of session \"%s\" from host \"%s\" with path: %s",
+           trace->id,
+           session->session_name,
+           session->hostname,
+           subpath);
 
 end:
        return trace;
@@ -126,7 +143,7 @@ error:
  * ctf_trace_put().
  */
 struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
-               const char *subpath)
+                                                 const char *subpath)
 {
        struct lttng_ht_node_str *node;
        struct lttng_ht_iter iter;
@@ -139,7 +156,7 @@ struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
                DBG("CTF Trace path %s not found", subpath);
                goto end;
        }
-       trace = caa_container_of(node, struct ctf_trace, node);
+       trace = lttng::utils::container_of(node, &ctf_trace::node);
        if (!ctf_trace_get(trace)) {
                trace = NULL;
        }
@@ -164,8 +181,8 @@ int ctf_trace_close(struct ctf_trace *trace)
        struct relay_stream *stream;
 
        rcu_read_lock();
-       cds_list_for_each_entry_rcu(stream, &trace->stream_list,
-                       stream_node) {
+       cds_list_for_each_entry_rcu(stream, &trace->stream_list, stream_node)
+       {
                /*
                 * Close stream since the connection owning the trace is being
                 * torn down.
This page took 0.025618 seconds and 4 git commands to generate.