2 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #define BT_LOG_TAG "COMP"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/graph/self-component.h>
29 #include <babeltrace/graph/component-const.h>
30 #include <babeltrace/graph/component-source-const.h>
31 #include <babeltrace/graph/component-filter-const.h>
32 #include <babeltrace/graph/component-sink-const.h>
33 #include <babeltrace/graph/component-internal.h>
34 #include <babeltrace/graph/component-class-internal.h>
35 #include <babeltrace/graph/component-source-internal.h>
36 #include <babeltrace/graph/component-filter-internal.h>
37 #include <babeltrace/graph/component-sink-internal.h>
38 #include <babeltrace/graph/connection-internal.h>
39 #include <babeltrace/graph/graph-internal.h>
40 #include <babeltrace/graph/notification-iterator-internal.h>
41 #include <babeltrace/graph/port-internal.h>
42 #include <babeltrace/babeltrace-internal.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/object.h>
45 #include <babeltrace/types.h>
46 #include <babeltrace/values.h>
47 #include <babeltrace/values-internal.h>
48 #include <babeltrace/assert-internal.h>
49 #include <babeltrace/assert-pre-internal.h>
54 struct bt_component
* (* const component_create_funcs
[])(
55 const struct bt_component_class
*) = {
56 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
57 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
58 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
62 void (*component_destroy_funcs
[])(struct bt_component
*) = {
63 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
64 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
65 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
69 void finalize_component(struct bt_component
*comp
)
71 typedef void (*method_t
)(void *);
73 method_t method
= NULL
;
77 switch (comp
->class->type
) {
78 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
80 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
82 method
= (method_t
) src_cc
->methods
.finalize
;
85 case BT_COMPONENT_CLASS_TYPE_FILTER
:
87 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
89 method
= (method_t
) flt_cc
->methods
.finalize
;
92 case BT_COMPONENT_CLASS_TYPE_SINK
:
94 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
96 method
= (method_t
) sink_cc
->methods
.finalize
;
104 BT_LIB_LOGD("Calling user's finalization method: "
105 "%![comp-]+c", comp
);
111 void destroy_component(struct bt_object
*obj
)
113 struct bt_component
*component
= NULL
;
121 * The component's reference count is 0 if we're here. Increment
122 * it to avoid a double-destroy (possibly infinitely recursive).
123 * This could happen for example if the component's finalization
124 * function does bt_object_get_ref() (or anything that causes
125 * bt_object_get_ref() to be called) on itself (ref. count goes
126 * from 0 to 1), and then bt_object_put_ref(): the reference
127 * count would go from 1 to 0 again and this function would be
131 component
= container_of(obj
, struct bt_component
, base
);
132 BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g",
133 component
, bt_component_borrow_graph(component
));
135 /* Call destroy listeners in reverse registration order */
136 BT_LOGD_STR("Calling destroy listeners.");
138 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
139 struct bt_component_destroy_listener
*listener
=
140 &g_array_index(component
->destroy_listeners
,
141 struct bt_component_destroy_listener
, i
);
143 listener
->func(component
, listener
->data
);
147 * User data is destroyed first, followed by the concrete
148 * component instance. Do not finalize if the component's user
149 * initialization method failed in the first place.
151 if (component
->initialized
) {
152 finalize_component(component
);
155 if (component
->destroy
) {
156 BT_LOGD_STR("Destroying type-specific data.");
157 component
->destroy(component
);
160 if (component
->input_ports
) {
161 BT_LOGD_STR("Destroying input ports.");
162 g_ptr_array_free(component
->input_ports
, TRUE
);
163 component
->input_ports
= NULL
;
166 if (component
->output_ports
) {
167 BT_LOGD_STR("Destroying output ports.");
168 g_ptr_array_free(component
->output_ports
, TRUE
);
169 component
->output_ports
= NULL
;
172 if (component
->destroy_listeners
) {
173 g_array_free(component
->destroy_listeners
, TRUE
);
174 component
->destroy_listeners
= NULL
;
177 if (component
->name
) {
178 g_string_free(component
->name
, TRUE
);
179 component
->name
= NULL
;
182 BT_LOGD_STR("Putting component class.");
183 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
187 enum bt_component_class_type
bt_component_get_class_type(
188 const struct bt_component
*component
)
190 BT_ASSERT_PRE_NON_NULL(component
, "Component");
191 return component
->class->type
;
195 struct bt_port
*add_port(
196 struct bt_component
*component
, GPtrArray
*ports
,
197 enum bt_port_type port_type
, const char *name
, void *user_data
)
199 struct bt_port
*new_port
= NULL
;
200 struct bt_graph
*graph
= NULL
;
202 BT_ASSERT_PRE_NON_NULL(component
, "Component");
203 BT_ASSERT_PRE_NON_NULL(name
, "Name");
204 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
205 graph
= bt_component_borrow_graph(component
);
206 BT_ASSERT_PRE(graph
&& !bt_graph_is_canceled(graph
),
207 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
210 // TODO: Validate that the name is not already used.
212 BT_LIB_LOGD("Adding port to component: %![comp-]+c, "
213 "port-type=%s, port-name=\"%s\"", component
,
214 bt_port_type_string(port_type
), name
);
216 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
218 BT_LOGE_STR("Cannot create port object.");
223 * No name clash, add the port.
224 * The component is now the port's parent; it should _not_
225 * hold a reference to the port since the port's lifetime
226 * is now protected by the component's own lifetime.
228 g_ptr_array_add(ports
, new_port
);
231 * Notify the graph's creator that a new port was added.
233 bt_object_get_ref(bt_component_borrow_graph(component
));
234 graph
= bt_component_borrow_graph(component
);
236 bt_graph_notify_port_added(graph
, new_port
);
237 BT_OBJECT_PUT_REF_AND_RESET(graph
);
240 BT_LIB_LOGD("Created and added port to component: "
241 "%![comp-]+c, %![port-]+p", component
, new_port
);
248 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
250 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
251 return (uint64_t) comp
->input_ports
->len
;
255 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
257 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
258 return (uint64_t) comp
->output_ports
->len
;
262 int bt_component_create(struct bt_component_class
*component_class
,
263 const char *name
, struct bt_component
**user_component
)
266 struct bt_component
*component
= NULL
;
267 enum bt_component_class_type type
;
269 BT_ASSERT(user_component
);
270 BT_ASSERT(component_class
);
273 type
= bt_component_class_get_type(component_class
);
274 BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, "
275 "comp-name=\"%s\"", component_class
, name
);
276 component
= component_create_funcs
[type
](component_class
);
278 BT_LOGE_STR("Cannot create specific component object.");
283 bt_object_init_shared_with_parent(&component
->base
,
285 component
->class = component_class
;
286 bt_object_get_no_null_check(component
->class);
287 component
->destroy
= component_destroy_funcs
[type
];
288 component
->name
= g_string_new(name
);
289 if (!component
->name
) {
290 BT_LOGE_STR("Failed to allocate one GString.");
295 component
->input_ports
= g_ptr_array_new_with_free_func(
296 (GDestroyNotify
) bt_object_try_spec_release
);
297 if (!component
->input_ports
) {
298 BT_LOGE_STR("Failed to allocate one GPtrArray.");
303 component
->output_ports
= g_ptr_array_new_with_free_func(
304 (GDestroyNotify
) bt_object_try_spec_release
);
305 if (!component
->output_ports
) {
306 BT_LOGE_STR("Failed to allocate one GPtrArray.");
311 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
312 sizeof(struct bt_component_destroy_listener
));
313 if (!component
->destroy_listeners
) {
314 BT_LOGE_STR("Failed to allocate one GArray.");
319 BT_LIB_LOGD("Created empty component from component class: "
320 "%![cc-]+C, %![comp-]+c", component_class
, component
);
321 BT_OBJECT_MOVE_REF(*user_component
, component
);
324 bt_object_put_ref(component
);
328 const char *bt_component_get_name(const struct bt_component
*component
)
330 BT_ASSERT_PRE_NON_NULL(component
, "Component");
331 return component
->name
->str
;
334 struct bt_component_class
*bt_component_borrow_class(
335 const struct bt_component
*component
)
337 BT_ASSERT_PRE_NON_NULL(component
, "Component");
338 return component
->class;
341 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
343 struct bt_component
*component
= (void *) self_comp
;
345 BT_ASSERT_PRE_NON_NULL(component
, "Component");
346 return component
->user_data
;
349 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
352 struct bt_component
*component
= (void *) self_comp
;
354 BT_ASSERT_PRE_NON_NULL(component
, "Component");
355 component
->user_data
= data
;
356 BT_LIB_LOGV("Set component's user data: %!+c", component
);
360 void bt_component_set_graph(struct bt_component
*component
,
361 struct bt_graph
*graph
)
363 bt_object_set_parent(&component
->base
,
364 graph
? &graph
->base
: NULL
);
367 bt_bool
bt_component_graph_is_canceled(const struct bt_component
*component
)
369 return bt_graph_is_canceled(
370 (void *) bt_object_borrow_parent(&component
->base
));
374 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
378 struct bt_port
*ret_port
= NULL
;
382 for (i
= 0; i
< ports
->len
; i
++) {
383 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
385 if (!strcmp(name
, port
->name
->str
)) {
395 struct bt_port_input
*bt_component_borrow_input_port_by_name(
396 struct bt_component
*comp
, const char *name
)
399 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
403 struct bt_port_output
*bt_component_borrow_output_port_by_name(
404 struct bt_component
*comp
, const char *name
)
406 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
408 borrow_port_by_name(comp
->output_ports
, name
);
412 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
414 BT_ASSERT(index
< ports
->len
);
415 return g_ptr_array_index(ports
, index
);
419 struct bt_port_input
*bt_component_borrow_input_port_by_index(
420 struct bt_component
*comp
, uint64_t index
)
422 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
423 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->input_ports
->len
);
425 borrow_port_by_index(comp
->input_ports
, index
);
429 struct bt_port_output
*bt_component_borrow_output_port_by_index(
430 struct bt_component
*comp
, uint64_t index
)
432 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
433 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->output_ports
->len
);
435 borrow_port_by_index(comp
->output_ports
, index
);
439 struct bt_port_input
*bt_component_add_input_port(
440 struct bt_component
*component
, const char *name
,
443 /* add_port() logs details */
445 add_port(component
, component
->input_ports
,
446 BT_PORT_TYPE_INPUT
, name
, user_data
);
450 struct bt_port_output
*bt_component_add_output_port(
451 struct bt_component
*component
, const char *name
,
454 /* add_port() logs details */
456 add_port(component
, component
->output_ports
,
457 BT_PORT_TYPE_OUTPUT
, name
, user_data
);
461 void remove_port_by_index(struct bt_component
*component
,
462 GPtrArray
*ports
, uint64_t index
)
464 struct bt_port
*port
;
465 struct bt_graph
*graph
;
468 BT_ASSERT(index
< ports
->len
);
469 port
= g_ptr_array_index(ports
, index
);
470 BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p",
473 /* Disconnect both ports of this port's connection, if any */
474 if (port
->connection
) {
475 bt_connection_end(port
->connection
, true);
479 * The port's current reference count can be 0 at this point,
480 * which means its parent (component) keeps it alive. We are
481 * about to remove the port from its parent's container (with
482 * the g_ptr_array_remove_index() call below), which in this
483 * case would destroy it. This is not good because we still
484 * need the port for the bt_graph_notify_port_removed() call
485 * below (in which its component is `NULL` as expected because
486 * of the bt_object_set_parent() call below).
488 * To avoid a destroyed port during the notification callback,
489 * get a reference now, and put it (destroying the port if its
490 * reference count is 0 at this point) after notifying the
493 bt_object_get_no_null_check(&port
->base
);
496 * Remove from parent's array of ports (weak refs). This never
497 * destroys the port object because its reference count is at
498 * least 1 thanks to the bt_object_get_no_null_check() call
501 g_ptr_array_remove_index(ports
, index
);
503 /* Detach port from its component parent */
504 bt_object_set_parent(&port
->base
, NULL
);
507 * Notify the graph's creator that a port is removed.
509 graph
= bt_component_borrow_graph(component
);
511 bt_graph_notify_port_removed(graph
, component
, port
);
514 BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p",
518 * Put the local reference. If this port's reference count was 0
519 * when entering this function, it is 1 now, so it is destroyed
522 bt_object_put_no_null_check(&port
->base
);
526 void bt_component_remove_port(struct bt_component
*component
,
527 struct bt_port
*port
)
530 GPtrArray
*ports
= NULL
;
532 BT_ASSERT(component
);
535 switch (port
->type
) {
536 case BT_PORT_TYPE_INPUT
:
537 ports
= component
->input_ports
;
539 case BT_PORT_TYPE_OUTPUT
:
540 ports
= component
->output_ports
;
548 for (i
= 0; i
< ports
->len
; i
++) {
549 struct bt_port
*cur_port
= g_ptr_array_index(ports
, i
);
551 if (cur_port
== port
) {
552 remove_port_by_index(component
,
558 BT_LIB_LOGW("Port to remove from component was not found: "
559 "%![comp-]+c, %![port-]+p", component
, port
);
566 enum bt_self_component_status
bt_component_accept_port_connection(
567 struct bt_component
*comp
, struct bt_port
*self_port
,
568 struct bt_port
*other_port
)
570 typedef enum bt_self_component_status (*method_t
)(
571 void *, void *, const void *);
573 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
574 method_t method
= NULL
;
577 BT_ASSERT(self_port
);
578 BT_ASSERT(other_port
);
580 switch (comp
->class->type
) {
581 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
583 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
585 switch (self_port
->type
) {
586 case BT_PORT_TYPE_OUTPUT
:
587 method
= (method_t
) src_cc
->methods
.accept_output_port_connection
;
595 case BT_COMPONENT_CLASS_TYPE_FILTER
:
597 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
599 switch (self_port
->type
) {
600 case BT_PORT_TYPE_INPUT
:
601 method
= (method_t
) flt_cc
->methods
.accept_input_port_connection
;
603 case BT_PORT_TYPE_OUTPUT
:
604 method
= (method_t
) flt_cc
->methods
.accept_output_port_connection
;
612 case BT_COMPONENT_CLASS_TYPE_SINK
:
614 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
616 switch (self_port
->type
) {
617 case BT_PORT_TYPE_INPUT
:
618 method
= (method_t
) sink_cc
->methods
.accept_input_port_connection
;
631 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
632 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
633 comp
, self_port
, other_port
);
634 status
= method(comp
, self_port
, (void *) other_port
);
635 BT_LOGD("User method returned: status=%s",
636 bt_self_component_status_string(status
));
643 enum bt_self_component_status
bt_component_port_connected(
644 struct bt_component
*comp
, struct bt_port
*self_port
,
645 struct bt_port
*other_port
)
647 typedef enum bt_self_component_status (*method_t
)(
648 void *, void *, const void *);
650 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
651 method_t method
= NULL
;
654 BT_ASSERT(self_port
);
655 BT_ASSERT(other_port
);
657 switch (comp
->class->type
) {
658 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
660 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
662 switch (self_port
->type
) {
663 case BT_PORT_TYPE_OUTPUT
:
664 method
= (method_t
) src_cc
->methods
.output_port_connected
;
672 case BT_COMPONENT_CLASS_TYPE_FILTER
:
674 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
676 switch (self_port
->type
) {
677 case BT_PORT_TYPE_INPUT
:
678 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
680 case BT_PORT_TYPE_OUTPUT
:
681 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
689 case BT_COMPONENT_CLASS_TYPE_SINK
:
691 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
693 switch (self_port
->type
) {
694 case BT_PORT_TYPE_INPUT
:
695 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
708 BT_LIB_LOGD("Calling user's \"port connected\" method: "
709 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
710 comp
, self_port
, other_port
);
711 status
= method(comp
, self_port
, (void *) other_port
);
712 BT_LOGD("User method returned: status=%s",
713 bt_self_component_status_string(status
));
720 void bt_component_port_disconnected(struct bt_component
*comp
,
721 struct bt_port
*port
)
723 typedef void (*method_t
)(void *, void *);
725 method_t method
= NULL
;
730 switch (comp
->class->type
) {
731 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
733 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
735 switch (port
->type
) {
736 case BT_PORT_TYPE_OUTPUT
:
737 method
= (method_t
) src_cc
->methods
.output_port_disconnected
;
745 case BT_COMPONENT_CLASS_TYPE_FILTER
:
747 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
749 switch (port
->type
) {
750 case BT_PORT_TYPE_INPUT
:
751 method
= (method_t
) flt_cc
->methods
.input_port_disconnected
;
753 case BT_PORT_TYPE_OUTPUT
:
754 method
= (method_t
) flt_cc
->methods
.output_port_disconnected
;
762 case BT_COMPONENT_CLASS_TYPE_SINK
:
764 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
766 switch (port
->type
) {
767 case BT_PORT_TYPE_INPUT
:
768 method
= (method_t
) sink_cc
->methods
.input_port_disconnected
;
781 BT_LIB_LOGD("Calling user's \"port disconnected\" method: "
782 "%![comp-]+c, %![port-]+p", comp
, port
);
788 void bt_component_add_destroy_listener(struct bt_component
*component
,
789 bt_component_destroy_listener_func func
, void *data
)
791 struct bt_component_destroy_listener listener
;
793 BT_ASSERT(component
);
795 listener
.func
= func
;
796 listener
.data
= data
;
797 g_array_append_val(component
->destroy_listeners
, listener
);
798 BT_LIB_LOGV("Added destroy listener: %![comp-]+c, "
799 "func-addr=%p, data-addr=%p",
800 component
, func
, data
);
804 void bt_component_remove_destroy_listener(struct bt_component
*component
,
805 bt_component_destroy_listener_func func
, void *data
)
809 BT_ASSERT(component
);
812 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
813 struct bt_component_destroy_listener
*listener
=
814 &g_array_index(component
->destroy_listeners
,
815 struct bt_component_destroy_listener
, i
);
817 if (listener
->func
== func
&& listener
->data
== data
) {
818 g_array_remove_index(component
->destroy_listeners
, i
);
820 BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, "
821 "func-addr=%p, data-addr=%p",
822 component
, func
, data
);