09c8b111acf3bae2d465bc4658517dfcf5fbf9c4
[babeltrace.git] / include / babeltrace / graph / graph-internal.h
1 #ifndef BABELTRACE_COMPONENT_COMPONENT_GRAPH_INTERNAL_H
2 #define BABELTRACE_COMPONENT_COMPONENT_GRAPH_INTERNAL_H
3
4 /*
5 * BabelTrace - Component Graph Internal
6 *
7 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/graph/graph.h>
31 #include <babeltrace/graph/component-status.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/object-internal.h>
34 #include <stdlib.h>
35 #include <assert.h>
36 #include <glib.h>
37
38 struct bt_component;
39 struct bt_port;
40
41 struct bt_graph {
42 /**
43 * A component graph contains components and point-to-point connection
44 * between these components.
45 *
46 * In terms of ownership:
47 * 1) The graph is the components' parent,
48 * 2) The graph is the connnections' parent,
49 * 3) Components share the ownership of their connections,
50 * 4) A connection holds weak references to its two component endpoints.
51 */
52 struct bt_object base;
53
54 /* Array of pointers to bt_connection. */
55 GPtrArray *connections;
56 /* Array of pointers to bt_component. */
57 GPtrArray *components;
58 /* Queue of pointers (weak references) to sink bt_components. */
59 GQueue *sinks_to_consume;
60
61 bt_bool canceled;
62 bt_bool in_remove_listener;
63 bt_bool has_sink;
64
65 /*
66 * If this is BT_FALSE, then the public API's consuming
67 * functions (bt_graph_consume() and bt_graph_run()) return
68 * BT_GRAPH_STATUS_CANNOT_CONSUME. The internal "no check"
69 * functions always work.
70 *
71 * In bt_output_port_notification_iterator_create(), on success,
72 * this flag is cleared so that the iterator remains the only
73 * consumer for the graph's lifetime.
74 */
75 bt_bool can_consume;
76
77 struct {
78 GArray *port_added;
79 GArray *port_removed;
80 GArray *ports_connected;
81 GArray *ports_disconnected;
82 } listeners;
83 };
84
85 static inline
86 void bt_graph_set_can_consume(struct bt_graph *graph, bt_bool can_consume)
87 {
88 assert(graph);
89 graph->can_consume = can_consume;
90 }
91
92 BT_HIDDEN
93 enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph);
94
95 BT_HIDDEN
96 enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
97 struct bt_component *sink);
98
99 BT_HIDDEN
100 void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port);
101
102 BT_HIDDEN
103 void bt_graph_notify_port_removed(struct bt_graph *graph,
104 struct bt_component *comp, struct bt_port *port);
105
106 BT_HIDDEN
107 void bt_graph_notify_ports_connected(struct bt_graph *graph,
108 struct bt_port *upstream_port, struct bt_port *downstream_port);
109
110 BT_HIDDEN
111 void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
112 struct bt_component *upstream_comp,
113 struct bt_component *downstream_comp,
114 struct bt_port *upstream_port,
115 struct bt_port *downstream_port);
116
117 BT_HIDDEN
118 void bt_graph_remove_connection(struct bt_graph *graph,
119 struct bt_connection *connection);
120
121 /*
122 * This only works with a component which is not connected at this
123 * point.
124 *
125 * Also the reference count of `component` should be 0 when you call
126 * this function, which means only `graph` owns the component, so it
127 * is safe to destroy.
128 */
129 BT_HIDDEN
130 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
131 struct bt_component *component);
132
133 static inline
134 const char *bt_graph_status_string(enum bt_graph_status status)
135 {
136 switch (status) {
137 case BT_GRAPH_STATUS_CANCELED:
138 return "BT_GRAPH_STATUS_CANCELED";
139 case BT_GRAPH_STATUS_AGAIN:
140 return "BT_GRAPH_STATUS_AGAIN";
141 case BT_GRAPH_STATUS_END:
142 return "BT_GRAPH_STATUS_END";
143 case BT_GRAPH_STATUS_OK:
144 return "BT_GRAPH_STATUS_OK";
145 case BT_GRAPH_STATUS_INVALID:
146 return "BT_GRAPH_STATUS_INVALID";
147 case BT_GRAPH_STATUS_NO_SINK:
148 return "BT_GRAPH_STATUS_NO_SINK";
149 case BT_GRAPH_STATUS_ERROR:
150 return "BT_GRAPH_STATUS_ERROR";
151 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
152 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
153 case BT_GRAPH_STATUS_NOMEM:
154 return "BT_GRAPH_STATUS_NOMEM";
155 case BT_GRAPH_STATUS_CANNOT_CONSUME:
156 return "BT_GRAPH_STATUS_CANNOT_CONSUME";
157 default:
158 return "(unknown)";
159 }
160 }
161
162 static inline
163 enum bt_graph_status bt_graph_status_from_component_status(
164 enum bt_component_status comp_status)
165 {
166 switch (comp_status) {
167 case BT_COMPONENT_STATUS_OK:
168 return BT_GRAPH_STATUS_OK;
169 case BT_COMPONENT_STATUS_END:
170 return BT_GRAPH_STATUS_END;
171 case BT_COMPONENT_STATUS_AGAIN:
172 return BT_GRAPH_STATUS_AGAIN;
173 case BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION:
174 return BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION;
175 case BT_COMPONENT_STATUS_ERROR:
176 return BT_GRAPH_STATUS_ERROR;
177 case BT_COMPONENT_STATUS_UNSUPPORTED:
178 return BT_GRAPH_STATUS_ERROR;
179 case BT_COMPONENT_STATUS_INVALID:
180 return BT_GRAPH_STATUS_INVALID;
181 case BT_COMPONENT_STATUS_NOMEM:
182 return BT_GRAPH_STATUS_NOMEM;
183 case BT_COMPONENT_STATUS_NOT_FOUND:
184 return BT_GRAPH_STATUS_ERROR;
185 default:
186 #ifdef BT_LOGF
187 BT_LOGF("Unknown component status: status=%d", comp_status);
188 #endif
189 abort();
190 }
191 }
192
193 #endif /* BABELTRACE_COMPONENT_COMPONENT_GRAPH_INTERNAL_H */
This page took 0.032676 seconds and 3 git commands to generate.