Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarLockFile.hpp
blob: a836ceeb9c4785de6687a4699df72baac9d0c1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once

#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;
};

}