Skip to content

Commit

Permalink
opt: use lockfree to register new modules (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix021 committed Nov 8, 2023
1 parent ba39bc4 commit 42f95bf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
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)
}
}

0 comments on commit 42f95bf

Please sign in to comment.