Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-relayd / session.cpp
index 677769ace1c8fd3b38709dbd4d6a084a1cf1d470..e05482f15622995673aa71aabe3abc92a0b0d779 100644 (file)
@@ -20,6 +20,7 @@
 #include <common/defaults.hpp>
 #include <common/fd-tracker/utils.hpp>
 #include <common/time.hpp>
+#include <common/urcu.hpp>
 #include <common/utils.hpp>
 #include <common/uuid.hpp>
 
@@ -40,7 +41,7 @@ static int init_session_output_path_group_by_host(struct relay_session *session)
         * else
         *   hostname/base_path
         */
-       char *session_directory = NULL;
+       char *session_directory = nullptr;
        int ret = 0;
 
        if (session->output_path[0] != '\0') {
@@ -110,7 +111,7 @@ static int init_session_output_path_group_by_session(struct relay_session *sessi
         * integral part of the name and how a user identify a session.
         */
        int ret = 0;
-       char *session_directory = NULL;
+       char *session_directory = nullptr;
        char creation_datetime[DATETIME_STR_LEN];
 
        if (session->output_path[0] != '\0') {
@@ -181,8 +182,8 @@ session_create_output_directory_handle(struct relay_session *session)
         * relayd_output_path/session_directory
         * e.g. /home/user/lttng-traces/hostname/session_name
         */
-       char *full_session_path = NULL;
-       struct lttng_directory_handle *handle = NULL;
+       char *full_session_path = nullptr;
+       struct lttng_directory_handle *handle = nullptr;
 
        pthread_mutex_lock(&session->lock);
        full_session_path = create_output_path(session->output_path);
@@ -206,7 +207,7 @@ end:
 static int session_set_anonymous_chunk(struct relay_session *session)
 {
        int ret = 0;
-       struct lttng_trace_chunk *chunk = NULL;
+       struct lttng_trace_chunk *chunk = nullptr;
        enum lttng_trace_chunk_status status;
        struct lttng_directory_handle *output_directory;
 
@@ -234,7 +235,7 @@ static int session_set_anonymous_chunk(struct relay_session *session)
        }
 
        session->current_trace_chunk = chunk;
-       chunk = NULL;
+       chunk = nullptr;
 end:
        lttng_trace_chunk_put(chunk);
        lttng_directory_handle_put(output_directory);
@@ -293,7 +294,7 @@ struct relay_session *session_create(const char *session_name,
                                     bool session_name_contains_creation_time)
 {
        int ret;
-       struct relay_session *session = NULL;
+       struct relay_session *session = nullptr;
 
        LTTNG_ASSERT(session_name);
        LTTNG_ASSERT(hostname);
@@ -325,8 +326,8 @@ struct relay_session *session_create(const char *session_name,
        lttng_ht_node_init_u64(&session->session_n, session->id);
        urcu_ref_init(&session->ref);
        CDS_INIT_LIST_HEAD(&session->recv_list);
-       pthread_mutex_init(&session->lock, NULL);
-       pthread_mutex_init(&session->recv_list_lock, NULL);
+       pthread_mutex_init(&session->lock, nullptr);
+       pthread_mutex_init(&session->recv_list_lock, nullptr);
 
        if (lttng_strncpy(session->session_name, session_name, sizeof(session->session_name))) {
                WARN("Session name exceeds maximal allowed length");
@@ -343,7 +344,7 @@ struct relay_session *session_create(const char *session_name,
        if (creation_time) {
                LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
        } else {
-               LTTNG_OPTIONAL_SET(&session->creation_time, time(NULL));
+               LTTNG_OPTIONAL_SET(&session->creation_time, time(nullptr));
                if (session->creation_time.value == (time_t) -1) {
                        PERROR("Failed to sample session creation time");
                        goto error;
@@ -432,7 +433,7 @@ struct relay_session *session_create(const char *session_name,
 
 error:
        session_put(session);
-       return NULL;
+       return nullptr;
 }
 
 /* Should be called with RCU read-side lock held. */
@@ -450,13 +451,13 @@ bool session_get(struct relay_session *session)
  */
 struct relay_session *session_get_by_id(uint64_t id)
 {
-       struct relay_session *session = NULL;
+       struct relay_session *session = nullptr;
        struct lttng_ht_node_u64 *node;
        struct lttng_ht_iter iter;
 
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        lttng_ht_lookup(sessions_ht, &id, &iter);
-       node = lttng_ht_iter_get_node_u64(&iter);
+       node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
        if (!node) {
                DBG("Session find by ID %" PRIu64 " id NOT found", id);
                goto end;
@@ -464,10 +465,9 @@ struct relay_session *session_get_by_id(uint64_t id)
        session = lttng::utils::container_of(node, &relay_session::session_n);
        DBG("Session find by ID %" PRIu64 " id found", id);
        if (!session_get(session)) {
-               session = NULL;
+               session = nullptr;
        }
 end:
-       rcu_read_unlock();
        return session;
 }
 
@@ -480,8 +480,6 @@ end:
 bool session_has_ongoing_rotation(const struct relay_session *session)
 {
        bool ongoing_rotation = false;
-       struct lttng_ht_iter iter;
-       struct relay_session *iterated_session;
 
        ASSERT_LOCKED(session->lock);
 
@@ -498,12 +496,14 @@ bool session_has_ongoing_rotation(const struct relay_session *session)
                goto end;
        }
 
-       rcu_read_lock();
        /*
         * Sample the 'ongoing_rotation' status of all relay sessions that
         * originate from the same session daemon session.
         */
-       cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, iterated_session, session_n.node) {
+       for (auto *iterated_session :
+            lttng::urcu::lfht_iteration_adapter<relay_session,
+                                                decltype(relay_session::session_n),
+                                                &relay_session::session_n>(*sessions_ht->ht)) {
                if (!session_get(iterated_session)) {
                        continue;
                }
@@ -548,7 +548,6 @@ bool session_has_ongoing_rotation(const struct relay_session *session)
                        break;
                }
        }
-       rcu_read_unlock();
 
 end:
        return ongoing_rotation;
@@ -556,7 +555,7 @@ end:
 
 static void rcu_destroy_session(struct rcu_head *rcu_head)
 {
-       struct relay_session *session = caa_container_of(rcu_head, struct relay_session, rcu_node);
+       auto *session = lttng::utils::container_of(rcu_head, &relay_session::rcu_node);
        /*
         * Since each trace has a reference on the session, it means
         * that if we are at the point where we teardown the session, no
@@ -588,14 +587,14 @@ static void destroy_session(struct relay_session *session)
        ret = session_delete(session);
        LTTNG_ASSERT(!ret);
        lttng_trace_chunk_put(session->current_trace_chunk);
-       session->current_trace_chunk = NULL;
+       session->current_trace_chunk = nullptr;
        lttng_trace_chunk_put(session->pending_closure_trace_chunk);
-       session->pending_closure_trace_chunk = NULL;
+       session->pending_closure_trace_chunk = nullptr;
        ret = sessiond_trace_chunk_registry_session_destroyed(sessiond_trace_chunk_registry,
                                                              session->sessiond_uuid);
        LTTNG_ASSERT(!ret);
        lttng_directory_handle_put(session->output_directory);
-       session->output_directory = NULL;
+       session->output_directory = nullptr;
        call_rcu(&session->rcu_node, rcu_destroy_session);
 }
 
@@ -611,17 +610,13 @@ void session_put(struct relay_session *session)
        if (!session) {
                return;
        }
-       rcu_read_lock();
+       const lttng::urcu::read_lock_guard read_lock;
        urcu_ref_put(&session->ref, session_release);
-       rcu_read_unlock();
 }
 
 int session_close(struct relay_session *session)
 {
        int ret = 0;
-       struct ctf_trace *trace;
-       struct lttng_ht_iter iter;
-       struct relay_stream *stream;
 
        pthread_mutex_lock(&session->lock);
        DBG("closing session %" PRIu64 ": is conn already closed %d",
@@ -630,23 +625,28 @@ int session_close(struct relay_session *session)
        session->connection_closed = true;
        pthread_mutex_unlock(&session->lock);
 
-       rcu_read_lock();
-       cds_lfht_for_each_entry (session->ctf_traces_ht->ht, &iter.iter, trace, node.node) {
+       for (auto *trace :
+            lttng::urcu::lfht_iteration_adapter<ctf_trace,
+                                                decltype(ctf_trace::node),
+                                                &ctf_trace::node>(*session->ctf_traces_ht->ht)) {
                ret = ctf_trace_close(trace);
                if (ret) {
-                       goto rcu_unlock;
+                       goto end;
                }
        }
-       cds_list_for_each_entry_rcu(stream, &session->recv_list, recv_node)
-       {
+
+       for (auto *stream :
+            lttng::urcu::rcu_list_iteration_adapter<relay_stream, &relay_stream::recv_node>(
+                    session->recv_list)) {
                /* Close streams which have not been published yet. */
                try_stream_close(stream);
        }
-rcu_unlock:
-       rcu_read_unlock();
+
+end:
        if (ret) {
                return ret;
        }
+
        /* Put self-reference from create. */
        session_put(session);
        return ret;
@@ -654,7 +654,7 @@ rcu_unlock:
 
 int session_abort(struct relay_session *session)
 {
-       int ret = 0;
+       const int ret = 0;
 
        if (!session) {
                return 0;
@@ -667,17 +667,16 @@ int session_abort(struct relay_session *session)
        return ret;
 }
 
-void print_sessions(void)
+void print_sessions()
 {
-       struct lttng_ht_iter iter;
-       struct relay_session *session;
-
        if (!sessions_ht) {
                return;
        }
 
-       rcu_read_lock();
-       cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, session, session_n.node) {
+       for (auto *session :
+            lttng::urcu::lfht_iteration_adapter<relay_session,
+                                                decltype(relay_session::session_n),
+                                                &relay_session::session_n>(*sessions_ht->ht)) {
                if (!session_get(session)) {
                        continue;
                }
@@ -687,5 +686,4 @@ void print_sessions(void)
                    session->id);
                session_put(session);
        }
-       rcu_read_unlock();
 }
This page took 0.033546 seconds and 4 git commands to generate.