c/pthread/sync.Mutex; sync.Mutex/Once; typepatch.IsPatched
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/pthread"
|
||||
"github.com/goplus/llgo/c/pthread/sync"
|
||||
)
|
||||
|
||||
@@ -31,29 +30,43 @@ const (
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
var onceParam pthread.Key
|
||||
type Mutex sync.Mutex
|
||||
|
||||
func init() {
|
||||
onceParam.Create(nil)
|
||||
}
|
||||
|
||||
type Once sync.Once
|
||||
|
||||
func (o *Once) Do(f func()) {
|
||||
ptr := c.Malloc(unsafe.Sizeof(f))
|
||||
*(*func())(ptr) = f
|
||||
onceParam.Set(ptr)
|
||||
if *(*c.Long)(unsafe.Pointer(o)) == 0 { // try init
|
||||
*(*sync.Once)(o) = sync.OnceInit
|
||||
func (m *Mutex) Lock() {
|
||||
if *(*c.Long)(unsafe.Pointer(m)) == 0 {
|
||||
(*sync.Mutex)(m).Init(nil)
|
||||
}
|
||||
onceDo(o, func() {
|
||||
ptr := onceParam.Get()
|
||||
(*(*func())(ptr))()
|
||||
c.Free(ptr)
|
||||
})
|
||||
(*sync.Mutex)(m).Lock()
|
||||
}
|
||||
|
||||
//go:linkname onceDo C.pthread_once
|
||||
func onceDo(o *Once, f func()) c.Int
|
||||
func (m *Mutex) TryLock() bool {
|
||||
if *(*c.Long)(unsafe.Pointer(m)) == 0 {
|
||||
(*sync.Mutex)(m).Init(nil)
|
||||
}
|
||||
return (*sync.Mutex)(m).TryLock() == 0
|
||||
}
|
||||
|
||||
func (m *Mutex) Unlock() {
|
||||
(*sync.Mutex)(m).Unlock()
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type Once struct {
|
||||
m Mutex
|
||||
done bool
|
||||
}
|
||||
|
||||
func (o *Once) Do(f func()) {
|
||||
println("Once.Do start", o.done)
|
||||
if !o.done {
|
||||
o.m.Lock()
|
||||
defer o.m.Unlock()
|
||||
if !o.done {
|
||||
o.done = true
|
||||
f()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -55,14 +55,29 @@ type iface struct {
|
||||
data unsafe.Pointer
|
||||
}
|
||||
|
||||
const (
|
||||
tagPatched = 0x17
|
||||
)
|
||||
|
||||
func IsPatched(pkg *types.Package) bool {
|
||||
p := (*typesPackage)(unsafe.Pointer(pkg))
|
||||
return *(*uint8)(unsafe.Pointer(&p.complete)) == tagPatched
|
||||
}
|
||||
|
||||
func setPatched(pkg *types.Package) {
|
||||
p := (*typesPackage)(unsafe.Pointer(pkg))
|
||||
*(*uint8)(unsafe.Pointer(&p.complete)) = tagPatched
|
||||
}
|
||||
|
||||
func setScope(pkg *types.Package, scope *types.Scope) {
|
||||
p := (*typesPackage)(unsafe.Pointer(pkg))
|
||||
p.scope = scope
|
||||
}
|
||||
|
||||
func setPkg(o types.Object, pkg *types.Package) {
|
||||
func setPkgAndParent(o types.Object, pkg *types.Package, parent *types.Scope) {
|
||||
data := (*iface)(unsafe.Pointer(&o)).data
|
||||
(*object)(data).pkg = pkg
|
||||
(*object)(data).parent = parent
|
||||
}
|
||||
|
||||
func getElems(scope *types.Scope) map[string]types.Object {
|
||||
@@ -87,10 +102,11 @@ func Pkg(pkg, alt *types.Package) *types.Package {
|
||||
|
||||
altScope := alt.Scope()
|
||||
for name, o := range getElems(altScope) {
|
||||
setPkg(o, pkg)
|
||||
setPkgAndParent(o, &ret, &scope)
|
||||
elems[name] = o
|
||||
}
|
||||
setElems(&scope, elems)
|
||||
setScope(&ret, &scope)
|
||||
setPatched(pkg)
|
||||
return &ret
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user