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