From 193b0a222cd7af7eb4eed92005c7d7d65e2f8af8 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 7 Nov 2018 00:01:27 -0800 Subject: [PATCH] cscore: Add Shutdown() function This allows ordered shutdown / destruction of sources and sinks prior to global singleton destruction. --- cscore/src/main/native/cpp/Instance.cpp | 13 +++++++++++-- cscore/src/main/native/cpp/Instance.h | 2 ++ cscore/src/main/native/cpp/cscore_c.cpp | 2 ++ cscore/src/main/native/cpp/cscore_cpp.cpp | 5 +++++ cscore/src/main/native/cpp/jni/CameraServerJNI.cpp | 2 ++ cscore/src/main/native/include/cscore_c.h | 7 +++++++ cscore/src/main/native/include/cscore_cpp.h | 7 +++++++ 7 files changed, 36 insertions(+), 2 deletions(-) diff --git a/cscore/src/main/native/cpp/Instance.cpp b/cscore/src/main/native/cpp/Instance.cpp index 655eecf917..8dd5c268df 100644 --- a/cscore/src/main/native/cpp/Instance.cpp +++ b/cscore/src/main/native/cpp/Instance.cpp @@ -45,8 +45,17 @@ Instance::Instance() : telemetry(notifier), networkListener(logger, notifier) { Instance::~Instance() {} Instance& Instance::GetInstance() { - static Instance inst; - return inst; + static Instance* inst = new Instance; + return *inst; +} + +void Instance::Shutdown() { + eventLoop.Stop(); + m_sinks.FreeAll(); + m_sources.FreeAll(); + networkListener.Stop(); + telemetry.Stop(); + notifier.Stop(); } void Instance::SetDefaultLogger() { logger.SetLogger(def_log_func); } diff --git a/cscore/src/main/native/cpp/Instance.h b/cscore/src/main/native/cpp/Instance.h index 9123f54263..0c566f376a 100644 --- a/cscore/src/main/native/cpp/Instance.h +++ b/cscore/src/main/native/cpp/Instance.h @@ -51,6 +51,8 @@ class Instance { static Instance& GetInstance(); + void Shutdown(); + wpi::Logger logger; Notifier notifier; Telemetry telemetry; diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index 9ed8aaad22..21106ec190 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -348,6 +348,8 @@ void CS_SetDefaultLogger(unsigned int min_level) { cs::SetDefaultLogger(min_level); } +void CS_Shutdown(void) { cs::Shutdown(); } + CS_Source* CS_EnumerateSources(int* count, CS_Status* status) { wpi::SmallVector buf; auto handles = cs::EnumerateSourceHandles(buf, status); diff --git a/cscore/src/main/native/cpp/cscore_cpp.cpp b/cscore/src/main/native/cpp/cscore_cpp.cpp index c1ae30f803..a4654c416a 100644 --- a/cscore/src/main/native/cpp/cscore_cpp.cpp +++ b/cscore/src/main/native/cpp/cscore_cpp.cpp @@ -665,6 +665,11 @@ void SetDefaultLogger(unsigned int min_level) { inst.logger.set_min_level(min_level); } +// +// Shutdown Function +// +void Shutdown() { Instance::GetInstance().Shutdown(); } + // // Utility Functions // diff --git a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp index ebdb3a4429..06c3c1e283 100644 --- a/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp +++ b/cscore/src/main/native/cpp/jni/CameraServerJNI.cpp @@ -87,6 +87,8 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) { } JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) { + cs::Shutdown(); + JNIEnv* env; if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) return; diff --git a/cscore/src/main/native/include/cscore_c.h b/cscore/src/main/native/include/cscore_c.h index 1163b3e734..ba728f8bf0 100644 --- a/cscore/src/main/native/include/cscore_c.h +++ b/cscore/src/main/native/include/cscore_c.h @@ -440,6 +440,13 @@ void CS_SetLogger(CS_LogFunc func, unsigned int min_level); void CS_SetDefaultLogger(unsigned int min_level); /** @} */ +/** + * @defgroup cscore_shutdown_cfunc Library Shutdown Function + * @{ + */ +void CS_Shutdown(void); +/** @} */ + /** * @defgroup cscore_utility_cfunc Utility Functions * @{ diff --git a/cscore/src/main/native/include/cscore_cpp.h b/cscore/src/main/native/include/cscore_cpp.h index 8272a9fb2c..fcce7e0279 100644 --- a/cscore/src/main/native/include/cscore_cpp.h +++ b/cscore/src/main/native/include/cscore_cpp.h @@ -377,6 +377,13 @@ void SetLogger(LogFunc func, unsigned int min_level); void SetDefaultLogger(unsigned int min_level); /** @} */ +/** + * @defgroup cscore_shutdown_func Library Shutdown Function + * @{ + */ +void Shutdown(); +/** @} */ + /** * @defgroup cscore_utility_func Utility Functions * @{