Skip to content

Commit

Permalink
[XrdCl] Ensure only one instance of DefaultEnv exists, closes #920
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichal committed Oct 16, 2019
1 parent 2beed12 commit 744136a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
46 changes: 24 additions & 22 deletions src/XrdCl/XrdClDefaultEnv.cc
Expand Up @@ -828,31 +828,33 @@ namespace XrdCl
log->SetTopicName( PlugInMgrMsg, "PlugInMgr" );
log->SetTopicName( ExDbgMsg, "ExDbgMsg" );
}
}


//------------------------------------------------------------------------------
// Static initialization and finalization
//------------------------------------------------------------------------------
int EnvInitializer::counter = 0;

//------------------------------------------------------------------------------
// The constructor will be invoked in every translation unit
// that includes XrdClDefaultEnv.hh, but the DefaultEnv will
// be initialized only in the first one
//------------------------------------------------------------------------------
EnvInitializer::EnvInitializer ()
{
if( counter++ == 0 ) XrdCl::DefaultEnv::Initialize();
}
//------------------------------------------------------------------------------
// Static initialization and finalization
//------------------------------------------------------------------------------
int EnvInitializer::counter = 0;

//------------------------------------------------------------------------------
// The constructor will be invoked in every translation unit
// that includes XrdClDefaultEnv.hh, but the DefaultEnv will
// be initialized only in the first one
//------------------------------------------------------------------------------
EnvInitializer::EnvInitializer ()
{
if( counter++ == 0 ) XrdCl::DefaultEnv::Initialize();
}

//------------------------------------------------------------------------------
// The destructor will be invoked in every translation unit
// that includes XrdClDefaultEnv.hh, but the DefaultEnv will
// be finalized only once in the last one
//------------------------------------------------------------------------------
EnvInitializer::~EnvInitializer ()
{
if( --counter == 0 ) XrdCl::DefaultEnv::Finalize();
}

//------------------------------------------------------------------------------
// The destructor will be invoked in every translation unit
// that includes XrdClDefaultEnv.hh, but the DefaultEnv will
// be finalized only once in the last one
//------------------------------------------------------------------------------
EnvInitializer::~EnvInitializer ()
{
if( --counter == 0 ) XrdCl::DefaultEnv::Finalize();
}

31 changes: 19 additions & 12 deletions src/XrdCl/XrdClDefaultEnv.hh
Expand Up @@ -43,12 +43,15 @@ namespace XrdCl
//----------------------------------------------------------------------------
class DefaultEnv: public Env
{
public:
friend struct EnvInitializer;

//------------------------------------------------------------------------
//! Constructor
//------------------------------------------------------------------------
DefaultEnv();

public:

//------------------------------------------------------------------------
//! Get default client environment
//------------------------------------------------------------------------
Expand Down Expand Up @@ -140,6 +143,13 @@ namespace XrdCl
//------------------------------------------------------------------------
static PlugInFactory *GetPlugInFactory( const std::string url );

//------------------------------------------------------------------------
//! Re-initialize the logging
//------------------------------------------------------------------------
static void ReInitializeLogging();

private:

//------------------------------------------------------------------------
//! Initialize the environment
//------------------------------------------------------------------------
Expand All @@ -150,12 +160,7 @@ namespace XrdCl
//------------------------------------------------------------------------
static void Finalize();

//------------------------------------------------------------------------
//! Re-initialize the logging
//------------------------------------------------------------------------
static void ReInitializeLogging();

private:
static void SetUpLog();

static XrdSysMutex sInitMutex;
Expand All @@ -171,13 +176,15 @@ namespace XrdCl
static TransportManager *sTransportManager;
static PlugInManager *sPlugInManager;
};

static struct EnvInitializer
{
EnvInitializer();
~EnvInitializer();
static int counter;
} initializer;
}

static struct EnvInitializer
{
EnvInitializer();
~EnvInitializer();
static int counter;
} initializer;


#endif // __XRD_CL_DEFAULT_ENV_HH__

0 comments on commit 744136a

Please sign in to comment.