From d5947bbb593b65bf05861527759a12808cd2c8c6 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Thu, 11 Dec 2025 11:57:53 +0100 Subject: [PATCH 01/14] Add GArrowThreadPool --- c_glib/arrow-glib/arrow-glib.h | 1 + c_glib/arrow-glib/meson.build | 3 + c_glib/arrow-glib/thread-pool.cpp | 102 ++++++++++++++++++++++++++++++ c_glib/arrow-glib/thread-pool.h | 41 ++++++++++++ c_glib/arrow-glib/thread-pool.hpp | 27 ++++++++ 5 files changed, 174 insertions(+) create mode 100644 c_glib/arrow-glib/thread-pool.cpp create mode 100644 c_glib/arrow-glib/thread-pool.h create mode 100644 c_glib/arrow-glib/thread-pool.hpp diff --git a/c_glib/arrow-glib/arrow-glib.h b/c_glib/arrow-glib/arrow-glib.h index 272b6ba1dae1..7db1bb4be888 100644 --- a/c_glib/arrow-glib/arrow-glib.h +++ b/c_glib/arrow-glib/arrow-glib.h @@ -43,6 +43,7 @@ #include #include #include +#include #include #include diff --git a/c_glib/arrow-glib/meson.build b/c_glib/arrow-glib/meson.build index b755ffb56ac9..ced550b80932 100644 --- a/c_glib/arrow-glib/meson.build +++ b/c_glib/arrow-glib/meson.build @@ -40,6 +40,7 @@ sources = files( 'table-builder.cpp', 'table.cpp', 'tensor.cpp', + 'thread-pool.cpp', 'timestamp-parser.cpp', 'type.cpp', ) @@ -97,6 +98,7 @@ c_headers = files( 'table-builder.h', 'table.h', 'tensor.h', + 'thread-pool.h', 'timestamp-parser.h', 'type.h', ) @@ -147,6 +149,7 @@ cpp_headers = files( 'table-builder.hpp', 'table.hpp', 'tensor.hpp', + 'thread-pool.hpp', 'timestamp-parser.hpp', 'type.hpp', ) diff --git a/c_glib/arrow-glib/thread-pool.cpp b/c_glib/arrow-glib/thread-pool.cpp new file mode 100644 index 000000000000..adbf6cad0c1f --- /dev/null +++ b/c_glib/arrow-glib/thread-pool.cpp @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include + +G_BEGIN_DECLS + +/** + * SECTION: thread-pool + * @section_id: thread-pool-classes + * @title: Thread pool classes + * @include: arrow-glib/arrow-glib.h + * + * #GArrowThreadPool is a class for thread pool management. + */ + +typedef struct GArrowThreadPoolPrivate_ +{ + std::shared_ptr thread_pool; +} GArrowThreadPoolPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowThreadPool, garrow_thread_pool, G_TYPE_OBJECT) + +#define GARROW_THREAD_POOL_GET_PRIVATE(obj) \ + static_cast( \ + garrow_thread_pool_get_instance_private(GARROW_THREAD_POOL(obj))) + +static void +garrow_thread_pool_finalize(GObject *object) +{ + auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); + priv->thread_pool.~shared_ptr(); + G_OBJECT_CLASS(garrow_thread_pool_parent_class)->finalize(object); +} + +static void +garrow_thread_pool_init(GArrowThreadPool *object) +{ + auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); + new (&priv->thread_pool) std::shared_ptr; +} + +static void +garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) +{ + auto gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = garrow_thread_pool_finalize; +} + +/** + * garrow_thread_pool_new: + * @num_threads: The number of threads in the pool. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: (nullable): A newly created #GArrowThreadPool on success, + * %NULL on error. + * + * Since: 23.0.0 + */ +GArrowThreadPool * +garrow_thread_pool_new(guint num_threads, GError **error) +{ + auto arrow_thread_pool_result = arrow::internal::ThreadPool::Make(num_threads); + if (garrow::check(error, arrow_thread_pool_result, "[thread-pool][new]")) { + auto thread_pool = GARROW_THREAD_POOL(g_object_new(GARROW_TYPE_THREAD_POOL, NULL)); + auto priv = GARROW_THREAD_POOL_GET_PRIVATE(thread_pool); + priv->thread_pool = *arrow_thread_pool_result; + return thread_pool; + } else { + return NULL; + } +} + +G_END_DECLS + +std::shared_ptr +garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool) +{ + if (!thread_pool) + return nullptr; + + auto priv = GARROW_THREAD_POOL_GET_PRIVATE(thread_pool); + return priv->thread_pool; +} diff --git a/c_glib/arrow-glib/thread-pool.h b/c_glib/arrow-glib/thread-pool.h new file mode 100644 index 000000000000..7c6f597ed47f --- /dev/null +++ b/c_glib/arrow-glib/thread-pool.h @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include + +#include + +G_BEGIN_DECLS + +#define GARROW_TYPE_THREAD_POOL (garrow_thread_pool_get_type()) +GARROW_AVAILABLE_IN_23_0 +G_DECLARE_DERIVABLE_TYPE( + GArrowThreadPool, garrow_thread_pool, GARROW, THREAD_POOL, GObject) +struct _GArrowThreadPoolClass +{ + GObjectClass parent_class; +}; + +GARROW_AVAILABLE_IN_23_0 +GArrowThreadPool * +garrow_thread_pool_new(guint num_threads, GError **error); + +G_END_DECLS diff --git a/c_glib/arrow-glib/thread-pool.hpp b/c_glib/arrow-glib/thread-pool.hpp new file mode 100644 index 000000000000..c1825e74129b --- /dev/null +++ b/c_glib/arrow-glib/thread-pool.hpp @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include + +#include + +std::shared_ptr +garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool); From b3f3eceee8bc715509641dfeb274e6f2ec189340 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Thu, 11 Dec 2025 12:02:15 +0100 Subject: [PATCH 02/14] Add thread_pool argument to garrow_execute_context_new --- c_glib/arrow-glib/compute.cpp | 14 +++++++++++++- c_glib/arrow-glib/compute.h | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 5f494f3bc7ba..2d403b2586a4 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -276,6 +277,7 @@ garrow_compute_initialize(GError **error) typedef struct GArrowExecuteContextPrivate_ { arrow::compute::ExecContext context; + std::shared_ptr thread_pool; } GArrowExecuteContextPrivate; G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT) @@ -289,6 +291,7 @@ garrow_execute_context_finalize(GObject *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); priv->context.~ExecContext(); + priv->thread_pool.~shared_ptr(); G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } @@ -297,6 +300,7 @@ garrow_execute_context_init(GArrowExecuteContext *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); new (&priv->context) arrow::compute::ExecContext(arrow::default_memory_pool(), nullptr); + new (&priv->thread_pool) std::shared_ptr; } static void @@ -309,15 +313,23 @@ garrow_execute_context_class_init(GArrowExecuteContextClass *klass) /** * garrow_execute_context_new: + * @thread_pool: (nullable): A #GArrowThreadPool or %NULL. * * Returns: A newly created #GArrowExecuteContext. * * Since: 1.0.0 */ GArrowExecuteContext * -garrow_execute_context_new(void) +garrow_execute_context_new(GArrowThreadPool *thread_pool) { auto execute_context = g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, NULL); + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(execute_context); + + auto arrow_thread_pool = garrow_thread_pool_get_raw(thread_pool); + priv->thread_pool = arrow_thread_pool; + new (&priv->context) + arrow::compute::ExecContext(arrow::default_memory_pool(), arrow_thread_pool.get()); + return GARROW_EXECUTE_CONTEXT(execute_context); } diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h index 0f689d147e3f..4667b658a433 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -22,6 +22,7 @@ #include #include #include +#include G_BEGIN_DECLS @@ -40,7 +41,7 @@ struct _GArrowExecuteContextClass GARROW_AVAILABLE_IN_1_0 GArrowExecuteContext * -garrow_execute_context_new(void); +garrow_execute_context_new(GArrowThreadPool *thread_pool); GARROW_AVAILABLE_IN_7_0 gboolean From 3bb6dad242d259e1df9a803d69d3263f3cbf2228 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Thu, 11 Dec 2025 12:03:01 +0100 Subject: [PATCH 03/14] Add context argument to garrow_execute_plan_new --- c_glib/arrow-glib/compute.cpp | 11 +++++++++-- c_glib/arrow-glib/compute.h | 2 +- c_glib/test/test-execute-plan.rb | 20 ++++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 2d403b2586a4..47ff940ceff6 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -1889,6 +1889,7 @@ garrow_execute_plan_class_init(GArrowExecutePlanClass *klass) /** * garrow_execute_plan_new: + * @context: (nullable): A #GArrowExecuteContext or %NULL. * @error: (nullable): Return location for a #GError or %NULL. * * Returns: (nullable): A newly created #GArrowExecutePlan on success, @@ -1897,9 +1898,15 @@ garrow_execute_plan_class_init(GArrowExecutePlanClass *klass) * Since: 6.0.0 */ GArrowExecutePlan * -garrow_execute_plan_new(GError **error) +garrow_execute_plan_new(GArrowExecuteContext *context, GError **error) { - auto arrow_plan_result = arrow::acero::ExecPlan::Make(); + arrow::Result> arrow_plan_result; + if (context) { + auto arrow_context = garrow_execute_context_get_raw(context); + arrow_plan_result = arrow::acero::ExecPlan::Make(*arrow_context); + } else { + arrow_plan_result = arrow::acero::ExecPlan::Make(); + } if (garrow::check(error, arrow_plan_result, "[execute-plan][new]")) { return GARROW_EXECUTE_PLAN( g_object_new(GARROW_TYPE_EXECUTE_PLAN, "plan", &(*arrow_plan_result), NULL)); diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h index 4667b658a433..2e2bd5c82d68 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -323,7 +323,7 @@ struct _GArrowExecutePlanClass GARROW_AVAILABLE_IN_6_0 GArrowExecutePlan * -garrow_execute_plan_new(GError **error); +garrow_execute_plan_new(GArrowExecuteContext *context, GError **error); GARROW_AVAILABLE_IN_6_0 GArrowExecuteNode * garrow_execute_plan_build_node(GArrowExecutePlan *plan, diff --git a/c_glib/test/test-execute-plan.rb b/c_glib/test/test-execute-plan.rb index da1433db559c..33ae686ea991 100644 --- a/c_glib/test/test-execute-plan.rb +++ b/c_glib/test/test-execute-plan.rb @@ -28,8 +28,8 @@ def execute(plan) end sub_test_case("aggregate") do - def build_plan - plan = Arrow::ExecutePlan.new + def build_plan(context = nil) + plan = Arrow::ExecutePlan.new(context) record_batch = build_record_batch(number: build_int8_array([1, 2, 3, 4, 5]), @@ -63,6 +63,22 @@ def test_by_string reader.read_all) end end + + def test_hash_first_with_single_thread + thread_pool = Arrow::ThreadPool.new(1) + context = Arrow::ExecuteContext.new(thread_pool) + plan, reader = build_plan(context) do + aggregations = [ + Arrow::Aggregation.new("hash_first", nil, "number", "first(number)"), + ] + Arrow::AggregateNodeOptions.new(aggregations, ["string"]) + end + execute(plan) do + assert_equal(build_table("string" => build_string_array(["a", "b"]), + "first(number)" => build_int8_array([1, 2])), + reader.read_all) + end + end end sub_test_case("hash join") do From 240ca70011924a296171c26429a8a0648c28ad09 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 08:06:39 +0100 Subject: [PATCH 04/14] Rename num_threads n_threads --- c_glib/arrow-glib/thread-pool.cpp | 6 +++--- c_glib/arrow-glib/thread-pool.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/c_glib/arrow-glib/thread-pool.cpp b/c_glib/arrow-glib/thread-pool.cpp index adbf6cad0c1f..4100c23f9616 100644 --- a/c_glib/arrow-glib/thread-pool.cpp +++ b/c_glib/arrow-glib/thread-pool.cpp @@ -67,7 +67,7 @@ garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) /** * garrow_thread_pool_new: - * @num_threads: The number of threads in the pool. + * @n_threads: The number of threads in the pool. * @error: (nullable): Return location for a #GError or %NULL. * * Returns: (nullable): A newly created #GArrowThreadPool on success, @@ -76,9 +76,9 @@ garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) * Since: 23.0.0 */ GArrowThreadPool * -garrow_thread_pool_new(guint num_threads, GError **error) +garrow_thread_pool_new(guint n_threads, GError **error) { - auto arrow_thread_pool_result = arrow::internal::ThreadPool::Make(num_threads); + auto arrow_thread_pool_result = arrow::internal::ThreadPool::Make(n_threads); if (garrow::check(error, arrow_thread_pool_result, "[thread-pool][new]")) { auto thread_pool = GARROW_THREAD_POOL(g_object_new(GARROW_TYPE_THREAD_POOL, NULL)); auto priv = GARROW_THREAD_POOL_GET_PRIVATE(thread_pool); diff --git a/c_glib/arrow-glib/thread-pool.h b/c_glib/arrow-glib/thread-pool.h index 7c6f597ed47f..4c10c69aa0bd 100644 --- a/c_glib/arrow-glib/thread-pool.h +++ b/c_glib/arrow-glib/thread-pool.h @@ -36,6 +36,6 @@ struct _GArrowThreadPoolClass GARROW_AVAILABLE_IN_23_0 GArrowThreadPool * -garrow_thread_pool_new(guint num_threads, GError **error); +garrow_thread_pool_new(guint n_threads, GError **error); G_END_DECLS From d5e2612fb26a2996245102fd35c492261f486dd0 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 08:11:04 +0100 Subject: [PATCH 05/14] Pass arrow::internal::ThraedPool as property instead of setting directly via priv --- c_glib/arrow-glib/thread-pool.cpp | 39 ++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/c_glib/arrow-glib/thread-pool.cpp b/c_glib/arrow-glib/thread-pool.cpp index 4100c23f9616..5f17c561683d 100644 --- a/c_glib/arrow-glib/thread-pool.cpp +++ b/c_glib/arrow-glib/thread-pool.cpp @@ -42,6 +42,10 @@ G_DEFINE_TYPE_WITH_PRIVATE(GArrowThreadPool, garrow_thread_pool, G_TYPE_OBJECT) static_cast( \ garrow_thread_pool_get_instance_private(GARROW_THREAD_POOL(obj))) +enum { + PROP_THREAD_POOL = 1, +}; + static void garrow_thread_pool_finalize(GObject *object) { @@ -50,6 +54,26 @@ garrow_thread_pool_finalize(GObject *object) G_OBJECT_CLASS(garrow_thread_pool_parent_class)->finalize(object); } +static void +garrow_thread_pool_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_THREAD_POOL: + priv->thread_pool = + *static_cast *>( + g_value_get_pointer(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + static void garrow_thread_pool_init(GArrowThreadPool *object) { @@ -63,6 +87,15 @@ garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) auto gobject_class = G_OBJECT_CLASS(klass); gobject_class->finalize = garrow_thread_pool_finalize; + gobject_class->set_property = garrow_thread_pool_set_property; + + GParamSpec *spec; + spec = g_param_spec_pointer( + "thread-pool", + "Thread pool", + "The raw std::shared_ptr *", + static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_THREAD_POOL, spec); } /** @@ -80,9 +113,9 @@ garrow_thread_pool_new(guint n_threads, GError **error) { auto arrow_thread_pool_result = arrow::internal::ThreadPool::Make(n_threads); if (garrow::check(error, arrow_thread_pool_result, "[thread-pool][new]")) { - auto thread_pool = GARROW_THREAD_POOL(g_object_new(GARROW_TYPE_THREAD_POOL, NULL)); - auto priv = GARROW_THREAD_POOL_GET_PRIVATE(thread_pool); - priv->thread_pool = *arrow_thread_pool_result; + auto arrow_thread_pool = *arrow_thread_pool_result; + auto thread_pool = GARROW_THREAD_POOL( + g_object_new(GARROW_TYPE_THREAD_POOL, "thread-pool", &arrow_thread_pool, nullptr)); return thread_pool; } else { return NULL; From 902e0041df97d54f57d82f645965ec0666aa4541 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 08:26:35 +0100 Subject: [PATCH 06/14] Rename thread-pool.{cpp,h,hpp} to executor.{cpp,h,hpp} --- c_glib/arrow-glib/arrow-glib.h | 2 +- c_glib/arrow-glib/compute.cpp | 2 +- c_glib/arrow-glib/compute.h | 2 +- c_glib/arrow-glib/{thread-pool.cpp => executor.cpp} | 13 ++++++------- c_glib/arrow-glib/{thread-pool.h => executor.h} | 0 c_glib/arrow-glib/{thread-pool.hpp => executor.hpp} | 2 +- c_glib/arrow-glib/meson.build | 6 +++--- 7 files changed, 13 insertions(+), 14 deletions(-) rename c_glib/arrow-glib/{thread-pool.cpp => executor.cpp} (93%) rename c_glib/arrow-glib/{thread-pool.h => executor.h} (100%) rename c_glib/arrow-glib/{thread-pool.hpp => executor.hpp} (96%) diff --git a/c_glib/arrow-glib/arrow-glib.h b/c_glib/arrow-glib/arrow-glib.h index 7db1bb4be888..76dfbd58f84c 100644 --- a/c_glib/arrow-glib/arrow-glib.h +++ b/c_glib/arrow-glib/arrow-glib.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -43,7 +44,6 @@ #include #include #include -#include #include #include diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 47ff940ceff6..03ef331a33dd 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -26,13 +26,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include #include diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h index 2e2bd5c82d68..4fc9bd63ce9e 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -21,8 +21,8 @@ #include #include +#include #include -#include G_BEGIN_DECLS diff --git a/c_glib/arrow-glib/thread-pool.cpp b/c_glib/arrow-glib/executor.cpp similarity index 93% rename from c_glib/arrow-glib/thread-pool.cpp rename to c_glib/arrow-glib/executor.cpp index 5f17c561683d..41f0414fddb1 100644 --- a/c_glib/arrow-glib/thread-pool.cpp +++ b/c_glib/arrow-glib/executor.cpp @@ -17,15 +17,15 @@ * under the License. */ -#include #include +#include G_BEGIN_DECLS /** - * SECTION: thread-pool - * @section_id: thread-pool-classes - * @title: Thread pool classes + * SECTION: executor + * @section_id: executor-classes + * @title: Executor classes * @include: arrow-glib/arrow-glib.h * * #GArrowThreadPool is a class for thread pool management. @@ -64,9 +64,8 @@ garrow_thread_pool_set_property(GObject *object, switch (prop_id) { case PROP_THREAD_POOL: - priv->thread_pool = - *static_cast *>( - g_value_get_pointer(value)); + priv->thread_pool = *static_cast *>( + g_value_get_pointer(value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); diff --git a/c_glib/arrow-glib/thread-pool.h b/c_glib/arrow-glib/executor.h similarity index 100% rename from c_glib/arrow-glib/thread-pool.h rename to c_glib/arrow-glib/executor.h diff --git a/c_glib/arrow-glib/thread-pool.hpp b/c_glib/arrow-glib/executor.hpp similarity index 96% rename from c_glib/arrow-glib/thread-pool.hpp rename to c_glib/arrow-glib/executor.hpp index c1825e74129b..7bae73bc558b 100644 --- a/c_glib/arrow-glib/thread-pool.hpp +++ b/c_glib/arrow-glib/executor.hpp @@ -21,7 +21,7 @@ #include -#include +#include std::shared_ptr garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool); diff --git a/c_glib/arrow-glib/meson.build b/c_glib/arrow-glib/meson.build index ced550b80932..023b02e012e4 100644 --- a/c_glib/arrow-glib/meson.build +++ b/c_glib/arrow-glib/meson.build @@ -30,6 +30,7 @@ sources = files( 'decimal.cpp', 'decoder.cpp', 'error.cpp', + 'executor.cpp', 'expression.cpp', 'field.cpp', 'interval.cpp', @@ -40,7 +41,6 @@ sources = files( 'table-builder.cpp', 'table.cpp', 'tensor.cpp', - 'thread-pool.cpp', 'timestamp-parser.cpp', 'type.cpp', ) @@ -88,6 +88,7 @@ c_headers = files( 'decimal.h', 'decoder.h', 'error.h', + 'executor.h', 'expression.h', 'field.h', 'interval.h', @@ -98,7 +99,6 @@ c_headers = files( 'table-builder.h', 'table.h', 'tensor.h', - 'thread-pool.h', 'timestamp-parser.h', 'type.h', ) @@ -139,6 +139,7 @@ cpp_headers = files( 'decimal.hpp', 'decoder.hpp', 'error.hpp', + 'executor.hpp', 'expression.hpp', 'field.hpp', 'interval.hpp', @@ -149,7 +150,6 @@ cpp_headers = files( 'table-builder.hpp', 'table.hpp', 'tensor.hpp', - 'thread-pool.hpp', 'timestamp-parser.hpp', 'type.hpp', ) From 87665775b566cc451b493fcbaaa92d8425321dc3 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 08:35:26 +0100 Subject: [PATCH 07/14] Set thread_pool as a property on ExecuteContext --- c_glib/arrow-glib/compute.cpp | 49 ++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 03ef331a33dd..f76687e73901 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -286,6 +286,10 @@ G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_ static_cast( \ garrow_execute_context_get_instance_private(GARROW_EXECUTE_CONTEXT(object))) +enum { + PROP_THREAD_POOL = 1, +}; + static void garrow_execute_context_finalize(GObject *object) { @@ -295,6 +299,30 @@ garrow_execute_context_finalize(GObject *object) G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } +static void +garrow_execute_context_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_THREAD_POOL: + { + auto thread_pool = GARROW_THREAD_POOL(g_value_get_object(value)); + auto arrow_thread_pool = garrow_thread_pool_get_raw(thread_pool); + priv->thread_pool = arrow_thread_pool; + priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), + arrow_thread_pool.get()); + break; + } + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + static void garrow_execute_context_init(GArrowExecuteContext *object) { @@ -309,6 +337,16 @@ garrow_execute_context_class_init(GArrowExecuteContextClass *klass) auto gobject_class = G_OBJECT_CLASS(klass); gobject_class->finalize = garrow_execute_context_finalize; + gobject_class->set_property = garrow_execute_context_set_property; + + GParamSpec *spec; + spec = g_param_spec_object( + "thread-pool", + "Thread pool", + "The GArrowThreadPool for execution", + GARROW_TYPE_THREAD_POOL, + static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_THREAD_POOL, spec); } /** @@ -322,15 +360,8 @@ garrow_execute_context_class_init(GArrowExecuteContextClass *klass) GArrowExecuteContext * garrow_execute_context_new(GArrowThreadPool *thread_pool) { - auto execute_context = g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, NULL); - auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(execute_context); - - auto arrow_thread_pool = garrow_thread_pool_get_raw(thread_pool); - priv->thread_pool = arrow_thread_pool; - new (&priv->context) - arrow::compute::ExecContext(arrow::default_memory_pool(), arrow_thread_pool.get()); - - return GARROW_EXECUTE_CONTEXT(execute_context); + return GARROW_EXECUTE_CONTEXT( + g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, "thread-pool", thread_pool, nullptr)); } typedef struct GArrowFunctionOptionsPrivate_ From 39e45e212406910be49e916761de3efa44252b70 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 10:00:04 +0100 Subject: [PATCH 08/14] Use GArrowThreadPool instead of std::shared_ptr --- c_glib/arrow-glib/compute.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index f76687e73901..87b77ec05b70 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -277,7 +277,7 @@ garrow_compute_initialize(GError **error) typedef struct GArrowExecuteContextPrivate_ { arrow::compute::ExecContext context; - std::shared_ptr thread_pool; + GArrowThreadPool *thread_pool; } GArrowExecuteContextPrivate; G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT) @@ -295,7 +295,9 @@ garrow_execute_context_finalize(GObject *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); priv->context.~ExecContext(); - priv->thread_pool.~shared_ptr(); + if (priv->thread_pool) { + g_object_unref(priv->thread_pool); + } G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } @@ -311,10 +313,16 @@ garrow_execute_context_set_property(GObject *object, case PROP_THREAD_POOL: { auto thread_pool = GARROW_THREAD_POOL(g_value_get_object(value)); - auto arrow_thread_pool = garrow_thread_pool_get_raw(thread_pool); - priv->thread_pool = arrow_thread_pool; - priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), - arrow_thread_pool.get()); + if (thread_pool) { + priv->thread_pool = GARROW_THREAD_POOL(g_object_ref(thread_pool)); + auto arrow_thread_pool = garrow_thread_pool_get_raw(thread_pool); + priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), + arrow_thread_pool.get()); + } else { + priv->thread_pool = nullptr; + priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), + nullptr); + } break; } default: @@ -328,7 +336,7 @@ garrow_execute_context_init(GArrowExecuteContext *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); new (&priv->context) arrow::compute::ExecContext(arrow::default_memory_pool(), nullptr); - new (&priv->thread_pool) std::shared_ptr; + priv->thread_pool = nullptr; } static void From 3846256cbe580155df7381e0164384a62a292470 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 10:21:06 +0100 Subject: [PATCH 09/14] Add Executor base class and accept that in ExecuteContext --- c_glib/arrow-glib/compute.cpp | 40 +++++++++++++++---------------- c_glib/arrow-glib/compute.h | 2 +- c_glib/arrow-glib/executor.cpp | 44 +++++++++++++++++++++++++++++++++- c_glib/arrow-glib/executor.h | 12 ++++++++-- c_glib/arrow-glib/executor.hpp | 3 +++ 5 files changed, 77 insertions(+), 24 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 87b77ec05b70..481fc7e9b32c 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -277,7 +277,7 @@ garrow_compute_initialize(GError **error) typedef struct GArrowExecuteContextPrivate_ { arrow::compute::ExecContext context; - GArrowThreadPool *thread_pool; + GArrowExecutor *executor; } GArrowExecuteContextPrivate; G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT) @@ -287,7 +287,7 @@ G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_ garrow_execute_context_get_instance_private(GARROW_EXECUTE_CONTEXT(object))) enum { - PROP_THREAD_POOL = 1, + PROP_EXECUTOR = 1, }; static void @@ -295,8 +295,8 @@ garrow_execute_context_finalize(GObject *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); priv->context.~ExecContext(); - if (priv->thread_pool) { - g_object_unref(priv->thread_pool); + if (priv->executor) { + g_object_unref(priv->executor); } G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } @@ -310,16 +310,16 @@ garrow_execute_context_set_property(GObject *object, auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); switch (prop_id) { - case PROP_THREAD_POOL: + case PROP_EXECUTOR: { - auto thread_pool = GARROW_THREAD_POOL(g_value_get_object(value)); - if (thread_pool) { - priv->thread_pool = GARROW_THREAD_POOL(g_object_ref(thread_pool)); - auto arrow_thread_pool = garrow_thread_pool_get_raw(thread_pool); + auto executor = GARROW_EXECUTOR(g_value_get_object(value)); + if (executor) { + priv->executor = GARROW_EXECUTOR(g_object_ref(executor)); + auto arrow_executor = garrow_executor_get_raw(executor); priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), - arrow_thread_pool.get()); + arrow_executor); } else { - priv->thread_pool = nullptr; + priv->executor = nullptr; priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), nullptr); } @@ -336,7 +336,7 @@ garrow_execute_context_init(GArrowExecuteContext *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); new (&priv->context) arrow::compute::ExecContext(arrow::default_memory_pool(), nullptr); - priv->thread_pool = nullptr; + priv->executor = nullptr; } static void @@ -349,27 +349,27 @@ garrow_execute_context_class_init(GArrowExecuteContextClass *klass) GParamSpec *spec; spec = g_param_spec_object( - "thread-pool", - "Thread pool", - "The GArrowThreadPool for execution", - GARROW_TYPE_THREAD_POOL, + "executor", + "Executor", + "The GArrowExecutor for execution", + GARROW_TYPE_EXECUTOR, static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - g_object_class_install_property(gobject_class, PROP_THREAD_POOL, spec); + g_object_class_install_property(gobject_class, PROP_EXECUTOR, spec); } /** * garrow_execute_context_new: - * @thread_pool: (nullable): A #GArrowThreadPool or %NULL. + * @executor: (nullable): A #GArrowExecutor or %NULL. * * Returns: A newly created #GArrowExecuteContext. * * Since: 1.0.0 */ GArrowExecuteContext * -garrow_execute_context_new(GArrowThreadPool *thread_pool) +garrow_execute_context_new(GArrowExecutor *executor) { return GARROW_EXECUTE_CONTEXT( - g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, "thread-pool", thread_pool, nullptr)); + g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, "executor", executor, nullptr)); } typedef struct GArrowFunctionOptionsPrivate_ diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h index 4fc9bd63ce9e..ce9515603243 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -41,7 +41,7 @@ struct _GArrowExecuteContextClass GARROW_AVAILABLE_IN_1_0 GArrowExecuteContext * -garrow_execute_context_new(GArrowThreadPool *thread_pool); +garrow_execute_context_new(GArrowExecutor *executor); GARROW_AVAILABLE_IN_7_0 gboolean diff --git a/c_glib/arrow-glib/executor.cpp b/c_glib/arrow-glib/executor.cpp index 41f0414fddb1..34ed29b28b11 100644 --- a/c_glib/arrow-glib/executor.cpp +++ b/c_glib/arrow-glib/executor.cpp @@ -28,15 +28,39 @@ G_BEGIN_DECLS * @title: Executor classes * @include: arrow-glib/arrow-glib.h * + * #GArrowExecutor is the base class for executor implementations. * #GArrowThreadPool is a class for thread pool management. */ +typedef struct GArrowExecutorPrivate_ +{ + arrow::internal::Executor *executor; +} GArrowExecutorPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecutor, garrow_executor, G_TYPE_OBJECT) + +#define GARROW_EXECUTOR_GET_PRIVATE(obj) \ + static_cast( \ + garrow_executor_get_instance_private(GARROW_EXECUTOR(obj))) + +static void +garrow_executor_init(GArrowExecutor *object) +{ + auto priv = GARROW_EXECUTOR_GET_PRIVATE(object); + priv->executor = nullptr; +} + +static void +garrow_executor_class_init(GArrowExecutorClass *klass) +{ +} + typedef struct GArrowThreadPoolPrivate_ { std::shared_ptr thread_pool; } GArrowThreadPoolPrivate; -G_DEFINE_TYPE_WITH_PRIVATE(GArrowThreadPool, garrow_thread_pool, G_TYPE_OBJECT) +G_DEFINE_TYPE_WITH_PRIVATE(GArrowThreadPool, garrow_thread_pool, GARROW_TYPE_EXECUTOR) #define GARROW_THREAD_POOL_GET_PRIVATE(obj) \ static_cast( \ @@ -61,11 +85,17 @@ garrow_thread_pool_set_property(GObject *object, GParamSpec *pspec) { auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); + auto executor_priv = GARROW_EXECUTOR_GET_PRIVATE(GARROW_EXECUTOR(object)); switch (prop_id) { case PROP_THREAD_POOL: priv->thread_pool = *static_cast *>( g_value_get_pointer(value)); + if (priv->thread_pool) { + executor_priv->executor = priv->thread_pool.get(); + } else { + executor_priv->executor = nullptr; + } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); @@ -78,6 +108,8 @@ garrow_thread_pool_init(GArrowThreadPool *object) { auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); new (&priv->thread_pool) std::shared_ptr; + auto executor_priv = GARROW_EXECUTOR_GET_PRIVATE(GARROW_EXECUTOR(object)); + executor_priv->executor = nullptr; } static void @@ -123,6 +155,16 @@ garrow_thread_pool_new(guint n_threads, GError **error) G_END_DECLS +arrow::internal::Executor * +garrow_executor_get_raw(GArrowExecutor *executor) +{ + if (!executor) + return nullptr; + + auto priv = GARROW_EXECUTOR_GET_PRIVATE(executor); + return priv->executor; +} + std::shared_ptr garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool) { diff --git a/c_glib/arrow-glib/executor.h b/c_glib/arrow-glib/executor.h index 4c10c69aa0bd..50dab0b26e5e 100644 --- a/c_glib/arrow-glib/executor.h +++ b/c_glib/arrow-glib/executor.h @@ -25,13 +25,21 @@ G_BEGIN_DECLS +#define GARROW_TYPE_EXECUTOR (garrow_executor_get_type()) +GARROW_AVAILABLE_IN_23_0 +G_DECLARE_DERIVABLE_TYPE(GArrowExecutor, garrow_executor, GARROW, EXECUTOR, GObject) +struct _GArrowExecutorClass +{ + GObjectClass parent_class; +}; + #define GARROW_TYPE_THREAD_POOL (garrow_thread_pool_get_type()) GARROW_AVAILABLE_IN_23_0 G_DECLARE_DERIVABLE_TYPE( - GArrowThreadPool, garrow_thread_pool, GARROW, THREAD_POOL, GObject) + GArrowThreadPool, garrow_thread_pool, GARROW, THREAD_POOL, GArrowExecutor) struct _GArrowThreadPoolClass { - GObjectClass parent_class; + GArrowExecutorClass parent_class; }; GARROW_AVAILABLE_IN_23_0 diff --git a/c_glib/arrow-glib/executor.hpp b/c_glib/arrow-glib/executor.hpp index 7bae73bc558b..f2b8481d95f0 100644 --- a/c_glib/arrow-glib/executor.hpp +++ b/c_glib/arrow-glib/executor.hpp @@ -23,5 +23,8 @@ #include +arrow::internal::Executor * +garrow_executor_get_raw(GArrowExecutor *executor); + std::shared_ptr garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool); From 199574638b8f52f99e10c09317134241fa20aa20 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Sun, 14 Dec 2025 10:35:23 +0100 Subject: [PATCH 10/14] Use std::shared_ptr to avoid needless construction --- c_glib/arrow-glib/compute.cpp | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 481fc7e9b32c..20f36c72e124 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -276,8 +276,7 @@ garrow_compute_initialize(GError **error) typedef struct GArrowExecuteContextPrivate_ { - arrow::compute::ExecContext context; - GArrowExecutor *executor; + std::shared_ptr context; } GArrowExecuteContextPrivate; G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT) @@ -294,10 +293,7 @@ static void garrow_execute_context_finalize(GObject *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); - priv->context.~ExecContext(); - if (priv->executor) { - g_object_unref(priv->executor); - } + priv->context.reset(); G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } @@ -313,16 +309,10 @@ garrow_execute_context_set_property(GObject *object, case PROP_EXECUTOR: { auto executor = GARROW_EXECUTOR(g_value_get_object(value)); - if (executor) { - priv->executor = GARROW_EXECUTOR(g_object_ref(executor)); - auto arrow_executor = garrow_executor_get_raw(executor); - priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), - arrow_executor); - } else { - priv->executor = nullptr; - priv->context = arrow::compute::ExecContext(arrow::default_memory_pool(), - nullptr); - } + auto arrow_executor = garrow_executor_get_raw(executor); + priv->context = + std::make_shared(arrow::default_memory_pool(), + arrow_executor); break; } default: @@ -335,8 +325,7 @@ static void garrow_execute_context_init(GArrowExecuteContext *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); - new (&priv->context) arrow::compute::ExecContext(arrow::default_memory_pool(), nullptr); - priv->executor = nullptr; + priv->context = nullptr; } static void @@ -6413,7 +6402,7 @@ arrow::compute::ExecContext * garrow_execute_context_get_raw(GArrowExecuteContext *context) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(context); - return &priv->context; + return priv->context.get(); } GArrowFunctionOptions * From c74387656f06c715ca9bf21fdedf3361ac03477f Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Tue, 16 Dec 2025 10:22:25 +0100 Subject: [PATCH 11/14] Fix shared pointer dereference --- c_glib/arrow-glib/compute.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 20f36c72e124..8edd07949f4c 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -293,7 +293,7 @@ static void garrow_execute_context_finalize(GObject *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); - priv->context.reset(); + priv->context.~shared_ptr(); G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } From 9c744fe8619fb99c0e50bd1a3fa5c0d1b1b9c8d6 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Tue, 16 Dec 2025 10:24:16 +0100 Subject: [PATCH 12/14] Prevent instantiation of Executor base class --- c_glib/arrow-glib/executor.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/c_glib/arrow-glib/executor.cpp b/c_glib/arrow-glib/executor.cpp index 34ed29b28b11..2ee72f048656 100644 --- a/c_glib/arrow-glib/executor.cpp +++ b/c_glib/arrow-glib/executor.cpp @@ -29,6 +29,7 @@ G_BEGIN_DECLS * @include: arrow-glib/arrow-glib.h * * #GArrowExecutor is the base class for executor implementations. + * * #GArrowThreadPool is a class for thread pool management. */ @@ -37,7 +38,7 @@ typedef struct GArrowExecutorPrivate_ arrow::internal::Executor *executor; } GArrowExecutorPrivate; -G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecutor, garrow_executor, G_TYPE_OBJECT) +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowExecutor, garrow_executor, G_TYPE_OBJECT) #define GARROW_EXECUTOR_GET_PRIVATE(obj) \ static_cast( \ From de46365c7c64f520e63ceaec75b63de5e22976a0 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Tue, 16 Dec 2025 10:30:13 +0100 Subject: [PATCH 13/14] Store shared ptr to thread pool in base class --- c_glib/arrow-glib/compute.cpp | 2 +- c_glib/arrow-glib/executor.cpp | 113 +++++++++++++-------------------- c_glib/arrow-glib/executor.hpp | 7 +- 3 files changed, 45 insertions(+), 77 deletions(-) diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 8edd07949f4c..e3ca7b496b41 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -312,7 +312,7 @@ garrow_execute_context_set_property(GObject *object, auto arrow_executor = garrow_executor_get_raw(executor); priv->context = std::make_shared(arrow::default_memory_pool(), - arrow_executor); + arrow_executor.get()); break; } default: diff --git a/c_glib/arrow-glib/executor.cpp b/c_glib/arrow-glib/executor.cpp index 2ee72f048656..26cb4f5fe974 100644 --- a/c_glib/arrow-glib/executor.cpp +++ b/c_glib/arrow-glib/executor.cpp @@ -20,6 +20,8 @@ #include #include +#include + G_BEGIN_DECLS /** @@ -33,10 +35,10 @@ G_BEGIN_DECLS * #GArrowThreadPool is a class for thread pool management. */ -typedef struct GArrowExecutorPrivate_ +struct GArrowExecutorPrivate { - arrow::internal::Executor *executor; -} GArrowExecutorPrivate; + std::shared_ptr executor; +}; G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowExecutor, garrow_executor, G_TYPE_OBJECT) @@ -44,59 +46,37 @@ G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowExecutor, garrow_executor, G_TYPE_OBJE static_cast( \ garrow_executor_get_instance_private(GARROW_EXECUTOR(obj))) +enum { + PROP_EXECUTOR = 1, +}; + static void garrow_executor_init(GArrowExecutor *object) { auto priv = GARROW_EXECUTOR_GET_PRIVATE(object); - priv->executor = nullptr; + new (&priv->executor) std::shared_ptr; } static void -garrow_executor_class_init(GArrowExecutorClass *klass) +garrow_executor_finalize(GObject *object) { -} - -typedef struct GArrowThreadPoolPrivate_ -{ - std::shared_ptr thread_pool; -} GArrowThreadPoolPrivate; - -G_DEFINE_TYPE_WITH_PRIVATE(GArrowThreadPool, garrow_thread_pool, GARROW_TYPE_EXECUTOR) - -#define GARROW_THREAD_POOL_GET_PRIVATE(obj) \ - static_cast( \ - garrow_thread_pool_get_instance_private(GARROW_THREAD_POOL(obj))) - -enum { - PROP_THREAD_POOL = 1, -}; - -static void -garrow_thread_pool_finalize(GObject *object) -{ - auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); - priv->thread_pool.~shared_ptr(); - G_OBJECT_CLASS(garrow_thread_pool_parent_class)->finalize(object); + auto priv = GARROW_EXECUTOR_GET_PRIVATE(object); + priv->executor.~shared_ptr(); + G_OBJECT_CLASS(garrow_executor_parent_class)->finalize(object); } static void -garrow_thread_pool_set_property(GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) +garrow_executor_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) { - auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); - auto executor_priv = GARROW_EXECUTOR_GET_PRIVATE(GARROW_EXECUTOR(object)); + auto priv = GARROW_EXECUTOR_GET_PRIVATE(GARROW_EXECUTOR(object)); switch (prop_id) { - case PROP_THREAD_POOL: - priv->thread_pool = *static_cast *>( + case PROP_EXECUTOR: + priv->executor = *static_cast *>( g_value_get_pointer(value)); - if (priv->thread_pool) { - executor_priv->executor = priv->thread_pool.get(); - } else { - executor_priv->executor = nullptr; - } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); @@ -105,29 +85,32 @@ garrow_thread_pool_set_property(GObject *object, } static void -garrow_thread_pool_init(GArrowThreadPool *object) -{ - auto priv = GARROW_THREAD_POOL_GET_PRIVATE(object); - new (&priv->thread_pool) std::shared_ptr; - auto executor_priv = GARROW_EXECUTOR_GET_PRIVATE(GARROW_EXECUTOR(object)); - executor_priv->executor = nullptr; -} - -static void -garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) +garrow_executor_class_init(GArrowExecutorClass *klass) { auto gobject_class = G_OBJECT_CLASS(klass); - gobject_class->finalize = garrow_thread_pool_finalize; - gobject_class->set_property = garrow_thread_pool_set_property; + gobject_class->finalize = garrow_executor_finalize; + gobject_class->set_property = garrow_executor_set_property; GParamSpec *spec; spec = g_param_spec_pointer( - "thread-pool", - "Thread pool", - "The raw std::shared_ptr *", + "executor", + "Executor", + "The raw std::shared_ptr *", static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); - g_object_class_install_property(gobject_class, PROP_THREAD_POOL, spec); + g_object_class_install_property(gobject_class, PROP_EXECUTOR, spec); +} + +G_DEFINE_TYPE(GArrowThreadPool, garrow_thread_pool, GARROW_TYPE_EXECUTOR) + +static void +garrow_thread_pool_init(GArrowThreadPool *object) +{ +} + +static void +garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) +{ } /** @@ -147,16 +130,16 @@ garrow_thread_pool_new(guint n_threads, GError **error) if (garrow::check(error, arrow_thread_pool_result, "[thread-pool][new]")) { auto arrow_thread_pool = *arrow_thread_pool_result; auto thread_pool = GARROW_THREAD_POOL( - g_object_new(GARROW_TYPE_THREAD_POOL, "thread-pool", &arrow_thread_pool, nullptr)); + g_object_new(GARROW_TYPE_THREAD_POOL, "executor", &arrow_thread_pool, nullptr)); return thread_pool; } else { - return NULL; + return nullptr; } } G_END_DECLS -arrow::internal::Executor * +std::shared_ptr garrow_executor_get_raw(GArrowExecutor *executor) { if (!executor) @@ -165,13 +148,3 @@ garrow_executor_get_raw(GArrowExecutor *executor) auto priv = GARROW_EXECUTOR_GET_PRIVATE(executor); return priv->executor; } - -std::shared_ptr -garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool) -{ - if (!thread_pool) - return nullptr; - - auto priv = GARROW_THREAD_POOL_GET_PRIVATE(thread_pool); - return priv->thread_pool; -} diff --git a/c_glib/arrow-glib/executor.hpp b/c_glib/arrow-glib/executor.hpp index f2b8481d95f0..e91823186ffb 100644 --- a/c_glib/arrow-glib/executor.hpp +++ b/c_glib/arrow-glib/executor.hpp @@ -19,12 +19,7 @@ #pragma once -#include - #include -arrow::internal::Executor * +std::shared_ptr garrow_executor_get_raw(GArrowExecutor *executor); - -std::shared_ptr -garrow_thread_pool_get_raw(GArrowThreadPool *thread_pool); From 75fd0bcecde0294ce775e0b4a6ea45a51a3cc177 Mon Sep 17 00:00:00 2001 From: Sten Larsson Date: Tue, 16 Dec 2025 10:38:11 +0100 Subject: [PATCH 14/14] Keep reference to exectuor in execute context to prevent GC --- c_glib/arrow-glib/compute.cpp | 55 +++++++++++++++++++++++++---- c_glib/test/test-execute-context.rb | 30 ++++++++++++++++ 2 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 c_glib/test/test-execute-context.rb diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index e3ca7b496b41..770e51dc0363 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -274,10 +274,11 @@ garrow_compute_initialize(GError **error) return garrow::check(error, status, "[compute][initialize]"); } -typedef struct GArrowExecuteContextPrivate_ +struct GArrowExecuteContextPrivate { std::shared_ptr context; -} GArrowExecuteContextPrivate; + GArrowExecutor *executor; +}; G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT) @@ -289,6 +290,19 @@ enum { PROP_EXECUTOR = 1, }; +static void +garrow_execute_context_dispose(GObject *object) +{ + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); + + if (priv->executor) { + g_object_unref(priv->executor); + priv->executor = nullptr; + } + + G_OBJECT_CLASS(garrow_execute_context_parent_class)->dispose(object); +} + static void garrow_execute_context_finalize(GObject *object) { @@ -308,8 +322,8 @@ garrow_execute_context_set_property(GObject *object, switch (prop_id) { case PROP_EXECUTOR: { - auto executor = GARROW_EXECUTOR(g_value_get_object(value)); - auto arrow_executor = garrow_executor_get_raw(executor); + priv->executor = GARROW_EXECUTOR(g_value_dup_object(value)); + auto arrow_executor = garrow_executor_get_raw(priv->executor); priv->context = std::make_shared(arrow::default_memory_pool(), arrow_executor.get()); @@ -321,11 +335,29 @@ garrow_execute_context_set_property(GObject *object, } } +static void +garrow_execute_context_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_EXECUTOR: + g_value_set_object(value, priv->executor); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + static void garrow_execute_context_init(GArrowExecuteContext *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); - priv->context = nullptr; + new (&priv->context) std::shared_ptr; } static void @@ -333,16 +365,25 @@ garrow_execute_context_class_init(GArrowExecuteContextClass *klass) { auto gobject_class = G_OBJECT_CLASS(klass); + gobject_class->dispose = garrow_execute_context_dispose; gobject_class->finalize = garrow_execute_context_finalize; gobject_class->set_property = garrow_execute_context_set_property; + gobject_class->get_property = garrow_execute_context_get_property; GParamSpec *spec; + /** + * GArrowExecuteContext:executor: + * + * The executor for execution. + * + * Since: 23.0.0 + */ spec = g_param_spec_object( "executor", "Executor", - "The GArrowExecutor for execution", + "The executor for execution", GARROW_TYPE_EXECUTOR, - static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + static_cast(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property(gobject_class, PROP_EXECUTOR, spec); } diff --git a/c_glib/test/test-execute-context.rb b/c_glib/test/test-execute-context.rb new file mode 100644 index 000000000000..fc6873d0a3c4 --- /dev/null +++ b/c_glib/test/test-execute-context.rb @@ -0,0 +1,30 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class TestExecuteContext < Test::Unit::TestCase + def setup + @context = Arrow::ExecuteContext.new + end + + def test_executor + assert_nil(@context.executor) + + executor = Arrow::ThreadPool.new(1) + context = Arrow::ExecuteContext.new(executor) + assert_equal(executor, context.executor) + end +end