Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TLockFile.cxx
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Jan Fiete Grosse-Oetringhaus, 04.06.07
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /**
13 \class TLockFile
14 \ingroup IO
15 
16 A scoped lock based on files.
17 
18 The RAAI idiom is used: the constructor blocks until lock is obtained.
19 Lock is released in the destructor.
20 Use it in scope-blocks like:
21 ~~~{.cpp}
22 {
23  TLockFile lock("path.to.lock.file");
24  // do something you need the lock for
25 } // lock is automatically released
26 ~~~
27 */
28 
29 #include "TLockFile.h"
30 #include "TSystem.h"
31 #include "TFile.h"
32 #include <time.h>
33 
34 ClassImp(TLockFile);
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Default constructor.
38 ///
39 /// Blocks until lock is obtained.
40 /// If a lock exists that is older than the given time limit,
41 /// the file is removed. If timeLimit <= 0, wait for ever.
42 
43 TLockFile::TLockFile(const char *path, Int_t timeLimit) : fPath(path)
44 {
45  while (1) {
46  if (Lock(fPath, timeLimit))
47  break;
48 
49  if (gDebug > 0)
50  Info("TLockFile", "did not aquire lock %s, sleeping...", fPath.Data());
51  gSystem->Sleep(1000);
52  }
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Destructor. Releases the lock.
57 
58 TLockFile::~TLockFile()
59 {
60  if (gDebug > 0)
61  Info("~TLockFile", "releasing lock %s", fPath.Data());
62 
63  gSystem->Unlink(fPath);
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// Internal function that locks with the given path.
68 
69 Bool_t TLockFile::Lock(const char *path, Int_t timeLimit)
70 {
71  Long_t modTime = 0;
72  if (gSystem->GetPathInfo(path, 0, (Long_t*) 0, 0, &modTime) == 0) {
73  if (timeLimit > 0) {
74  if (gDebug > 0)
75  Info("Lock", "%s modification time %ld, %ld seconds ago", path, modTime, time(0) - modTime);
76  if (time(0) - modTime > timeLimit){
77  gSystem->Unlink(path);
78  if (gDebug > 0)
79  Info("Lock", "time expired, removed %s", path);
80  } else
81  return kFALSE;
82  } else
83  return kFALSE;
84  }
85 
86  TString spath = path;
87  spath += "?filetype=raw";
88  TFile *file = TFile::Open(spath, "CREATE");
89  if (!file)
90  return kFALSE;
91 
92  file->Close();
93  delete file;
94 
95  // chance access to 666, so if the lock is expired, other users can remove it
96  // (attention, currently only supported for local files systems)
97  gSystem->Chmod(path, 0666);
98 
99  if (gDebug > 0)
100  Info("Lock", "obtained lock %s", path);
101 
102  return kTRUE;
103 }