-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bring loong64 support #154
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2022 Loongson Inc. | ||
// | ||
// 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. | ||
|
||
//go:build loong64 && !gccgo && !appengine | ||
// +build loong64,!gccgo,!appengine | ||
|
||
package lscq | ||
|
||
import ( | ||
"sync" | ||
"unsafe" | ||
) | ||
|
||
type uint128 struct { | ||
data [2]uint64 | ||
mu sync.Mutex | ||
_ [128 - unsafe.Sizeof(sync.Mutex{})]byte | ||
} | ||
|
||
func compareAndSwapUint128(addr *uint128, old1, old2, new1, new2 uint64) (swapped bool) { | ||
addr.mu.Lock() | ||
defer addr.mu.Unlock() | ||
if addr.data[0] == old1 && addr.data[1] == old2 { | ||
addr.data[0] = new1 | ||
addr.data[1] = new2 | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
func loadUint128(addr *uint128) (val uint128) { | ||
addr.mu.Lock() | ||
defer addr.mu.Unlock() | ||
val = loadUint128Asm(addr) | ||
return | ||
} | ||
|
||
func loadUint128Asm(addr *uint128) (val uint128) | ||
|
||
func loadSCQNodePointer(addr unsafe.Pointer) (val scqNodePointer) | ||
|
||
func loadSCQNodeUint64(addr unsafe.Pointer) (val scqNodeUint64) | ||
|
||
func atomicTestAndSetFirstBit(addr *uint64) (val uint64) | ||
|
||
func atomicTestAndSetSecondBit(addr *uint64) (val uint64) | ||
|
||
func resetNode(addr unsafe.Pointer) | ||
|
||
//go:nosplit | ||
func compareAndSwapSCQNodePointer(addr *scqNodePointer, old, new scqNodePointer) (swapped bool) { | ||
// Ref: src/runtime/atomic_pointer.go:sync_atomic_CompareAndSwapPointer | ||
if runtimeEnableWriteBarrier() { | ||
runtimeatomicwb(&addr.data, new.data) | ||
} | ||
return compareAndSwapUint128((*uint128)(runtimenoescape(unsafe.Pointer(addr))), old.flags, uint64(uintptr(old.data)), new.flags, uint64(uintptr(new.data))) | ||
} | ||
|
||
func compareAndSwapSCQNodeUint64(addr *scqNodeUint64, old, new scqNodeUint64) (swapped bool) { | ||
return compareAndSwapUint128((*uint128)(unsafe.Pointer(addr)), old.flags, old.data, new.flags, new.data) | ||
} | ||
|
||
func runtimeEnableWriteBarrier() bool | ||
|
||
//go:linkname runtimeatomicwb runtime.atomicwb | ||
//go:noescape | ||
func runtimeatomicwb(ptr *unsafe.Pointer, new unsafe.Pointer) | ||
|
||
//go:linkname runtimenoescape runtime.noescape | ||
func runtimenoescape(p unsafe.Pointer) unsafe.Pointer | ||
|
||
//go:nosplit | ||
func atomicWriteBarrier(ptr *unsafe.Pointer) { | ||
// For SCQ dequeue only. (fastpath) | ||
if runtimeEnableWriteBarrier() { | ||
runtimeatomicwb(ptr, nil) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2022 Loongson Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里也是 |
||
// | ||
// 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. | ||
|
||
// +build loong64,!gccgo,!appengine | ||
|
||
#include "textflag.h" | ||
|
||
//func loadUint128(addr *uint128) (ret uint128) | ||
TEXT ·loadUint128Asm(SB),NOSPLIT,$0-24 | ||
MOVV addr+0(FP), R4 | ||
DBAR | ||
MOVV 0(R4), R5 | ||
MOVV 8(R4), R6 | ||
DBAR | ||
MOVV R5, ret+8(FP) | ||
MOVV R6, ret+16(FP) | ||
DBAR | ||
RET | ||
|
||
// func loadSCQNodeUint64(addr unsafe.Pointer)(val scqNodeUint64) | ||
TEXT ·loadSCQNodeUint64(SB),NOSPLIT,$0 | ||
JMP ·loadUint128(SB) | ||
|
||
// func loadSCQNodePointer(addr unsafe.Pointer)(val scqNodePointer) | ||
TEXT ·loadSCQNodePointer(SB),NOSPLIT,$0 | ||
JMP ·loadUint128(SB) | ||
|
||
// func atomicTestAndSetFirstBit(addr *uint64)(val uint64) | ||
TEXT ·atomicTestAndSetFirstBit(SB),NOSPLIT,$0 | ||
MOVV addr+0(FP), R4 | ||
MOVV $(1<<63), R6 | ||
DBAR | ||
testAndSetFirstBit_again: | ||
LLV (R4), R5 | ||
OR R6, R5, R5 | ||
MOVV R5, R7 | ||
SCV R5, (R4) | ||
BEQ R5, testAndSetFirstBit_again | ||
DBAR | ||
MOVV R7, val+8(FP) | ||
RET | ||
|
||
// func atomicTestAndSetSecondBit(addr *uint64)(val uint64) | ||
TEXT ·atomicTestAndSetSecondBit(SB),NOSPLIT,$0 | ||
MOVV addr+0(FP), R4 | ||
MOVV $(1<<62), R6 | ||
DBAR | ||
testAndSetSecBit_again: | ||
LLV (R4), R5 | ||
OR R6, R5, R5 | ||
MOVV R5, R7 | ||
SCV R5, (R4) | ||
BEQ R5, testAndSetSecBit_again | ||
DBAR | ||
MOVV R7, val+8(FP) | ||
RET | ||
|
||
// func resetNode(addr unsafe.Pointer) | ||
TEXT ·resetNode(SB),NOSPLIT,$0 | ||
MOVV addr+0(FP), R4 | ||
MOVV R0, 8(R4) | ||
MOVV $(1<<62), R6 | ||
DBAR | ||
resetNode_again: | ||
LLV (R4), R5 | ||
OR R6, R5, R5 | ||
SCV R5, (R4) | ||
BEQ R5, resetNode_again | ||
DBAR | ||
RET | ||
|
||
// func runtimeEnableWriteBarrier() bool | ||
TEXT ·runtimeEnableWriteBarrier(SB),NOSPLIT,$0 | ||
MOVW runtime·writeBarrier(SB), R0 | ||
DBAR | ||
MOVB R0, ret+0(FP) | ||
RET |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,8 @@ type pointerSCQ struct { | |
type scqNodePointer struct { | ||
flags uint64 // isSafe 1-bit + isEmpty 1-bit + cycle 62-bit | ||
data unsafe.Pointer | ||
_ sync.Mutex // Only for architectures that do not support 128bit atomic operations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里增加两个字段,把 scqNodePointer 的长度从 128 增加到了 256,会不会影响 cacheline 的利用进而影响性能? @zhangyunhao116 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, added a comment below :) |
||
_ [128 - unsafe.Sizeof(sync.Mutex{})]byte // The x86_64 instruction CMPXCHG16B requires 128bit alignment | ||
} | ||
|
||
func (q *pointerSCQ) Enqueue(data unsafe.Pointer) bool { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要不改成这样"Copyright 2022 Loongson Inc. and ByteDance Inc."避免一下后续的法务风险?