00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __libutilxx__sella__util__LockFile_H__
00010 #define __libutilxx__sella__util__LockFile_H__
00011
00012 #include "../../common.h"
00013 #include "FileException.h"
00014
00015 #include <limits.h>
00016 #include <stdlib.h>
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 #include <unistd.h>
00020
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <time.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026
00027 #include <string>
00028 #include <mutex>
00029
00030 namespace sella {
00031 namespace util {
00032 class LockFile;
00033 }
00034 }
00035
00036 class sella::util::LockFile {
00037 public:
00038 static const useconds_t RetryInterval = 100000;
00039
00040 public:
00041 LockFile(const std::string &fullpath, useconds_t staleAge = -1, bool removeStale = true, uid_t uid = -1, gid_t gid = -1, int mode = 0644) throw ();
00042 virtual ~LockFile();
00043
00044 void setRemoveStale(bool on = true);
00045 void setStaleAge(useconds_t staleAge = -1) throw ();
00046 void setUID(uid_t uid = -1) throw ();
00047 void setGID(gid_t gid = -1) throw ();
00048 void setMode(int mode = 0644) throw ();
00049
00050 bool lock(useconds_t retryInterval = RetryInterval) throw (FileException);
00051 bool tryLock(void) throw (FileException);
00052 bool tryLockUntil(time_t timeout, useconds_t retryInterval = RetryInterval) throw (FileException);
00053 bool tryLockFor(time_t duration, useconds_t retryInterval = RetryInterval) throw (FileException);
00054 bool tryLockCount(int64_t retries, useconds_t retryInterval = RetryInterval) throw (FileException);
00055 bool unlock(void) throw (FileException);
00056 bool remove(void) throw ();
00057
00058 bool ownLock(void) throw ();
00059 bool isLocked(void) throw ();
00060 bool isStale(void) throw ();
00061
00062 protected:
00063 bool lock_nonblock(void) throw (FileException);
00064 bool unlink(void) throw ();
00065 void close(void) throw ();
00066
00067 private:
00068 std::string fullpath;
00069 useconds_t stale;
00070 bool removeStale;
00071 uid_t uid;
00072 gid_t gid;
00073 int mode;
00074 int fd;
00075 std::mutex mutex;
00076 };
00077
00078 #endif
00079
00080
00081
00082