forked from lthn/blockchain
implemented static objects wrapper, which make possible to pre-handle beginning of static obj death
This commit is contained in:
parent
c4149503d0
commit
131f4a8347
4 changed files with 88 additions and 64 deletions
|
|
@ -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<std::string, uint64_t> channels_err_stat_container_type;
|
||||
inline epee::locked_object_proxy<channels_err_stat_container_type> get_channels_errors_stat_container()
|
||||
{
|
||||
static std::recursive_mutex cs;
|
||||
static channels_err_stat_container_type errors_by_channel;
|
||||
static epee::static_helpers::wrapper<std::recursive_mutex> cs;
|
||||
static epee::static_helpers::wrapper<channels_err_stat_container_type> errors_by_channel;
|
||||
epee::locked_object_proxy<channels_err_stat_container_type> res(errors_by_channel, cs);
|
||||
return res;
|
||||
}
|
||||
|
|
@ -1194,7 +1194,7 @@ namespace log_space
|
|||
class log_singletone
|
||||
{
|
||||
public:
|
||||
friend class initializer<log_singletone>;
|
||||
friend class static_helpers::initializer<log_singletone>;
|
||||
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_singletone> log_initializer;
|
||||
const static static_helpers::initializer<log_singletone> log_initializer;
|
||||
/************************************************************************/
|
||||
/* */
|
||||
// /************************************************************************/
|
||||
|
|
|
|||
|
|
@ -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 <limits>
|
||||
#include <set>
|
||||
#include <iterator>
|
||||
#include <boost/thread.hpp>
|
||||
#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 to_initialize>
|
||||
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 to_initialize>
|
||||
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<class t_base>
|
||||
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_
|
||||
|
|
@ -35,7 +35,7 @@
|
|||
#include <boost/thread/recursive_mutex.hpp>
|
||||
|
||||
#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<abstract_singleton<deadlock_guard> > singleton_initializer;
|
||||
//const static initializer<abstract_singleton<deadlock_guard> > singleton_initializer;
|
||||
|
||||
/************************************************************************/
|
||||
/* */
|
||||
|
|
|
|||
|
|
@ -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<plain_wallet_instance> ptr(new plain_wallet_instance());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue