Commit | Line | Data |
---|---|---|
c7eee084 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
c7eee084 PP |
3 | * |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "QUERY-EXECUTOR" | |
24 | #include <babeltrace/lib-logging-internal.h> | |
25 | ||
c5b9b441 PP |
26 | #include <babeltrace/assert-internal.h> |
27 | #include <babeltrace/assert-pre-internal.h> | |
0d72b8c3 | 28 | #include <babeltrace/graph/query-executor-const.h> |
c7eee084 PP |
29 | #include <babeltrace/graph/query-executor.h> |
30 | #include <babeltrace/graph/query-executor-internal.h> | |
31 | #include <babeltrace/graph/component-class.h> | |
32 | #include <babeltrace/graph/component-class-internal.h> | |
c6bd8523 PP |
33 | #include <babeltrace/value.h> |
34 | #include <babeltrace/value-const.h> | |
c7eee084 PP |
35 | #include <babeltrace/object-internal.h> |
36 | #include <babeltrace/compiler-internal.h> | |
37 | ||
38 | static | |
39 | void bt_query_executor_destroy(struct bt_object *obj) | |
40 | { | |
41 | struct bt_query_executor *query_exec = | |
42 | container_of(obj, struct bt_query_executor, base); | |
43 | ||
d94d92ac | 44 | BT_LOGD("Destroying query executor: addr=%p", query_exec); |
c7eee084 PP |
45 | g_free(query_exec); |
46 | } | |
47 | ||
0d72b8c3 | 48 | struct bt_query_executor *bt_query_executor_create(void) |
c7eee084 PP |
49 | { |
50 | struct bt_query_executor *query_exec; | |
51 | ||
52 | BT_LOGD_STR("Creating query executor."); | |
53 | query_exec = g_new0(struct bt_query_executor, 1); | |
54 | if (!query_exec) { | |
55 | BT_LOGE_STR("Failed to allocate one query executor."); | |
56 | goto end; | |
57 | } | |
58 | ||
3fea54f6 PP |
59 | bt_object_init_shared(&query_exec->base, |
60 | bt_query_executor_destroy); | |
c7eee084 PP |
61 | BT_LOGD("Created query executor: addr=%p", query_exec); |
62 | ||
63 | end: | |
d94d92ac | 64 | return (void *) query_exec; |
c7eee084 PP |
65 | } |
66 | ||
0d72b8c3 PP |
67 | enum bt_query_executor_status bt_query_executor_query( |
68 | struct bt_query_executor *query_exec, | |
69 | const struct bt_component_class *comp_cls, | |
05e21286 PP |
70 | const char *object, const struct bt_value *params, |
71 | const struct bt_value **user_result) | |
c7eee084 | 72 | { |
0d72b8c3 PP |
73 | typedef enum bt_query_status (*method_t)(void *, const void *, |
74 | const void *, const void *, const void *); | |
c7eee084 | 75 | |
d94d92ac | 76 | enum bt_query_status status; |
d72535e6 | 77 | enum bt_query_executor_status exec_status; |
d94d92ac | 78 | method_t method = NULL; |
c7eee084 | 79 | |
d94d92ac PP |
80 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
81 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
82 | BT_ASSERT_PRE_NON_NULL(object, "Object"); | |
83 | BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)"); | |
84 | BT_ASSERT_PRE(!query_exec->canceled, "Query executor is canceled."); | |
85 | ||
86 | if (!params) { | |
87 | params = bt_value_null; | |
c7eee084 PP |
88 | } |
89 | ||
d94d92ac PP |
90 | switch (comp_cls->type) { |
91 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
92 | { | |
93 | struct bt_component_class_source *src_cc = (void *) comp_cls; | |
94 | ||
95 | method = (method_t) src_cc->methods.query; | |
96 | break; | |
c7eee084 | 97 | } |
d94d92ac PP |
98 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
99 | { | |
100 | struct bt_component_class_filter *flt_cc = (void *) comp_cls; | |
c7eee084 | 101 | |
d94d92ac PP |
102 | method = (method_t) flt_cc->methods.query; |
103 | break; | |
c7eee084 | 104 | } |
d94d92ac PP |
105 | case BT_COMPONENT_CLASS_TYPE_SINK: |
106 | { | |
107 | struct bt_component_class_sink *sink_cc = (void *) comp_cls; | |
c7eee084 | 108 | |
d94d92ac PP |
109 | method = (method_t) sink_cc->methods.query; |
110 | break; | |
111 | } | |
112 | default: | |
113 | abort(); | |
c7eee084 PP |
114 | } |
115 | ||
d94d92ac | 116 | if (!method) { |
c7eee084 | 117 | /* Not an error: nothing to query */ |
d94d92ac PP |
118 | BT_LIB_LOGD("Component class has no registered query method: " |
119 | "%!+C", comp_cls); | |
d72535e6 | 120 | exec_status = BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED; |
c7eee084 PP |
121 | goto end; |
122 | } | |
123 | ||
d94d92ac PP |
124 | BT_LIB_LOGD("Calling user's query method: " |
125 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v", | |
126 | query_exec, comp_cls, object, params); | |
127 | *user_result = NULL; | |
0d72b8c3 PP |
128 | status = method((void *) comp_cls, query_exec, object, params, |
129 | user_result); | |
d94d92ac PP |
130 | BT_LIB_LOGD("User method returned: status=%s, %![res-]+v", |
131 | bt_query_status_string(status), *user_result); | |
d94d92ac PP |
132 | BT_ASSERT_PRE(status != BT_QUERY_STATUS_OK || *user_result, |
133 | "User method returned `BT_QUERY_STATUS_OK` without a result."); | |
d72535e6 | 134 | exec_status = (int) status; |
c7eee084 | 135 | if (query_exec->canceled) { |
d94d92ac | 136 | BT_OBJECT_PUT_REF_AND_RESET(*user_result); |
cb9547ee | 137 | exec_status = BT_QUERY_EXECUTOR_STATUS_CANCELED; |
c7eee084 | 138 | goto end; |
c7eee084 PP |
139 | } |
140 | ||
141 | end: | |
d72535e6 | 142 | return exec_status; |
c7eee084 PP |
143 | } |
144 | ||
0d72b8c3 PP |
145 | enum bt_query_executor_status bt_query_executor_cancel( |
146 | struct bt_query_executor *query_exec) | |
c7eee084 | 147 | { |
d94d92ac | 148 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
c7eee084 PP |
149 | query_exec->canceled = BT_TRUE; |
150 | BT_LOGV("Canceled query executor: addr=%p", query_exec); | |
d72535e6 | 151 | return BT_QUERY_EXECUTOR_STATUS_OK; |
c7eee084 PP |
152 | } |
153 | ||
0d72b8c3 | 154 | bt_bool bt_query_executor_is_canceled(const struct bt_query_executor *query_exec) |
c7eee084 | 155 | { |
d94d92ac PP |
156 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
157 | return query_exec->canceled; | |
c7eee084 | 158 | } |
c5b9b441 PP |
159 | |
160 | void bt_query_executor_get_ref(const struct bt_query_executor *query_executor) | |
161 | { | |
162 | bt_object_get_ref(query_executor); | |
163 | } | |
164 | ||
165 | void bt_query_executor_put_ref(const struct bt_query_executor *query_executor) | |
166 | { | |
167 | bt_object_put_ref(query_executor); | |
168 | } |