You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2010/06/04 00:56:57 UTC

svn commit: r951193 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util: IdGenerator.cpp IdGenerator.h

Author: tabish
Date: Thu Jun  3 22:56:57 2010
New Revision: 951193

URL: http://svn.apache.org/viewvc?rev=951193&view=rev
Log:
Fix some issues with static data causing a mutex to be used before the library is initialized. 

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.cpp?rev=951193&r1=951192&r2=951193&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.cpp Thu Jun  3 22:56:57 2010
@@ -34,10 +34,7 @@ using namespace decaf::util;
 using namespace decaf::util::concurrent;
 
 ////////////////////////////////////////////////////////////////////////////////
-IdGenerator::StaticInit IdGenerator::statics;
-
-////////////////////////////////////////////////////////////////////////////////
-IdGenerator::StaticInit::StaticInit() : UNIQUE_STUB(), instanceCount(0), hostname() {
+IdGenerator::StaticData::StaticData() : UNIQUE_STUB(), instanceCount(0), hostname() {
 
     std::string stub = "";
 
@@ -57,18 +54,11 @@ IdGenerator::StaticInit::StaticInit() : 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-IdGenerator::IdGenerator() : seed(), sequence(0) {
-    synchronized( &statics.mutex ) {
-        this->seed = std::string( "ID:" ) + statics.hostname +
-                     statics.UNIQUE_STUB + Long::toString( statics.instanceCount++ ) + ":";
-    }
+IdGenerator::IdGenerator() : prefix(), seed(), sequence(0) {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-IdGenerator::IdGenerator( const std::string& prefix ) : seed(), sequence(0) {
-    synchronized( &statics.mutex ) {
-        this->seed = prefix + statics.UNIQUE_STUB + Long::toString( statics.instanceCount++ ) + ":";
-    }
+IdGenerator::IdGenerator( const std::string& prefix ) : prefix(prefix), seed(), sequence(0) {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -80,7 +70,20 @@ std::string IdGenerator::generateId() co
 
     std::string result;
 
+    StaticData& statics = IdGenerator::getClassStaticData();
+
     synchronized( &statics.mutex ) {
+
+        if( seed.empty() ) {
+
+            if( prefix.empty() ) {
+                this->seed = std::string( "ID:" ) + statics.hostname +
+                             statics.UNIQUE_STUB + Long::toString( statics.instanceCount++ ) + ":";
+            } else {
+                this->seed = prefix + statics.UNIQUE_STUB + Long::toString( statics.instanceCount++ ) + ":";
+            }
+        }
+
         result = this->seed + Long::toString( this->sequence++ );
     }
 
@@ -138,3 +141,9 @@ int IdGenerator::compare( const std::str
 
     return result;
 }
+
+////////////////////////////////////////////////////////////////////////////////
+IdGenerator::StaticData& IdGenerator::getClassStaticData() {
+    static IdGenerator::StaticData statics;
+    return statics;
+}

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.h?rev=951193&r1=951192&r2=951193&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/util/IdGenerator.h Thu Jun  3 22:56:57 2010
@@ -29,7 +29,7 @@ namespace util {
     class AMQCPP_API IdGenerator {
     private:
 
-        class StaticInit {
+        class StaticData {
         public:
 
             std::string UNIQUE_STUB;
@@ -37,12 +37,11 @@ namespace util {
             std::string hostname;
             mutable decaf::util::concurrent::Mutex mutex;
 
-            StaticInit();
+            StaticData();
         };
 
-        static StaticInit statics;
-
-        std::string seed;
+        std::string prefix;
+        mutable std::string seed;
         mutable long long sequence;
 
     public:
@@ -68,9 +67,7 @@ namespace util {
          *
          * @return the previously retrieved host name.
          */
-        static std::string getHostname() {
-            return statics.hostname;
-        }
+        static std::string getHostname();
 
         /**
          * Gets the seed value from a Generated Id, the count portion is removed.
@@ -98,6 +95,10 @@ namespace util {
          */
         static int compare( const std::string& id1, const std::string& id2 );
 
+    private:
+
+        static StaticData& getClassStaticData();
+
     };
 
 }}