代码拉取完成,页面将自动刷新
// 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 gmlock
import (
"gitee.com/johng/gf/g/container/gtype"
"sync"
)
// 互斥锁对象
type Mutex struct {
mu sync.RWMutex
wid *gtype.Int64 // 当前Lock产生的唯一id(主要用于计时Unlock的校验)
rcount *gtype.Int // RLock次数
wcount *gtype.Int // Lock次数
}
// 创建一把内存锁使用的底层RWMutex
func NewMutex() *Mutex {
return &Mutex{
wid : gtype.NewInt64(),
rcount : gtype.NewInt(),
wcount : gtype.NewInt(),
}
}
func (l *Mutex) Lock() {
l.wcount.Add(1)
l.mu.Lock()
l.wid.Add(1)
}
// 安全的Unlock
func (l *Mutex) Unlock() {
if l.wcount.Val() > 0 {
if l.wcount.Add(-1) >= 0 {
l.mu.Unlock()
} else {
// 标准库这里会panic
l.wcount.Add(1)
}
}
}
func (l *Mutex) RLock() {
l.rcount.Add(1)
l.mu.RLock()
}
// 安全的RUnlock
func (l *Mutex) RUnlock() {
if l.rcount.Val() > 0 {
if l.rcount.Add(-1) >= 0 {
l.mu.RUnlock()
} else {
// 标准库这里会panic
l.rcount.Add(1)
}
}
}
// 不阻塞Lock
func (l *Mutex) TryLock() bool {
// 初步读写次数检查, 但无法保证原子性
if l.wcount.Val() == 0 && l.rcount.Val() == 0 {
// 第二次检查, 保证原子操作
if l.wcount.Add(1) == 1 {
l.mu.Lock()
l.wid.Add(1)
return true
} else {
l.wcount.Add(-1)
}
}
return false
}
// 不阻塞RLock
func (l *Mutex) TryRLock() bool {
// 只要不存在写锁
if l.wcount.Val() == 0 {
l.rcount.Add(1)
l.mu.RLock()
return true
}
return false
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。