lib: rename include dir to babeltrace2
[babeltrace.git] / 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
a36bfb16 24#define BT_LOG_TAG "CONNECTION"
3fadfbc0 25#include <babeltrace2/lib-logging-internal.h>
a36bfb16 26
3fadfbc0
MJ
27#include <babeltrace2/assert-internal.h>
28#include <babeltrace2/assert-pre-internal.h>
29#include <babeltrace2/graph/message-iterator-internal.h>
30#include <babeltrace2/graph/component-internal.h>
31#include <babeltrace2/graph/connection-internal.h>
32#include <babeltrace2/graph/connection-const.h>
33#include <babeltrace2/graph/graph-internal.h>
34#include <babeltrace2/graph/port-internal.h>
35#include <babeltrace2/object-internal.h>
36#include <babeltrace2/compiler-internal.h>
0fbb9a9f 37#include <stdlib.h>
784cdc68
JG
38#include <glib.h>
39
40static
d94d92ac 41void destroy_connection(struct bt_object *obj)
784cdc68
JG
42{
43 struct bt_connection *connection = container_of(obj,
44 struct bt_connection, base);
bd14d768 45
d94d92ac 46 BT_LIB_LOGD("Destroying connection: %!+x", connection);
a36bfb16 47
bd14d768 48 /*
d0fea130
PP
49 * Make sure that each message iterator which was created for
50 * this connection is finalized before we destroy it. Once a
51 * message iterator is finalized, all its method return NULL or
52 * the BT_MESSAGE_ITERATOR_STATUS_CANCELED status.
bd14d768
PP
53 *
54 * Because connections are destroyed before components within a
d6e69534 55 * graph, this ensures that message iterators are always
bd14d768 56 * finalized before their upstream component.
c42ea0af
PP
57 *
58 * Ending the connection does exactly this. We pass `false` to
59 * bt_connection_end() here to avoid removing this connection
60 * from the graph: if we're here, we're already in the graph's
61 * destructor.
bd14d768 62 */
c42ea0af
PP
63 bt_connection_end(connection, false);
64 g_ptr_array_free(connection->iterators, TRUE);
d94d92ac 65 connection->iterators = NULL;
784cdc68
JG
66
67 /*
65300d60 68 * No bt_object_put_ref on ports as a connection only holds _weak_
c42ea0af 69 * references to them.
784cdc68
JG
70 */
71 g_free(connection);
72}
73
f167d3c0 74static
d94d92ac 75void try_remove_connection_from_graph(struct bt_connection *connection)
f167d3c0 76{
3fea54f6 77 void *graph = (void *) bt_object_borrow_parent(&connection->base);
f167d3c0 78
3fea54f6 79 if (connection->base.ref_count > 0 ||
f167d3c0
PP
80 connection->downstream_port ||
81 connection->upstream_port ||
82 connection->iterators->len > 0) {
83 return;
84 }
85
86 /*
87 * At this point we know that:
88 *
a36bfb16 89 * 1. The connection is ended (ports were disconnected).
d6e69534 90 * 2. All the message iterators that this connection
f167d3c0
PP
91 * created, if any, are finalized.
92 * 3. The connection's reference count is 0, so only the
93 * parent (graph) owns this connection after this call.
94 *
95 * In other words, no other object than the graph knows this
96 * connection.
97 *
98 * It is safe to remove the connection from the graph, therefore
99 * destroying it.
100 */
d94d92ac
PP
101 BT_LIB_LOGD("Removing self from graph's connections: "
102 "%![graph-]+g, %![conn-]+x", graph, connection);
f167d3c0
PP
103 bt_graph_remove_connection(graph, connection);
104}
105
106static
d94d92ac 107void parent_is_owner(struct bt_object *obj)
f167d3c0
PP
108{
109 struct bt_connection *connection = container_of(obj,
110 struct bt_connection, base);
111
d94d92ac 112 try_remove_connection_from_graph(connection);
890882ef
PP
113}
114
784cdc68 115BT_HIDDEN
d94d92ac 116struct bt_connection *bt_connection_create(struct bt_graph *graph,
72b913fb
PP
117 struct bt_port *upstream_port,
118 struct bt_port *downstream_port)
784cdc68
JG
119{
120 struct bt_connection *connection = NULL;
121
d94d92ac
PP
122 BT_LIB_LOGD("Creating connection: "
123 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
124 graph, upstream_port, downstream_port);
784cdc68
JG
125 connection = g_new0(struct bt_connection, 1);
126 if (!connection) {
a36bfb16 127 BT_LOGE_STR("Failed to allocate one connection.");
784cdc68
JG
128 goto end;
129 }
130
3fea54f6 131 bt_object_init_shared_with_parent(&connection->base,
d94d92ac 132 destroy_connection);
3fea54f6 133 bt_object_set_parent_is_owner_listener_func(&connection->base,
d94d92ac 134 parent_is_owner);
bd14d768
PP
135 connection->iterators = g_ptr_array_new();
136 if (!connection->iterators) {
a36bfb16 137 BT_LOGE_STR("Failed to allocate a GPtrArray.");
65300d60 138 BT_OBJECT_PUT_REF_AND_RESET(connection);
bd14d768
PP
139 goto end;
140 }
141
784cdc68 142 /* Weak references are taken, see comment in header. */
72b913fb
PP
143 connection->upstream_port = upstream_port;
144 connection->downstream_port = downstream_port;
d94d92ac 145 BT_LIB_LOGD("Setting upstream port's connection: %!+p", upstream_port);
72b913fb 146 bt_port_set_connection(upstream_port, connection);
d94d92ac
PP
147 BT_LIB_LOGD("Setting downstream port's connection: %!+p",
148 downstream_port);
72b913fb 149 bt_port_set_connection(downstream_port, connection);
3fea54f6 150 bt_object_set_parent(&connection->base, &graph->base);
d94d92ac 151 BT_LIB_LOGD("Created connection: %!+x", connection);
a36bfb16 152
784cdc68
JG
153end:
154 return connection;
155}
156
72b913fb 157BT_HIDDEN
d94d92ac 158void bt_connection_end(struct bt_connection *conn, bool try_remove_from_graph)
72b913fb 159{
72b913fb
PP
160 struct bt_port *downstream_port = conn->downstream_port;
161 struct bt_port *upstream_port = conn->upstream_port;
f167d3c0 162 size_t i;
72b913fb 163
d94d92ac 164 BT_LIB_LOGD("Ending connection: %!+x, try-remove-from-graph=%d",
c42ea0af
PP
165 conn, try_remove_from_graph);
166
d94d92ac 167 /*
d6e69534 168 * Any of the following message callback functions could
d94d92ac
PP
169 * remove one of the connection's ports from its component. To
170 * make sure that at least logging in called functions works
171 * with existing objects, get a local reference on both ports.
172 */
173 bt_object_get_ref(downstream_port);
174 bt_object_get_ref(upstream_port);
175
72b913fb 176 if (downstream_port) {
d94d92ac
PP
177 BT_LIB_LOGD("Disconnecting connection's downstream port: %!+p",
178 downstream_port);
72b913fb
PP
179 bt_port_set_connection(downstream_port, NULL);
180 conn->downstream_port = NULL;
181 }
182
183 if (upstream_port) {
d94d92ac
PP
184 BT_LIB_LOGD("Disconnecting connection's upstream port: %!+p",
185 upstream_port);
72b913fb
PP
186 bt_port_set_connection(upstream_port, NULL);
187 conn->upstream_port = NULL;
188 }
189
d94d92ac
PP
190 /*
191 * It is safe to put the local port references now that we don't
192 * need them anymore. This could indeed destroy them.
193 */
194 bt_object_put_ref(downstream_port);
195 bt_object_put_ref(upstream_port);
f167d3c0
PP
196
197 /*
d0fea130
PP
198 * Because this connection is ended, finalize each message
199 * iterator created from it.
200 *
201 * In practice, this only happens when the connection is
202 * destroyed and not all its message iterators were finalized,
203 * which is on graph destruction.
f167d3c0
PP
204 */
205 for (i = 0; i < conn->iterators->len; i++) {
d6e69534 206 struct bt_self_component_port_input_message_iterator *iterator =
f167d3c0
PP
207 g_ptr_array_index(conn->iterators, i);
208
d6e69534 209 BT_LIB_LOGD("Finalizing message iterator created by "
d94d92ac 210 "this ended connection: %![iter-]+i", iterator);
d0fea130 211 bt_self_component_port_input_message_iterator_try_finalize(
d94d92ac 212 iterator);
f167d3c0
PP
213
214 /*
215 * Make sure this iterator does not try to remove itself
216 * from this connection's iterators on destruction
217 * because this connection won't exist anymore.
218 */
d6e69534 219 bt_self_component_port_input_message_iterator_set_connection(
90157d89 220 iterator, NULL);
f167d3c0
PP
221 }
222
223 g_ptr_array_set_size(conn->iterators, 0);
c42ea0af
PP
224
225 if (try_remove_from_graph) {
d94d92ac 226 try_remove_connection_from_graph(conn);
c42ea0af 227 }
72b913fb
PP
228}
229
0d72b8c3
PP
230const struct bt_port_output *bt_connection_borrow_upstream_port_const(
231 const struct bt_connection *connection)
784cdc68 232{
d94d92ac
PP
233 BT_ASSERT_PRE_NON_NULL(connection, "Connection");
234 return (void *) connection->upstream_port;
784cdc68
JG
235}
236
0d72b8c3
PP
237const struct bt_port_input *bt_connection_borrow_downstream_port_const(
238 const struct bt_connection *connection)
784cdc68 239{
d94d92ac
PP
240 BT_ASSERT_PRE_NON_NULL(connection, "Connection");
241 return (void *) connection->downstream_port;
784cdc68 242}
bd14d768
PP
243
244BT_HIDDEN
245void bt_connection_remove_iterator(struct bt_connection *conn,
d6e69534 246 struct bt_self_component_port_input_message_iterator *iterator)
bd14d768
PP
247{
248 g_ptr_array_remove(conn->iterators, iterator);
d6e69534 249 BT_LIB_LOGV("Removed message iterator from connection: "
d94d92ac
PP
250 "%![conn-]+x, %![iter-]+i", conn, iterator);
251 try_remove_connection_from_graph(conn);
bd14d768 252}
090a4c0a 253
c5b9b441
PP
254void bt_connection_get_ref(const struct bt_connection *connection)
255{
256 bt_object_get_ref(connection);
257}
258
259void bt_connection_put_ref(const struct bt_connection *connection)
260{
261 bt_object_put_ref(connection);
262}
This page took 0.052632 seconds and 4 git commands to generate.