Rename bt_ctf_X -> bt_X, maintain backward compat. for pre-2.0 CTF writer
[babeltrace.git] / include / babeltrace / graph / notification-iterator-internal.h
CommitLineData
33b34c43
PP
1#ifndef BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H
2#define BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H
47e5a032
JG
3
4/*
5 * BabelTrace - Notification Iterator Internal
6 *
7 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 8 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/babeltrace-internal.h>
890882ef 30#include <babeltrace/object-internal.h>
fb2dcc52 31#include <babeltrace/ref-internal.h>
bd14d768 32#include <babeltrace/graph/connection.h>
b2e0c907
PP
33#include <babeltrace/graph/notification.h>
34#include <babeltrace/graph/notification-iterator.h>
90157d89 35#include <babeltrace/graph/private-connection-private-notification-iterator.h>
c55a9f58 36#include <babeltrace/types.h>
088d4023 37#include <stdbool.h>
8ed535b5 38#include <assert.h>
47e5a032 39
3230ee6b 40struct bt_port;
8ed535b5 41struct bt_graph;
3230ee6b 42
90157d89
PP
43enum bt_notification_iterator_type {
44 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
8ed535b5 45 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
fa054faf
PP
46};
47
90157d89
PP
48enum bt_private_connection_notification_iterator_notif_type {
49 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT = (1U << 0),
50 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY = (1U << 1),
51 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN = (1U << 2),
52 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END = (1U << 3),
53 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN = (1U << 4),
54 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END = (1U << 5),
55 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS = (1U << 6),
56 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS = (1U << 7),
57};
58
59enum bt_private_connection_notification_iterator_state {
088d4023 60 /* Iterator is not initialized. */
90157d89 61 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED,
088d4023 62
bd14d768 63 /* Iterator is active, not at the end yet, and not finalized. */
90157d89 64 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE,
bd14d768
PP
65
66 /*
67 * Iterator is ended, not finalized yet: the "next" method
68 * returns BT_NOTIFICATION_ITERATOR_STATUS_END.
69 */
90157d89 70 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED,
bd14d768
PP
71
72 /*
73 * Iterator is finalized, but not at the end yet. This means
74 * that the "next" method can still return queued notifications
75 * before returning the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
76 * status.
77 */
90157d89 78 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED,
bd14d768
PP
79
80 /*
81 * Iterator is finalized and ended: the "next" method always
82 * returns BT_NOTIFICATION_ITERATOR_STATUS_CANCELED.
83 */
90157d89 84 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED,
bd14d768
PP
85};
86
47e5a032 87struct bt_notification_iterator {
b8a06801 88 struct bt_object base;
90157d89 89 enum bt_notification_iterator_type type;
8ed535b5 90 struct bt_notification *current_notification; /* owned by this */
90157d89
PP
91};
92
93struct bt_notification_iterator_private_connection {
94 struct bt_notification_iterator base;
bd14d768
PP
95 struct bt_component *upstream_component; /* Weak */
96 struct bt_port *upstream_port; /* Weak */
97 struct bt_connection *connection; /* Weak */
3230ee6b
PP
98 GQueue *queue; /* struct bt_notification * (owned by this) */
99
100 /*
101 * This hash table keeps the state of a stream as viewed by
102 * this notification iterator. This is used to:
103 *
104 * * Automatically enqueue "stream begin", "packet begin",
105 * "packet end", and "stream end" notifications depending
106 * on the stream's state and on the next notification returned
107 * by the upstream component.
108 *
109 * * Make sure that, once the notification iterator has seen
110 * a "stream end" notification for a given stream, that no
111 * other notifications which refer to this stream can be
112 * delivered by this iterator.
113 *
50842bdc 114 * The key (struct bt_stream *) is not owned by this. The
3230ee6b
PP
115 * value is an allocated state structure.
116 */
117 GHashTable *stream_states;
118
119 /*
120 * This is an array of actions which can be rolled back. It's
121 * similar to the memento pattern, but it's not exactly that. It
122 * is allocated once and reset for each notification to process.
123 * More details near the implementation.
124 */
125 GArray *actions;
126
fa054faf
PP
127 /*
128 * This is a mask of notifications to which the user of this
129 * iterator is subscribed
90157d89
PP
130 * (see enum bt_private_connection_notification_iterator_notif_type
131 * above).
fa054faf
PP
132 */
133 uint32_t subscription_mask;
134
90157d89 135 enum bt_private_connection_notification_iterator_state state;
47e5a032
JG
136 void *user_data;
137};
138
8ed535b5
PP
139struct bt_notification_iterator_output_port {
140 struct bt_notification_iterator base;
141 struct bt_graph *graph; /* Owned by this */
142 struct bt_component *colander; /* Owned by this */
143 struct bt_port *output_port; /* Owned by this */
144};
145
146static inline
147struct bt_notification *bt_notification_iterator_borrow_current_notification(
148 struct bt_notification_iterator *iterator)
149{
150 assert(iterator);
151 return iterator->current_notification;
152}
153
154static inline
155void bt_notification_iterator_replace_current_notification(
156 struct bt_notification_iterator *iterator,
157 struct bt_notification *notification)
158{
159 assert(iterator);
160 bt_put(iterator->current_notification);
161 iterator->current_notification = bt_get(notification);
162}
163
890882ef 164static inline
90157d89 165struct bt_notification_iterator_private_connection *
6d137876 166bt_private_connection_notification_iterator_borrow_from_private(
90157d89 167 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
890882ef
PP
168{
169 return (void *) private_notification_iterator;
170}
171
172static inline
90157d89
PP
173struct bt_private_connection_private_notification_iterator *
174bt_private_connection_private_notification_iterator_from_notification_iterator(
175 struct bt_notification_iterator_private_connection *iterator)
890882ef 176{
90157d89 177 return (void *) iterator;
890882ef
PP
178}
179
47e5a032 180BT_HIDDEN
90157d89 181enum bt_connection_status bt_private_connection_notification_iterator_create(
73d5c1ad 182 struct bt_component *upstream_comp,
fa054faf 183 struct bt_port *upstream_port,
bd14d768 184 const enum bt_notification_type *notification_types,
73d5c1ad 185 struct bt_connection *connection,
90157d89 186 struct bt_notification_iterator_private_connection **iterator);
47e5a032 187
bd14d768 188BT_HIDDEN
90157d89
PP
189void bt_private_connection_notification_iterator_finalize(
190 struct bt_notification_iterator_private_connection *iterator);
bd14d768
PP
191
192BT_HIDDEN
90157d89
PP
193void bt_private_connection_notification_iterator_set_connection(
194 struct bt_notification_iterator_private_connection *iterator,
bd14d768
PP
195 struct bt_connection *connection);
196
a36bfb16
PP
197static inline
198const char *bt_notification_iterator_status_string(
199 enum bt_notification_iterator_status status)
200{
201 switch (status) {
202 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
203 return "BT_NOTIFICATION_ITERATOR_STATUS_CANCELED";
204 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
205 return "BT_NOTIFICATION_ITERATOR_STATUS_AGAIN";
206 case BT_NOTIFICATION_ITERATOR_STATUS_END:
207 return "BT_NOTIFICATION_ITERATOR_STATUS_END";
208 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
209 return "BT_NOTIFICATION_ITERATOR_STATUS_OK";
210 case BT_NOTIFICATION_ITERATOR_STATUS_INVALID:
211 return "BT_NOTIFICATION_ITERATOR_STATUS_INVALID";
212 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
213 return "BT_NOTIFICATION_ITERATOR_STATUS_ERROR";
214 case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM:
215 return "BT_NOTIFICATION_ITERATOR_STATUS_NOMEM";
216 case BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED:
217 return "BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED";
218 default:
219 return "(unknown)";
220 }
221};
222
8f9d7550 223static inline
90157d89
PP
224const char *bt_private_connection_notification_iterator_state_string(
225 enum bt_private_connection_notification_iterator_state state)
8f9d7550
PP
226{
227 switch (state) {
90157d89
PP
228 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE:
229 return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE";
230 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED:
231 return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED";
232 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
233 return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED";
234 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
235 return "BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED";
8f9d7550
PP
236 default:
237 return "(unknown)";
238 }
239};
240
33b34c43 241#endif /* BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H */
This page took 0.050345 seconds and 4 git commands to generate.