cscore: Add Shutdown() function

This allows ordered shutdown / destruction of sources and sinks prior to
global singleton destruction.
This commit is contained in:
Peter Johnson
2018-11-07 00:01:27 -08:00
parent 76f5d153fa
commit 193b0a222c
7 changed files with 36 additions and 2 deletions

View File

@@ -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); }

View File

@@ -51,6 +51,8 @@ class Instance {
static Instance& GetInstance();
void Shutdown();
wpi::Logger logger;
Notifier notifier;
Telemetry telemetry;

View File

@@ -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<CS_Source, 32> buf;
auto handles = cs::EnumerateSourceHandles(buf, status);

View File

@@ -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
//

View File

@@ -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<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
return;

View File

@@ -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
* @{

View File

@@ -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
* @{