00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00039
00040
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
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
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
00093
00094 gSystem->Chmod(path, 0666);
00095
00096 if (gDebug > 0)
00097 Info("Lock", "obtained lock %s", path);
00098
00099 return kTRUE;
00100 }