2018-05-09 18:29:46 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
|
|
|
|
|
|
// 文件锁.
|
|
|
|
|
|
package gflock
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2018-05-10 16:07:14 +08:00
|
|
|
|
"sync"
|
2018-10-22 11:13:00 +08:00
|
|
|
|
"gitee.com/johng/gf/third/github.com/theckman/go-flock"
|
2018-05-09 18:29:46 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 文件锁
|
|
|
|
|
|
type Locker struct {
|
2018-05-15 23:28:44 +08:00
|
|
|
|
mu sync.RWMutex // 用于外部接口调用的互斥锁(阻塞机制)
|
|
|
|
|
|
flock *flock.Flock // 底层文件锁对象
|
2018-05-09 18:29:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建文件锁
|
|
|
|
|
|
func New(file string) *Locker {
|
2018-05-10 17:48:47 +08:00
|
|
|
|
dir := gfile.TempDir() + gfile.Separator + "gflock"
|
|
|
|
|
|
if !gfile.Exists(dir) {
|
|
|
|
|
|
gfile.Mkdir(dir)
|
|
|
|
|
|
}
|
|
|
|
|
|
path := dir + gfile.Separator + file
|
2018-05-09 18:29:46 +08:00
|
|
|
|
lock := flock.NewFlock(path)
|
|
|
|
|
|
return &Locker{
|
2018-05-10 16:07:14 +08:00
|
|
|
|
flock : lock,
|
2018-05-09 18:29:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-10 17:48:47 +08:00
|
|
|
|
func (l *Locker) Path() string {
|
|
|
|
|
|
return l.flock.Path()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-15 23:28:44 +08:00
|
|
|
|
// 当前文件锁是否处于锁定状态(Lock)
|
|
|
|
|
|
func (l *Locker) IsLocked() bool {
|
|
|
|
|
|
return l.flock.Locked()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试Lock文件,如果失败立即返回
|
|
|
|
|
|
func (l *Locker) TryLock() bool {
|
|
|
|
|
|
ok, _ := l.flock.TryLock()
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
l.mu.Lock()
|
|
|
|
|
|
}
|
|
|
|
|
|
return ok
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试RLock文件,如果失败立即返回
|
|
|
|
|
|
func (l *Locker) TryRLock() bool {
|
|
|
|
|
|
ok, _ := l.flock.TryRLock()
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
l.mu.RLock()
|
|
|
|
|
|
}
|
|
|
|
|
|
return ok
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-10 16:07:14 +08:00
|
|
|
|
func (l *Locker) Lock() {
|
|
|
|
|
|
l.mu.Lock()
|
|
|
|
|
|
l.flock.Lock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Locker) UnLock() {
|
|
|
|
|
|
l.flock.Unlock()
|
|
|
|
|
|
l.mu.Unlock()
|
|
|
|
|
|
}
|
2018-05-09 18:29:46 +08:00
|
|
|
|
|
2018-05-10 16:07:14 +08:00
|
|
|
|
func (l *Locker) RLock() {
|
|
|
|
|
|
l.mu.RLock()
|
|
|
|
|
|
l.flock.RLock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Locker) RUnlock() {
|
|
|
|
|
|
l.flock.Unlock()
|
|
|
|
|
|
l.mu.RUnlock()
|
|
|
|
|
|
}
|