diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarLockFile.hpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/core/StarLockFile.hpp')
-rw-r--r-- | source/core/StarLockFile.hpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/source/core/StarLockFile.hpp b/source/core/StarLockFile.hpp new file mode 100644 index 0000000..af89c7f --- /dev/null +++ b/source/core/StarLockFile.hpp @@ -0,0 +1,42 @@ +#ifndef STAR_LOCK_FILE_HPP +#define STAR_LOCK_FILE_HPP + +#include "StarMaybe.hpp" +#include "StarString.hpp" + +namespace Star { + +class LockFile { +public: + // Convenience function, tries to acquire a lock, and if succesfull returns an + // already locked + // LockFile. + static Maybe<LockFile> acquireLock(String const& filename, int64_t lockTimeout = 1000); + + LockFile(String const& filename); + LockFile(LockFile&& lockFile); + // Automatically unlocks. + ~LockFile(); + + LockFile(LockFile const&) = delete; + LockFile& operator=(LockFile const&) = delete; + + LockFile& operator=(LockFile&& lockFile); + + // Wait at most timeout time to acquire the file lock, and return true if the + // lock was acquired. If timeout is negative, wait forever. + bool lock(int64_t timeout = 0); + void unlock(); + + bool isLocked() const; + +private: + static int64_t const MaximumSleepMillis = 25; + + String m_filename; + shared_ptr<void> m_handle; +}; + +} + +#endif |