Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
16A scoped lock based on files.
17
18The RAAI idiom is used: the constructor blocks until lock is obtained.
19Lock is released in the destructor.
20Use 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 <ctime>
33
34
35////////////////////////////////////////////////////////////////////////////////
36/// Default constructor.
37///
38/// Blocks until lock is obtained.
39/// If a lock exists that is older than the given time limit,
40/// the file is removed. If timeLimit <= 0, wait for ever.
41
42TLockFile::TLockFile(const char *path, Int_t timeLimit) : fPath(path)
43{
44 while (1) {
45 if (Lock(fPath, timeLimit))
46 break;
47
48 if (gDebug > 0)
49 Info("TLockFile", "did not aquire lock %s, sleeping...", fPath.Data());
50 gSystem->Sleep(1000);
51 }
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Destructor. Releases the lock.
56
58{
59 if (gDebug > 0)
60 Info("~TLockFile", "releasing lock %s", fPath.Data());
61
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// Internal function that locks with the given path.
67
69{
70 Long_t modTime = 0;
71 if (gSystem->GetPathInfo(path, 0, (Long_t*) 0, 0, &modTime) == 0) {
72 if (timeLimit > 0) {
73 if (gDebug > 0)
74 Info("Lock", "%s modification time %ld, %ld seconds ago", path, modTime, time(0) - modTime);
75 if (time(0) - modTime > timeLimit){
76 gSystem->Unlink(path);
77 if (gDebug > 0)
78 Info("Lock", "time expired, removed %s", path);
79 } else
80 return kFALSE;
81 } else
82 return kFALSE;
83 }
84
85 TString spath = path;
86 spath += "?filetype=raw";
87 TFile *file = TFile::Open(spath, "CREATE");
88 if (!file)
89 return kFALSE;
90
91 file->Close();
92 delete file;
93
94 // chance access to 666, so if the lock is expired, other users can remove it
95 // (attention, currently only supported for local files systems)
96 gSystem->Chmod(path, 0666);
97
98 if (gDebug > 0)
99 Info("Lock", "obtained lock %s", path);
100
101 return kTRUE;
102}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:627
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3764
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:958
~TLockFile() override
Destructor. Releases the lock.
Definition TLockFile.cxx:57
TString fPath
Path to file holding the lock.
Definition TLockFile.h:26
TLockFile(const TLockFile &)=delete
Bool_t Lock(const char *path, Int_t timeLimit)
Internal function that locks with the given path.
Definition TLockFile.cxx:68
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1045
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
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:1519
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:1409
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:435
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1392