www.mooseframework.org
Public Member Functions | Protected Attributes | List of all members
LockFile Class Reference

Gets an exclusive lock on a file. More...

#include <LockFile.h>

Public Member Functions

 LockFile (const std::string &filename, bool do_lock=true)
 
 ~LockFile ()
 

Protected Attributes

const bool _do_lock
 
int _fd
 
const std::string _filename
 

Detailed Description

Gets an exclusive lock on a file.

This uses RAII to obtain the lock in the constructor and release the lock in the destructor. Additionally, to allow for easier use as a stack variable, an optional bool is allowed to specify that no locking is actually done. This is useful for the case where only certain processors need to obtain the lock.

Definition at line 28 of file LockFile.h.

Constructor & Destructor Documentation

LockFile::LockFile ( const std::string &  filename,
bool  do_lock = true 
)

Definition at line 21 of file LockFile.C.

22  : _do_lock(do_lock), _fd(-1), _filename(filename)
23 {
24  if (_do_lock)
25  {
26  _fd = open(filename.c_str(), O_RDWR | O_CREAT, 0666);
27  if (_fd == -1)
28  mooseError("Failed to open file", filename);
29  if (flock(_fd, LOCK_EX) != 0)
30  mooseWarning("Failed to lock file ", filename);
31  }
32 }
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
const std::string _filename
Definition: LockFile.h:37
int _fd
Definition: LockFile.h:36
const bool _do_lock
Definition: LockFile.h:35
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194
LockFile::~LockFile ( )

Definition at line 34 of file LockFile.C.

35 {
36  if (_do_lock)
37  {
38  if (flock(_fd, LOCK_UN) != 0)
39  mooseWarning("Failed to unlock file ", _filename);
40  close(_fd);
41  }
42 }
const std::string _filename
Definition: LockFile.h:37
int _fd
Definition: LockFile.h:36
the_pc close()
const bool _do_lock
Definition: LockFile.h:35
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194

Member Data Documentation

const bool LockFile::_do_lock
protected

Definition at line 35 of file LockFile.h.

Referenced by LockFile(), and ~LockFile().

int LockFile::_fd
protected

Definition at line 36 of file LockFile.h.

Referenced by LockFile(), and ~LockFile().

const std::string LockFile::_filename
protected

Definition at line 37 of file LockFile.h.

Referenced by ~LockFile().


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