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 <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
43TLockFile::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
69Bool_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}
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
Int_t gDebug
Definition TROOT.cxx:597
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
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:4075
A scoped lock based on files.
Definition TLockFile.h:19
~TLockFile() override
Destructor. Releases the lock.
Definition TLockFile.cxx:58
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:69
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:961
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
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:1493
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:1385
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:424
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1368
Definition file.py:1