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