From 5e32f46df468fdf908bd68fe0fedabc34539a742 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Wed, 12 Mar 2025 23:31:16 +0300 Subject: [PATCH 1/6] Update api.hxx, close.hxx, and 2 more files... --- include/zenoh/api.hxx | 3 +++ include/zenoh/api/close.hxx | 38 ++++++++++++++++++++++++++++++ include/zenoh/api/session.hxx | 28 +++++++++++++++++++++- tests/zenohc/close.cxx | 44 +++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 include/zenoh/api/close.hxx create mode 100644 tests/zenohc/close.cxx diff --git a/include/zenoh/api.hxx b/include/zenoh/api.hxx index 9f9bd9f7..0ddbd5bb 100644 --- a/include/zenoh/api.hxx +++ b/include/zenoh/api.hxx @@ -33,6 +33,9 @@ #include "api/reply.hxx" #include "api/sample.hxx" #include "api/scout.hxx" +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) +#include "api/close.hxx" +#endif #include "api/session.hxx" #include "api/subscriber.hxx" #include "api/timestamp.hxx" diff --git a/include/zenoh/api/close.hxx b/include/zenoh/api/close.hxx new file mode 100644 index 00000000..5a9ced0d --- /dev/null +++ b/include/zenoh/api/close.hxx @@ -0,0 +1,38 @@ + +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, + +#pragma once + +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) + +namespace zenoh { + +/// A close operation handle. +class CloseHandle : public Owned<::zc_owned_concurrent_close_handle_t> { + friend class Session; + CloseHandle(zenoh::detail::null_object_t) : Owned(nullptr){}; + + public: + /// @brief Blocks until corresponding close operation completes. + /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be + /// thrown in case of error. + void join(ZResult* err = nullptr) { + __ZENOH_RESULT_CHECK(zc_concurrent_close_handle_wait(interop::as_moved_c_ptr(*this)), err, + "Failed to wait for close operation"); + } +}; + +} // namespace zenoh + +#endif diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index 0a3bcb04..b30e1003 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -36,6 +36,9 @@ #if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API) #include "shm/client_storage/client_storage.hxx" #endif +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) +#include "close.hxx" +#endif namespace zenoh { namespace ext { @@ -44,7 +47,7 @@ class SessionExt; /// A Zenoh session. class Session : public Owned<::z_owned_session_t> { - Session(zenoh::detail::null_object_t) : Owned(nullptr){}; + Session(zenoh::detail::null_object_t) : Owned(nullptr) {}; public: /// @brief Options to be passed when opening a ``Session``. @@ -63,6 +66,14 @@ class Session : public Owned<::z_owned_session_t> { /// @brief Options to be passed when closing a ``Session``. struct SessionCloseOptions { +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) + /// The timeout for close operation in milliseconds. 0 means default close timeout which is 10 seconds. + uint32_t timeout_ms = 10000; + /// A function to receive close handle. If set, the close operation will be executed concurrently + /// in separate task, and this function will receive a handle to be used for controlling + /// close execution. + std::function out_concurrent; +#endif /// @name Fields static SessionCloseOptions create_default() { return {}; } }; @@ -1111,8 +1122,23 @@ class Session : public Owned<::z_owned_session_t> { /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. void close(SessionCloseOptions&& options = SessionCloseOptions::create_default(), ZResult* err = nullptr) { +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) + CloseHandle close_handle(zenoh::detail::null_object); + ::z_close_options_t close_opts; + ::z_close_options_default(&close_opts); + close_opts.internal_timeout_ms = options.timeout_ms; + if (options.out_concurrent) { + close_opts.internal_out_concurrent = &close_handle._0; + } + __ZENOH_RESULT_CHECK(::z_close(interop::as_loaned_c_ptr(*this), &close_opts), err, + "Failed to close the session"); + if(*err == Z_OK && options.out_concurrent) { + options.out_concurrent(std::move(close_handle)); + } +#else (void)options; __ZENOH_RESULT_CHECK(::z_close(interop::as_loaned_c_ptr(*this), nullptr), err, "Failed to close the session"); +#endif } /// @brief Check if session is closed. diff --git a/tests/zenohc/close.cxx b/tests/zenohc/close.cxx new file mode 100644 index 00000000..93f64af2 --- /dev/null +++ b/tests/zenohc/close.cxx @@ -0,0 +1,44 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include "zenoh.hxx" + +using namespace zenoh; + +#undef NDEBUG +#include + +void test_session_close_in_drop() { + auto session = Session::open(Config::create_default()); +} + +void test_session_close() { + auto session = Session::open(Config::create_default()); + session.close(); +} + +void test_session_close_in_background() { + auto session = Session::open(Config::create_default()); + + auto close_options = SessionCloseOptions::create_default(); + close_options.out_concurrent = [](CloseHandle&& h) { h.wait(); } + + session.close(std::move(close_options)); +} + +int main() { + test_session_close_in_drop(); + test_session_close(); + test_session_close_in_background(); +} \ No newline at end of file From 1da68ad65de99f4f2a0e9d5cb60fcc51ffda5216 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Mar 2025 00:24:00 +0300 Subject: [PATCH 2/6] support concurrent close --- include/zenoh/api/close.hxx | 2 +- include/zenoh/api/session.hxx | 4 ++-- tests/zenohc/close.cxx | 13 +++++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/zenoh/api/close.hxx b/include/zenoh/api/close.hxx index 5a9ced0d..56a922d4 100644 --- a/include/zenoh/api/close.hxx +++ b/include/zenoh/api/close.hxx @@ -27,7 +27,7 @@ class CloseHandle : public Owned<::zc_owned_concurrent_close_handle_t> { /// @brief Blocks until corresponding close operation completes. /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be /// thrown in case of error. - void join(ZResult* err = nullptr) { + void wait(ZResult* err = nullptr) { __ZENOH_RESULT_CHECK(zc_concurrent_close_handle_wait(interop::as_moved_c_ptr(*this)), err, "Failed to wait for close operation"); } diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index 1df0041c..97661597 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -72,7 +72,7 @@ class Session : public Owned<::z_owned_session_t> { /// A function to receive close handle. If set, the close operation will be executed concurrently /// in separate task, and this function will receive a handle to be used for controlling /// close execution. - std::function out_concurrent; + std::function out_concurrent = nullptr; #endif /// @name Fields static SessionCloseOptions create_default() { return {}; } @@ -1150,7 +1150,7 @@ class Session : public Owned<::z_owned_session_t> { } __ZENOH_RESULT_CHECK(::z_close(interop::as_loaned_c_ptr(*this), &close_opts), err, "Failed to close the session"); - if(*err == Z_OK && options.out_concurrent) { + if(options.out_concurrent && (!err || *err == Z_OK)) { options.out_concurrent(std::move(close_handle)); } #else diff --git a/tests/zenohc/close.cxx b/tests/zenohc/close.cxx index 93f64af2..59f4cbb4 100644 --- a/tests/zenohc/close.cxx +++ b/tests/zenohc/close.cxx @@ -28,17 +28,26 @@ void test_session_close() { session.close(); } +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) void test_session_close_in_background() { auto session = Session::open(Config::create_default()); - auto close_options = SessionCloseOptions::create_default(); - close_options.out_concurrent = [](CloseHandle&& h) { h.wait(); } + bool close_called = false; + auto close_options = Session::SessionCloseOptions::create_default(); + close_options.out_concurrent = [&close_called](CloseHandle&& h) { h.wait(); close_called = true; }; session.close(std::move(close_options)); + + if(!close_called) { + exit(-1); + } } +#endif int main() { test_session_close_in_drop(); test_session_close(); +#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) test_session_close_in_background(); +#endif } \ No newline at end of file From f4a443108b36abb317eb2379f985adde0bf7a9ea Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Thu, 13 Mar 2025 00:40:53 +0300 Subject: [PATCH 3/6] clang format --- include/zenoh/api/session.hxx | 4 ++-- tests/zenohc/close.cxx | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/zenoh/api/session.hxx b/include/zenoh/api/session.hxx index 97661597..bfb9d336 100644 --- a/include/zenoh/api/session.hxx +++ b/include/zenoh/api/session.hxx @@ -47,7 +47,7 @@ class SessionExt; /// A Zenoh session. class Session : public Owned<::z_owned_session_t> { - Session(zenoh::detail::null_object_t) : Owned(nullptr) {}; + Session(zenoh::detail::null_object_t) : Owned(nullptr){}; public: /// @brief Options to be passed when opening a ``Session``. @@ -1150,7 +1150,7 @@ class Session : public Owned<::z_owned_session_t> { } __ZENOH_RESULT_CHECK(::z_close(interop::as_loaned_c_ptr(*this), &close_opts), err, "Failed to close the session"); - if(options.out_concurrent && (!err || *err == Z_OK)) { + if (options.out_concurrent && (!err || *err == Z_OK)) { options.out_concurrent(std::move(close_handle)); } #else diff --git a/tests/zenohc/close.cxx b/tests/zenohc/close.cxx index 59f4cbb4..1fc4791a 100644 --- a/tests/zenohc/close.cxx +++ b/tests/zenohc/close.cxx @@ -19,9 +19,7 @@ using namespace zenoh; #undef NDEBUG #include -void test_session_close_in_drop() { - auto session = Session::open(Config::create_default()); -} +void test_session_close_in_drop() { auto session = Session::open(Config::create_default()); } void test_session_close() { auto session = Session::open(Config::create_default()); @@ -34,11 +32,14 @@ void test_session_close_in_background() { bool close_called = false; auto close_options = Session::SessionCloseOptions::create_default(); - close_options.out_concurrent = [&close_called](CloseHandle&& h) { h.wait(); close_called = true; }; + close_options.out_concurrent = [&close_called](CloseHandle&& h) { + h.wait(); + close_called = true; + }; session.close(std::move(close_options)); - if(!close_called) { + if (!close_called) { exit(-1); } } From e79eeef001d81eb47adf98db57a7634f77ee5bec Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Wed, 26 Mar 2025 11:53:19 +0300 Subject: [PATCH 4/6] Update zenoh-c --- zenoh-c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zenoh-c b/zenoh-c index 94a4fe36..f976ddc7 160000 --- a/zenoh-c +++ b/zenoh-c @@ -1 +1 @@ -Subproject commit 94a4fe36c44c0f48acb797db50724adc85161d7e +Subproject commit f976ddc73781f6e5468f0e4484ec6408ed574b9e From 59deff1ea68527dfb727d27d8df62200ea4af058 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Wed, 26 Mar 2025 12:49:35 +0300 Subject: [PATCH 5/6] Update zenoh-c --- zenoh-c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zenoh-c b/zenoh-c index f976ddc7..f035a112 160000 --- a/zenoh-c +++ b/zenoh-c @@ -1 +1 @@ -Subproject commit f976ddc73781f6e5468f0e4484ec6408ed574b9e +Subproject commit f035a1121bff576c72bbc26a9c38a96eb568da21 From be3f5fe23b478a71fdb8a9749fa327a971ac8c81 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Wed, 26 Mar 2025 13:12:30 +0300 Subject: [PATCH 6/6] [skip ci] Update year in close.hxx --- include/zenoh/api/close.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zenoh/api/close.hxx b/include/zenoh/api/close.hxx index 56a922d4..9cb0c8b1 100644 --- a/include/zenoh/api/close.hxx +++ b/include/zenoh/api/close.hxx @@ -1,6 +1,6 @@ // -// Copyright (c) 2024 ZettaScale Technology +// Copyright (c) 2025 ZettaScale Technology // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License 2.0 which is available at