//*CMZ :  2.24/01 02/02/2000  12.45.29  by  Fons Rademakers
//*CMZ :  2.23/12 03/01/2000  09.37.10  by  Fons Rademakers
//*CMZ :  2.23/09 10/11/99  09.35.49  by  Rene Brun
//*CMZ :  2.22/06 21/06/99  17.49.20  by  Rene Brun
//*CMZ :  2.00/00 03/11/97  22.36.08  by  Victor Perev
//*CMZ :  1.03/03 27/08/97  13.01.58  by  Victor Perev
//*-- Author :    Fons Rademakers   26/06/97

//*KEEP,CopyRight,T=C.
/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/
//*KEND.

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TMutex                                                               //
//                                                                      //
// This class implements mutex locks. A mutex is a mutual exclusive     //
// lock. The actual work is done via the TMutexImp class (either        //
// TPosixMutex, TSolarisMutex or TNTMutex).                             //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

//*KEEP,TMutex,T=C++.
#include "TMutex.h"
//*KEEP,TThreadFactory,T=C++.
#include "TThreadFactory.h"
//*KEND.


ClassImp(TLockGuard)
ClassImp(TMutex)

//______________________________________________________________________________
 TMutex::TMutex(Bool_t recursive)
{
   // Create a mutex lock. The actual mutex implementation will be
   // provided via the TThreadFactory.

   fMutexImp = gThreadFactory->CreateMutexImp();
   fId  = 0;
   fRef = recursive ? 0 : -1;

   if (!fMutexImp)
      Error("TMutex", "could not create TMutexImp");
}

//______________________________________________________________________________
 Int_t TMutex::Lock()
{
   // Lock the mutex. Returns 0 when no error, 13 when mutex was already locked
   // by this thread.

   UInt_t id = TThread::SelfId();
   if (id == fId) {
      if (fRef < 0) {
         Error("Lock", "mutex is already locked by this thread %dn", fId);
         return 13;
      } else {
         fRef++;
         return 0;
      }
   }
   Int_t iret = fMutexImp->Lock();
   fId = id;
   return iret;
}

//______________________________________________________________________________
 Int_t TMutex::TryLock()
{
   // Try to lock mutex. Returns 0 when no error, 13 when mutex was already
   // locked by this thread.

   UInt_t id = TThread::SelfId();
   if (id == fId) {
      if (fRef < 0) {
         Error("TryLock", "mutex is already locked by this thread %dn", fId);
         return 13;
      } else {
         fRef++;
         return 0;
      }
   }
   Int_t iret = fMutexImp->TryLock();
   if (iret == 0) fId = id;
   return iret;
}

//______________________________________________________________________________
 Int_t TMutex::UnLock()
{
   // Unlock the mutex. Returns 0 when no error, 13 when mutex was already
   // locked by this thread.

   UInt_t id = TThread::SelfId();
   if (id != fId) {
      Error("UnLock", "thread %d tries to unlock mutex locked by thread %dn", id, fId);
      return 13;
   }

   if (fRef > 0) {
      fRef--;
      return 0;
   }
   fId = 0;
   return fMutexImp->UnLock();
}

//______________________________________________________________________________
 Int_t TMutex::CleanUp()
{
   // Clean up of mutex if it belongs to thread.

   if (UInt_t(TThread::SelfId()) != fId) return 0;
   if (fRef > 0) fRef = 0;
   return UnLock();
}


ROOT page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.