lib: add bt_{graph,query_executor}_add_interrupter()
[babeltrace.git] / src / lib / graph / connection.c
CommitLineData
784cdc68 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
784cdc68
JG
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
784cdc68
JG
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/CONNECTION"
c2d9d9cf 25#include "lib/logging.h"
a36bfb16 26
578e048b
MJ
27#include "common/assert.h"
28#include "lib/assert-pre.h"
51375aa9 29#include "lib/assert-post.h"
3fadfbc0 30#include <babeltrace2/graph/connection-const.h>
578e048b
MJ
31#include "lib/object.h"
32#include "compat/compiler.h"
0fbb9a9f 33#include <stdlib.h>
784cdc68
JG
34#include <glib.h>
35
578e048b
MJ
36#include "component.h"
37#include "connection.h"
38#include "graph.h"
39#include "message/iterator.h"
40#include "port.h"
41
784cdc68 42static
d94d92ac 43void destroy_connection(struct bt_object *obj)
784cdc68
JG
44{
45 struct bt_connection *connection = container_of(obj,
46 struct bt_connection, base);
bd14d768 47
3f7d4d90 48 BT_LIB_LOGI("Destroying connection: %!+x", connection);
a36bfb16 49
bd14d768 50 /*
d0fea130
PP
51 * Make sure that each message iterator which was created for
52 * this connection is finalized before we destroy it. Once a
9b4f9b42 53 * message iterator is finalized, you cannot use it.
bd14d768
PP
54 *
55 * Because connections are destroyed before components within a
d6e69534 56 * graph, this ensures that message iterators are always
bd14d768 57 * finalized before their upstream component.
c42ea0af
PP
58 *
59 * Ending the connection does exactly this. We pass `false` to
60 * bt_connection_end() here to avoid removing this connection
61 * from the graph: if we're here, we're already in the graph's
62 * destructor.
bd14d768 63 */
c42ea0af
PP
64 bt_connection_end(connection, false);
65 g_ptr_array_free(connection->iterators, TRUE);
d94d92ac 66 connection->iterators = NULL;
784cdc68
JG
67
68 /*
65300d60 69 * No bt_object_put_ref on ports as a connection only holds _weak_
c42ea0af 70 * references to them.
784cdc68
JG
71 */
72 g_free(connection);
73}
74
f167d3c0 75static
d94d92ac 76void try_remove_connection_from_graph(struct bt_connection *connection)
f167d3c0 77{
3fea54f6 78 void *graph = (void *) bt_object_borrow_parent(&connection->base);
f167d3c0 79
3fea54f6 80 if (connection->base.ref_count > 0 ||
f167d3c0
PP
81 connection->downstream_port ||
82 connection->upstream_port ||
83 connection->iterators->len > 0) {
84 return;
85 }
86
87 /*
88 * At this point we know that:
89 *
a36bfb16 90 * 1. The connection is ended (ports were disconnected).
d6e69534 91 * 2. All the message iterators that this connection
f167d3c0
PP
92 * created, if any, are finalized.
93 * 3. The connection's reference count is 0, so only the
94 * parent (graph) owns this connection after this call.
95 *
96 * In other words, no other object than the graph knows this
97 * connection.
98 *
99 * It is safe to remove the connection from the graph, therefore
100 * destroying it.
101 */
d94d92ac
PP
102 BT_LIB_LOGD("Removing self from graph's connections: "
103 "%![graph-]+g, %![conn-]+x", graph, connection);
f167d3c0
PP
104 bt_graph_remove_connection(graph, connection);
105}
106
107static
d94d92ac 108void parent_is_owner(struct bt_object *obj)
f167d3c0
PP
109{
110 struct bt_connection *connection = container_of(obj,
111 struct bt_connection, base);
112
d94d92ac 113 try_remove_connection_from_graph(connection);
890882ef
PP
114}
115
784cdc68 116BT_HIDDEN
d94d92ac 117struct bt_connection *bt_connection_create(struct bt_graph *graph,
72b913fb
PP
118 struct bt_port *upstream_port,
119 struct bt_port *downstream_port)
784cdc68
JG
120{
121 struct bt_connection *connection = NULL;
122
3f7d4d90 123 BT_LIB_LOGI("Creating connection: "
d94d92ac
PP
124 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
125 graph, upstream_port, downstream_port);
784cdc68
JG
126 connection = g_new0(struct bt_connection, 1);
127 if (!connection) {
870631a2 128 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one connection.");
784cdc68
JG
129 goto end;
130 }
131
3fea54f6 132 bt_object_init_shared_with_parent(&connection->base,
d94d92ac 133 destroy_connection);
3fea54f6 134 bt_object_set_parent_is_owner_listener_func(&connection->base,
d94d92ac 135 parent_is_owner);
bd14d768
PP
136 connection->iterators = g_ptr_array_new();
137 if (!connection->iterators) {
870631a2 138 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
65300d60 139 BT_OBJECT_PUT_REF_AND_RESET(connection);
bd14d768
PP
140 goto end;
141 }
142
784cdc68 143 /* Weak references are taken, see comment in header. */
72b913fb
PP
144 connection->upstream_port = upstream_port;
145 connection->downstream_port = downstream_port;
d94d92ac 146 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port);
72b913fb 147 bt_port_set_connection(upstream_port, connection);
d94d92ac
PP
148 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
149 downstream_port);
72b913fb 150 bt_port_set_connection(downstream_port, connection);
3fea54f6 151 bt_object_set_parent(&connection->base, &graph->base);
3f7d4d90 152 BT_LIB_LOGI("Created connection: %!+x", connection);
a36bfb16 153
784cdc68
JG
154end:
155 return connection;
156}
157
72b913fb 158BT_HIDDEN
d94d92ac 159void bt_connection_end(struct bt_connection *conn, bool try_remove_from_graph)
72b913fb 160{
72b913fb
PP
161 struct bt_port *downstream_port = conn->downstream_port;
162 struct bt_port *upstream_port = conn->upstream_port;
f167d3c0 163 size_t i;
72b913fb 164
3f7d4d90 165 BT_LIB_LOGI("Ending connection: %!+x, try-remove-from-graph=%d",
c42ea0af
PP
166 conn, try_remove_from_graph);
167
d94d92ac 168 /*
d6e69534 169 * Any of the following message callback functions could
d94d92ac
PP
170 * remove one of the connection's ports from its component. To
171 * make sure that at least logging in called functions works
172 * with existing objects, get a local reference on both ports.
173 */
174 bt_object_get_ref(downstream_port);
175 bt_object_get_ref(upstream_port);
176
72b913fb 177 if (downstream_port) {
d94d92ac
PP
178 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
179 downstream_port);
72b913fb
PP
180 bt_port_set_connection(downstream_port, NULL);
181 conn->downstream_port = NULL;
182 }
183
184 if (upstream_port) {
d94d92ac
PP
185 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
186 upstream_port);
72b913fb
PP
187 bt_port_set_connection(upstream_port, NULL);
188 conn->upstream_port = NULL;
189 }
190
d94d92ac
PP
191 /*
192 * It is safe to put the local port references now that we don't
193 * need them anymore. This could indeed destroy them.
194 */
195 bt_object_put_ref(downstream_port);
196 bt_object_put_ref(upstream_port);
f167d3c0
PP
197
198 /*
d0fea130
PP
199 * Because this connection is ended, finalize each message
200 * iterator created from it.
201 *
202 * In practice, this only happens when the connection is
203 * destroyed and not all its message iterators were finalized,
204 * which is on graph destruction.
f167d3c0
PP
205 */
206 for (i = 0; i < conn->iterators->len; i++) {
d6e69534 207 struct bt_self_component_port_input_message_iterator *iterator =
f167d3c0
PP
208 g_ptr_array_index(conn->iterators, i);
209
d6e69534 210 BT_LIB_LOGD("Finalizing message iterator created by "
d94d92ac 211 "this ended connection: %![iter-]+i", iterator);
d0fea130 212 bt_self_component_port_input_message_iterator_try_finalize(
d94d92ac 213 iterator);
f167d3c0
PP
214
215 /*
216 * Make sure this iterator does not try to remove itself
217 * from this connection's iterators on destruction
218 * because this connection won't exist anymore.
219 */
d6e69534 220 bt_self_component_port_input_message_iterator_set_connection(
90157d89 221 iterator, NULL);
f167d3c0
PP
222 }
223
224 g_ptr_array_set_size(conn->iterators, 0);
c42ea0af
PP
225
226 if (try_remove_from_graph) {
d94d92ac 227 try_remove_connection_from_graph(conn);
c42ea0af 228 }
72b913fb
PP
229}
230
0d72b8c3
PP
231const struct bt_port_output *bt_connection_borrow_upstream_port_const(
232 const struct bt_connection *connection)
784cdc68 233{
bdb288b3 234 BT_ASSERT_PRE_DEV_NON_NULL(connection, "Connection");
d94d92ac 235 return (void *) connection->upstream_port;
784cdc68
JG
236}
237
0d72b8c3
PP
238const struct bt_port_input *bt_connection_borrow_downstream_port_const(
239 const struct bt_connection *connection)
784cdc68 240{
bdb288b3 241 BT_ASSERT_PRE_DEV_NON_NULL(connection, "Connection");
d94d92ac 242 return (void *) connection->downstream_port;
784cdc68 243}
bd14d768
PP
244
245BT_HIDDEN
246void bt_connection_remove_iterator(struct bt_connection *conn,
d6e69534 247 struct bt_self_component_port_input_message_iterator *iterator)
bd14d768
PP
248{
249 g_ptr_array_remove(conn->iterators, iterator);
3f7d4d90 250 BT_LIB_LOGD("Removed message iterator from connection: "
d94d92ac
PP
251 "%![conn-]+x, %![iter-]+i", conn, iterator);
252 try_remove_connection_from_graph(conn);
bd14d768 253}
090a4c0a 254
c5b9b441
PP
255void bt_connection_get_ref(const struct bt_connection *connection)
256{
257 bt_object_get_ref(connection);
258}
259
260void bt_connection_put_ref(const struct bt_connection *connection)
261{
262 bt_object_put_ref(connection);
263}
This page took 0.057621 seconds and 4 git commands to generate.