Commit | Line | Data |
---|---|---|
8b285017 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
8b285017 | 3 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
8b285017 JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
f3034355 PP |
24 | #define BT_LOG_TAG "PORT" |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
c5b9b441 PP |
27 | #include <babeltrace/assert-internal.h> |
28 | #include <babeltrace/assert-pre-internal.h> | |
0d72b8c3 PP |
29 | #include <babeltrace/graph/port-const.h> |
30 | #include <babeltrace/graph/port-input-const.h> | |
31 | #include <babeltrace/graph/port-output-const.h> | |
d94d92ac PP |
32 | #include <babeltrace/graph/self-component-port.h> |
33 | #include <babeltrace/graph/self-component-port-input.h> | |
34 | #include <babeltrace/graph/self-component-port-output.h> | |
b2e0c907 PP |
35 | #include <babeltrace/graph/component-internal.h> |
36 | #include <babeltrace/graph/port-internal.h> | |
37 | #include <babeltrace/graph/connection-internal.h> | |
8b285017 | 38 | #include <babeltrace/object-internal.h> |
3d9990ac | 39 | #include <babeltrace/compiler-internal.h> |
8b285017 JG |
40 | |
41 | static | |
d94d92ac | 42 | void destroy_port(struct bt_object *obj) |
8b285017 | 43 | { |
d94d92ac | 44 | struct bt_port *port = (void *) obj; |
8b285017 | 45 | |
d94d92ac | 46 | BT_LIB_LOGD("Destroying port: %!+p", port); |
f3034355 | 47 | |
8b285017 JG |
48 | if (port->name) { |
49 | g_string_free(port->name, TRUE); | |
d94d92ac | 50 | port->name = NULL; |
8b285017 | 51 | } |
f3034355 | 52 | |
8b285017 JG |
53 | g_free(port); |
54 | } | |
55 | ||
56 | BT_HIDDEN | |
57 | struct bt_port *bt_port_create(struct bt_component *parent_component, | |
3e9b0023 | 58 | enum bt_port_type type, const char *name, void *user_data) |
8b285017 | 59 | { |
72b913fb | 60 | struct bt_port *port = NULL; |
8b285017 | 61 | |
f6ccaed9 PP |
62 | BT_ASSERT(name); |
63 | BT_ASSERT(parent_component); | |
64 | BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); | |
d94d92ac | 65 | BT_ASSERT(strlen(name) > 0); |
8b285017 JG |
66 | port = g_new0(struct bt_port, 1); |
67 | if (!port) { | |
f3034355 | 68 | BT_LOGE_STR("Failed to allocate one port."); |
8b285017 JG |
69 | goto end; |
70 | } | |
71 | ||
d94d92ac PP |
72 | BT_LIB_LOGD("Creating port for component: %![comp-]+c, port-type=%s, " |
73 | "port-name=\"%s\"", parent_component, bt_port_type_string(type), | |
74 | name); | |
75 | bt_object_init_shared_with_parent(&port->base, destroy_port); | |
8b285017 JG |
76 | port->name = g_string_new(name); |
77 | if (!port->name) { | |
f3034355 | 78 | BT_LOGE_STR("Failed to allocate one GString."); |
65300d60 | 79 | BT_OBJECT_PUT_REF_AND_RESET(port); |
8b285017 JG |
80 | goto end; |
81 | } | |
82 | ||
83 | port->type = type; | |
3e9b0023 | 84 | port->user_data = user_data; |
3fea54f6 | 85 | bt_object_set_parent(&port->base, &parent_component->base); |
d94d92ac PP |
86 | BT_LIB_LOGD("Created port for component: " |
87 | "%![comp-]+c, %![port-]+p", parent_component, port); | |
f3034355 | 88 | |
8b285017 JG |
89 | end: |
90 | return port; | |
91 | } | |
92 | ||
0d72b8c3 | 93 | const char *bt_port_get_name(const struct bt_port *port) |
8b285017 | 94 | { |
d94d92ac PP |
95 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
96 | return port->name->str; | |
8b285017 JG |
97 | } |
98 | ||
0d72b8c3 | 99 | enum bt_port_type bt_port_get_type(const struct bt_port *port) |
8b285017 | 100 | { |
d94d92ac PP |
101 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
102 | return port->type; | |
8b285017 JG |
103 | } |
104 | ||
0d72b8c3 PP |
105 | const struct bt_connection *bt_port_borrow_connection_const( |
106 | const struct bt_port *port) | |
8b285017 | 107 | { |
d94d92ac PP |
108 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
109 | return port->connection; | |
8b285017 JG |
110 | } |
111 | ||
0d72b8c3 PP |
112 | const struct bt_component *bt_port_borrow_component_const( |
113 | const struct bt_port *port) | |
890882ef | 114 | { |
d94d92ac | 115 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
0d72b8c3 | 116 | return bt_port_borrow_component_inline(port); |
890882ef PP |
117 | } |
118 | ||
d94d92ac PP |
119 | struct bt_self_component *bt_self_component_port_borrow_component( |
120 | struct bt_self_component_port *port) | |
890882ef | 121 | { |
d94d92ac PP |
122 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
123 | return (void *) bt_object_borrow_parent((void *) port); | |
890882ef PP |
124 | } |
125 | ||
8b285017 | 126 | BT_HIDDEN |
72b913fb | 127 | void bt_port_set_connection(struct bt_port *port, |
8b285017 | 128 | struct bt_connection *connection) |
72b913fb PP |
129 | { |
130 | /* | |
131 | * Don't take a reference on connection as its existence is | |
132 | * guaranteed by the existence of the graph in which the | |
133 | * connection exists. | |
134 | */ | |
135 | port->connection = connection; | |
d94d92ac PP |
136 | BT_LIB_LOGV("Set port's connection: %![port-]+p, %![conn-]+x", port, |
137 | connection); | |
72b913fb PP |
138 | } |
139 | ||
d0fea130 PP |
140 | static inline |
141 | bool port_connection_iterators_are_finalized(struct bt_port *port) | |
142 | { | |
143 | bool ret = true; | |
144 | struct bt_connection *conn = port->connection; | |
145 | uint64_t i; | |
146 | ||
147 | if (!conn) { | |
148 | goto end; | |
149 | } | |
150 | ||
151 | for (i = 0; i < conn->iterators->len; i++) { | |
152 | struct bt_self_component_port_input_message_iterator *iterator = | |
153 | conn->iterators->pdata[i]; | |
154 | ||
155 | BT_ASSERT(iterator); | |
156 | ||
157 | if (iterator->state != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING && | |
158 | iterator->state != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED) { | |
159 | BT_ASSERT_PRE_MSG("Message iterator is not being finalized or finalized: " | |
160 | "%!+i", iterator); | |
161 | ret = false; | |
162 | goto end; | |
163 | } | |
164 | } | |
165 | ||
166 | end: | |
167 | return ret; | |
168 | } | |
169 | ||
d94d92ac PP |
170 | enum bt_self_component_port_status bt_self_component_port_remove_from_component( |
171 | struct bt_self_component_port *self_port) | |
8b285017 | 172 | { |
d94d92ac | 173 | struct bt_port *port = (void *) self_port; |
72b913fb | 174 | struct bt_component *comp = NULL; |
8b285017 | 175 | |
d94d92ac | 176 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
d0fea130 PP |
177 | BT_ASSERT_PRE(port_connection_iterators_are_finalized(port), |
178 | "At least one message iterator using this port has the wrong state."); | |
d94d92ac | 179 | comp = (void *) bt_object_borrow_parent(&port->base); |
7d62a2ab | 180 | if (!comp) { |
d94d92ac PP |
181 | BT_LIB_LOGV("Port already removed from its component: %!+p", |
182 | port); | |
7d62a2ab PP |
183 | goto end; |
184 | } | |
f3034355 PP |
185 | |
186 | /* bt_component_remove_port() logs details */ | |
d94d92ac | 187 | bt_component_remove_port(comp, port); |
72b913fb | 188 | |
8b285017 | 189 | end: |
d94d92ac | 190 | return BT_SELF_PORT_STATUS_OK; |
8b285017 JG |
191 | } |
192 | ||
0d72b8c3 | 193 | bt_bool bt_port_is_connected(const struct bt_port *port) |
8b285017 | 194 | { |
d94d92ac PP |
195 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
196 | return port->connection ? BT_TRUE : BT_FALSE; | |
8b285017 | 197 | } |
a45a0b60 | 198 | |
0d72b8c3 | 199 | void *bt_self_component_port_get_data(const struct bt_self_component_port *port) |
a45a0b60 | 200 | { |
d94d92ac PP |
201 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
202 | return ((struct bt_port *) port)->user_data; | |
a45a0b60 | 203 | } |
c5b9b441 PP |
204 | |
205 | void bt_port_get_ref(const struct bt_port *port) | |
206 | { | |
207 | bt_object_get_ref(port); | |
208 | } | |
209 | ||
210 | void bt_port_put_ref(const struct bt_port *port) | |
211 | { | |
212 | bt_object_put_ref(port); | |
213 | } | |
214 | ||
215 | void bt_port_input_get_ref(const struct bt_port_input *port_input) | |
216 | { | |
217 | bt_object_get_ref(port_input); | |
218 | } | |
219 | ||
220 | void bt_port_input_put_ref(const struct bt_port_input *port_input) | |
221 | { | |
222 | bt_object_put_ref(port_input); | |
223 | } | |
224 | ||
225 | void bt_port_output_get_ref(const struct bt_port_output *port_output) | |
226 | { | |
227 | bt_object_get_ref(port_output); | |
228 | } | |
229 | ||
230 | void bt_port_output_put_ref(const struct bt_port_output *port_output) | |
231 | { | |
232 | bt_object_put_ref(port_output); | |
233 | } |