lib: introduce bt_message_iterator_class
[babeltrace.git] / src / lib / graph / iterator.c
index 121e4bd9a8ed4ca70eb4d6971b83d0d9cd97ea27..4dd69c6ae69477c26b54ac35e3c22b2033c79ef0 100644 (file)
@@ -69,6 +69,7 @@
 #include "component-source.h"
 #include "connection.h"
 #include "graph.h"
+#include "message-iterator-class.h"
 #include "message/discarded-items.h"
 #include "message/event.h"
 #include "message/iterator.h"
@@ -169,19 +170,24 @@ void bt_self_component_port_input_message_iterator_try_finalize(
                struct bt_self_component_port_input_message_iterator *iterator)
 {
        uint64_t i;
-       typedef void (*method_t)(void *);
-
-       struct bt_component_class *comp_class = NULL;
-       method_t method = NULL;
+       bool call_user_finalize = true;
 
        BT_ASSERT(iterator);
 
        switch (iterator->state) {
        case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
-               /* Skip user finalization if user initialization failed */
-               BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
-                       "%!+i", iterator);
-               goto end;
+               /*
+                * If this function is called while the iterator is in the
+                * NON_INITIALIZED state, it means the user initialization
+                * method has either not been called, or has failed.  We
+                * therefore don't want to call the user finalization method.
+                * However, the initialization method might have created some
+                * upstream message iterators before failing, so we want to
+                * execute the rest of this function, which unlinks the related
+                * iterators.
+                */
+               call_user_finalize = false;
+               break;
        case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
                /* Already finalized */
                BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
@@ -200,35 +206,49 @@ void bt_self_component_port_input_message_iterator_try_finalize(
        set_self_comp_port_input_msg_iterator_state(iterator,
                BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
        BT_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 *src_comp_cls =
-                       (void *) comp_class;
+       if (call_user_finalize) {
+               typedef void (*method_t)(void *);
+               method_t method = NULL;
+               struct bt_component_class *comp_class =
+                       iterator->upstream_component->class;
+
+               switch (comp_class->type) {
+               case BT_COMPONENT_CLASS_TYPE_SOURCE:
+               {
+                       struct bt_component_class_source *src_comp_cls =
+                               (void *) comp_class;
 
-               method = (method_t) src_comp_cls->methods.msg_iter_finalize;
-               break;
-       }
-       case BT_COMPONENT_CLASS_TYPE_FILTER:
-       {
-               struct bt_component_class_filter *flt_comp_cls =
-                       (void *) comp_class;
+                       method = (method_t) src_comp_cls->msg_iter_cls->methods.finalize;
+                       break;
+               }
+               case BT_COMPONENT_CLASS_TYPE_FILTER:
+               {
+                       struct bt_component_class_filter *flt_comp_cls =
+                               (void *) comp_class;
 
-               method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
-               break;
-       }
-       default:
-               /* Unreachable */
-               bt_common_abort();
-       }
+                       method = (method_t) flt_comp_cls->msg_iter_cls->methods.finalize;
+                       break;
+               }
+               default:
+                       /* Unreachable */
+                       bt_common_abort();
+               }
 
-       if (method) {
-               BT_LIB_LOGD("Calling user's finalization method: %!+i",
-                       iterator);
-               method(iterator);
+               if (method) {
+                       const bt_error *saved_error;
+
+                       saved_error = bt_current_thread_take_error();
+
+                       BT_LIB_LOGD("Calling user's finalization method: %!+i",
+                               iterator);
+                       method(iterator);
+
+                       if (saved_error) {
+                               BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
+                       }
+               }
        }
 
        /* Detach upstream message iterators */
@@ -299,10 +319,7 @@ int create_self_component_input_port_message_iterator(
                struct bt_self_component_port_input *self_port,
                struct bt_self_component_port_input_message_iterator **message_iterator)
 {
-       typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t)(
-                       void *, void *, void *, void *);
-
-       init_method_t init_method = NULL;
+       bt_message_iterator_class_initialize_method init_method = NULL;
        struct bt_self_component_port_input_message_iterator *iterator =
                NULL;
        struct bt_self_component_port_input_message_iterator *downstream_msg_iter =
@@ -390,19 +407,19 @@ int create_self_component_input_port_message_iterator(
 
                iterator->methods.next =
                        (bt_self_component_port_input_message_iterator_next_method)
-                               src_comp_cls->methods.msg_iter_next;
+                               src_comp_cls->msg_iter_cls->methods.next;
                iterator->methods.seek_ns_from_origin =
                        (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
-                               src_comp_cls->methods.msg_iter_seek_ns_from_origin;
+                               src_comp_cls->msg_iter_cls->methods.seek_ns_from_origin;
                iterator->methods.seek_beginning =
                        (bt_self_component_port_input_message_iterator_seek_beginning_method)
-                               src_comp_cls->methods.msg_iter_seek_beginning;
+                               src_comp_cls->msg_iter_cls->methods.seek_beginning;
                iterator->methods.can_seek_ns_from_origin =
                        (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
-                               src_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
+                               src_comp_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
                iterator->methods.can_seek_beginning =
                        (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
-                               src_comp_cls->methods.msg_iter_can_seek_beginning;
+                               src_comp_cls->msg_iter_cls->methods.can_seek_beginning;
                break;
        }
        case BT_COMPONENT_CLASS_TYPE_FILTER:
@@ -412,19 +429,19 @@ int create_self_component_input_port_message_iterator(
 
                iterator->methods.next =
                        (bt_self_component_port_input_message_iterator_next_method)
-                               flt_comp_cls->methods.msg_iter_next;
+                               flt_comp_cls->msg_iter_cls->methods.next;
                iterator->methods.seek_ns_from_origin =
                        (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
-                               flt_comp_cls->methods.msg_iter_seek_ns_from_origin;
+                               flt_comp_cls->msg_iter_cls->methods.seek_ns_from_origin;
                iterator->methods.seek_beginning =
                        (bt_self_component_port_input_message_iterator_seek_beginning_method)
-                               flt_comp_cls->methods.msg_iter_seek_beginning;
+                               flt_comp_cls->msg_iter_cls->methods.seek_beginning;
                iterator->methods.can_seek_ns_from_origin =
                        (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
-                               flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
+                               flt_comp_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
                iterator->methods.can_seek_beginning =
                        (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
-                               flt_comp_cls->methods.msg_iter_can_seek_beginning;
+                               flt_comp_cls->msg_iter_cls->methods.can_seek_beginning;
                break;
        }
        default:
@@ -451,8 +468,7 @@ int create_self_component_input_port_message_iterator(
                struct bt_component_class_source *src_comp_cls =
                        (void *) upstream_comp_cls;
 
-               init_method =
-                       (init_method_t) src_comp_cls->methods.msg_iter_initialize;
+               init_method = src_comp_cls->msg_iter_cls->methods.initialize;
                break;
        }
        case BT_COMPONENT_CLASS_TYPE_FILTER:
@@ -460,8 +476,7 @@ int create_self_component_input_port_message_iterator(
                struct bt_component_class_filter *flt_comp_cls =
                        (void *) upstream_comp_cls;
 
-               init_method =
-                       (init_method_t) flt_comp_cls->methods.msg_iter_initialize;
+               init_method = flt_comp_cls->msg_iter_cls->methods.initialize;
                break;
        }
        default:
@@ -470,13 +485,17 @@ int create_self_component_input_port_message_iterator(
        }
 
        if (init_method) {
-               enum bt_component_class_message_iterator_initialize_method_status iter_status;
+               enum bt_message_iterator_class_initialize_method_status iter_status;
 
                BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
-               iter_status = init_method(iterator, &iterator->config, upstream_comp,
-                       upstream_port);
+               iter_status = init_method(
+                       (struct bt_self_message_iterator *) iterator,
+                       &iterator->config,
+                       (struct bt_self_component *) upstream_comp,
+                       (struct bt_self_component_port_output *) upstream_port);
                BT_LOGD("User method returned: status=%s",
                        bt_common_func_status_string(iter_status));
+               BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status);
                if (iter_status != BT_FUNC_STATUS_OK) {
                        BT_LIB_LOGW_APPEND_CAUSE(
                                "Component input port message iterator initialization method failed: "
@@ -526,6 +545,7 @@ bt_self_component_port_input_message_iterator_create_from_message_iterator(
                struct bt_self_component_port_input *input_port,
                struct bt_self_component_port_input_message_iterator **message_iterator)
 {
+       BT_ASSERT_PRE_NO_ERROR();
        BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
        return create_self_component_input_port_message_iterator(self_msg_iter,
                input_port, message_iterator);
@@ -537,6 +557,7 @@ bt_self_component_port_input_message_iterator_create_from_sink_component(
                struct bt_self_component_port_input *input_port,
                struct bt_self_component_port_input_message_iterator **message_iterator)
 {
+       BT_ASSERT_PRE_NO_ERROR();
        BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component");
        return create_self_component_input_port_message_iterator(NULL,
                input_port, message_iterator);
@@ -643,8 +664,15 @@ bool clock_snapshots_are_monotonic_one(
                goto end;
        }
 
-       clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ns_from_origin);
+       clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(
+               clock_snapshot, &ns_from_origin);
        if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
+               /*
+                * bt_clock_snapshot_get_ns_from_origin can return
+                * OVERFLOW_ERROR.  We don't really want to report an error to
+                * our caller, so just clear it.
+                */
+               bt_current_thread_clear_error();
                goto end;
        }
 
@@ -842,12 +870,12 @@ end:
  */
 
 static
-enum bt_component_class_message_iterator_next_method_status
+enum bt_message_iterator_class_next_method_status
 call_iterator_next_method(
                struct bt_self_component_port_input_message_iterator *iterator,
                bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
 {
-       enum bt_component_class_message_iterator_next_method_status status;
+       enum bt_message_iterator_class_next_method_status status;
 
        BT_ASSERT_DBG(iterator->methods.next);
        BT_LOGD_STR("Calling user's \"next\" method.");
@@ -862,6 +890,8 @@ call_iterator_next_method(
                        "Clock snapshots are not monotonic");
        }
 
+       BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status);
+
        return status;
 }
 
@@ -872,6 +902,7 @@ bt_self_component_port_input_message_iterator_next(
 {
        enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
 
+       BT_ASSERT_PRE_DEV_NO_ERROR();
        BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
        BT_ASSERT_PRE_DEV_NON_NULL(msgs, "Message array (output)");
        BT_ASSERT_PRE_DEV_NON_NULL(user_count, "Message count (output)");
@@ -978,6 +1009,7 @@ bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
 {
        enum bt_message_iterator_can_seek_ns_from_origin_status status;
 
+       BT_ASSERT_PRE_NO_ERROR();
        BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
        BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)");
        BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
@@ -1000,6 +1032,8 @@ bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
                status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
                        ns_from_origin, can_seek);
 
+               BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
+
                if (status != BT_FUNC_STATUS_OK) {
                        BT_LIB_LOGW_APPEND_CAUSE(
                                "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
@@ -1046,6 +1080,7 @@ bt_self_component_port_input_message_iterator_can_seek_beginning(
 {
        enum bt_message_iterator_can_seek_beginning_status status;
 
+       BT_ASSERT_PRE_NO_ERROR();
        BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
        BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)");
        BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
@@ -1070,6 +1105,7 @@ bt_self_component_port_input_message_iterator_can_seek_beginning(
                                *can_seek == BT_FALSE,
                        "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
                        *can_seek, iterator);
+               BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
        } else {
                *can_seek = BT_FALSE;
                status = BT_FUNC_STATUS_OK;
@@ -1137,6 +1173,7 @@ bt_self_component_port_input_message_iterator_seek_beginning(
 {
        int status;
 
+       BT_ASSERT_PRE_NO_ERROR();
        BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
        BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
        BT_ASSERT_PRE(
@@ -1165,6 +1202,7 @@ bt_self_component_port_input_message_iterator_seek_beginning(
                status == BT_FUNC_STATUS_AGAIN,
                "Unexpected status: %![iter-]+i, status=%s",
                iterator, bt_common_func_status_string(status));
+       BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
        if (status < 0) {
                BT_LIB_LOGW_APPEND_CAUSE(
                        "Component input port message iterator's \"seek beginning\" method failed: "
@@ -1631,7 +1669,7 @@ end:
  */
 
 static
-enum bt_component_class_message_iterator_next_method_status post_auto_seek_next(
+enum bt_message_iterator_class_next_method_status post_auto_seek_next(
                struct bt_self_component_port_input_message_iterator *iterator,
                bt_message_array_const msgs, uint64_t capacity,
                uint64_t *count)
@@ -1700,6 +1738,7 @@ bt_self_component_port_input_message_iterator_seek_ns_from_origin(
        GHashTable *stream_states = NULL;
        bt_bool can_seek_by_itself;
 
+       BT_ASSERT_PRE_NO_ERROR();
        BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
        BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
        BT_ASSERT_PRE(
@@ -1723,7 +1762,7 @@ bt_self_component_port_input_message_iterator_seek_ns_from_origin(
 
        /* Check if the iterator can seek by itself.  If not we'll use autoseek. */
        if (iterator->methods.can_seek_ns_from_origin) {
-               bt_component_class_message_iterator_can_seek_ns_from_origin_method_status
+               bt_message_iterator_class_can_seek_ns_from_origin_method_status
                        can_seek_status;
 
                can_seek_status =
@@ -1752,6 +1791,7 @@ bt_self_component_port_input_message_iterator_seek_ns_from_origin(
                        status == BT_FUNC_STATUS_AGAIN,
                        "Unexpected status: %![iter-]+i, status=%s",
                        iterator, bt_common_func_status_string(status));
+               BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
                if (status < 0) {
                        BT_LIB_LOGW_APPEND_CAUSE(
                                "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
@@ -1764,8 +1804,7 @@ bt_self_component_port_input_message_iterator_seek_ns_from_origin(
                 * particular time.  We will seek to the beginning and fast
                 * forward to the right place.
                 */
-               enum bt_component_class_message_iterator_can_seek_beginning_method_status
-                       can_seek_status;
+               enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
                bt_bool can_seek_beginning;
 
                can_seek_status = iterator->methods.can_seek_beginning(iterator,
This page took 0.02883 seconds and 4 git commands to generate.