Commit | Line | Data |
---|---|---|
abe30a8f PP |
1 | #ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H |
2 | #define BABELTRACE_GRAPH_GRAPH_INTERNAL_H | |
c0418dd9 JG |
3 | |
4 | /* | |
5 | * BabelTrace - Component Graph Internal | |
6 | * | |
f60c8b34 | 7 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
c0418dd9 JG |
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 | ||
b2e0c907 | 30 | #include <babeltrace/graph/graph.h> |
a256a42d | 31 | #include <babeltrace/graph/component-status.h> |
f7c3ac09 | 32 | #include <babeltrace/graph/notification.h> |
c0418dd9 JG |
33 | #include <babeltrace/babeltrace-internal.h> |
34 | #include <babeltrace/object-internal.h> | |
f7c3ac09 | 35 | #include <babeltrace/object-pool-internal.h> |
8b45963b | 36 | #include <babeltrace/assert-internal.h> |
a256a42d | 37 | #include <stdlib.h> |
c0418dd9 JG |
38 | #include <glib.h> |
39 | ||
1bf957a0 PP |
40 | struct bt_component; |
41 | struct bt_port; | |
42 | ||
f60c8b34 JG |
43 | struct bt_graph { |
44 | /** | |
45 | * A component graph contains components and point-to-point connection | |
46 | * between these components. | |
c0418dd9 | 47 | * |
f60c8b34 JG |
48 | * In terms of ownership: |
49 | * 1) The graph is the components' parent, | |
50 | * 2) The graph is the connnections' parent, | |
51 | * 3) Components share the ownership of their connections, | |
52 | * 4) A connection holds weak references to its two component endpoints. | |
c0418dd9 | 53 | */ |
f60c8b34 JG |
54 | struct bt_object base; |
55 | ||
56 | /* Array of pointers to bt_connection. */ | |
57 | GPtrArray *connections; | |
58 | /* Array of pointers to bt_component. */ | |
59 | GPtrArray *components; | |
60 | /* Queue of pointers (weak references) to sink bt_components. */ | |
61 | GQueue *sinks_to_consume; | |
1bf957a0 | 62 | |
202a3a13 | 63 | bt_bool canceled; |
eb87cce7 | 64 | bt_bool in_remove_listener; |
aebb2750 | 65 | bt_bool has_sink; |
c3ac0193 PP |
66 | |
67 | /* | |
68 | * If this is BT_FALSE, then the public API's consuming | |
69 | * functions (bt_graph_consume() and bt_graph_run()) return | |
70 | * BT_GRAPH_STATUS_CANNOT_CONSUME. The internal "no check" | |
71 | * functions always work. | |
72 | * | |
73 | * In bt_output_port_notification_iterator_create(), on success, | |
74 | * this flag is cleared so that the iterator remains the only | |
75 | * consumer for the graph's lifetime. | |
76 | */ | |
9ef22b36 | 77 | bt_bool can_consume; |
202a3a13 | 78 | |
1bf957a0 PP |
79 | struct { |
80 | GArray *port_added; | |
81 | GArray *port_removed; | |
f345f8bb PP |
82 | GArray *ports_connected; |
83 | GArray *ports_disconnected; | |
1bf957a0 | 84 | } listeners; |
f7c3ac09 PP |
85 | |
86 | /* Pool of `struct bt_notification_event *` */ | |
87 | struct bt_object_pool event_notif_pool; | |
88 | ||
89 | /* Pool of `struct bt_notification_packet_begin *` */ | |
90 | struct bt_object_pool packet_begin_notif_pool; | |
91 | ||
92 | /* Pool of `struct bt_notification_packet_end *` */ | |
93 | struct bt_object_pool packet_end_notif_pool; | |
94 | ||
95 | /* | |
96 | * Array of `struct bt_notification *` (weak). | |
97 | * | |
98 | * This is an array of all the notifications ever created from | |
99 | * this graph. Some of them can be in one of the pools above, | |
100 | * some of them can be at large. Because each notification has a | |
101 | * weak pointer to the graph containing its pool, we need to | |
102 | * notify each notification that the graph is gone on graph | |
103 | * destruction. | |
104 | * | |
105 | * TODO: When we support a maximum size for object pools, | |
106 | * add a way for a notification to remove itself from this | |
107 | * array (on destruction). | |
108 | */ | |
109 | GPtrArray *notifications; | |
c0418dd9 JG |
110 | }; |
111 | ||
c3ac0193 | 112 | static inline |
371361a1 | 113 | void _bt_graph_set_can_consume(struct bt_graph *graph, bt_bool can_consume) |
c3ac0193 | 114 | { |
8b45963b | 115 | BT_ASSERT(graph); |
c3ac0193 PP |
116 | graph->can_consume = can_consume; |
117 | } | |
118 | ||
371361a1 PP |
119 | #ifdef BT_DEV_MODE |
120 | # define bt_graph_set_can_consume _bt_graph_set_can_consume | |
121 | #else | |
122 | # define bt_graph_set_can_consume(_graph, _can_consume) | |
123 | #endif | |
c3ac0193 PP |
124 | |
125 | BT_HIDDEN | |
126 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, | |
127 | struct bt_component *sink); | |
128 | ||
1bf957a0 PP |
129 | BT_HIDDEN |
130 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port); | |
131 | ||
132 | BT_HIDDEN | |
133 | void bt_graph_notify_port_removed(struct bt_graph *graph, | |
134 | struct bt_component *comp, struct bt_port *port); | |
135 | ||
136 | BT_HIDDEN | |
f345f8bb PP |
137 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
138 | struct bt_port *upstream_port, struct bt_port *downstream_port); | |
1bf957a0 PP |
139 | |
140 | BT_HIDDEN | |
f345f8bb PP |
141 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
142 | struct bt_component *upstream_comp, | |
143 | struct bt_component *downstream_comp, | |
144 | struct bt_port *upstream_port, | |
145 | struct bt_port *downstream_port); | |
1bf957a0 | 146 | |
f167d3c0 PP |
147 | BT_HIDDEN |
148 | void bt_graph_remove_connection(struct bt_graph *graph, | |
149 | struct bt_connection *connection); | |
150 | ||
c3ac0193 PP |
151 | /* |
152 | * This only works with a component which is not connected at this | |
153 | * point. | |
154 | * | |
155 | * Also the reference count of `component` should be 0 when you call | |
156 | * this function, which means only `graph` owns the component, so it | |
157 | * is safe to destroy. | |
158 | */ | |
159 | BT_HIDDEN | |
160 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, | |
161 | struct bt_component *component); | |
162 | ||
f7c3ac09 PP |
163 | BT_HIDDEN |
164 | void bt_graph_add_notification(struct bt_graph *graph, | |
165 | struct bt_notification *notif); | |
166 | ||
262e5473 PP |
167 | static inline |
168 | const char *bt_graph_status_string(enum bt_graph_status status) | |
169 | { | |
170 | switch (status) { | |
171 | case BT_GRAPH_STATUS_CANCELED: | |
172 | return "BT_GRAPH_STATUS_CANCELED"; | |
173 | case BT_GRAPH_STATUS_AGAIN: | |
174 | return "BT_GRAPH_STATUS_AGAIN"; | |
175 | case BT_GRAPH_STATUS_END: | |
176 | return "BT_GRAPH_STATUS_END"; | |
177 | case BT_GRAPH_STATUS_OK: | |
178 | return "BT_GRAPH_STATUS_OK"; | |
262e5473 PP |
179 | case BT_GRAPH_STATUS_INVALID: |
180 | return "BT_GRAPH_STATUS_INVALID"; | |
181 | case BT_GRAPH_STATUS_NO_SINK: | |
182 | return "BT_GRAPH_STATUS_NO_SINK"; | |
183 | case BT_GRAPH_STATUS_ERROR: | |
184 | return "BT_GRAPH_STATUS_ERROR"; | |
a256a42d PP |
185 | case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION: |
186 | return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION"; | |
187 | case BT_GRAPH_STATUS_NOMEM: | |
188 | return "BT_GRAPH_STATUS_NOMEM"; | |
262e5473 PP |
189 | default: |
190 | return "(unknown)"; | |
191 | } | |
192 | } | |
193 | ||
a256a42d PP |
194 | static inline |
195 | enum bt_graph_status bt_graph_status_from_component_status( | |
196 | enum bt_component_status comp_status) | |
197 | { | |
198 | switch (comp_status) { | |
199 | case BT_COMPONENT_STATUS_OK: | |
200 | return BT_GRAPH_STATUS_OK; | |
201 | case BT_COMPONENT_STATUS_END: | |
202 | return BT_GRAPH_STATUS_END; | |
203 | case BT_COMPONENT_STATUS_AGAIN: | |
204 | return BT_GRAPH_STATUS_AGAIN; | |
205 | case BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION: | |
206 | return BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION; | |
207 | case BT_COMPONENT_STATUS_ERROR: | |
208 | return BT_GRAPH_STATUS_ERROR; | |
209 | case BT_COMPONENT_STATUS_UNSUPPORTED: | |
210 | return BT_GRAPH_STATUS_ERROR; | |
211 | case BT_COMPONENT_STATUS_INVALID: | |
212 | return BT_GRAPH_STATUS_INVALID; | |
213 | case BT_COMPONENT_STATUS_NOMEM: | |
214 | return BT_GRAPH_STATUS_NOMEM; | |
215 | case BT_COMPONENT_STATUS_NOT_FOUND: | |
216 | return BT_GRAPH_STATUS_ERROR; | |
217 | default: | |
218 | #ifdef BT_LOGF | |
219 | BT_LOGF("Unknown component status: status=%d", comp_status); | |
220 | #endif | |
221 | abort(); | |
222 | } | |
223 | } | |
224 | ||
abe30a8f | 225 | #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */ |