Index: /trunk/SophyaLib/BaseTools/segdatablock.h
===================================================================
--- /trunk/SophyaLib/BaseTools/segdatablock.h	(revision 3213)
+++ /trunk/SophyaLib/BaseTools/segdatablock.h	(revision 3214)
@@ -69,5 +69,4 @@
   {
     mSRef = NULL;
-    if (gThsop == NULL) gThsop = new ThSafeOp;
     SetSize(segsz, nbseg);
   }
@@ -75,8 +74,8 @@
   SegDataBlock(const SegDataBlock<T>& a)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
   }
   //! copy constructor with specification of flags for data sharing and element value copy
@@ -84,8 +83,8 @@
   {
     if (share) {
-      gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+      a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
       mSRef = a.mSRef;
       mSRef->nref++;
-      gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+      a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     }
     else {
@@ -97,11 +96,10 @@
   SegDataBlock(const SegDBInterface<T>& a)
   {
-    if (gThsop == NULL) gThsop = new ThSafeOp;
     SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a);
     if (sdb != NULL) { 
-      gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+      sdb->mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
       mSRef = sdb->mSRef;
       mSRef->nref++;
-      gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+      sdb->mSRefg->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     }
     else Clone(a, true);
@@ -111,7 +109,5 @@
   {
     //DEL    cout << " DEBUG-~SegDataBlock() " << hex << mSRef << dec << " NRef()= " << NRef() << endl;
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
   }
 
@@ -121,8 +117,9 @@
   {
     T * p = new T[mSRef->segsize];
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->dseg.push_back(p);
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
-    return( mSRef->dseg.size());
+    size_t rs = mSRef->dseg.size();
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    return( rs );
   }
 
@@ -131,21 +128,21 @@
   virtual void SetSize(size_t segsz, size_t nbseg=0)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
     mSRef = new SDREF;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->nref = 1;
     mSRef->segsize = segsz;
     mSRef->dsid = AnyDataObj::getUniqueId();
     for(size_t k=0; k<nbseg; k++) Extend_P();
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
   }
 //! Shares the data between two SegDataBlock objects
   void Share(const SegDataBlock<T>& a)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
   }
 
