mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-12 21:18:03 +08:00
chore: rebuild sync.Once visit code
This commit is contained in:
parent
8b9813079b
commit
974332c0cc
@ -3,10 +3,9 @@ package net
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/buf"
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
|
"github.com/metacubex/mihomo/common/once"
|
||||||
)
|
)
|
||||||
|
|
||||||
type earlyConn struct {
|
type earlyConn struct {
|
||||||
@ -44,8 +43,7 @@ func (conn *earlyConn) Upstream() any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (conn *earlyConn) Success() bool {
|
func (conn *earlyConn) Success() bool {
|
||||||
// atomic visit sync.Once.done
|
return once.Done(&conn.resOnce) && conn.resErr == nil
|
||||||
return atomic.LoadUint32((*uint32)(unsafe.Pointer(&conn.resOnce))) == 1 && conn.resErr == nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *earlyConn) ReaderReplaceable() bool {
|
func (conn *earlyConn) ReaderReplaceable() bool {
|
||||||
|
26
common/once/once_go120.go
Normal file
26
common/once/once_go120.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
//go:build !go1.22
|
||||||
|
|
||||||
|
package once
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Once struct {
|
||||||
|
done uint32
|
||||||
|
m sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func Done(once *sync.Once) bool {
|
||||||
|
// atomic visit sync.Once.done
|
||||||
|
return atomic.LoadUint32((*uint32)(unsafe.Pointer(once))) == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func Reset(once *sync.Once) {
|
||||||
|
o := (*Once)(unsafe.Pointer(once))
|
||||||
|
o.m.Lock()
|
||||||
|
defer o.m.Unlock()
|
||||||
|
atomic.StoreUint32(&o.done, 0)
|
||||||
|
}
|
26
common/once/once_go122.go
Normal file
26
common/once/once_go122.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
//go:build go1.22
|
||||||
|
|
||||||
|
package once
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Once struct {
|
||||||
|
done atomic.Uint32
|
||||||
|
m sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func Done(once *sync.Once) bool {
|
||||||
|
// atomic visit sync.Once.done
|
||||||
|
return (*atomic.Uint32)(unsafe.Pointer(once)).Load() == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func Reset(once *sync.Once) {
|
||||||
|
o := (*Once)(unsafe.Pointer(once))
|
||||||
|
o.m.Lock()
|
||||||
|
defer o.m.Unlock()
|
||||||
|
o.done.Store(0)
|
||||||
|
}
|
@ -8,6 +8,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
mihomoOnce "github.com/metacubex/mihomo/common/once"
|
||||||
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
@ -96,5 +97,5 @@ func DownloadMMDB(path string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Reload() {
|
func Reload() {
|
||||||
once = sync.Once{}
|
mihomoOnce.Reset(&once)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user