BUG#:8701
authormarek <marek>
Mon, 1 Feb 2010 15:34:13 +0000 (15:34 +0000)
committermarek <marek>
Mon, 1 Feb 2010 15:34:13 +0000 (15:34 +0000)
TITLE: helgrind reporting data race condition in CMPI_ThreadContext

DESCRIPTION:

src/Pegasus/ProviderManager2/CMPI/CMPI_ThreadContext.cpp
src/Pegasus/ProviderManager2/CMPI/CMPI_ThreadContext.h

index 3a78798dbb1a9d62fa8a3a3a28a577a22bbf1fac..1b1d26d209ce9e91cc56c81a6a2bdadff5bae519 100644 (file)
 PEGASUS_USING_STD;
 PEGASUS_NAMESPACE_BEGIN
 
-TSDKeyType CMPI_ThreadContext::contextKey;
-Once CMPI_ThreadContext::contextKeyOnce = PEGASUS_ONCE_INITIALIZER;
+// this static gets initialized on load of CMPIProviderManager library
+// in the constructor of CMPI_ThreadContextKey the key for the thread-specific
+// storage used by CMPI is created
+// This was done to ensure that the key gets created exactly once, saving the
+// need to check for it on later access.
+CMPI_ThreadContextKey CMPI_ThreadContext::globalThreadContextKey;
 
 CMPI_ThreadContext::CMPI_ThreadContext(
 const CMPIBroker *mb,
@@ -51,9 +55,9 @@ const CMPIContext *ctx )
     CIMfirst=CIMlast=NULL;
     broker=mb;
     context=ctx;
-    TSDKeyType k=getContextKey();
-    prev=(CMPI_ThreadContext*)TSDKey::get_thread_specific(k);
-    TSDKey::set_thread_specific(k,this);
+    prev=(CMPI_ThreadContext*)
+        TSDKey::get_thread_specific(globalThreadContextKey.contextKey);
+    TSDKey::set_thread_specific(globalThreadContextKey.contextKey,this);
     return;
 }
 
@@ -65,9 +69,7 @@ CMPI_ThreadContext::~CMPI_ThreadContext()
         (reinterpret_cast<CMPIInstance*>(cur))->ft->release(
         reinterpret_cast<CMPIInstance*>(cur));
     }
-
-    TSDKeyType k=getContextKey();
-    TSDKey::set_thread_specific(k,prev);
+    TSDKey::set_thread_specific(globalThreadContextKey.contextKey,prev);
 }
 
 PEGASUS_NAMESPACE_END
index 420d9943700c877adec0b346319a2eed636a78bf..fdfb42ff216337be94c37c29b9abd1a74bfa403c 100644 (file)
@@ -39,7 +39,6 @@
 #endif
 
 #include <Pegasus/Common/TSDKey.h>
-#include <Pegasus/Common/Once.h>
 #include <Pegasus/Provider/CMPI/cmpidt.h>
 #include <Pegasus/Provider/CMPI/cmpift.h>
 #include "CMPI_Object.h"
@@ -55,18 +54,22 @@ PEGASUS_NAMESPACE_BEGIN
                     { if (i->n) i->n->p=i->p; else l=i->p; \
                       if (i->p) i->p->n=i->n; else f=i->n;}
 
+class CMPI_ThreadContextKey
+{
+public:
+    CMPI_ThreadContextKey()
+    {
+        TSDKey::create(&contextKey);
+    };
+    ~CMPI_ThreadContextKey()
+    {
+        TSDKey::destroy(contextKey);
+    };
+    TSDKeyType contextKey;
+};
+
 class CMPI_ThreadContext
 {
-    /**
-       static pthread_key_t contextKey;
-    */
-    static TSDKeyType contextKey;
-    static Once contextKeyOnce;
-    static void context_key_alloc();
-    /**
-       static pthread_key_t getContextKey();
-    */
-    static TSDKeyType getContextKey();
     CMPI_ThreadContext* prev;
     const CMPIBroker *broker;
     const CMPIContext *context;
@@ -86,6 +89,8 @@ public:
    */
     CMPI_ThreadContext(const CMPIBroker*,const CMPIContext*);
     ~CMPI_ThreadContext();
+
+    static CMPI_ThreadContextKey globalThreadContextKey;
 };
 
 PEGASUS_NAMESPACE_END
@@ -95,17 +100,6 @@ PEGASUS_NAMESPACE_END
 
 PEGASUS_NAMESPACE_BEGIN
 
-inline void CMPI_ThreadContext::context_key_alloc()
-{
-    TSDKey::create(&contextKey);
-}
-
-inline TSDKeyType CMPI_ThreadContext::getContextKey()
-{
-    once(&contextKeyOnce, context_key_alloc);
-    return contextKey;
-}
-
 inline void CMPI_ThreadContext::add(CMPI_Object *o)
 {
     ENQ_TOP_LIST(o,CIMfirst,CIMlast,next,prev);
@@ -140,8 +134,8 @@ inline void CMPI_ThreadContext::remObject(CMPI_Object* o)
 
 inline CMPI_ThreadContext* CMPI_ThreadContext::getThreadContext()
 {
-    TSDKeyType k=getContextKey();
-    return(CMPI_ThreadContext*)TSDKey::get_thread_specific(k);
+    return (CMPI_ThreadContext*)
+        TSDKey::get_thread_specific(globalThreadContextKey.contextKey);
 }
 
 inline const CMPIBroker* CMPI_ThreadContext::getBroker()