ARC SDK
FileLock.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef __ARC_FILELOCK_H__
4 #define __ARC_FILELOCK_H__
5 
6 #include <string>
7 
8 #include <arc/Logger.h>
9 
10 namespace Arc {
11 
13 
36  class FileLock {
37  public:
39  const static int DEFAULT_LOCK_TIMEOUT;
41  const static std::string LOCK_SUFFIX;
42 
44 
50  FileLock(const std::string& filename,
51  unsigned int timeout=DEFAULT_LOCK_TIMEOUT,
52  bool use_pid=true);
53 
55 
67  bool acquire(bool& lock_removed);
68 
70 
75  bool acquire();
76 
78 
82  bool release(bool force=false);
83 
85 
93  int check(bool log_error = true);
94 
96  static std::string getLockSuffix();
97 
98  private:
100  std::string filename;
102  std::string lock_file;
104  int timeout;
106  bool use_pid;
108  std::string pid;
110  std::string hostname;
112  static Logger logger;
113 
115  bool acquire_(bool& lock_removed);
116 
118  bool write_pid(int h);
119  };
120 
121 } // namespace Arc
122 
123 #endif
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
static const int DEFAULT_LOCK_TIMEOUT
Default timeout for a lock.
Definition: FileLock.h:39
bool release(bool force=false)
Release the lock.
static const std::string LOCK_SUFFIX
Suffix added to file name to make lock file.
Definition: FileLock.h:41
FileLock(const std::string &filename, unsigned int timeout=DEFAULT_LOCK_TIMEOUT, bool use_pid=true)
Create a new FileLock object.
A logger class.
Definition: Logger.h:493
A general file locking class.
Definition: FileLock.h:36
int check(bool log_error=true)
Check the lock is valid.
bool acquire()
Acquire the lock.
static std::string getLockSuffix()
Get the lock suffix used.