You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tsfile.apache.org by co...@apache.org on 2026/09/10 10:59:48 UTC

(tsfile) 14/17: feat(cpp): add single-thread async close coordinator

This is an automated email from the ASF dual-hosted git repository.

ColinLeeo pushed a commit to branch feat/labview-bulk-io
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit 0459304efe7c566aa17c9e9bcb09a1ddd3a8ec75
Author: ColinLee <sh...@163.com>
AuthorDate: Thu Sep 10 18:12:39 2026 +0800

    feat(cpp): add single-thread async close coordinator
---
 cpp/src/labview_wrapper/CMakeLists.txt      |  11 +-
 cpp/src/labview_wrapper/async_close.cc      |  91 ++++++++++++++++
 cpp/src/labview_wrapper/async_close.h       |  70 ++++++++++++
 cpp/src/labview_wrapper/test_async_close.cc | 163 ++++++++++++++++++++++++++++
 cpp/test/CMakeLists.txt                     |   2 +
 5 files changed, 336 insertions(+), 1 deletion(-)

diff --git a/cpp/src/labview_wrapper/CMakeLists.txt b/cpp/src/labview_wrapper/CMakeLists.txt
index d984fae18..d84b89be2 100644
--- a/cpp/src/labview_wrapper/CMakeLists.txt
+++ b/cpp/src/labview_wrapper/CMakeLists.txt
@@ -19,7 +19,7 @@ under the License.
 
 message(STATUS "Running in src/labview_wrapper directory")
 
-add_library(tsfile_labview SHARED tsfile_labview.cc)
+add_library(tsfile_labview SHARED tsfile_labview.cc async_close.cc)
 target_include_directories(tsfile_labview PRIVATE ${PROJECT_SRC_DIR})
 target_link_libraries(tsfile_labview PRIVATE tsfile)
 
@@ -41,6 +41,15 @@ install(FILES tsfile_labview.h DESTINATION include)
 option(BUILD_LABVIEW_WRAPPER_TESTS "Build LabVIEW shim standalone tests"
        ${BUILD_TEST})
 if (BUILD_LABVIEW_WRAPPER_TESTS)
