struct bt_port *downstream_port;
/* Upstream port. */
struct bt_port *upstream_port;
+
+ /*
+ * Weak references to all the notification iterators that were
+ * created on this connection.
+ */
+ GPtrArray *iterators;
};
static inline
BT_HIDDEN
void bt_connection_disconnect_ports(struct bt_connection *conn);
+BT_HIDDEN
+void bt_connection_remove_iterator(struct bt_connection *conn,
+ struct bt_notification_iterator *iterator);
+
#endif /* BABELTRACE_COMPONENT_CONNECTION_INTERNAL_H */
#include <babeltrace/babeltrace-internal.h>
#include <babeltrace/object-internal.h>
#include <babeltrace/ref-internal.h>
+#include <babeltrace/graph/connection.h>
#include <babeltrace/graph/notification.h>
#include <babeltrace/graph/notification-iterator.h>
#include <babeltrace/graph/private-notification-iterator.h>
BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END = (1U << 5),
};
+enum bt_notification_iterator_state {
+ /* Iterator is active, not at the end yet, and not finalized. */
+ BT_NOTIFICATION_ITERATOR_STATE_ACTIVE,
+
+ /*
+ * Iterator is ended, not finalized yet: the "next" method
+ * returns BT_NOTIFICATION_ITERATOR_STATUS_END.
+ */
+ BT_NOTIFICATION_ITERATOR_STATE_ENDED,
+
+ /*
+ * Iterator is finalized, but not at the end yet. This means
+ * that the "next" method can still return queued notifications
+ * before returning the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
+ * status.
+ */
+ BT_NOTIFICATION_ITERATOR_STATE_FINALIZED,
+
+ /*
+ * Iterator is finalized and ended: the "next" method always
+ * returns BT_NOTIFICATION_ITERATOR_STATUS_CANCELED.
+ */
+ BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED,
+};
+
struct bt_notification_iterator {
struct bt_object base;
- struct bt_component *upstream_component; /* owned by this */
- struct bt_port *upstream_port; /* owned by this */
+ struct bt_component *upstream_component; /* Weak */
+ struct bt_port *upstream_port; /* Weak */
+ struct bt_connection *connection; /* Weak */
struct bt_notification *current_notification; /* owned by this */
GQueue *queue; /* struct bt_notification * (owned by this) */
*/
uint32_t subscription_mask;
- bt_bool is_ended;
+ enum bt_notification_iterator_state state;
void *user_data;
};
struct bt_notification_iterator *bt_notification_iterator_create(
struct bt_component *upstream_component,
struct bt_port *upstream_port,
- const enum bt_notification_type *notification_types);
+ const enum bt_notification_type *notification_types,
+ struct bt_connection *connection);
/**
* Validate a notification iterator.
enum bt_notification_iterator_status bt_notification_iterator_validate(
struct bt_notification_iterator *iterator);
+BT_HIDDEN
+void bt_notification_iterator_finalize(
+ struct bt_notification_iterator *iterator);
+
+BT_HIDDEN
+void bt_notification_iterator_set_connection(
+ struct bt_notification_iterator *iterator,
+ struct bt_connection *connection);
+
#endif /* BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H */
* Status code. Errors are always negative.
*/
enum bt_notification_iterator_status {
+ /** Canceled. */
+ BT_NOTIFICATION_ITERATOR_STATUS_CANCELED = 125,
/** No notifications available for now. Try again later. */
BT_NOTIFICATION_ITERATOR_STATUS_AGAIN = 11,
/** No more notifications to be delivered. */
return;
}
+ /*
+ * The component's reference count is 0 if we're here. Increment
+ * it to avoid a double-destroy (possibly infinitely recursive).
+ * This could happen for example if the component's finalization
+ * function does bt_get() (or anything that causes bt_get() to
+ * be called) on itself (ref. count goes from 0 to 1), and then
+ * bt_put(): the reference count would go from 1 to 0 again and
+ * this function would be called again.
+ */
+ obj->ref_count.count++;
component = container_of(obj, struct bt_component, base);
/* Call destroy listeners in reverse registration order */
{
struct bt_connection *connection = container_of(obj,
struct bt_connection, base);
+ size_t i;
+
+ /*
+ * Make sure that each notification iterator which was created
+ * for this connection is finalized before we destroy it. Once a
+ * notification iterator is finalized, all its method return
+ * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status.
+ *
+ * Because connections are destroyed before components within a
+ * graph, this ensures that notification iterators are always
+ * finalized before their upstream component.
+ */
+ if (connection->iterators) {
+ for (i = 0; i < connection->iterators->len; i++) {
+ struct bt_notification_iterator *iterator =
+ g_ptr_array_index(connection->iterators, i);
+
+ bt_notification_iterator_finalize(iterator);
+
+ /*
+ * Make sure this iterator does not try to
+ * remove itself from this connection's
+ * iterators on destruction because this
+ * connection won't exist anymore.
+ */
+ bt_notification_iterator_set_connection(iterator,
+ NULL);
+ }
+
+ g_ptr_array_free(connection->iterators, TRUE);
+ }
/*
* No bt_put on ports as a connection only holds _weak_ references
}
bt_object_init(connection, bt_connection_destroy);
+ connection->iterators = g_ptr_array_new();
+ if (!connection->iterators) {
+ BT_PUT(connection);
+ goto end;
+ }
+
/* Weak references are taken, see comment in header. */
connection->upstream_port = upstream_port;
connection->downstream_port = downstream_port;
}
connection = bt_connection_from_private(private_connection);
-
if (!connection->upstream_port || !connection->downstream_port) {
goto error;
}
}
iterator = bt_notification_iterator_create(upstream_component,
- upstream_port, notification_types);
+ upstream_port, notification_types, connection);
if (!iterator) {
goto error;
}
goto error;
}
+ g_ptr_array_add(connection->iterators, iterator);
goto end;
error:
bt_put(upstream_component);
return iterator;
}
+
+BT_HIDDEN
+void bt_connection_remove_iterator(struct bt_connection *conn,
+ struct bt_notification_iterator *iterator)
+{
+ g_ptr_array_remove(conn->iterators, iterator);
+}
struct bt_graph *graph = container_of(obj,
struct bt_graph, base);
- if (graph->components) {
- g_ptr_array_free(graph->components, TRUE);
- }
+ /*
+ * The graph's reference count is 0 if we're here. Increment
+ * it to avoid a double-destroy (possibly infinitely recursive)
+ * in this situation:
+ *
+ * 1. We put and destroy a connection.
+ * 2. This connection's destructor finalizes its active
+ * notification iterators.
+ * 3. A notification iterator's finalization function gets a
+ * new reference on its component (reference count goes from
+ * 0 to 1).
+ * 4. Since this component's reference count goes to 1, it takes
+ * a reference on its parent (this graph). This graph's
+ * reference count goes from 0 to 1.
+ * 5. The notification iterator's finalization function puts its
+ * component reference (reference count goes from 1 to 0).
+ * 6. Since this component's reference count goes from 1 to 0,
+ * it puts its parent (this graph). This graph's reference
+ * count goes from 1 to 0.
+ * 7. Since this graph's reference count goes from 1 to 0,
+ * its destructor is called (this function).
+ *
+ * With the incrementation below, the graph's reference count at
+ * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
+ * ensures that this function is not called two times.
+ */
+ obj->ref_count.count++;
+
if (graph->connections) {
g_ptr_array_free(graph->connections, TRUE);
}
+ if (graph->components) {
+ g_ptr_array_free(graph->components, TRUE);
+ }
if (graph->sinks_to_consume) {
g_queue_free(graph->sinks_to_consume);
}
#include <babeltrace/ctf-ir/event-internal.h>
#include <babeltrace/ctf-ir/packet-internal.h>
#include <babeltrace/ctf-ir/stream-internal.h>
+#include <babeltrace/graph/connection-internal.h>
#include <babeltrace/graph/component.h>
#include <babeltrace/graph/component-source-internal.h>
#include <babeltrace/graph/component-class-internal.h>
void bt_notification_iterator_destroy(struct bt_object *obj)
{
struct bt_notification_iterator *iterator;
- struct bt_component_class *comp_class;
assert(obj);
- iterator = container_of(obj, struct bt_notification_iterator,
- base);
- assert(iterator->upstream_component);
- comp_class = iterator->upstream_component->class;
-
- /* Call user-defined destroy method */
- switch (comp_class->type) {
- case BT_COMPONENT_CLASS_TYPE_SOURCE:
- {
- struct bt_component_class_source *source_class;
-
- source_class = container_of(comp_class, struct bt_component_class_source, parent);
-
- if (source_class->methods.iterator.finalize) {
- source_class->methods.iterator.finalize(
- bt_private_notification_iterator_from_notification_iterator(iterator));
- }
- break;
- }
- case BT_COMPONENT_CLASS_TYPE_FILTER:
- {
- struct bt_component_class_filter *filter_class;
- filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
-
- if (filter_class->methods.iterator.finalize) {
- filter_class->methods.iterator.finalize(
- bt_private_notification_iterator_from_notification_iterator(iterator));
- }
- break;
- }
- default:
- /* Unreachable */
- assert(0);
- }
+ /*
+ * The notification iterator's reference count is 0 if we're
+ * here. Increment it to avoid a double-destroy (possibly
+ * infinitely recursive). This could happen for example if the
+ * notification iterator's finalization function does bt_get()
+ * (or anything that causes bt_get() to be called) on itself
+ * (ref. count goes from 0 to 1), and then bt_put(): the
+ * reference count would go from 1 to 0 again and this function
+ * would be called again.
+ */
+ obj->ref_count.count++;
+ iterator = container_of(obj, struct bt_notification_iterator, base);
+ bt_notification_iterator_finalize(iterator);
if (iterator->queue) {
struct bt_notification *notif;
g_array_free(iterator->actions, TRUE);
}
+ if (iterator->connection) {
+ /*
+ * Remove ourself from the originating connection so
+ * that it does not try to finalize a dangling pointer
+ * later.
+ */
+ bt_connection_remove_iterator(iterator->connection, iterator);
+ }
+
bt_put(iterator->current_notification);
- bt_put(iterator->upstream_component);
- bt_put(iterator->upstream_port);
g_free(iterator);
}
+BT_HIDDEN
+void bt_notification_iterator_finalize(
+ struct bt_notification_iterator *iterator)
+{
+ struct bt_component_class *comp_class = NULL;
+ bt_component_class_notification_iterator_finalize_method
+ finalize_method = NULL;
+
+ assert(iterator);
+
+ switch (iterator->state) {
+ case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED:
+ case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
+ /* Already finalized */
+ return;
+ default:
+ break;
+ }
+
+ assert(iterator->upstream_component);
+ comp_class = iterator->upstream_component->class;
+
+ /* Call user-defined destroy method */
+ switch (comp_class->type) {
+ case BT_COMPONENT_CLASS_TYPE_SOURCE:
+ {
+ struct bt_component_class_source *source_class;
+
+ source_class = container_of(comp_class, struct bt_component_class_source, parent);
+ finalize_method = source_class->methods.iterator.finalize;
+ break;
+ }
+ case BT_COMPONENT_CLASS_TYPE_FILTER:
+ {
+ struct bt_component_class_filter *filter_class;
+
+ filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
+ finalize_method = filter_class->methods.iterator.finalize;
+ break;
+ }
+ default:
+ /* Unreachable */
+ assert(0);
+ }
+
+ if (finalize_method) {
+ finalize_method(
+ bt_private_notification_iterator_from_notification_iterator(iterator));
+ }
+
+ if (iterator->state == BT_NOTIFICATION_ITERATOR_STATE_ENDED) {
+ iterator->state = BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
+ } else {
+ iterator->state = BT_NOTIFICATION_ITERATOR_STATE_FINALIZED;
+ }
+
+ iterator->upstream_component = NULL;
+ iterator->upstream_port = NULL;
+}
+
+BT_HIDDEN
+void bt_notification_iterator_set_connection(
+ struct bt_notification_iterator *iterator,
+ struct bt_connection *connection)
+{
+ assert(iterator);
+ iterator->connection = connection;
+}
+
static
int create_subscription_mask_from_notification_types(
struct bt_notification_iterator *iterator,
struct bt_notification_iterator *bt_notification_iterator_create(
struct bt_component *upstream_comp,
struct bt_port *upstream_port,
- const enum bt_notification_type *notification_types)
+ const enum bt_notification_type *notification_types,
+ struct bt_connection *connection)
{
enum bt_component_class_type type;
struct bt_notification_iterator *iterator = NULL;
goto error;
}
- iterator->upstream_component = bt_get(upstream_comp);
- iterator->upstream_port = bt_get(upstream_port);
+ iterator->upstream_component = upstream_comp;
+ iterator->upstream_port = upstream_port;
+ iterator->connection = connection;
+ iterator->state = BT_NOTIFICATION_ITERATOR_STATE_ACTIVE;
goto end;
error:
goto end;
}
- if (iterator->is_ended) {
+ switch (iterator->state) {
+ case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
+ status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
+ goto end;
+ case BT_NOTIFICATION_ITERATOR_STATE_ENDED:
status = BT_NOTIFICATION_ITERATOR_STATUS_END;
goto end;
+ default:
+ break;
}
assert(iterator->upstream_component);
goto end;
}
- if (iterator->queue->length == 0) {
- status = BT_NOTIFICATION_ITERATOR_STATUS_END;
- }
+ if (iterator->state == BT_NOTIFICATION_ITERATOR_STATE_FINALIZED) {
+ iterator->state =
+ BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
- iterator->is_ended = BT_TRUE;
+ if (iterator->queue->length == 0) {
+ status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
+ }
+ } else {
+ iterator->state =
+ BT_NOTIFICATION_ITERATOR_STATE_ENDED;
+
+ if (iterator->queue->length == 0) {
+ status = BT_NOTIFICATION_ITERATOR_STATUS_END;
+ }
+ }
goto end;
case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;