blob: 56cbacefea8cedb690c283e2f0db594f2f509723 (
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
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
65
66
67
68
69
70
71
72
73
74
75
|
module filelock
import time
fn C.DeleteFileW(&u16) bool
fn C.CreateFileW(&u16, u32, u32, voidptr, u32, u32, voidptr) voidptr
fn C.CloseHandle(voidptr) bool
pub fn (mut l FileLock) unlink() {
if l.fd != -1 {
C.CloseHandle(l.fd)
l.fd = -1
}
t_wide := l.name.to_wide()
C.DeleteFileW(t_wide)
}
pub fn (mut l FileLock) acquire() ?bool {
if l.fd != -1 {
// lock already acquired by this instance
return false
}
fd := open(l.name)
if fd == -1 {
return error('cannot create lock file $l.name')
}
l.fd = fd
return true
}
pub fn (mut l FileLock) release() bool {
if l.fd != -1 {
C.CloseHandle(l.fd)
l.fd = -1
t_wide := l.name.to_wide()
C.DeleteFileW(t_wide)
return true
}
return false
}
pub fn (mut l FileLock) wait_acquire(s int) ?bool {
fin := time.now().add(s)
for time.now() < fin {
if l.try_acquire() {
return true
}
time.sleep(1 * time.millisecond)
}
return false
}
fn open(f string) voidptr {
f_wide := f.to_wide()
// locking it
fd := C.CreateFileW(f_wide, C.GENERIC_READ | C.GENERIC_WRITE, 0, 0, C.OPEN_ALWAYS,
C.FILE_ATTRIBUTE_NORMAL, 0)
if fd == C.INVALID_HANDLE_VALUE {
fd == -1
}
return fd
}
pub fn (mut l FileLock) try_acquire() bool {
if l.fd != -1 {
// lock already acquired by this instance
return false
}
fd := open(l.name)
if fd == -1 {
return false
}
l.fd = fd
return true
}
|