relayd: move relay_session locking outside of make_viewer_streams
[lttng-tools.git] / src / bin / lttng-relayd / viewer-session.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <common/common.h>
22 #include <urcu/rculist.h>
23
24 #include "lttng-relayd.h"
25 #include "ctf-trace.h"
26 #include "session.h"
27 #include "viewer-session.h"
28 #include "viewer-stream.h"
29 #include "stream.h"
30
31 struct relay_viewer_session *viewer_session_create(void)
32 {
33 struct relay_viewer_session *vsession;
34
35 vsession = zmalloc(sizeof(*vsession));
36 if (!vsession) {
37 goto end;
38 }
39 CDS_INIT_LIST_HEAD(&vsession->session_list);
40 end:
41 return vsession;
42 }
43
44 /* The existence of session must be guaranteed by the caller. */
45 int viewer_session_attach(struct relay_viewer_session *vsession,
46 struct relay_session *session)
47 {
48 int ret = 0;
49
50 ASSERT_LOCKED(session->lock);
51
52 /* Will not fail, as per the ownership guarantee. */
53 if (!session_get(session)) {
54 ret = -1;
55 goto end;
56 }
57 if (session->viewer_attached) {
58 ret = -1;
59 } else {
60 session->viewer_attached = true;
61 }
62
63 if (!ret) {
64 pthread_mutex_lock(&vsession->session_list_lock);
65 /* Ownership is transfered to the list. */
66 cds_list_add_rcu(&session->viewer_session_node,
67 &vsession->session_list);
68 pthread_mutex_unlock(&vsession->session_list_lock);
69 } else {
70 /* Put our local ref. */
71 session_put(session);
72 }
73 end:
74 return ret;
75 }
76
77 /* The existence of session must be guaranteed by the caller. */
78 static int viewer_session_detach(struct relay_viewer_session *vsession,
79 struct relay_session *session)
80 {
81 int ret = 0;
82
83 pthread_mutex_lock(&session->lock);
84 if (!session->viewer_attached) {
85 ret = -1;
86 } else {
87 session->viewer_attached = false;
88 }
89
90 if (!ret) {
91 pthread_mutex_lock(&vsession->session_list_lock);
92 cds_list_del_rcu(&session->viewer_session_node);
93 pthread_mutex_unlock(&vsession->session_list_lock);
94 /* Release reference held by the list. */
95 session_put(session);
96 }
97 /* Safe since we know the session exists. */
98 pthread_mutex_unlock(&session->lock);
99 return ret;
100 }
101
102 void viewer_session_destroy(struct relay_viewer_session *vsession)
103 {
104 free(vsession);
105 }
106
107 /*
108 * Release ownership of all the streams of one session and detach the viewer.
109 */
110 void viewer_session_close_one_session(struct relay_viewer_session *vsession,
111 struct relay_session *session)
112 {
113 struct lttng_ht_iter iter;
114 struct relay_viewer_stream *vstream;
115
116 /*
117 * TODO: improvement: create more efficient list of
118 * vstream per session.
119 */
120 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter,
121 vstream, stream_n.node) {
122 if (!viewer_stream_get(vstream)) {
123 continue;
124 }
125 if (vstream->stream->trace->session != session) {
126 viewer_stream_put(vstream);
127 continue;
128 }
129 /* Put local reference. */
130 viewer_stream_put(vstream);
131 /*
132 * We have reached one of the viewer stream's lifetime
133 * end condition. This "put" will cause the proper
134 * teardown of the viewer stream.
135 */
136 viewer_stream_put(vstream);
137 }
138
139 viewer_session_detach(vsession, session);
140 }
141
142 void viewer_session_close(struct relay_viewer_session *vsession)
143 {
144 struct relay_session *session;
145
146 rcu_read_lock();
147 cds_list_for_each_entry_rcu(session,
148 &vsession->session_list, viewer_session_node) {
149 viewer_session_close_one_session(vsession, session);
150 }
151 rcu_read_unlock();
152 }
153
154 /*
155 * Check if a connection is attached to a session.
156 * Return 1 if attached, 0 if not attached, a negative value on error.
157 */
158 int viewer_session_is_attached(struct relay_viewer_session *vsession,
159 struct relay_session *session)
160 {
161 struct relay_session *iter;
162 int found = 0;
163
164 pthread_mutex_lock(&session->lock);
165 if (!vsession) {
166 goto end;
167 }
168 if (!session->viewer_attached) {
169 goto end;
170 }
171 rcu_read_lock();
172 cds_list_for_each_entry_rcu(iter,
173 &vsession->session_list,
174 viewer_session_node) {
175 if (session == iter) {
176 found = 1;
177 goto end_rcu_unlock;
178 }
179 }
180 end_rcu_unlock:
181 rcu_read_unlock();
182 end:
183 pthread_mutex_unlock(&session->lock);
184 return found;
185 }
This page took 0.033199 seconds and 5 git commands to generate.