lib: add interrupter API
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 20 Jul 2019 22:20:05 +0000 (18:20 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 24 Jul 2019 14:17:16 +0000 (10:17 -0400)
This patch adds an interrupter API to the library.

An interrupter is a very simple shared object which you can set and
reset. On the const side, you can get whether or not the interrupter is
set.

The goal of this object is to be used as an async-signal-safe way to
interrupt a graph or a query executor. An interrupter object can
eventually be shared by many message iterators, interrupting all of them
at once when setting the interrupter. Moreover, a single message
iterator can have many interrupters, making it possible to interrupt one
or more specific message iterators from different interruption sources
(signal, graphical user interface input, thread-specific, etc.).

The user code is still responsible for checking the message iterator's
or sink component's interrupter's state at regular interval when
performing "long" operations.

You can create an interrupter object with bt_interrupter_create(). The
interrupter object is not set at creation time. You can set it with
bt_interrupter_set() and reset it with bt_interrupter_reset().

You can get whether or not the interrupter is set with
bt_interrupter_is_set().

This patch only adds the API: interrupter objects are not used anywhere
yet.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I610d48b70412d752b34b8d5760adf99a68d20704
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1733

CONTRIBUTING.adoc
include/Makefile.am
include/babeltrace2/babeltrace.h
include/babeltrace2/graph/interrupter-const.h [new file with mode: 0644]
include/babeltrace2/graph/interrupter.h [new file with mode: 0644]
include/babeltrace2/types.h
src/lib/graph/Makefile.am
src/lib/graph/interrupter.c [new file with mode: 0644]
src/lib/graph/interrupter.h [new file with mode: 0644]
src/lib/lib-logging.c

index 81e964f04311c335a4887d9a0f35f17e4040ccc4..46984b34ad1ab0b0079b682073ff60d3cf45b6fa 100644 (file)
@@ -713,6 +713,10 @@ The available format specifiers are:
 |Graph
 |`+const struct bt_graph *+`
 
+|`z`
+|Interrupter
+|`+struct bt_interrupter *+`
+
 |`l`
 |Plugin
 |`+const struct bt_plugin *+`
index f04512201d40db75b5845a81ef9cbdcf84661dbb..4baed59b982e60b8c17cad0248472b93fa96886c 100644 (file)
@@ -87,6 +87,8 @@ babeltrace2graphinclude_HEADERS = \
        babeltrace2/graph/connection-const.h \
        babeltrace2/graph/graph-const.h \
        babeltrace2/graph/graph.h \
+       babeltrace2/graph/interrupter-const.h \
+       babeltrace2/graph/interrupter.h \
        babeltrace2/graph/message-const.h \
        babeltrace2/graph/message-discarded-events-const.h \
        babeltrace2/graph/message-discarded-events.h \
index 62a75bf5310d4de88c87170d74bfb7cf64322c15..1be659069912eb7b01d772f75711435fa9a1dce9 100644 (file)
 #include <babeltrace2/graph/connection-const.h>
 #include <babeltrace2/graph/graph-const.h>
 #include <babeltrace2/graph/graph.h>
+#include <babeltrace2/graph/interrupter-const.h>
+#include <babeltrace2/graph/interrupter.h>
 #include <babeltrace2/graph/port-const.h>
 #include <babeltrace2/graph/port-input-const.h>
 #include <babeltrace2/graph/port-output-const.h>
diff --git a/include/babeltrace2/graph/interrupter-const.h b/include/babeltrace2/graph/interrupter-const.h
new file mode 100644 (file)
index 0000000..8a85dcc
--- /dev/null
@@ -0,0 +1,59 @@
+#ifndef BABELTRACE2_GRAPH_INTERRUPTER_CONST_H
+#define BABELTRACE2_GRAPH_INTERRUPTER_CONST_H
+
+/*
+ * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __BT_IN_BABELTRACE_H
+# error "Please include <babeltrace2/babeltrace.h> instead."
+#endif
+
+#include <babeltrace2/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern bt_bool bt_interrupter_is_set(const bt_interrupter *interrupter);
+
+extern void bt_interrupter_get_ref(const bt_interrupter *interrupter);
+
+extern void bt_interrupter_put_ref(const bt_interrupter *interrupter);
+
+#define BT_INTERRUPTER_PUT_REF_AND_RESET(_var)         \
+       do {                                            \
+               bt_interrupter_put_ref(_var);           \
+               (_var) = NULL;                          \
+       } while (0)
+
+#define BT_INTERRUPTER_MOVE_REF(_var_dst, _var_src)    \
+       do {                                            \
+               bt_interrupter_put_ref(_var_dst);       \
+               (_var_dst) = (_var_src);                \
+               (_var_src) = NULL;                      \
+       } while (0)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE2_GRAPH_INTERRUPTER_CONST_H */
diff --git a/include/babeltrace2/graph/interrupter.h b/include/babeltrace2/graph/interrupter.h
new file mode 100644 (file)
index 0000000..9537753
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef BABELTRACE2_GRAPH_INTERRUPTER_H
+#define BABELTRACE2_GRAPH_INTERRUPTER_H
+
+/*
+ * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __BT_IN_BABELTRACE_H
+# error "Please include <babeltrace2/babeltrace.h> instead."
+#endif
+
+#include <babeltrace2/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern bt_interrupter *bt_interrupter_create(void);
+
+extern void bt_interrupter_set(bt_interrupter *interrupter);
+
+extern void bt_interrupter_reset(bt_interrupter *interrupter);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE2_GRAPH_INTERRUPTER_H */
index afdbd454a16e4e5488016bd527c2f0f7731c4c08..5b49f50cbb9f700d79f46328028f2e77a8c593f1 100644 (file)
@@ -100,8 +100,8 @@ typedef struct bt_field bt_field;
 typedef struct bt_field_class bt_field_class;
 typedef struct bt_field_class_enumeration_mapping bt_field_class_enumeration_mapping;
 typedef struct bt_field_class_enumeration_signed_mapping bt_field_class_enumeration_signed_mapping;
-typedef struct bt_field_class_structure_member bt_field_class_structure_member;
 typedef struct bt_field_class_enumeration_unsigned_mapping bt_field_class_enumeration_unsigned_mapping;
+typedef struct bt_field_class_structure_member bt_field_class_structure_member;
 typedef struct bt_field_class_variant_option bt_field_class_variant_option;
 typedef struct bt_field_class_variant_with_selector_signed_option bt_field_class_variant_with_selector_signed_option;
 typedef struct bt_field_class_variant_with_selector_unsigned_option bt_field_class_variant_with_selector_unsigned_option;
@@ -113,6 +113,7 @@ typedef struct bt_integer_range_set_signed bt_integer_range_set_signed;
 typedef struct bt_integer_range_set_unsigned bt_integer_range_set_unsigned;
 typedef struct bt_integer_range_signed bt_integer_range_signed;
 typedef struct bt_integer_range_unsigned bt_integer_range_unsigned;
+typedef struct bt_interrupter bt_interrupter;
 typedef struct bt_message bt_message;
 typedef struct bt_message_iterator bt_message_iterator;
 typedef struct bt_object bt_object;
index a7376fb26ab45e57acfcbeb0cda4094c10af0de1..a7a9c03720106ff1c4546836e0f7f54e76d3d98c 100644 (file)
@@ -20,6 +20,8 @@ libgraph_la_SOURCES = \
        connection.h \
        graph.c \
        graph.h \
+       interrupter.c \
+       interrupter.h \
        iterator.c \
        port.c \
        port.h \
diff --git a/src/lib/graph/interrupter.c b/src/lib/graph/interrupter.c
new file mode 100644 (file)
index 0000000..2a066dd
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define BT_LOG_TAG "LIB/INTERRUPTER"
+#include "lib/logging.h"
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <babeltrace2/babeltrace.h>
+
+#include "interrupter.h"
+#include "lib/assert-pre.h"
+
+static
+void destroy_interrupter(struct bt_object *obj)
+{
+       g_free(obj);
+}
+
+extern struct bt_interrupter *bt_interrupter_create(void)
+{
+       struct bt_interrupter *intr = g_new0(struct bt_interrupter, 1);
+
+       if (!intr) {
+               BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one interrupter.");
+               goto error;
+       }
+
+       bt_object_init_shared(&intr->base, destroy_interrupter);
+       goto end;
+
+error:
+       BT_OBJECT_PUT_REF_AND_RESET(intr);
+
+end:
+       return intr;
+}
+
+void bt_interrupter_set(struct bt_interrupter *intr)
+{
+       BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
+       intr->is_set = true;
+}
+
+void bt_interrupter_reset(struct bt_interrupter *intr)
+{
+       BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
+       intr->is_set = false;
+}
+
+bt_bool bt_interrupter_is_set(const struct bt_interrupter *intr)
+{
+       BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
+       return (bt_bool) intr->is_set;
+}
+
+void bt_interrupter_get_ref(const struct bt_interrupter *intr)
+{
+       bt_object_get_ref(intr);
+}
+
+void bt_interrupter_put_ref(const struct bt_interrupter *intr)
+{
+       bt_object_put_ref(intr);
+}
diff --git a/src/lib/graph/interrupter.h b/src/lib/graph/interrupter.h
new file mode 100644 (file)
index 0000000..46fd153
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef BABELTRACE_GRAPH_INTERRUPTER_INTERNAL_H
+#define BABELTRACE_GRAPH_INTERRUPTER_INTERNAL_H
+
+/*
+ * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <glib.h>
+#include <babeltrace2/babeltrace.h>
+
+#include "lib/object.h"
+
+struct bt_interrupter {
+       struct bt_object base;
+       bool is_set;
+};
+
+#endif /* BABELTRACE_GRAPH_INTERRUPTER_INTERNAL_H */
index 33f9d2f5bd2d6d3a11bc7fe5112bac7069ff9059..91db4a31e4cc43fc504c92243db310cff12e6feb 100644 (file)
@@ -41,6 +41,7 @@
 #include "value.h"
 #include "integer-range-set.h"
 #include "object-pool.h"
+#include "graph/interrupter.h"
 #include "graph/component-class.h"
 #include "graph/component-class-sink-colander.h"
 #include "graph/component-filter.h"
@@ -807,6 +808,12 @@ static inline void format_clock_snapshot(char **buf_ch, bool extended,
        }
 }
 
+static inline void format_interrupter(char **buf_ch, bool extended,
+               const char *prefix, const struct bt_interrupter *intr)
+{
+       BUF_APPEND(", %sis-set=%d", PRFIELD(intr->is_set));
+}
+
 static inline void format_value(char **buf_ch, bool extended,
                const char *prefix, const struct bt_value *value)
 {
@@ -1446,6 +1453,9 @@ static inline void handle_conversion_specifier_bt(void *priv_data,
        case 'g':
                format_graph(buf_ch, extended, prefix, obj);
                break;
+       case 'z':
+               format_interrupter(buf_ch, extended, prefix, obj);
+               break;
        case 'o':
                format_object_pool(buf_ch, extended, prefix, obj);
                break;
This page took 0.030655 seconds and 4 git commands to generate.