+    add_executable(tsfile_labview_async_close_coordinator_test
+                   test_async_close.cc async_close.cc)
+    target_include_directories(tsfile_labview_async_close_coordinator_test
+                               PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
+    set_target_properties(tsfile_labview_async_close_coordinator_test PROPERTIES
+        CXX_STANDARD 11
+        CXX_STANDARD_REQUIRED ON
+        RUNTIME_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH})
+
     foreach(test_name smoke read_only dump block block_read)
         add_executable(tsfile_labview_${test_name}_test test_${test_name}.c)
         target_link_libraries(tsfile_labview_${test_name}_test
diff --git a/cpp/src/labview_wrapper/async_close.cc b/cpp/src/labview_wrapper/async_close.cc
new file mode 100644
index 000000000..ca4376f66
--- /dev/null
+++ b/cpp/src/labview_wrapper/async_close.cc
@@ -0,0 +1,91 @@
+/*
+ * 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 "async_close.h"
+
+#include <new>
+#include <utility>
+
+#include "cwrapper/errno_define_c.h"
+
+namespace labview {
+namespace {
+
+LV_Status RunAction(const AsyncCloseCoordinator::CloseAction& action) {
+    try {
+        return action();
+    } catch (const std::bad_alloc&) {
+        return RET_OOM;
+    } catch (...) {
+        return RET_FILE_CLOSE_ERR;
+    }
+}
+
+}  // namespace
+
+LV_Status AsyncCloseTask::Wait() {
+    std::unique_lock<std::mutex> lock(mutex_);
+    completed_.wait(lock, [this] { return done_; });
+    return status_;
+}
+
+void AsyncCloseTask::Complete(LV_Status status) {
+    {
+        std::lock_guard<std::mutex> lock(mutex_);
+        status_ = status;
+        done_ = true;
+    }
+    completed_.notify_all();
+}
+
+AsyncCloseCoordinator::~AsyncCloseCoordinator() {
+    std::lock_guard<std::mutex> lock(submission_mutex_);
+    JoinWorker();
+}
+
+LV_Status AsyncCloseCoordinator::Submit(
+    CloseAction action, std::shared_ptr<AsyncCloseTask>* out_task) {
+    if (!action || out_task == nullptr) {
+        return RET_INVALID_ARG;
+    }
+    out_task->reset();
+
+    std::lock_guard<std::mutex> lock(submission_mutex_);
+    JoinWorker();
+
+    std::shared_ptr<AsyncCloseTask> task;
+    try {
+        task = std::make_shared<AsyncCloseTask>();
+        worker_ =
+            std::thread([task, action] { task->Complete(RunAction(action)); });
+    } catch (...) {
+        return RunAction(action);
+    }
+
+    *out_task = std::move(task);
+    return RET_OK;
+}
+
+void AsyncCloseCoordinator::JoinWorker() {
+    if (worker_.joinable()) {
+        worker_.join();
+    }
+}
+
+}  // namespace labview
diff --git a/cpp/src/labview_wrapper/async_close.h b/cpp/src/labview_wrapper/async_close.h
new file mode 100644
index 000000000..96214e7cf
--- /dev/null
+++ b/cpp/src/labview_wrapper/async_close.h
@@ -0,0 +1,70 @@
+/*
+ * 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.
+ */
+
+#ifndef SRC_LABVIEW_WRAPPER_ASYNC_CLOSE_H_
+#define SRC_LABVIEW_WRAPPER_ASYNC_CLOSE_H_
+
+#include <condition_variable>
+#include <functional>
+#include <memory>
+#include <mutex>
+#include <thread>
+
+#include "tsfile_labview.h"
+
+namespace labview {
+
+class AsyncCloseTask {
+   public:
+    LV_Status Wait();
+
+   private:
+    friend class AsyncCloseCoordinator;
+
+    void Complete(LV_Status status);
+
+    std::mutex mutex_;
+    std::condition_variable completed_;
+    bool done_ = false;
+    LV_Status status_ = 0;
+};
+
+class AsyncCloseCoordinator {
+   public:
+    using CloseAction = std::function<LV_Status()>;
+
+    AsyncCloseCoordinator() = default;
+    ~AsyncCloseCoordinator();
+
+    AsyncCloseCoordinator(const AsyncCloseCoordinator&) = delete;
+    AsyncCloseCoordinator& operator=(const AsyncCloseCoordinator&) = delete;
+
+    LV_Status Submit(CloseAction action,
+                     std::shared_ptr<AsyncCloseTask>* out_task);
+
+   private:
+    void JoinWorker();
+
+    std::mutex submission_mutex_;
+    std::thread worker_;
+};
+
+}  // namespace labview
+
+#endif  // SRC_LABVIEW_WRAPPER_ASYNC_CLOSE_H_
diff --git a/cpp/src/labview_wrapper/test_async_close.cc b/cpp/src/labview_wrapper/test_async_close.cc
new file mode 100644
index 000000000..6b234151d
--- /dev/null
+++ b/cpp/src/labview_wrapper/test_async_close.cc
@@ -0,0 +1,163 @@
+/*
+ * 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 <atomic>
+#include <chrono>
+#include <condition_variable>
+#include <cstdio>
+#include <memory>
+#include <mutex>
+#include <thread>
+
+#include "async_close.h"
+
+#define CHECK(condition)                                              \
+    do {                                                              \
+        if (!(condition)) {                                           \
+            std::fprintf(stderr, "FAIL: %s at line %d\n", #condition, \
+                         __LINE__);                                   \
+            return false;                                             \
+        }                                                             \
+    } while (0)
+
+namespace {
+
+class Gate {
+   public:
+    void Open() {
+        std::lock_guard<std::mutex> lock(mutex_);
+        open_ = true;
+        condition_.notify_all();
+    }
+
+    void Wait() {
+        std::unique_lock<std::mutex> lock(mutex_);
+        condition_.wait(lock, [this] { return open_; });
+    }
+
+   private:
+    std::mutex mutex_;
+    std::condition_variable condition_;
+    bool open_ = false;
+};
+
+void update_max(std::atomic<int>* maximum, int value) {
+    int observed = maximum->load();
+    while (observed < value &&
+           !maximum->compare_exchange_weak(observed, value)) {
+    }
+}
+
+bool test_submissions_are_serialized() {
+    labview::AsyncCloseCoordinator coordinator;
+    std::shared_ptr<labview::AsyncCloseTask> first;
+    std::shared_ptr<labview::AsyncCloseTask> second;
+    Gate first_started;
+    Gate release_first;
+    Gate second_submit_started;
+    std::atomic<int> running(0);
+    std::atomic<int> max_running(0);
+    std::atomic<bool> second_submit_done(false);
+    LV_Status second_submit_status = -1;
+
+    CHECK(coordinator.Submit(
+              [&] {
+                  const int active = ++running;
+                  update_max(&max_running, active);
+                  first_started.Open();
+                  release_first.Wait();
+                  --running;
+                  return static_cast<LV_Status>(11);
+              },
+              &first) == 0);
+    first_started.Wait();
+
+    std::thread submitter([&] {
+        second_submit_started.Open();
+        second_submit_status = coordinator.Submit(
+            [&] {
+                const int active = ++running;
+                update_max(&max_running, active);
+                --running;
+                return static_cast<LV_Status>(22);
+            },
+            &second);
+        second_submit_done.store(true);
+    });
+
+    second_submit_started.Wait();
+    std::this_thread::sleep_for(std::chrono::milliseconds(20));
+    CHECK(!second_submit_done.load());
+    release_first.Open();
+    submitter.join();
+
+    CHECK(second_submit_status == 0);
+    CHECK(first != nullptr);
+    CHECK(second != nullptr);
+    CHECK(first->Wait() == 11);
+    CHECK(second->Wait() == 22);
+    CHECK(max_running.load() == 1);
+    return true;
+}
+
+bool test_destructor_joins_last_worker() {
+    std::unique_ptr<labview::AsyncCloseCoordinator> coordinator(
+        new labview::AsyncCloseCoordinator());
+    std::shared_ptr<labview::AsyncCloseTask> task;
+    Gate action_started;
+    Gate release_action;
+    Gate destructor_started;
+    std::atomic<bool> destructor_done(false);
+
+    CHECK(coordinator->Submit(
+              [&] {
+                  action_started.Open();
+                  release_action.Wait();
+                  return static_cast<LV_Status>(33);
+              },
+              &task) == 0);
+    action_started.Wait();
+
+    std::thread destroyer([&] {
+        destructor_started.Open();
+        coordinator.reset();
+        destructor_done.store(true);
+    });
+    destructor_started.Wait();
+    std::this_thread::sleep_for(std::chrono::milliseconds(20));
+    CHECK(!destructor_done.load());
+    release_action.Open();
+    destroyer.join();
+
+    CHECK(destructor_done.load());
+    CHECK(task->Wait() == 33);
+    return true;
+}
+
+}  // namespace
+
+int main() {
+    if (!test_submissions_are_serialized()) {
+        return 1;
+    }
+    if (!test_destructor_joins_last_worker()) {
+        return 1;
+    }
+    return 0;
+}
diff --git a/cpp/test/CMakeLists.txt b/cpp/test/CMakeLists.txt
index da9d3f22e..8b213ee37 100644
--- a/cpp/test/CMakeLists.txt
+++ b/cpp/test/CMakeLists.txt
@@ -357,6 +357,8 @@ else()
 endif()
 
 if(TARGET tsfile_labview_smoke_test)
+    add_test(NAME LabVIEWAsyncCloseCoordinatorTest
+             COMMAND $<TARGET_FILE:tsfile_labview_async_close_coordinator_test>)
     add_test(NAME LabVIEWSmokeTest
              COMMAND $<TARGET_FILE:tsfile_labview_smoke_test>)
     add_test(NAME LabVIEWReadOnlyTest