-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathInterProcessLock.h
64 lines (53 loc) · 2.15 KB
/
InterProcessLock.h
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef INTERPROCESS_LOCK_H_INCLUDED
#define INTERPROCESS_LOCK_H_INCLUDED
#include <memory>
#include <string>
/// Interprocess lock
///
/// Use this class to limit a given operation to a single processes. For
/// example, to ensure only a single instance of a GUI application runs.
///
/// An acquired lock has process lifetime - the system will clean it up in the
/// event that the process crashes.
///
/// On unix, the implementation is based on the flock() API, so the lock is
/// shared between parent and child after a call to either fork() or execve().
/// In this case, both processes must exit or call unlock() before the lock is
/// released.
class InterProcessLock
{
public:
/// Create lock named with the UTF-8 string `lockName`
InterProcessLock(const std::string& lockName);
/// Calls unlock()
~InterProcessLock();
/// Try to acquire the lock, returning immediately.
///
/// Return true if the lock was acquired; return false if the lock
/// could not be acquired immediately (ie, it's held by another
/// process).
bool tryLock();
/// Inherit lock from a parent process.
///
/// The lockId parameter provides a way pass a system dependent lock id
/// from a parent to child process. Depending on the system, the lock
/// may already have been inherited during the creation of the child
/// process, in which case this is just book keeping and hooks things
/// up so that unlock() can be used.
///
/// Return true if the lock was successfully inherited, false otherwise.
bool inherit(const std::string& lockId);
/// Release the lock and clean up.
///
/// Do nothing if the lock wasn't previously acquired.
void unlock();
/// Format lock identifier as a string for passing to a child process.
///
/// If currently unlocked, return the empty string.
std::string makeLockId() const;
private:
class Impl;
// Hidden system-dependent implementation
std::unique_ptr<Impl> m_impl;
};
#endif // INTERPROCESS_LOCK_H_INCLUDED