Commit | Line | Data |
---|---|---|
de713ce0 | 1 | /* |
de713ce0 JG |
2 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
ab0d387b PP |
25 | #define BT_LOG_TAG "COMP" |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
d94d92ac | 28 | #include <babeltrace/graph/self-component.h> |
b2e0c907 PP |
29 | #include <babeltrace/graph/component.h> |
30 | #include <babeltrace/graph/component-internal.h> | |
31 | #include <babeltrace/graph/component-class-internal.h> | |
32 | #include <babeltrace/graph/component-source-internal.h> | |
33 | #include <babeltrace/graph/component-filter-internal.h> | |
34 | #include <babeltrace/graph/component-sink-internal.h> | |
b2e0c907 PP |
35 | #include <babeltrace/graph/connection-internal.h> |
36 | #include <babeltrace/graph/graph-internal.h> | |
37 | #include <babeltrace/graph/notification-iterator-internal.h> | |
d94d92ac | 38 | #include <babeltrace/graph/port-internal.h> |
de713ce0 | 39 | #include <babeltrace/babeltrace-internal.h> |
3d9990ac | 40 | #include <babeltrace/compiler-internal.h> |
65300d60 | 41 | #include <babeltrace/object.h> |
c55a9f58 | 42 | #include <babeltrace/types.h> |
ab0d387b PP |
43 | #include <babeltrace/values.h> |
44 | #include <babeltrace/values-internal.h> | |
f6ccaed9 | 45 | #include <babeltrace/assert-internal.h> |
d94d92ac | 46 | #include <babeltrace/assert-pre-internal.h> |
9ac68eb1 | 47 | #include <stdint.h> |
ab0d387b | 48 | #include <inttypes.h> |
de713ce0 | 49 | |
7c7c0433 JG |
50 | static |
51 | struct bt_component * (* const component_create_funcs[])( | |
36712f1d | 52 | struct bt_component_class *) = { |
d3e4dcd8 PP |
53 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
54 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, | |
55 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, | |
7c7c0433 JG |
56 | }; |
57 | ||
72b913fb PP |
58 | static |
59 | void (*component_destroy_funcs[])(struct bt_component *) = { | |
60 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy, | |
61 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy, | |
62 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy, | |
63 | }; | |
64 | ||
b8a06801 | 65 | static |
d94d92ac PP |
66 | void finalize_component(struct bt_component *comp) |
67 | { | |
68 | typedef void (*method_t)(void *); | |
69 | ||
70 | method_t method = NULL; | |
71 | ||
72 | BT_ASSERT(comp); | |
73 | ||
74 | switch (comp->class->type) { | |
75 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
76 | { | |
77 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
78 | ||
79 | method = (method_t) src_cc->methods.finalize; | |
80 | break; | |
81 | } | |
82 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
83 | { | |
84 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
85 | ||
86 | method = (method_t) flt_cc->methods.finalize; | |
87 | break; | |
88 | } | |
89 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
90 | { | |
91 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
92 | ||
93 | method = (method_t) sink_cc->methods.finalize; | |
94 | break; | |
95 | } | |
96 | default: | |
97 | abort(); | |
98 | } | |
99 | ||
100 | if (method) { | |
101 | BT_LIB_LOGD("Calling user's finalization method: " | |
102 | "%![comp-]+c", comp); | |
103 | method(comp); | |
104 | } | |
105 | } | |
106 | ||
107 | static | |
108 | void destroy_component(struct bt_object *obj) | |
b8a06801 JG |
109 | { |
110 | struct bt_component *component = NULL; | |
3230ee6b | 111 | int i; |
b8a06801 JG |
112 | |
113 | if (!obj) { | |
114 | return; | |
115 | } | |
116 | ||
bd14d768 PP |
117 | /* |
118 | * The component's reference count is 0 if we're here. Increment | |
119 | * it to avoid a double-destroy (possibly infinitely recursive). | |
120 | * This could happen for example if the component's finalization | |
d94d92ac PP |
121 | * function does bt_object_get_ref() (or anything that causes |
122 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
123 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
124 | * count would go from 1 to 0 again and this function would be | |
125 | * called again. | |
bd14d768 | 126 | */ |
3fea54f6 | 127 | obj->ref_count++; |
b8a06801 | 128 | component = container_of(obj, struct bt_component, base); |
d94d92ac PP |
129 | BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g", |
130 | component, bt_component_borrow_graph(component)); | |
3230ee6b PP |
131 | |
132 | /* Call destroy listeners in reverse registration order */ | |
ab0d387b PP |
133 | BT_LOGD_STR("Calling destroy listeners."); |
134 | ||
3230ee6b PP |
135 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
136 | struct bt_component_destroy_listener *listener = | |
137 | &g_array_index(component->destroy_listeners, | |
138 | struct bt_component_destroy_listener, i); | |
139 | ||
140 | listener->func(component, listener->data); | |
141 | } | |
142 | ||
7c7c0433 | 143 | /* |
36712f1d PP |
144 | * User data is destroyed first, followed by the concrete |
145 | * component instance. Do not finalize if the component's user | |
146 | * initialization method failed in the first place. | |
b8a06801 | 147 | */ |
d94d92ac PP |
148 | if (component->initialized) { |
149 | finalize_component(component); | |
7c7c0433 | 150 | } |
b8a06801 | 151 | |
ab09f844 | 152 | if (component->destroy) { |
ab0d387b | 153 | BT_LOGD_STR("Destroying type-specific data."); |
ab09f844 JG |
154 | component->destroy(component); |
155 | } | |
156 | ||
72b913fb | 157 | if (component->input_ports) { |
ab0d387b | 158 | BT_LOGD_STR("Destroying input ports."); |
72b913fb | 159 | g_ptr_array_free(component->input_ports, TRUE); |
d94d92ac | 160 | component->input_ports = NULL; |
72b913fb | 161 | } |
b8a06801 | 162 | |
72b913fb | 163 | if (component->output_ports) { |
ab0d387b | 164 | BT_LOGD_STR("Destroying output ports."); |
72b913fb | 165 | g_ptr_array_free(component->output_ports, TRUE); |
d94d92ac | 166 | component->output_ports = NULL; |
b8a06801 JG |
167 | } |
168 | ||
3230ee6b PP |
169 | if (component->destroy_listeners) { |
170 | g_array_free(component->destroy_listeners, TRUE); | |
d94d92ac | 171 | component->destroy_listeners = NULL; |
3230ee6b PP |
172 | } |
173 | ||
ab0d387b PP |
174 | if (component->name) { |
175 | g_string_free(component->name, TRUE); | |
d94d92ac | 176 | component->name = NULL; |
ab0d387b PP |
177 | } |
178 | ||
d94d92ac PP |
179 | BT_LOGD_STR("Putting component class."); |
180 | BT_OBJECT_PUT_REF_AND_RESET(component->class); | |
72b913fb | 181 | g_free(component); |
b8a06801 | 182 | } |
de713ce0 | 183 | |
890882ef PP |
184 | enum bt_component_class_type bt_component_get_class_type( |
185 | struct bt_component *component) | |
5645cd95 | 186 | { |
d94d92ac PP |
187 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
188 | return component->class->type; | |
5645cd95 JG |
189 | } |
190 | ||
72b913fb | 191 | static |
d94d92ac | 192 | struct bt_port *add_port( |
72b913fb | 193 | struct bt_component *component, GPtrArray *ports, |
3e9b0023 | 194 | enum bt_port_type port_type, const char *name, void *user_data) |
72b913fb | 195 | { |
72b913fb | 196 | struct bt_port *new_port = NULL; |
1bf957a0 | 197 | struct bt_graph *graph = NULL; |
72b913fb | 198 | |
d94d92ac PP |
199 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
200 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
201 | BT_ASSERT_PRE(strlen(name) > 0, "Name is empty"); | |
202 | graph = bt_component_borrow_graph(component); | |
203 | BT_ASSERT_PRE(graph && !bt_graph_is_canceled(graph), | |
204 | "Component's graph is canceled: %![comp-]+c, %![graph-]+g", | |
205 | component, graph); | |
72b913fb | 206 | |
d94d92ac | 207 | // TODO: Validate that the name is not already used. |
ab0d387b | 208 | |
d94d92ac | 209 | BT_LIB_LOGD("Adding port to component: %![comp-]+c, " |
ab0d387b | 210 | "port-type=%s, port-name=\"%s\"", component, |
ab0d387b PP |
211 | bt_port_type_string(port_type), name); |
212 | ||
3e9b0023 | 213 | new_port = bt_port_create(component, port_type, name, user_data); |
72b913fb | 214 | if (!new_port) { |
d94d92ac | 215 | BT_LOGE_STR("Cannot create port object."); |
72b913fb PP |
216 | goto end; |
217 | } | |
218 | ||
219 | /* | |
220 | * No name clash, add the port. | |
221 | * The component is now the port's parent; it should _not_ | |
222 | * hold a reference to the port since the port's lifetime | |
223 | * is now protected by the component's own lifetime. | |
224 | */ | |
225 | g_ptr_array_add(ports, new_port); | |
1bf957a0 PP |
226 | |
227 | /* | |
228 | * Notify the graph's creator that a new port was added. | |
229 | */ | |
e5be10ef | 230 | graph = bt_object_get_ref(bt_component_borrow_graph(component)); |
1bf957a0 PP |
231 | if (graph) { |
232 | bt_graph_notify_port_added(graph, new_port); | |
65300d60 | 233 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
1bf957a0 PP |
234 | } |
235 | ||
d94d92ac PP |
236 | BT_LIB_LOGD("Created and added port to component: " |
237 | "%![comp-]+c, %![port-]+p", component, new_port); | |
ab0d387b | 238 | |
72b913fb PP |
239 | end: |
240 | return new_port; | |
241 | } | |
242 | ||
243 | BT_HIDDEN | |
d94d92ac | 244 | uint64_t bt_component_get_input_port_count(struct bt_component *comp) |
72b913fb | 245 | { |
d94d92ac PP |
246 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
247 | return (uint64_t) comp->input_ports->len; | |
72b913fb PP |
248 | } |
249 | ||
250 | BT_HIDDEN | |
d94d92ac | 251 | uint64_t bt_component_get_output_port_count(struct bt_component *comp) |
72b913fb | 252 | { |
d94d92ac PP |
253 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
254 | return (uint64_t) comp->output_ports->len; | |
72b913fb PP |
255 | } |
256 | ||
dd8a4547 | 257 | BT_HIDDEN |
d94d92ac | 258 | int bt_component_create(struct bt_component_class *component_class, |
36712f1d | 259 | const char *name, struct bt_component **user_component) |
38b48196 | 260 | { |
d94d92ac | 261 | int ret = 0; |
38b48196 | 262 | struct bt_component *component = NULL; |
d3e4dcd8 | 263 | enum bt_component_class_type type; |
38b48196 | 264 | |
f6ccaed9 PP |
265 | BT_ASSERT(user_component); |
266 | BT_ASSERT(component_class); | |
267 | BT_ASSERT(name); | |
38b48196 | 268 | |
7c7c0433 | 269 | type = bt_component_class_get_type(component_class); |
d94d92ac PP |
270 | BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, " |
271 | "comp-name=\"%s\"", component_class, name); | |
36712f1d | 272 | component = component_create_funcs[type](component_class); |
7c7c0433 | 273 | if (!component) { |
ab0d387b | 274 | BT_LOGE_STR("Cannot create specific component object."); |
d94d92ac | 275 | ret = -1; |
7c7c0433 JG |
276 | goto end; |
277 | } | |
278 | ||
3fea54f6 | 279 | bt_object_init_shared_with_parent(&component->base, |
d94d92ac | 280 | destroy_component); |
65300d60 | 281 | component->class = bt_object_get_ref(component_class); |
72b913fb | 282 | component->destroy = component_destroy_funcs[type]; |
7c7c0433 | 283 | component->name = g_string_new(name); |
4b70dd83 | 284 | if (!component->name) { |
ab0d387b | 285 | BT_LOGE_STR("Failed to allocate one GString."); |
d94d92ac | 286 | ret = -1; |
7c7c0433 JG |
287 | goto end; |
288 | } | |
289 | ||
72b913fb | 290 | component->input_ports = g_ptr_array_new_with_free_func( |
3fea54f6 | 291 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 292 | if (!component->input_ports) { |
ab0d387b | 293 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
d94d92ac | 294 | ret = -1; |
72b913fb PP |
295 | goto end; |
296 | } | |
297 | ||
298 | component->output_ports = g_ptr_array_new_with_free_func( | |
3fea54f6 | 299 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 300 | if (!component->output_ports) { |
ab0d387b | 301 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
d94d92ac | 302 | ret = -1; |
72b913fb PP |
303 | goto end; |
304 | } | |
305 | ||
3230ee6b PP |
306 | component->destroy_listeners = g_array_new(FALSE, TRUE, |
307 | sizeof(struct bt_component_destroy_listener)); | |
308 | if (!component->destroy_listeners) { | |
ab0d387b | 309 | BT_LOGE_STR("Failed to allocate one GArray."); |
d94d92ac | 310 | ret = -1; |
3230ee6b PP |
311 | goto end; |
312 | } | |
313 | ||
d94d92ac PP |
314 | BT_LIB_LOGD("Created empty component from component class: " |
315 | "%![cc-]+C, %![comp-]+c", component_class, component); | |
65300d60 | 316 | BT_OBJECT_MOVE_REF(*user_component, component); |
ab0d387b | 317 | |
38b48196 | 318 | end: |
65300d60 | 319 | bt_object_put_ref(component); |
d94d92ac | 320 | return ret; |
6358c163 PP |
321 | } |
322 | ||
de713ce0 JG |
323 | const char *bt_component_get_name(struct bt_component *component) |
324 | { | |
d94d92ac PP |
325 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
326 | return component->name->str; | |
de713ce0 JG |
327 | } |
328 | ||
d94d92ac | 329 | struct bt_component_class *bt_component_borrow_class( |
38b48196 | 330 | struct bt_component *component) |
de713ce0 | 331 | { |
d94d92ac PP |
332 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
333 | return component->class; | |
de713ce0 JG |
334 | } |
335 | ||
d94d92ac | 336 | void *bt_self_component_get_data(struct bt_self_component *self_comp) |
de713ce0 | 337 | { |
d94d92ac | 338 | struct bt_component *component = (void *) self_comp; |
890882ef | 339 | |
d94d92ac PP |
340 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
341 | return component->user_data; | |
de713ce0 JG |
342 | } |
343 | ||
d94d92ac | 344 | void bt_self_component_set_data(struct bt_self_component *self_comp, |
de713ce0 JG |
345 | void *data) |
346 | { | |
d94d92ac | 347 | struct bt_component *component = (void *) self_comp; |
ab0d387b | 348 | |
d94d92ac | 349 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
de713ce0 | 350 | component->user_data = data; |
d94d92ac | 351 | BT_LIB_LOGV("Set component's user data: %!+c", component); |
de713ce0 | 352 | } |
366e034f JG |
353 | |
354 | BT_HIDDEN | |
f60c8b34 | 355 | void bt_component_set_graph(struct bt_component *component, |
366e034f JG |
356 | struct bt_graph *graph) |
357 | { | |
3fea54f6 PP |
358 | bt_object_set_parent(&component->base, |
359 | graph ? &graph->base : NULL); | |
366e034f JG |
360 | } |
361 | ||
e5be10ef | 362 | bt_bool bt_component_graph_is_canceled(struct bt_component *component) |
366e034f | 363 | { |
e5be10ef PP |
364 | return bt_graph_is_canceled( |
365 | (void *) bt_object_borrow_parent(&component->base)); | |
366e034f JG |
366 | } |
367 | ||
72b913fb | 368 | static |
d94d92ac | 369 | struct bt_port *borrow_port_by_name(GPtrArray *ports, |
9ac68eb1 | 370 | const char *name) |
366e034f | 371 | { |
d94d92ac | 372 | uint64_t i; |
366e034f JG |
373 | struct bt_port *ret_port = NULL; |
374 | ||
f6ccaed9 | 375 | BT_ASSERT(name); |
72b913fb | 376 | |
366e034f JG |
377 | for (i = 0; i < ports->len; i++) { |
378 | struct bt_port *port = g_ptr_array_index(ports, i); | |
366e034f | 379 | |
d94d92ac PP |
380 | if (!strcmp(name, port->name->str)) { |
381 | ret_port = port; | |
366e034f JG |
382 | break; |
383 | } | |
384 | } | |
385 | ||
386 | return ret_port; | |
387 | } | |
388 | ||
389 | BT_HIDDEN | |
d94d92ac PP |
390 | struct bt_port_input *bt_component_borrow_input_port_by_name( |
391 | struct bt_component *comp, const char *name) | |
72b913fb | 392 | { |
f6ccaed9 | 393 | BT_ASSERT(comp); |
d94d92ac | 394 | return (void *) borrow_port_by_name(comp->input_ports, name); |
72b913fb PP |
395 | } |
396 | ||
397 | BT_HIDDEN | |
d94d92ac PP |
398 | struct bt_port_output *bt_component_borrow_output_port_by_name( |
399 | struct bt_component *comp, const char *name) | |
72b913fb | 400 | { |
d94d92ac PP |
401 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
402 | return (void *) | |
403 | borrow_port_by_name(comp->output_ports, name); | |
72b913fb PP |
404 | } |
405 | ||
406 | static | |
d94d92ac | 407 | struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index) |
366e034f | 408 | { |
d94d92ac PP |
409 | BT_ASSERT(index < ports->len); |
410 | return g_ptr_array_index(ports, index); | |
366e034f JG |
411 | } |
412 | ||
413 | BT_HIDDEN | |
d94d92ac PP |
414 | struct bt_port_input *bt_component_borrow_input_port_by_index( |
415 | struct bt_component *comp, uint64_t index) | |
366e034f | 416 | { |
d94d92ac PP |
417 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
418 | BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len); | |
419 | return (void *) | |
420 | borrow_port_by_index(comp->input_ports, index); | |
72b913fb | 421 | } |
366e034f | 422 | |
72b913fb | 423 | BT_HIDDEN |
d94d92ac PP |
424 | struct bt_port_output *bt_component_borrow_output_port_by_index( |
425 | struct bt_component *comp, uint64_t index) | |
72b913fb | 426 | { |
d94d92ac PP |
427 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
428 | BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len); | |
429 | return (void *) | |
430 | borrow_port_by_index(comp->output_ports, index); | |
72b913fb | 431 | } |
366e034f | 432 | |
72b913fb | 433 | BT_HIDDEN |
d94d92ac | 434 | struct bt_port_input *bt_component_add_input_port( |
3e9b0023 PP |
435 | struct bt_component *component, const char *name, |
436 | void *user_data) | |
72b913fb | 437 | { |
d94d92ac PP |
438 | /* add_port() logs details */ |
439 | return (void *) | |
440 | add_port(component, component->input_ports, | |
441 | BT_PORT_TYPE_INPUT, name, user_data); | |
72b913fb | 442 | } |
366e034f | 443 | |
72b913fb | 444 | BT_HIDDEN |
d94d92ac | 445 | struct bt_port_output *bt_component_add_output_port( |
3e9b0023 PP |
446 | struct bt_component *component, const char *name, |
447 | void *user_data) | |
72b913fb | 448 | { |
d94d92ac PP |
449 | /* add_port() logs details */ |
450 | return (void *) | |
451 | add_port(component, component->output_ports, | |
452 | BT_PORT_TYPE_OUTPUT, name, user_data); | |
72b913fb PP |
453 | } |
454 | ||
455 | static | |
d94d92ac PP |
456 | void remove_port_by_index(struct bt_component *component, |
457 | GPtrArray *ports, uint64_t index) | |
72b913fb PP |
458 | { |
459 | struct bt_port *port; | |
1bf957a0 | 460 | struct bt_graph *graph; |
72b913fb | 461 | |
f6ccaed9 PP |
462 | BT_ASSERT(ports); |
463 | BT_ASSERT(index < ports->len); | |
72b913fb | 464 | port = g_ptr_array_index(ports, index); |
d94d92ac PP |
465 | BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p", |
466 | component, port); | |
ab0d387b | 467 | |
72b913fb PP |
468 | /* Disconnect both ports of this port's connection, if any */ |
469 | if (port->connection) { | |
c42ea0af | 470 | bt_connection_end(port->connection, true); |
f60c8b34 JG |
471 | } |
472 | ||
d94d92ac PP |
473 | /* |
474 | * The port's current reference count can be 0 at this point, | |
475 | * which means its parent (component) keeps it alive. We are | |
476 | * about to remove the port from its parent's container (with | |
477 | * the g_ptr_array_remove_index() call below), which in this | |
478 | * case would destroy it. This is not good because we still | |
479 | * need the port for the bt_graph_notify_port_removed() call | |
480 | * below (in which its component is `NULL` as expected because | |
481 | * of the bt_object_set_parent() call below). | |
482 | * | |
483 | * To avoid a destroyed port during the notification callback, | |
484 | * get a reference now, and put it (destroying the port if its | |
485 | * reference count is 0 at this point) after notifying the | |
486 | * private graph's user. | |
487 | */ | |
488 | bt_object_get_no_null_check(&port->base); | |
489 | ||
490 | /* | |
491 | * Remove from parent's array of ports (weak refs). This never | |
492 | * destroys the port object because its reference count is at | |
493 | * least 1 thanks to the bt_object_get_no_null_check() call | |
494 | * above. | |
495 | */ | |
72b913fb PP |
496 | g_ptr_array_remove_index(ports, index); |
497 | ||
498 | /* Detach port from its component parent */ | |
d94d92ac | 499 | bt_object_set_parent(&port->base, NULL); |
72b913fb | 500 | |
1bf957a0 PP |
501 | /* |
502 | * Notify the graph's creator that a port is removed. | |
503 | */ | |
d94d92ac | 504 | graph = bt_component_borrow_graph(component); |
1bf957a0 PP |
505 | if (graph) { |
506 | bt_graph_notify_port_removed(graph, component, port); | |
1bf957a0 | 507 | } |
ab0d387b | 508 | |
d94d92ac PP |
509 | BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p", |
510 | component, port); | |
511 | ||
512 | /* | |
513 | * Put the local reference. If this port's reference count was 0 | |
514 | * when entering this function, it is 1 now, so it is destroyed | |
515 | * immediately. | |
516 | */ | |
517 | bt_object_put_no_null_check(&port->base); | |
366e034f JG |
518 | } |
519 | ||
520 | BT_HIDDEN | |
d94d92ac PP |
521 | void bt_component_remove_port(struct bt_component *component, |
522 | struct bt_port *port) | |
366e034f | 523 | { |
d94d92ac | 524 | uint64_t i; |
72b913fb | 525 | GPtrArray *ports = NULL; |
366e034f | 526 | |
d94d92ac PP |
527 | BT_ASSERT(component); |
528 | BT_ASSERT(port); | |
366e034f | 529 | |
d94d92ac PP |
530 | switch (port->type) { |
531 | case BT_PORT_TYPE_INPUT: | |
72b913fb | 532 | ports = component->input_ports; |
d94d92ac PP |
533 | break; |
534 | case BT_PORT_TYPE_OUTPUT: | |
72b913fb | 535 | ports = component->output_ports; |
d94d92ac PP |
536 | break; |
537 | default: | |
538 | abort(); | |
72b913fb | 539 | } |
366e034f | 540 | |
f6ccaed9 | 541 | BT_ASSERT(ports); |
366e034f | 542 | |
72b913fb PP |
543 | for (i = 0; i < ports->len; i++) { |
544 | struct bt_port *cur_port = g_ptr_array_index(ports, i); | |
545 | ||
546 | if (cur_port == port) { | |
d94d92ac | 547 | remove_port_by_index(component, |
72b913fb | 548 | ports, i); |
366e034f JG |
549 | goto end; |
550 | } | |
551 | } | |
72b913fb | 552 | |
d94d92ac PP |
553 | BT_LIB_LOGW("Port to remove from component was not found: " |
554 | "%![comp-]+c, %![port-]+p", component, port); | |
ab0d387b | 555 | |
366e034f | 556 | end: |
d94d92ac | 557 | return; |
366e034f | 558 | } |
f60c8b34 JG |
559 | |
560 | BT_HIDDEN | |
d94d92ac | 561 | enum bt_self_component_status bt_component_accept_port_connection( |
8f4799f7 PP |
562 | struct bt_component *comp, struct bt_port *self_port, |
563 | struct bt_port *other_port) | |
f60c8b34 | 564 | { |
d94d92ac PP |
565 | typedef enum bt_self_component_status (*method_t)( |
566 | void *, void *, void *); | |
567 | ||
568 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
569 | method_t method = NULL; | |
f60c8b34 | 570 | |
f6ccaed9 PP |
571 | BT_ASSERT(comp); |
572 | BT_ASSERT(self_port); | |
573 | BT_ASSERT(other_port); | |
72b913fb | 574 | |
d94d92ac PP |
575 | switch (comp->class->type) { |
576 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
577 | { | |
578 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
579 | ||
580 | switch (self_port->type) { | |
581 | case BT_PORT_TYPE_OUTPUT: | |
582 | method = (method_t) src_cc->methods.accept_output_port_connection; | |
583 | break; | |
584 | default: | |
585 | abort(); | |
586 | } | |
587 | ||
588 | break; | |
589 | } | |
590 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
591 | { | |
592 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
593 | ||
594 | switch (self_port->type) { | |
595 | case BT_PORT_TYPE_INPUT: | |
596 | method = (method_t) flt_cc->methods.accept_input_port_connection; | |
597 | break; | |
598 | case BT_PORT_TYPE_OUTPUT: | |
599 | method = (method_t) flt_cc->methods.accept_output_port_connection; | |
600 | break; | |
601 | default: | |
602 | abort(); | |
603 | } | |
604 | ||
605 | break; | |
606 | } | |
607 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
608 | { | |
609 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
610 | ||
611 | switch (self_port->type) { | |
612 | case BT_PORT_TYPE_INPUT: | |
613 | method = (method_t) sink_cc->methods.accept_input_port_connection; | |
614 | break; | |
615 | default: | |
616 | abort(); | |
617 | } | |
618 | ||
619 | break; | |
620 | } | |
621 | default: | |
622 | abort(); | |
623 | } | |
624 | ||
625 | if (method) { | |
626 | BT_LIB_LOGD("Calling user's \"accept port connection\" method: " | |
627 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", | |
628 | comp, self_port, other_port); | |
629 | status = method(comp, self_port, other_port); | |
ab0d387b | 630 | BT_LOGD("User method returned: status=%s", |
d94d92ac | 631 | bt_self_component_status_string(status)); |
f60c8b34 JG |
632 | } |
633 | ||
634 | return status; | |
635 | } | |
72b913fb | 636 | |
0d8b4d8e | 637 | BT_HIDDEN |
d94d92ac PP |
638 | enum bt_self_component_status bt_component_port_connected( |
639 | struct bt_component *comp, struct bt_port *self_port, | |
640 | struct bt_port *other_port) | |
0d8b4d8e | 641 | { |
d94d92ac PP |
642 | typedef enum bt_self_component_status (*method_t)( |
643 | void *, void *, void *); | |
644 | ||
645 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; | |
646 | method_t method = NULL; | |
bf55043c | 647 | |
f6ccaed9 PP |
648 | BT_ASSERT(comp); |
649 | BT_ASSERT(self_port); | |
650 | BT_ASSERT(other_port); | |
0d8b4d8e | 651 | |
d94d92ac PP |
652 | switch (comp->class->type) { |
653 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
654 | { | |
655 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
656 | ||
657 | switch (self_port->type) { | |
658 | case BT_PORT_TYPE_OUTPUT: | |
659 | method = (method_t) src_cc->methods.output_port_connected; | |
660 | break; | |
661 | default: | |
662 | abort(); | |
663 | } | |
664 | ||
665 | break; | |
666 | } | |
667 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
668 | { | |
669 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
670 | ||
671 | switch (self_port->type) { | |
672 | case BT_PORT_TYPE_INPUT: | |
673 | method = (method_t) flt_cc->methods.input_port_connected; | |
674 | break; | |
675 | case BT_PORT_TYPE_OUTPUT: | |
676 | method = (method_t) flt_cc->methods.output_port_connected; | |
677 | break; | |
678 | default: | |
679 | abort(); | |
680 | } | |
681 | ||
682 | break; | |
683 | } | |
684 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
685 | { | |
686 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
687 | ||
688 | switch (self_port->type) { | |
689 | case BT_PORT_TYPE_INPUT: | |
690 | method = (method_t) sink_cc->methods.input_port_connected; | |
691 | break; | |
692 | default: | |
693 | abort(); | |
694 | } | |
695 | ||
696 | break; | |
697 | } | |
698 | default: | |
699 | abort(); | |
700 | } | |
701 | ||
702 | if (method) { | |
703 | BT_LIB_LOGD("Calling user's \"port connected\" method: " | |
704 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", | |
705 | comp, self_port, other_port); | |
706 | status = method(comp, self_port, other_port); | |
707 | BT_LOGD("User method returned: status=%s", | |
708 | bt_self_component_status_string(status)); | |
0d8b4d8e | 709 | } |
bf55043c PP |
710 | |
711 | return status; | |
0d8b4d8e PP |
712 | } |
713 | ||
72b913fb PP |
714 | BT_HIDDEN |
715 | void bt_component_port_disconnected(struct bt_component *comp, | |
716 | struct bt_port *port) | |
717 | { | |
d94d92ac PP |
718 | typedef void (*method_t)(void *, void *); |
719 | ||
720 | method_t method = NULL; | |
721 | ||
f6ccaed9 PP |
722 | BT_ASSERT(comp); |
723 | BT_ASSERT(port); | |
72b913fb | 724 | |
d94d92ac PP |
725 | switch (comp->class->type) { |
726 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
727 | { | |
728 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
729 | ||
730 | switch (port->type) { | |
731 | case BT_PORT_TYPE_OUTPUT: | |
732 | method = (method_t) src_cc->methods.output_port_disconnected; | |
733 | break; | |
734 | default: | |
735 | abort(); | |
736 | } | |
737 | ||
738 | break; | |
739 | } | |
740 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
741 | { | |
742 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
743 | ||
744 | switch (port->type) { | |
745 | case BT_PORT_TYPE_INPUT: | |
746 | method = (method_t) flt_cc->methods.input_port_disconnected; | |
747 | break; | |
748 | case BT_PORT_TYPE_OUTPUT: | |
749 | method = (method_t) flt_cc->methods.output_port_disconnected; | |
750 | break; | |
751 | default: | |
752 | abort(); | |
753 | } | |
754 | ||
755 | break; | |
756 | } | |
757 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
758 | { | |
759 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
760 | ||
761 | switch (port->type) { | |
762 | case BT_PORT_TYPE_INPUT: | |
763 | method = (method_t) sink_cc->methods.input_port_disconnected; | |
764 | break; | |
765 | default: | |
766 | abort(); | |
767 | } | |
768 | ||
769 | break; | |
770 | } | |
771 | default: | |
772 | abort(); | |
773 | } | |
774 | ||
775 | if (method) { | |
776 | BT_LIB_LOGD("Calling user's \"port disconnected\" method: " | |
777 | "%![comp-]+c, %![port-]+p", comp, port); | |
778 | method(comp, port); | |
72b913fb PP |
779 | } |
780 | } | |
3230ee6b PP |
781 | |
782 | BT_HIDDEN | |
783 | void bt_component_add_destroy_listener(struct bt_component *component, | |
784 | bt_component_destroy_listener_func func, void *data) | |
785 | { | |
786 | struct bt_component_destroy_listener listener; | |
787 | ||
f6ccaed9 PP |
788 | BT_ASSERT(component); |
789 | BT_ASSERT(func); | |
3230ee6b PP |
790 | listener.func = func; |
791 | listener.data = data; | |
792 | g_array_append_val(component->destroy_listeners, listener); | |
d94d92ac | 793 | BT_LIB_LOGV("Added destroy listener: %![comp-]+c, " |
ab0d387b | 794 | "func-addr=%p, data-addr=%p", |
d94d92ac | 795 | component, func, data); |
3230ee6b PP |
796 | } |
797 | ||
798 | BT_HIDDEN | |
799 | void bt_component_remove_destroy_listener(struct bt_component *component, | |
800 | bt_component_destroy_listener_func func, void *data) | |
801 | { | |
d94d92ac | 802 | uint64_t i; |
3230ee6b | 803 | |
f6ccaed9 PP |
804 | BT_ASSERT(component); |
805 | BT_ASSERT(func); | |
3230ee6b PP |
806 | |
807 | for (i = 0; i < component->destroy_listeners->len; i++) { | |
808 | struct bt_component_destroy_listener *listener = | |
809 | &g_array_index(component->destroy_listeners, | |
810 | struct bt_component_destroy_listener, i); | |
811 | ||
812 | if (listener->func == func && listener->data == data) { | |
813 | g_array_remove_index(component->destroy_listeners, i); | |
814 | i--; | |
d94d92ac | 815 | BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, " |
ab0d387b | 816 | "func-addr=%p, data-addr=%p", |
d94d92ac | 817 | component, func, data); |
3230ee6b PP |
818 | } |
819 | } | |
820 | } |