1
0
Fork 0

fixed startup race conditions

This commit is contained in:
Jan Steemann 2014-06-04 15:18:20 +02:00
parent a43d5c3f7d
commit f7049b8922
6 changed files with 47 additions and 48 deletions

View File

@ -47,8 +47,7 @@ using namespace triagens::arango;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void triagens::arango::ClusterCommRestCallback(string& coordinator, void triagens::arango::ClusterCommRestCallback(string& coordinator,
triagens::rest::HttpResponse* response) triagens::rest::HttpResponse* response) {
{
ClusterComm::instance()->asyncAnswer(coordinator, response); ClusterComm::instance()->asyncAnswer(coordinator, response);
} }
@ -80,22 +79,13 @@ ClusterComm::~ClusterComm () {
cleanupAllQueues(); cleanupAllQueues();
} }
////////////////////////////////////////////////////////////////////////////////
/// @brief the actual singleton instance
////////////////////////////////////////////////////////////////////////////////
ClusterComm* ClusterComm::_theinstance = 0;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief getter for our singleton instance /// @brief getter for our singleton instance
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
ClusterComm* ClusterComm::instance () { ClusterComm* ClusterComm::instance () {
// This does not have to be thread-safe, because we guarantee that static ClusterComm* Instance = new ClusterComm();
// this is called very early in the startup phase when there is still return Instance;
// a single thread.
assert(_theinstance != 0);
return _theinstance;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -103,10 +93,19 @@ ClusterComm* ClusterComm::instance () {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void ClusterComm::initialise () { void ClusterComm::initialise () {
assert(_theinstance == 0); auto* i = instance();
_theinstance = new ClusterComm(); // this now happens exactly once i->startBackgroundThread();
}
_theinstance->startBackgroundThread();
////////////////////////////////////////////////////////////////////////////////
/// @brief cleanup function to call once when shutting down
////////////////////////////////////////////////////////////////////////////////
void ClusterComm::cleanup () {
auto i = instance();
assert(i != nullptr);
delete i;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -254,10 +254,7 @@ void ClusterCommRestCallback(string& coordinator, rest::HttpResponse* response);
/// @brief cleanup function to call once when shutting down /// @brief cleanup function to call once when shutting down
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void cleanup () { static void cleanup ();
delete _theinstance;
_theinstance = 0;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not connection errors should be logged as errors /// @brief whether or not connection errors should be logged as errors

View File

@ -236,18 +236,13 @@ void CollectionInfoCurrent::copyAllJsons () {
// --SECTION-- private methods // --SECTION-- private methods
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
ClusterInfo* ClusterInfo::_theinstance = 0;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief returns an instance of the cluster info class /// @brief returns an instance of the cluster info class
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
ClusterInfo* ClusterInfo::instance () { ClusterInfo* ClusterInfo::instance () {
// This does not have to be thread-safe, because we guarantee that static ClusterInfo* Instance = new ClusterInfo();
// this is called very early in the startup phase when there is still return Instance;
// a single thread.
assert(_theinstance != 0);
return _theinstance;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -255,8 +250,18 @@ ClusterInfo* ClusterInfo::instance () {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void ClusterInfo::initialise () { void ClusterInfo::initialise () {
assert(_theinstance == 0); instance();
_theinstance = new ClusterInfo(); // this now happens exactly once }
////////////////////////////////////////////////////////////////////////////////
/// @brief cleanup function to call once when shutting down
////////////////////////////////////////////////////////////////////////////////
void ClusterInfo::cleanup () {
auto i = instance();
assert(i != nullptr);
delete i;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -751,10 +751,7 @@ namespace triagens {
/// @brief cleanup function to call once when shutting down /// @brief cleanup function to call once when shutting down
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void cleanup () { static void cleanup ();
delete _theinstance;
_theinstance = 0;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// --SECTION-- public methods // --SECTION-- public methods

View File

@ -65,19 +65,13 @@ ServerState::~ServerState () {
// --SECTION-- public static methods // --SECTION-- public static methods
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief the actual singleton instance
////////////////////////////////////////////////////////////////////////////////
ServerState* ServerState::_theinstance = 0;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief create the (sole) instance /// @brief create the (sole) instance
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
ServerState* ServerState::instance () { ServerState* ServerState::instance () {
assert(_theinstance != 0); static ServerState* Instance = new ServerState();
return _theinstance; return Instance;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -85,8 +79,18 @@ ServerState* ServerState::instance () {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void ServerState::initialise () { void ServerState::initialise () {
assert(_theinstance == 0); instance();
_theinstance = new ServerState(); // this now happens exactly once }
////////////////////////////////////////////////////////////////////////////////
/// @brief cleanup function to call once when shutting down
////////////////////////////////////////////////////////////////////////////////
void ServerState::cleanup () {
auto i = instance();
assert(i != nullptr);
delete i;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -117,10 +117,7 @@ namespace triagens {
/// @brief cleanup function to call once when shutting down /// @brief cleanup function to call once when shutting down
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void cleanup () { static void cleanup ();
delete _theinstance;
_theinstance = 0;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief get the string representation of a role /// @brief get the string representation of a role