lib: remove TODO in add_port
[babeltrace.git] / src / lib / graph / component.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/COMPONENT"
9 #include "lib/logging.h"
10
11 #include "common/common.h"
12 #include "common/assert.h"
13 #include "lib/assert-cond.h"
14 #include <babeltrace2/graph/self-component.h>
15 #include <babeltrace2/graph/component.h>
16 #include <babeltrace2/graph/graph.h>
17 #include "common/macros.h"
18 #include "compat/compiler.h"
19 #include <babeltrace2/types.h>
20 #include <babeltrace2/value.h>
21 #include "lib/value.h"
22 #include <stdint.h>
23 #include <inttypes.h>
24
25 #include "component.h"
26 #include "component-class.h"
27 #include "component-source.h"
28 #include "component-filter.h"
29 #include "component-sink.h"
30 #include "connection.h"
31 #include "graph.h"
32 #include "message/iterator.h"
33 #include "port.h"
34 #include "lib/func-status.h"
35
36 static
37 struct bt_component * (* const component_create_funcs[])(
38 const struct bt_component_class *) = {
39 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
40 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
41 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
42 };
43
44 static
45 void (*component_destroy_funcs[])(struct bt_component *) = {
46 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy,
47 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy,
48 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy,
49 };
50
51 static
52 void finalize_component(struct bt_component *comp)
53 {
54 typedef void (*method_t)(void *);
55 const char *method_name;
56
57 method_t method = NULL;
58
59 BT_ASSERT(comp);
60
61 switch (comp->class->type) {
62 case BT_COMPONENT_CLASS_TYPE_SOURCE:
63 {
64 struct bt_component_class_source *src_cc = (void *) comp->class;
65
66 method = (method_t) src_cc->methods.finalize;
67 method_name = "bt_component_class_source_finalize_method";
68 break;
69 }
70 case BT_COMPONENT_CLASS_TYPE_FILTER:
71 {
72 struct bt_component_class_filter *flt_cc = (void *) comp->class;
73
74 method = (method_t) flt_cc->methods.finalize;
75 method_name = "bt_component_class_filter_finalize_method";
76 break;
77 }
78 case BT_COMPONENT_CLASS_TYPE_SINK:
79 {
80 struct bt_component_class_sink *sink_cc = (void *) comp->class;
81
82 method = (method_t) sink_cc->methods.finalize;
83 method_name = "bt_component_class_sink_finalize_method";
84 break;
85 }
86 default:
87 bt_common_abort();
88 }
89
90 if (method) {
91 const struct bt_error *saved_error;
92
93 saved_error = bt_current_thread_take_error();
94
95 BT_LIB_LOGI("Calling user's component finalization method: "
96 "%![comp-]+c", comp);
97 method(comp);
98 BT_ASSERT_POST_NO_ERROR(method_name);
99
100 if (saved_error) {
101 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
102 }
103 }
104 }
105
106 static
107 void destroy_component(struct bt_object *obj)
108 {
109 struct bt_component *component = NULL;
110 int i;
111
112 if (!obj) {
113 return;
114 }
115
116 /*
117 * The component's reference count is 0 if we're here. Increment
118 * it to avoid a double-destroy (possibly infinitely recursive).
119 * This could happen for example if the component's finalization
120 * function does bt_object_get_ref() (or anything that causes
121 * bt_object_get_ref() to be called) on itself (ref. count goes
122 * from 0 to 1), and then bt_object_put_ref(): the reference
123 * count would go from 1 to 0 again and this function would be
124 * called again.
125 */
126 obj->ref_count++;
127 component = container_of(obj, struct bt_component, base);
128 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
129 component, bt_component_borrow_graph(component));
130
131 /* Call destroy listeners in reverse registration order */
132 BT_LOGD_STR("Calling destroy listeners.");
133
134 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
135 struct bt_component_destroy_listener *listener =
136 &g_array_index(component->destroy_listeners,
137 struct bt_component_destroy_listener, i);
138
139 listener->func(component, listener->data);
140 }
141
142 /*
143 * User data is destroyed first, followed by the concrete
144 * component instance. Do not finalize if the component's user
145 * initialization method failed in the first place.
146 */
147 if (component->initialized) {
148 finalize_component(component);
149 }
150
151 if (component->destroy) {
152 BT_LOGD_STR("Destroying type-specific data.");
153 component->destroy(component);
154 }
155
156 if (component->input_ports) {
157 BT_LOGD_STR("Destroying input ports.");
158 g_ptr_array_free(component->input_ports, TRUE);
159 component->input_ports = NULL;
160 }
161
162 if (component->output_ports) {
163 BT_LOGD_STR("Destroying output ports.");
164 g_ptr_array_free(component->output_ports, TRUE);
165 component->output_ports = NULL;
166 }
167
168 if (component->destroy_listeners) {
169 g_array_free(component->destroy_listeners, TRUE);
170 component->destroy_listeners = NULL;
171 }
172
173 if (component->name) {
174 g_string_free(component->name, TRUE);
175 component->name = NULL;
176 }
177
178 BT_LOGD_STR("Putting component class.");
179 BT_OBJECT_PUT_REF_AND_RESET(component->class);
180 g_free(component);
181 }
182
183 BT_EXPORT
184 enum bt_component_class_type bt_component_get_class_type(
185 const struct bt_component *component)
186 {
187 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component);
188 return component->class->type;
189 }
190
191 static
192 bool port_name_is_unique(GPtrArray *ports, const char *name)
193 {
194 guint i;
195 bool unique;
196
197 for (i = 0; i < ports->len; i++) {
198 struct bt_port *port = g_ptr_array_index(ports, i);
199
200 if (strcmp(port->name->str, name) == 0) {
201 unique = false;
202 goto end;
203 }
204 }
205
206 unique = true;
207
208 end:
209 return unique;
210 }
211
212 static
213 enum bt_self_component_add_port_status add_port(
214 struct bt_component *component, GPtrArray *ports,
215 enum bt_port_type port_type, const char *name, void *user_data,
216 struct bt_port **port, const char *api_func, bool input)
217 {
218 struct bt_port *new_port = NULL;
219 struct bt_graph *graph = NULL;
220 enum bt_self_component_add_port_status status;
221
222 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func);
223 BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func, component);
224 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func, name);
225 BT_ASSERT_PRE_FROM_FUNC(api_func,
226 input ? "input" : "output" "-port-name-is-unique",
227 port_name_is_unique(component->output_ports, name),
228 input ? "Input" : "Output"
229 " port name is not unique: name=\"%s\", %![comp-]c",
230 name, component);
231 BT_ASSERT_PRE_FROM_FUNC(api_func, "name-is-not-empty",
232 strlen(name) > 0, "Name is empty");
233 graph = bt_component_borrow_graph(component);
234 BT_ASSERT_PRE_FROM_FUNC(api_func, "graph-is-not-configured",
235 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
236 "Component's graph is already configured: "
237 "%![comp-]+c, %![graph-]+g", component, graph);
238
239 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
240 "port-type=%s, port-name=\"%s\"", component,
241 bt_port_type_string(port_type), name);
242
243 new_port = bt_port_create(component, port_type, name, user_data);
244 if (!new_port) {
245 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
246 status = BT_FUNC_STATUS_MEMORY_ERROR;
247 goto error;
248 }
249
250 /*
251 * No name clash, add the port.
252 * The component is now the port's parent; it should _not_
253 * hold a reference to the port since the port's lifetime
254 * is now protected by the component's own lifetime.
255 */
256 g_ptr_array_add(ports, new_port);
257
258 /*
259 * Notify the graph's creator that a new port was added.
260 */
261 graph = bt_component_borrow_graph(component);
262 if (graph) {
263 enum bt_graph_listener_func_status listener_status;
264
265 listener_status = bt_graph_notify_port_added(graph, new_port);
266 if (listener_status != BT_FUNC_STATUS_OK) {
267 bt_graph_make_faulty(graph);
268 status = (int) listener_status;
269 goto error;
270 }
271 }
272
273 BT_LIB_LOGI("Created and added port to component: "
274 "%![comp-]+c, %![port-]+p", component, new_port);
275
276 *port = new_port;
277 status = BT_FUNC_STATUS_OK;
278
279 goto end;
280 error:
281 /*
282 * We need to release the reference that we would otherwise have
283 * returned to the caller.
284 */
285 BT_PORT_PUT_REF_AND_RESET(new_port);
286
287 end:
288 return status;
289 }
290
291 uint64_t bt_component_get_input_port_count(const struct bt_component *comp,
292 const char *api_func)
293 {
294 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func, comp);
295 return (uint64_t) comp->input_ports->len;
296 }
297
298 uint64_t bt_component_get_output_port_count(const struct bt_component *comp,
299 const char *api_func)
300 {
301 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func, comp);
302 return (uint64_t) comp->output_ports->len;
303 }
304
305 int bt_component_create(struct bt_component_class *component_class,
306 const char *name, bt_logging_level log_level,
307 struct bt_component **user_component)
308 {
309 int ret = 0;
310 struct bt_component *component = NULL;
311 enum bt_component_class_type type;
312
313 BT_ASSERT(user_component);
314 BT_ASSERT(component_class);
315 BT_ASSERT(name);
316 type = bt_component_class_get_type(component_class);
317 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
318 "comp-name=\"%s\", log-level=%s", component_class, name,
319 bt_common_logging_level_string(log_level));
320 component = component_create_funcs[type](component_class);
321 if (!component) {
322 BT_LIB_LOGE_APPEND_CAUSE(
323 "Cannot create specific component object.");
324 ret = -1;
325 goto end;
326 }
327
328 bt_object_init_shared_with_parent(&component->base, destroy_component);
329 component->class = component_class;
330 bt_object_get_ref_no_null_check(component->class);
331 component->destroy = component_destroy_funcs[type];
332 component->name = g_string_new(name);
333 if (!component->name) {
334 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
335 ret = -1;
336 goto end;
337 }
338
339 component->log_level = log_level;
340 component->input_ports = g_ptr_array_new_with_free_func(
341 (GDestroyNotify) bt_object_try_spec_release);
342 if (!component->input_ports) {
343 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
344 ret = -1;
345 goto end;
346 }
347
348 component->output_ports = g_ptr_array_new_with_free_func(
349 (GDestroyNotify) bt_object_try_spec_release);
350 if (!component->output_ports) {
351 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
352 ret = -1;
353 goto end;
354 }
355
356 component->destroy_listeners = g_array_new(FALSE, TRUE,
357 sizeof(struct bt_component_destroy_listener));
358 if (!component->destroy_listeners) {
359 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
360 ret = -1;
361 goto end;
362 }
363
364 BT_LIB_LOGI("Created empty component from component class: "
365 "%![cc-]+C, %![comp-]+c", component_class, component);
366 BT_OBJECT_MOVE_REF(*user_component, component);
367
368 end:
369 bt_object_put_ref(component);
370 return ret;
371 }
372
373 BT_EXPORT
374 const char *bt_component_get_name(const struct bt_component *component)
375 {
376 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component);
377 return component->name->str;
378 }
379
380 BT_EXPORT
381 const struct bt_component_class *bt_component_borrow_class_const(
382 const struct bt_component *component)
383 {
384 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component);
385 return component->class;
386 }
387
388 BT_EXPORT
389 void *bt_self_component_get_data(const struct bt_self_component *self_comp)
390 {
391 struct bt_component *component = (void *) self_comp;
392
393 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component);
394 return component->user_data;
395 }
396
397 BT_EXPORT
398 void bt_self_component_set_data(struct bt_self_component *self_comp,
399 void *data)
400 {
401 struct bt_component *component = (void *) self_comp;
402
403 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component);
404 component->user_data = data;
405 BT_LIB_LOGD("Set component's user data: %!+c", component);
406 }
407
408 void bt_component_set_graph(struct bt_component *component,
409 struct bt_graph *graph)
410 {
411 bt_object_set_parent(&component->base,
412 graph ? &graph->base : NULL);
413 }
414
415 static
416 struct bt_port *borrow_port_by_name(GPtrArray *ports,
417 const char *name, const char *api_func)
418 {
419 uint64_t i;
420 struct bt_port *ret_port = NULL;
421
422 BT_ASSERT_PRE_DEV_NAME_NON_NULL_FROM_FUNC(api_func, name);
423
424 for (i = 0; i < ports->len; i++) {
425 struct bt_port *port = g_ptr_array_index(ports, i);
426
427 if (strcmp(name, port->name->str) == 0) {
428 ret_port = port;
429 break;
430 }
431 }
432
433 return ret_port;
434 }
435
436 struct bt_port_input *bt_component_borrow_input_port_by_name(
437 struct bt_component *comp, const char *name,
438 const char *api_func)
439 {
440 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func, comp);
441 return (void *) borrow_port_by_name(comp->input_ports, name, api_func);
442 }
443
444 struct bt_port_output *bt_component_borrow_output_port_by_name(
445 struct bt_component *comp, const char *name,
446 const char *api_func)
447 {
448 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func, comp);
449 return (void *)
450 borrow_port_by_name(comp->output_ports, name, api_func);
451 }
452
453 static
454 struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index,
455 const char *api_func)
456 {
457 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func, index, ports->len);
458 return g_ptr_array_index(ports, index);
459 }
460
461 struct bt_port_input *bt_component_borrow_input_port_by_index(
462 struct bt_component *comp, uint64_t index,
463 const char *api_func)
464 {
465 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func, comp);
466 return (void *)
467 borrow_port_by_index(comp->input_ports, index, api_func);
468 }
469
470 struct bt_port_output *bt_component_borrow_output_port_by_index(
471 struct bt_component *comp, uint64_t index,
472 const char *api_func)
473 {
474 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func, comp);
475 return (void *)
476 borrow_port_by_index(comp->output_ports, index, api_func);
477 }
478
479 enum bt_self_component_add_port_status bt_component_add_input_port(
480 struct bt_component *component, const char *name,
481 void *user_data, struct bt_port **port, const char *api_func)
482 {
483 /* add_port() logs details and checks preconditions */
484 return add_port(component, component->input_ports,
485 BT_PORT_TYPE_INPUT, name, user_data, port, api_func, true);
486 }
487
488 enum bt_self_component_add_port_status bt_component_add_output_port(
489 struct bt_component *component, const char *name,
490 void *user_data, struct bt_port **port,
491 const char *api_func)
492 {
493 /* add_port() logs details and checks preconditions */
494 return add_port(component, component->output_ports,
495 BT_PORT_TYPE_OUTPUT, name, user_data, port, api_func, false);
496 }
497
498 enum bt_component_class_port_connected_method_status
499 bt_component_port_connected(
500 struct bt_component *comp, struct bt_port *self_port,
501 struct bt_port *other_port)
502 {
503 typedef enum bt_component_class_port_connected_method_status (*method_t)(
504 void *, void *, const void *);
505
506 enum bt_component_class_port_connected_method_status status =
507 BT_FUNC_STATUS_OK;
508 method_t method = NULL;
509 const char *method_name = NULL;
510
511 BT_ASSERT(comp);
512 BT_ASSERT(self_port);
513 BT_ASSERT(other_port);
514
515 switch (comp->class->type) {
516 case BT_COMPONENT_CLASS_TYPE_SOURCE:
517 {
518 struct bt_component_class_source *src_cc = (void *) comp->class;
519
520 switch (self_port->type) {
521 case BT_PORT_TYPE_OUTPUT:
522 method = (method_t) src_cc->methods.output_port_connected;
523 method_name = "bt_component_class_source_output_port_connected_method";
524 break;
525 default:
526 bt_common_abort();
527 }
528
529 break;
530 }
531 case BT_COMPONENT_CLASS_TYPE_FILTER:
532 {
533 struct bt_component_class_filter *flt_cc = (void *) comp->class;
534
535 switch (self_port->type) {
536 case BT_PORT_TYPE_INPUT:
537 method = (method_t) flt_cc->methods.input_port_connected;
538 method_name = "bt_component_class_filter_input_port_connected_method";
539 break;
540 case BT_PORT_TYPE_OUTPUT:
541 method = (method_t) flt_cc->methods.output_port_connected;
542 method_name = "bt_component_class_filter_output_port_connected_method";
543 break;
544 default:
545 bt_common_abort();
546 }
547
548 break;
549 }
550 case BT_COMPONENT_CLASS_TYPE_SINK:
551 {
552 struct bt_component_class_sink *sink_cc = (void *) comp->class;
553
554 switch (self_port->type) {
555 case BT_PORT_TYPE_INPUT:
556 method = (method_t) sink_cc->methods.input_port_connected;
557 method_name = "bt_component_class_sink_input_port_connected_method";
558 break;
559 default:
560 bt_common_abort();
561 }
562
563 break;
564 }
565 default:
566 bt_common_abort();
567 }
568
569 if (method) {
570 BT_LIB_LOGD("Calling user's \"port connected\" method: "
571 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
572 comp, self_port, other_port);
573 status = (int) method(comp, self_port, (void *) other_port);
574 BT_LOGD("User method returned: status=%s",
575 bt_common_func_status_string(status));
576 BT_ASSERT_POST(method_name, "valid-status",
577 status == BT_FUNC_STATUS_OK ||
578 status == BT_FUNC_STATUS_ERROR ||
579 status == BT_FUNC_STATUS_MEMORY_ERROR,
580 "Unexpected returned component status: status=%s",
581 bt_common_func_status_string(status));
582 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_name, status);
583 }
584
585 return status;
586 }
587
588 void bt_component_add_destroy_listener(struct bt_component *component,
589 bt_component_destroy_listener_func func, void *data)
590 {
591 struct bt_component_destroy_listener listener;
592
593 BT_ASSERT(component);
594 BT_ASSERT(func);
595 listener.func = func;
596 listener.data = data;
597 g_array_append_val(component->destroy_listeners, listener);
598 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
599 "func-addr=%p, data-addr=%p",
600 component, func, data);
601 }
602
603 void bt_component_remove_destroy_listener(struct bt_component *component,
604 bt_component_destroy_listener_func func, void *data)
605 {
606 uint64_t i;
607
608 BT_ASSERT(component);
609 BT_ASSERT(func);
610
611 for (i = 0; i < component->destroy_listeners->len; i++) {
612 struct bt_component_destroy_listener *listener =
613 &g_array_index(component->destroy_listeners,
614 struct bt_component_destroy_listener, i);
615
616 if (listener->func == func && listener->data == data) {
617 g_array_remove_index(component->destroy_listeners, i);
618 i--;
619 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
620 "func-addr=%p, data-addr=%p",
621 component, func, data);
622 }
623 }
624 }
625
626 BT_EXPORT
627 bt_logging_level bt_component_get_logging_level(
628 const struct bt_component *component)
629 {
630 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component);
631 return component->log_level;
632 }
633
634 BT_EXPORT
635 uint64_t bt_self_component_get_graph_mip_version(
636 bt_self_component *self_component)
637 {
638 struct bt_component *comp = (void *) self_component;
639
640 BT_ASSERT_PRE_COMP_NON_NULL(self_component);
641 return bt_component_borrow_graph(comp)->mip_version;
642 }
643
644 BT_EXPORT
645 void bt_component_get_ref(const struct bt_component *component)
646 {
647 bt_object_get_ref(component);
648 }
649
650 BT_EXPORT
651 void bt_component_put_ref(const struct bt_component *component)
652 {
653 bt_object_put_ref(component);
654 }
This page took 0.043205 seconds and 4 git commands to generate.