TLockFile.cxx

Go to the documentation of this file.
00001 // @(#)root/io:$Id: TLockFile.cxx 36484 2010-11-02 16:00:10Z rdm $
00002 // Author: Jan Fiete Grosse-Oetringhaus, 04.06.07
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TLockFile                                                            //
00015 //                                                                      //
00016 // Lock an object using a file.                                         //
00017 // Constructor blocks until lock is obtained. Lock is released in the   //
00018 // destructor.                                                          //
00019 //                                                                      //
00020 // Use it in scope-blocks like:                                         //
00021 // {                                                                    //
00022 //    TLockFile lock("path.to.lock.file");                              //
00023 //    // do something you need the lock for                             //
00024 // } // lock is automatically released                                  //
00025 //                                                                      //
00026 //////////////////////////////////////////////////////////////////////////
00027 
00028 #include "TLockFile.h"
00029 #include "TSystem.h"
00030 #include "TFile.h"
00031 #include <time.h>
00032 
00033 ClassImp(TLockFile)
00034 
00035 //______________________________________________________________________________
00036 TLockFile::TLockFile(const char *path, Int_t timeLimit) : fPath(path)
00037 {
00038    // Default constructor. Blocks until lock is obtained.
00039    // If a lock exists that is older than the given time limit,
00040    // the file is removed. If timeLimit <= 0, wait for ever.
00041 
00042    while (1) {
00043       if (Lock(fPath, timeLimit))
00044          break;
00045 
00046       if (gDebug > 0)
00047          Info("TLockFile", "did not aquire lock %s, sleeping...", fPath.Data());
00048       gSystem->Sleep(1000);
00049    }
00050 }
00051 
00052 //______________________________________________________________________________
00053 TLockFile::~TLockFile()
00054 {
00055    // Destructor. Releases the lock.
00056 
00057    if (gDebug > 0)
00058       Info("~TLockFile", "releasing lock %s", fPath.Data());
00059 
00060    gSystem->Unlink(fPath);
00061 }
00062 
00063 //______________________________________________________________________________
00064 Bool_t TLockFile::Lock(const char *path, Int_t timeLimit)
00065 {
00066    // Internal function that locks with the given path.
00067 
00068    Long_t modTime = 0;
00069    if (gSystem->GetPathInfo(path, 0, (Long_t*) 0, 0, &modTime) == 0) {
00070       if (timeLimit > 0) {
00071          if (gDebug > 0)
00072             Info("Lock", "%s modification time %ld, %ld seconds ago", path, modTime, time(0) - modTime);
00073          if (time(0) - modTime > timeLimit){
00074             gSystem->Unlink(path);
00075             if (gDebug > 0)
00076                Info("Lock", "time expired, removed %s", path);
00077          } else
00078             return kFALSE;
00079       } else
00080          return kFALSE;
00081    }
00082 
00083    TString spath = path;
00084    spath += "?filetype=raw";
00085    TFile *file = TFile::Open(spath, "CREATE");
00086    if (!file)
00087       return kFALSE;
00088 
00089    file->Close();
00090    delete file;
00091 
00092    // chance access to 666, so if the lock is expired, other users can remove it
00093    // (attention, currently only supported for local files systems)
00094    gSystem->Chmod(path, 0666);
00095 
00096    if (gDebug > 0)
00097       Info("Lock", "obtained lock %s", path);
00098 
00099    return kTRUE;
00100 }

Generated on Tue Jul 5 14:30:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1