Logo ROOT   6.12/07
Reference Guide
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 
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 
59 {
60  if (gDebug > 0)
61  Info("~TLockFile", "releasing lock %s", fPath.Data());
62 
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 }
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition: TSystem.cxx:1374
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
Basic string class.
Definition: TString.h:125
TLockFile(const TLockFile &)
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3950
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1357
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:445
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
virtual int Chmod(const char *file, UInt_t mode)
Set the file permission bits. Returns -1 in case or error, 0 otherwise.
Definition: TSystem.cxx:1482
A scoped lock based on files.
Definition: TLockFile.h:19
#define ClassImp(name)
Definition: Rtypes.h:359
virtual ~TLockFile()
Destructor. Releases the lock.
Definition: TLockFile.cxx:58
Definition: file.py:1
R__EXTERN Int_t gDebug
Definition: Rtypes.h:86
const Bool_t kTRUE
Definition: RtypesCore.h:87
Bool_t Lock(const char *path, Int_t timeLimit)
Internal function that locks with the given path.
Definition: TLockFile.cxx:69
TString fPath
Path to file holding the lock.
Definition: TLockFile.h:26
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:916
const char * Data() const
Definition: TString.h:345