gf/g/os/gflock/gflock.go

83 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
// Package gflock implements a thread-safe sync.Locker interface for file locking.
//
// 文件锁.
package gflock
import (
"sync"
"github.com/gogf/gf/third/github.com/theckman/go-flock"
"github.com/gogf/gf/g/os/gfile"
)
// 文件锁
type Locker struct {
mu sync.RWMutex // 用于外部接口调用的互斥锁(阻塞机制)
flock *flock.Flock // 底层文件锁对象
}
// 创建文件锁
func New(file string) *Locker {
dir := gfile.TempDir() + gfile.Separator + "gflock"
if !gfile.Exists(dir) {
gfile.Mkdir(dir)
}
path := dir + gfile.Separator + file
lock := flock.NewFlock(path)
return &Locker{
flock : lock,
}
}
func (l *Locker) Path() string {
return l.flock.Path()
}
// 当前文件锁是否处于锁定状态(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
}
func (l *Locker) Lock() {
l.mu.Lock()
l.flock.Lock()
}
func (l *Locker) UnLock() {
l.flock.Unlock()
l.mu.Unlock()
}
func (l *Locker) RLock() {
l.mu.RLock()
l.flock.RLock()
}
func (l *Locker) RUnlock() {
l.flock.Unlock()
l.mu.RUnlock()
}