Belle II Software light-2406-ragdoll
FileSystem::Lock Class Reference

Helper class for locking a file. More...

#include <FileSystem.h>

Public Member Functions

 Lock (const std::string &fileName, bool readonly=false)
 Construct a Lock object for the given file.
 
 ~Lock ()
 Destructor.
 
bool lock (int timeout=300, bool ignoreErrors=false)
 Try to lock the file.
 

Private Attributes

int m_file
 File descriptor of file to be locked.
 
bool m_readOnly
 if this is a read-only lock (multiple processes can hold one).
 

Detailed Description

Helper class for locking a file.

Definition at line 97 of file FileSystem.h.

Constructor & Destructor Documentation

◆ Lock()

Lock ( const std::string &  fileName,
bool  readonly = false 
)
explicit

Construct a Lock object for the given file.

Parameters
fileNameName of the file to be locked (if it does not exist, it will be created)
readonlycreate a read-only lock (multiple processes can hold one)

Definition at line 177 of file FileSystem.cc.

177 :
178 m_readOnly(readonly)
179{
180 const int mode = readonly ? O_RDONLY : O_RDWR;
181 m_file = open(fileName.c_str(), mode | O_CREAT, 0640);
182}
bool m_readOnly
if this is a read-only lock (multiple processes can hold one).
Definition: FileSystem.h:124
int m_file
File descriptor of file to be locked.
Definition: FileSystem.h:123

◆ ~Lock()

~Lock ( )

Destructor.

Releases the lock

Definition at line 184 of file FileSystem.cc.

185{
186 if (m_file >= 0) close(m_file);
187}

Member Function Documentation

◆ lock()

bool lock ( int  timeout = 300,
bool  ignoreErrors = false 
)

Try to lock the file.

Note
Locks are not exclusive inside the same process, i.e. lock() will succeed even if a lock is already held by the current process.
Parameters
timeoutTime in seconds to wait for a lock (default is rather high to deal with slow FS at KEKCC)
ignoreErrorsif true just return if locking was unsuccessful but don't print an error
Returns
True if the lock could be obtained, false if file could not be opened or timeout is reached

Definition at line 189 of file FileSystem.cc.

190{
191 if (m_file < 0) return false;
192
193 auto const maxtime = std::chrono::steady_clock::now() + std::chrono::seconds(timeout);
194 std::default_random_engine random;
195 std::uniform_int_distribution<int> uniform(1, 100);
196
197 /* Note:
198 * Previously, this used flock(), which doesn't work with GPFS.
199 * fcntl() does, and also should be more likely to work on NFS.
200 * If you use the 'nolock' mount option to NFS, you are on your own.
201 */
202 struct flock fl;
203 memset(&fl, '\0', sizeof(fl));
204 fl.l_type = m_readOnly ? F_RDLCK : F_WRLCK;
205 //lock entire file
206 fl.l_whence = SEEK_SET;
207 fl.l_start = 0;
208 fl.l_len = 0;
209
210 while (true) {
211 int lock = fcntl(m_file, F_SETLK, &fl);
212 if (lock == 0)
213 return true;
214 else if (std::chrono::steady_clock::now() > maxtime)
215 break;
216 if (errno != EAGAIN && errno != EACCES && errno != EINTR) break;
217 usleep(uniform(random) * 1000);
218 }
219 if (!ignoreErrors) B2ERROR("Locking failed: " << strerror(errno));
220 return false;
221}
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
Definition: FileSystem.cc:189

Member Data Documentation

◆ m_file

int m_file
private

File descriptor of file to be locked.

Definition at line 123 of file FileSystem.h.

◆ m_readOnly

bool m_readOnly
private

if this is a read-only lock (multiple processes can hold one).

Definition at line 124 of file FileSystem.h.


The documentation for this class was generated from the following files: