build: separate compiler and libs
This commit is contained in:
139
compiler/internal/lib/sync/atomic/atomic.go
Normal file
139
compiler/internal/lib/sync/atomic/atomic.go
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package atomic
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoPackage = true
|
||||
)
|
||||
|
||||
type valtype interface {
|
||||
~int | ~uint | ~uintptr | ~int32 | ~uint32 | ~int64 | ~uint64 | ~unsafe.Pointer
|
||||
}
|
||||
|
||||
//go:linkname SwapInt32 llgo.atomicXchg
|
||||
func SwapInt32(addr *int32, new int32) (old int32)
|
||||
|
||||
//go:linkname SwapInt64 llgo.atomicXchg
|
||||
func SwapInt64(addr *int64, new int64) (old int64)
|
||||
|
||||
//go:linkname SwapUint32 llgo.atomicXchg
|
||||
func SwapUint32(addr *uint32, new uint32) (old uint32)
|
||||
|
||||
//go:linkname SwapUint64 llgo.atomicXchg
|
||||
func SwapUint64(addr *uint64, new uint64) (old uint64)
|
||||
|
||||
//go:linkname SwapUintptr llgo.atomicXchg
|
||||
func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)
|
||||
|
||||
//go:linkname SwapPointer llgo.atomicXchg
|
||||
func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
|
||||
|
||||
// llgo:link atomicCmpXchg llgo.atomicCmpXchg
|
||||
func atomicCmpXchg[T valtype](ptr *T, old, new T) (T, bool) { return old, false }
|
||||
|
||||
func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool) {
|
||||
_, swapped = atomicCmpXchg(addr, old, new)
|
||||
return
|
||||
}
|
||||
|
||||
func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool) {
|
||||
_, swapped = atomicCmpXchg(addr, old, new)
|
||||
return
|
||||
}
|
||||
|
||||
func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool) {
|
||||
_, swapped = atomicCmpXchg(addr, old, new)
|
||||
return
|
||||
}
|
||||
|
||||
func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool) {
|
||||
_, swapped = atomicCmpXchg(addr, old, new)
|
||||
return
|
||||
}
|
||||
|
||||
func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool) {
|
||||
_, swapped = atomicCmpXchg(addr, old, new)
|
||||
return
|
||||
}
|
||||
|
||||
func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) {
|
||||
_, swapped = atomicCmpXchg(addr, old, new)
|
||||
return
|
||||
}
|
||||
|
||||
// llgo:link atomicAdd llgo.atomicAdd
|
||||
func atomicAdd[T valtype](ptr *T, v T) T { return v }
|
||||
|
||||
func AddInt32(addr *int32, delta int32) (new int32) {
|
||||
return atomicAdd(addr, delta) + delta
|
||||
}
|
||||
|
||||
func AddUint32(addr *uint32, delta uint32) (new uint32) {
|
||||
return atomicAdd(addr, delta) + delta
|
||||
}
|
||||
|
||||
func AddInt64(addr *int64, delta int64) (new int64) {
|
||||
return atomicAdd(addr, delta) + delta
|
||||
}
|
||||
|
||||
func AddUint64(addr *uint64, delta uint64) (new uint64) {
|
||||
return atomicAdd(addr, delta) + delta
|
||||
}
|
||||
|
||||
func AddUintptr(addr *uintptr, delta uintptr) (new uintptr) {
|
||||
return atomicAdd(addr, delta) + delta
|
||||
}
|
||||
|
||||
//go:linkname LoadInt32 llgo.atomicLoad
|
||||
func LoadInt32(addr *int32) (val int32)
|
||||
|
||||
//go:linkname LoadInt64 llgo.atomicLoad
|
||||
func LoadInt64(addr *int64) (val int64)
|
||||
|
||||
//go:linkname LoadUint32 llgo.atomicLoad
|
||||
func LoadUint32(addr *uint32) (val uint32)
|
||||
|
||||
//go:linkname LoadUint64 llgo.atomicLoad
|
||||
func LoadUint64(addr *uint64) (val uint64)
|
||||
|
||||
//go:linkname LoadUintptr llgo.atomicLoad
|
||||
func LoadUintptr(addr *uintptr) (val uintptr)
|
||||
|
||||
//go:linkname LoadPointer llgo.atomicLoad
|
||||
func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
|
||||
|
||||
//go:linkname StoreInt32 llgo.atomicStore
|
||||
func StoreInt32(addr *int32, val int32)
|
||||
|
||||
//go:linkname StoreInt64 llgo.atomicStore
|
||||
func StoreInt64(addr *int64, val int64)
|
||||
|
||||
//go:linkname StoreUint32 llgo.atomicStore
|
||||
func StoreUint32(addr *uint32, val uint32)
|
||||
|
||||
//go:linkname StoreUint64 llgo.atomicStore
|
||||
func StoreUint64(addr *uint64, val uint64)
|
||||
|
||||
//go:linkname StoreUintptr llgo.atomicStore
|
||||
func StoreUintptr(addr *uintptr, val uintptr)
|
||||
|
||||
//go:linkname StorePointer llgo.atomicStore
|
||||
func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
|
||||
176
compiler/internal/lib/sync/atomic/value.go
Normal file
176
compiler/internal/lib/sync/atomic/value.go
Normal file
@@ -0,0 +1,176 @@
|
||||
// Copyright 2014 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package atomic
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Value struct {
|
||||
v any
|
||||
}
|
||||
|
||||
// efaceWords is interface{} internal representation.
|
||||
type efaceWords struct {
|
||||
typ unsafe.Pointer
|
||||
data unsafe.Pointer
|
||||
}
|
||||
|
||||
// Load returns the value set by the most recent Store.
|
||||
// It returns nil if there has been no call to Store for this Value.
|
||||
func (v *Value) Load() (val any) {
|
||||
vp := (*efaceWords)(unsafe.Pointer(v))
|
||||
typ := LoadPointer(&vp.typ)
|
||||
if typ == nil || typ == unsafe.Pointer(&firstStoreInProgress) {
|
||||
// First store not yet completed.
|
||||
return nil
|
||||
}
|
||||
data := LoadPointer(&vp.data)
|
||||
vlp := (*efaceWords)(unsafe.Pointer(&val))
|
||||
vlp.typ = typ
|
||||
vlp.data = data
|
||||
return
|
||||
}
|
||||
|
||||
var firstStoreInProgress byte
|
||||
|
||||
// Store sets the value of the Value v to val.
|
||||
// All calls to Store for a given Value must use values of the same concrete type.
|
||||
// Store of an inconsistent type panics, as does Store(nil).
|
||||
func (v *Value) Store(val any) {
|
||||
if val == nil {
|
||||
panic("sync/atomic: store of nil value into Value")
|
||||
}
|
||||
vp := (*efaceWords)(unsafe.Pointer(v))
|
||||
vlp := (*efaceWords)(unsafe.Pointer(&val))
|
||||
for {
|
||||
typ := LoadPointer(&vp.typ)
|
||||
if typ == nil {
|
||||
// Attempt to start first store.
|
||||
// Disable preemption so that other goroutines can use
|
||||
// active spin wait to wait for completion.
|
||||
if !CompareAndSwapPointer(&vp.typ, nil, unsafe.Pointer(&firstStoreInProgress)) {
|
||||
continue
|
||||
}
|
||||
// Complete first store.
|
||||
StorePointer(&vp.data, vlp.data)
|
||||
StorePointer(&vp.typ, vlp.typ)
|
||||
return
|
||||
}
|
||||
if typ == unsafe.Pointer(&firstStoreInProgress) {
|
||||
// First store in progress. Wait.
|
||||
// Since we disable preemption around the first store,
|
||||
// we can wait with active spinning.
|
||||
continue
|
||||
}
|
||||
// First store completed. Check type and overwrite data.
|
||||
if typ != vlp.typ {
|
||||
panic("sync/atomic: store of inconsistently typed value into Value")
|
||||
}
|
||||
StorePointer(&vp.data, vlp.data)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Swap stores new into Value and returns the previous value. It returns nil if
|
||||
// the Value is empty.
|
||||
//
|
||||
// All calls to Swap for a given Value must use values of the same concrete
|
||||
// type. Swap of an inconsistent type panics, as does Swap(nil).
|
||||
func (v *Value) Swap(new any) (old any) {
|
||||
if new == nil {
|
||||
panic("sync/atomic: swap of nil value into Value")
|
||||
}
|
||||
vp := (*efaceWords)(unsafe.Pointer(v))
|
||||
np := (*efaceWords)(unsafe.Pointer(&new))
|
||||
for {
|
||||
typ := LoadPointer(&vp.typ)
|
||||
if typ == nil {
|
||||
// Attempt to start first store.
|
||||
// Disable preemption so that other goroutines can use
|
||||
// active spin wait to wait for completion; and so that
|
||||
// GC does not see the fake type accidentally.
|
||||
if !CompareAndSwapPointer(&vp.typ, nil, unsafe.Pointer(&firstStoreInProgress)) {
|
||||
continue
|
||||
}
|
||||
// Complete first store.
|
||||
StorePointer(&vp.data, np.data)
|
||||
StorePointer(&vp.typ, np.typ)
|
||||
return nil
|
||||
}
|
||||
if typ == unsafe.Pointer(&firstStoreInProgress) {
|
||||
// First store in progress. Wait.
|
||||
// Since we disable preemption around the first store,
|
||||
// we can wait with active spinning.
|
||||
continue
|
||||
}
|
||||
// First store completed. Check type and overwrite data.
|
||||
if typ != np.typ {
|
||||
panic("sync/atomic: swap of inconsistently typed value into Value")
|
||||
}
|
||||
op := (*efaceWords)(unsafe.Pointer(&old))
|
||||
op.typ, op.data = np.typ, SwapPointer(&vp.data, np.data)
|
||||
return old
|
||||
}
|
||||
}
|
||||
|
||||
// CompareAndSwap executes the compare-and-swap operation for the Value.
|
||||
//
|
||||
// All calls to CompareAndSwap for a given Value must use values of the same
|
||||
// concrete type. CompareAndSwap of an inconsistent type panics, as does
|
||||
// CompareAndSwap(old, nil).
|
||||
func (v *Value) CompareAndSwap(old, new any) (swapped bool) {
|
||||
if new == nil {
|
||||
panic("sync/atomic: compare and swap of nil value into Value")
|
||||
}
|
||||
vp := (*efaceWords)(unsafe.Pointer(v))
|
||||
np := (*efaceWords)(unsafe.Pointer(&new))
|
||||
op := (*efaceWords)(unsafe.Pointer(&old))
|
||||
if op.typ != nil && np.typ != op.typ {
|
||||
panic("sync/atomic: compare and swap of inconsistently typed values")
|
||||
}
|
||||
for {
|
||||
typ := LoadPointer(&vp.typ)
|
||||
if typ == nil {
|
||||
if old != nil {
|
||||
return false
|
||||
}
|
||||
// Attempt to start first store.
|
||||
// Disable preemption so that other goroutines can use
|
||||
// active spin wait to wait for completion; and so that
|
||||
// GC does not see the fake type accidentally.
|
||||
if !CompareAndSwapPointer(&vp.typ, nil, unsafe.Pointer(&firstStoreInProgress)) {
|
||||
continue
|
||||
}
|
||||
// Complete first store.
|
||||
StorePointer(&vp.data, np.data)
|
||||
StorePointer(&vp.typ, np.typ)
|
||||
return true
|
||||
}
|
||||
if typ == unsafe.Pointer(&firstStoreInProgress) {
|
||||
// First store in progress. Wait.
|
||||
// Since we disable preemption around the first store,
|
||||
// we can wait with active spinning.
|
||||
continue
|
||||
}
|
||||
// First store completed. Check type and overwrite data.
|
||||
if typ != np.typ {
|
||||
panic("sync/atomic: compare and swap of inconsistently typed value into Value")
|
||||
}
|
||||
// Compare old and current via runtime equality check.
|
||||
// This allows value types to be compared, something
|
||||
// not offered by the package functions.
|
||||
// CompareAndSwapPointer below only ensures vp.data
|
||||
// has not changed since LoadPointer.
|
||||
data := LoadPointer(&vp.data)
|
||||
var i any
|
||||
(*efaceWords)(unsafe.Pointer(&i)).typ = typ
|
||||
(*efaceWords)(unsafe.Pointer(&i)).data = data
|
||||
if i != old {
|
||||
return false
|
||||
}
|
||||
return CompareAndSwapPointer(&vp.data, data, np.data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user