From 131f4a8347e16807d37347439663a240e1028246 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sun, 17 May 2020 21:38:36 +0200 Subject: [PATCH] implemented static objects wrapper, which make possible to pre-handle beginning of static obj death --- contrib/epee/include/misc_log_ex.h | 10 +- ...{static_initializer.h => static_helpers.h} | 111 ++++++++++-------- contrib/epee/include/syncobj.h | 4 +- src/wallet/plain_wallet_api.cpp | 27 +++-- 4 files changed, 88 insertions(+), 64 deletions(-) rename contrib/epee/include/{static_initializer.h => static_helpers.h} (50%) diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index 782ef241..f1d846ad 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -61,7 +61,7 @@ DISABLE_VS_WARNINGS(4100) #include "misc_helpers.h" -#include "static_initializer.h" +#include "static_helpers.h" #include "string_tools.h" #include "time_helper.h" #include "misc_os_dependent.h" @@ -946,8 +946,8 @@ namespace log_space typedef std::map channels_err_stat_container_type; inline epee::locked_object_proxy get_channels_errors_stat_container() { - static std::recursive_mutex cs; - static channels_err_stat_container_type errors_by_channel; + static epee::static_helpers::wrapper cs; + static epee::static_helpers::wrapper errors_by_channel; epee::locked_object_proxy res(errors_by_channel, cs); return res; } @@ -1194,7 +1194,7 @@ namespace log_space class log_singletone { public: - friend class initializer; + friend class static_helpers::initializer; friend class logger; static int get_log_detalisation_level() { @@ -1611,7 +1611,7 @@ POP_GCC_WARNINGS //static int get_set_error_filter(bool is_need_set = false) }; - const static initializer log_initializer; + const static static_helpers::initializer log_initializer; /************************************************************************/ /* */ // /************************************************************************/ diff --git a/contrib/epee/include/static_initializer.h b/contrib/epee/include/static_helpers.h similarity index 50% rename from contrib/epee/include/static_initializer.h rename to contrib/epee/include/static_helpers.h index 43ccff60..1a3dce05 100644 --- a/contrib/epee/include/static_initializer.h +++ b/contrib/epee/include/static_helpers.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net +// Copyright (c) 2006-2020, Andrey N. Sabelnikov, www.sabelnikov.net // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -26,59 +26,74 @@ -#ifndef _STATIC_INITIALIZER_H_ -#define _STATIC_INITIALIZER_H_ - - +#pragma once +#include +#include +#include +#include +#include "include_base_utils.h" +#include "auto_val_init.h" namespace epee { -/*********************************************************************** -class initializer - useful to initialize some static classes - which have init() and un_init() static members -************************************************************************/ + namespace static_helpers + { + template + class initializer + { + public: + initializer() + { + to_initialize::init(); + //get_set_is_initialized(true, true); + } + ~initializer() + { + to_initialize::un_init(); + //get_set_is_uninitialized(true, true); + } + }; + typedef void(*static_destroy_handler_type)(); -template -class initializer -{ -public: - initializer() - { - to_initialize::init(); - //get_set_is_initialized(true, true); - } - ~initializer() - { - to_initialize::un_init(); - //get_set_is_uninitialized(true, true); - } + inline + bool set_or_call_on_destruct(bool set = false, static_destroy_handler_type destr_ptr = nullptr) + { + volatile static bool deinit_called = false; + volatile static static_destroy_handler_type static_destroy_handler = nullptr; + + if (set) + { + static_destroy_handler = destr_ptr; + return true; + } + if (!deinit_called) + { + std::cout << "[ENTERING DESTROY CALLBACK]: " << std::endl; + if (static_destroy_handler) + static_destroy_handler(); + + deinit_called = true; + std::cout << "[DESTROY CALLBACK FINISHED]: " << std::endl; + } + + return true; + } + + template + struct wrapper : public t_base + { + ~wrapper() + { + std::cout << "[DESTROYING STATIC]: " << typeid(t_base).name() << std::endl; + set_or_call_on_destruct(); + } + + }; + + } + - /*static inline bool is_initialized() - { - return get_set_is_initialized(); - } - static inline bool is_uninitialized() - { - return get_set_is_uninitialized(); - } -private: - static inline bool get_set_is_initialized(bool need_to_set = false, bool val_to_set= false) - { - static bool val_is_initialized = false; - if(need_to_set) - val_is_initialized = val_to_set; - return val_is_initialized; - } - static inline bool get_set_is_uninitialized(bool need_to_set = false, bool val_to_set = false) - { - static bool val_is_uninitialized = false; - if(need_to_set) - val_is_uninitialized = val_to_set; - return val_is_uninitialized; - }*/ -}; } -#endif //_STATIC_INITIALIZER_H_ diff --git a/contrib/epee/include/syncobj.h b/contrib/epee/include/syncobj.h index 3494194a..c994241b 100644 --- a/contrib/epee/include/syncobj.h +++ b/contrib/epee/include/syncobj.h @@ -35,7 +35,7 @@ #include #include "singleton.h" -#include "static_initializer.h" +#include "static_helpers.h" #include "misc_helpers.h" //#define DISABLE_DEADLOCK_GUARD @@ -532,7 +532,7 @@ namespace epee } }; - const static initializer > singleton_initializer; + //const static initializer > singleton_initializer; /************************************************************************/ /* */ diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 94d5ae0a..82880631 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -12,6 +12,8 @@ #include "wallets_manager.h" #include "common/base58.h" #include "common/config_encrypt_helper.h" +#include "static_helpers.h" + #define ANDROID_PACKAGE_NAME "com.zano_mobile" @@ -57,17 +59,22 @@ namespace plain_wallet void deinit(); } - - - -#ifdef MOBILE_WALLET_BUILD -void on_lib_unload() __attribute__((destructor)); -void on_lib_unload() { - std::cout << "[ON_LIB_UNLOAD]-->>" << ENDL; +void static_destroy_handler() +{ + std::cout << "[DESTROY CALLBACK HANDLER STARTED]: " << std::endl; plain_wallet::deinit(); - std::cout << "[ON_LIB_UNLOAD]<<--" << ENDL; + std::cout << "[DESTROY CALLBACK HANDLER FINISHED]: " << std::endl; } -#endif + + +// #ifdef MOBILE_WALLET_BUILD +// void on_lib_unload() __attribute__((destructor)); +// void on_lib_unload() { +// std::cout << "[ON_LIB_UNLOAD]-->>" << ENDL; +// plain_wallet::deinit(); +// std::cout << "[ON_LIB_UNLOAD]<<--" << ENDL; +// } +// #endif namespace plain_wallet { @@ -173,6 +180,8 @@ namespace plain_wallet return epee::serialization::store_t_to_json(ok_response); } + epee::static_helpers::set_or_call_on_destruct(true, static_destroy_handler); + std::cout << "[INIT PLAIN_WALLET_INSTANCE]" << ENDL; std::shared_ptr ptr(new plain_wallet_instance());