fix issue in gmlock

This commit is contained in:
john
2019-06-18 19:19:43 +08:00
parent a7dcc2c9c6
commit 5888b0e06a
4 changed files with 23 additions and 10 deletions

View File

@ -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) {

View 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
}

View File

@ -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) {

View 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))
}