mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
fix issue in gmlock
This commit is contained in:
@ -17,6 +17,7 @@ import (
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
@ -356,6 +357,10 @@ func (l *Logger) GetBacktrace(skip...int) string {
|
||||
// Find the true caller file path using custom skip.
|
||||
index := 1
|
||||
goRoot := runtime.GOROOT()
|
||||
if goRoot != "" {
|
||||
goRoot = strings.ReplaceAll(goRoot, "\\", "/")
|
||||
goRoot = regexp.QuoteMeta(goRoot)
|
||||
}
|
||||
for i := from + customSkip + l.btSkip; i < 1000; i++ {
|
||||
if _, file, cline, ok := runtime.Caller(i); ok && len(file) > 2 {
|
||||
if (goRoot == "" || !gregex.IsMatchString("^" + goRoot, file)) && !gregex.IsMatchString(`<autogenerated>`, file) {
|
||||
|
||||
@ -63,12 +63,13 @@ func (m *Mutex) Unlock() {
|
||||
// it returns false.
|
||||
func (m *Mutex) TryLock() bool {
|
||||
if m.locking.Cas(false, true) {
|
||||
m.mu.Lock()
|
||||
// State should be changed after locks.
|
||||
m.state.Set(-1)
|
||||
m.wid.Add(1)
|
||||
if m.state.Cas(0, -1) {
|
||||
m.mu.Lock()
|
||||
m.wid.Add(1)
|
||||
m.locking.Set(false)
|
||||
return true
|
||||
}
|
||||
m.locking.Set(false)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -315,8 +316,12 @@ func getBacktrace(skip...int) string {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Get the caller backtrace from business caller file.
|
||||
// Converting all file separator to "/".
|
||||
goRoot := runtime.GOROOT()
|
||||
if goRoot != "" {
|
||||
goRoot = strings.ReplaceAll(goRoot, "\\", "/")
|
||||
goRoot = regexp.QuoteMeta(goRoot)
|
||||
}
|
||||
for i := from + customSkip; i < 10000; i++ {
|
||||
if _, file, cline, ok := runtime.Caller(i); ok && file != "" {
|
||||
if reg, _ := regexp.Compile(`<autogenerated>`); reg.MatchString(file) {
|
||||
|
||||
@ -2,11 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"github.com/gogf/gf/g/os/gmlock"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := sync.RWMutex{}
|
||||
m.Lock()
|
||||
fmt.Println(m)
|
||||
key := "test3"
|
||||
gmlock.Lock(key, 200*time.Millisecond)
|
||||
fmt.Println("TryLock:", gmlock.TryLock(key))
|
||||
fmt.Println("TryLock:", gmlock.TryLock(key))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user