@@ -153,10 +150,10 @@
   void Clone(const SegDBInterface<T> & a, bool cpval=true)
   {
-    gThsop->lock();   //  (ThreadSafe) - Start of atomic operation 
     Delete();
     mSRef = new SDREF;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->nref = 1;
     mSRef->segsize = a.SegmentSize();
-    gThsop->unlock();   //  (ThreadSafe) - End of atomic operation
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     for(size_t k=0; k<a.NbSegments(); k++) {
       Extend();
@@ -254,6 +251,11 @@
   {
     if (mSRef == NULL) return;
+    mSRef->gThsop.lock();
     mSRef->nref--;
-    if (mSRef->nref > 0)  { mSRef = NULL; return; }
+    if (mSRef->nref > 0)  { 
+     mSRef->gThsop.unlock();
+     mSRef = NULL; 
+      return; 
+    }
     //DEL cout << " DEBUG-SegDataBlock::Delete() NbSegments() = " << NbSegments() << endl;
     for(size_t k=0; k<NbSegments(); k++) {
@@ -261,4 +263,5 @@
       mSRef->dseg[k] = NULL;
     }
+    mSRef->gThsop.unlock();
     delete mSRef;
     mSRef = NULL;
@@ -280,12 +283,11 @@
     std::vector<T *> dseg;
     size_t segsize;
+    ThSafeOp gThsop;  // Mutex for thread safe operation 
   } SDREF;
   /*! \endcond */
   SDREF *  mSRef;    //!< SDREF structure for reference sharing
 
-  static ThSafeOp* gThsop;  //!< Mutex for thread safe operation 
 };
 
-template <class T> ThSafeOp* SegDataBlock<T>::gThsop  = NULL;  // static member initialized to NULL
 
 //! Definition of operator \<\< for ascii formatted output of SegDataBlock
Index: /trunk/SophyaLib/BaseTools/swsegdb.h
===================================================================
--- /trunk/SophyaLib/BaseTools/swsegdb.h	(revision 3213)
+++ /trunk/SophyaLib/BaseTools/swsegdb.h	(revision 3214)
@@ -84,8 +84,12 @@
   SwSegDataBlock(const SwSegDataBlock<T>& a)
   {
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
+    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    size_t segsz = mSRef->segsize;
+    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    buff = new T[segsz];
+    bidx = -1;        
+    fgcstbuff = true; 
   }
 
@@ -113,12 +117,9 @@
   {
     Delete();
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = new SWSDREF;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef->nref = 1;
     mSRef->segsize = segsz;
     mSRef->dsid = AnyDataObj::getUniqueId();
-    mSRef->buff = new T[segsz];
-    mSRef->bidx = -1;        
-    mSRef->fgcstbuff = true; 
     for(size_t k=0; k<nbseg; k++) {
       mSRef->swp.push_back(0);
@@ -127,4 +128,7 @@
     mSRef->swapper = NULL;
     mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    buff = new T[segsz];
+    bidx = -1;        
+    fgcstbuff = true; 
   }
 
@@ -148,21 +152,13 @@
   virtual T* GetSegment(size_t k) 
   {
-    T* rs = NULL;
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     getSeg(k);
-    mSRef->fgcstbuff = false;
-    rs = mSRef->buff;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
-    return rs;
+    fgcstbuff = false;
+    return buff;
   }
   //! Return the const (read-only) pointer to data segment \b k         
   virtual T const * GetCstSegment(size_t k) const 
   {
-    T const * rs = NULL;
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
-    if (getSeg(k))  mSRef->fgcstbuff = true;
-    rs = mSRef->buff;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
-    return rs;    
+    if (getSeg(k))  fgcstbuff = true;
+    return buff;    
   }
 
@@ -171,8 +167,12 @@
   {
     Delete();
-    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
+    a.mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
     mSRef = a.mSRef;
     mSRef->nref++;
-    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    size_t segsz = mSRef->segsize;
+    a.mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    buff = new T[segsz];
+    bidx = -1;        
+    fgcstbuff = true; 
     return *this;
   }
@@ -182,11 +182,11 @@
   {
     mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
-    if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
-      int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx, 
-					       mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
-      mSRef->swp[mSRef->bidx] = nswp;
-      mSRef->fgwp[mSRef->bidx] = true;
-      mSRef->bidx = -1;
-      mSRef->fgcstbuff = true;  
+    if ((bidx >= 0) && !fgcstbuff) {
+      int_8 nswp = mSRef->swapper->WriteToSwap(buff, mSRef->segsize, bidx, 
+					       mSRef->swp[bidx], mSRef->fgwp[bidx]);
+      mSRef->swp[bidx] = nswp;
+      mSRef->fgwp[bidx] = true;
+      bidx = -1;
+      fgcstbuff = true;  
    }
     mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
@@ -211,26 +211,29 @@
     if (mSRef->nref > 0)  { 
       mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+      delete[] buff;
       mSRef = NULL; 
       return; 
     }
     if (mSRef->swapper) delete mSRef->swapper;  
-    delete[] mSRef->buff;
     mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
     delete mSRef;
+    delete[] buff;
     mSRef = NULL;  
   } 
   bool getSeg(size_t k) const
   {
-    if (k == mSRef->bidx)  return false;
-    if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
-      int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx, 
-					       mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
-      mSRef->swp[mSRef->bidx] = nswp;
-      mSRef->fgwp[mSRef->bidx] = true;
+    if (k == bidx)  return false;
+    mSRef->gThsop.lock();   //  (ThreadSafe) - Start of atomic operation 
+    if ((bidx >= 0) && !fgcstbuff) {
+      int_8 nswp = mSRef->swapper->WriteToSwap(buff, mSRef->segsize, bidx, 
+					       mSRef->swp[bidx], mSRef->fgwp[bidx]);
+      mSRef->swp[bidx] = nswp;
+      mSRef->fgwp[bidx] = true;
     }
     if (mSRef->fgwp[k]) 
-      mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], mSRef->buff, mSRef->segsize);
+      mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], buff, mSRef->segsize);
     //DEL-02022007    else { delete[] mSRef->buff;  mSRef->buff = new T[mSRef->segsize]; }  
-    mSRef->bidx = k;
+    mSRef->gThsop.unlock();   //  (ThreadSafe) - End of atomic operation
+    bidx = k;
     return true;
   }
@@ -243,7 +246,4 @@
     mutable std::vector< int_8 > swp;  // swap position tag for each segment
     mutable std::vector< bool > fgwp;  // swap flag (true = already swapped)  for each segment
-    mutable T * buff;                // Data buffer   
-    mutable int_8 bidx;              // segment index (number) corresponding to buffer 
-    mutable bool fgcstbuff;            // true : this is a constant T * buff<
     DataSwapperInterface<T> * swapper;   // Data swapper
     ThSafeOp gThsop;         // Mutex for thread safe operation one / SWSDREF struct 
@@ -251,4 +251,7 @@
   //! \endcond
   SWSDREF *  mSRef;    //!< SWSDREF structure for reference sharing
+  mutable T * buff;                // Data buffer   
+  mutable int_8 bidx;              // segment index (number) corresponding to buffer 
+  mutable bool fgcstbuff;          // true : this is a constant T * buff
 
 };
