www.mooseframework.org
LockFile.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "LockFile.h"
16 #include "MooseError.h"
17 #include <fcntl.h>
18 #include <sys/file.h>
19 #include <unistd.h>
20 
21 LockFile::LockFile(const std::string & filename, bool do_lock)
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 }
33 
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 }
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
the_pc close()
const bool _do_lock
Definition: LockFile.h:35
~LockFile()
Definition: LockFile.C:34
LockFile(const std::string &filename, bool do_lock=true)
Definition: LockFile.C:21
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194