Skip to content
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

opt: use lockfree to register new modules #552

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 27 additions & 7 deletions loader/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,44 @@
package loader

import (
`sync`
"sync/atomic"
"unsafe"
_ `unsafe`
)

//go:linkname lastmoduledatap runtime.lastmoduledatap
//goland:noinspection GoUnusedGlobalVariable
var lastmoduledatap *moduledata

var moduledataMux sync.Mutex

func registerModule(mod *moduledata) {
moduledataMux.Lock()
lastmoduledatap.next = mod
lastmoduledatap = mod
moduledataMux.Unlock()
registerModuleLockFree(&lastmoduledatap, mod)
}

//go:linkname moduledataverify1 runtime.moduledataverify1
func moduledataverify1(_ *moduledata)

func registerModuleLockFree(tail **moduledata, mod *moduledata) {
for {
oldTail := loadModule(tail)
if casModule(tail, oldTail, mod) {
storeModule(&oldTail.next, mod)
break
}
}
}

func loadModule(p **moduledata) *moduledata {
return (*moduledata)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}

func storeModule(p **moduledata, value *moduledata) {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(value))
}

func casModule(p **moduledata, oldValue *moduledata, newValue *moduledata) bool {
return atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(p)),
unsafe.Pointer(oldValue),
unsafe.Pointer(newValue),
)
}
48 changes: 48 additions & 0 deletions loader/stubs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 ByteDance 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.
*/

package loader

import (
"testing"
"sync"
)

func Test_registerModuleLockFree(t *testing.T) {
n, parallel := 1000, 8
head := moduledata{}
tail := &head
wg := sync.WaitGroup{}
wg.Add(parallel)
filler := func(n int) {
defer wg.Done()
for i := 0; i < n; i++ {
m := &moduledata{}
registerModuleLockFree(&tail, m)
}
}
for i := 0; i < parallel; i++ {
go filler(n)
}
wg.Wait()
i := 0
for p := head.next; p != nil; p = p.next {
i += 1
}
if i != parallel * n {
t.Errorf("got %v, expected %v", i, parallel * n)
}
}