forked from alexflint/go-filemutex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilemutex_flock.go
72 lines (61 loc) · 1.52 KB
/
filemutex_flock.go
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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd
package filemutex
import (
"sync"
"syscall"
)
const (
mkdirPerm = 0750
)
// FileMutex is similar to sync.RWMutex, but also synchronizes across processes.
// This implementation is based on flock syscall.
type FileMutex struct {
mu sync.RWMutex
fd int
path string
}
func New(filename string) (*FileMutex, error) {
fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY, mkdirPerm)
if err != nil {
return nil, err
}
return &FileMutex{fd: fd, path: filename}, nil
}
func (m *FileMutex) Lock() {
m.mu.Lock()
if err := syscall.Flock(m.fd, syscall.LOCK_EX); err != nil {
panic(err)
}
}
func (m *FileMutex) Unlock() {
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
panic(err)
}
m.mu.Unlock()
}
func (m *FileMutex) RLock() {
m.mu.RLock()
if err := syscall.Flock(m.fd, syscall.LOCK_SH); err != nil {
panic(err)
}
}
func (m *FileMutex) RUnlock() {
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
panic(err)
}
m.mu.RUnlock()
}
// Close does an Unlock() combined with closing and unlinking the associated
// lock file. You should create a New() FileMutex for every Lock() attempt if
// using Close().
func (m *FileMutex) Close() {
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
panic(err)
}
syscall.Close(m.fd)
syscall.Unlink(m.path)
m.mu.Unlock()
}