-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:(loader) avoid race on
lastmoduledatap
with go plugin (only eff…
…ects on byted-tango) (#707)
- Loading branch information
Showing
5 changed files
with
96 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= | ||
github.com/bytedance/sonic/loader v0.2.2/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= | ||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// +build !bytedance_tango | ||
|
||
/** | ||
* Copyright 2024 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 | ||
* | ||
* https://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 ( | ||
"sync/atomic" | ||
"unsafe" | ||
) | ||
|
||
func registerModule(mod *moduledata) { | ||
registerModuleLockFree(&lastmoduledatap, mod) | ||
} | ||
|
||
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), | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// +build bytedance_tango | ||
|
||
/** | ||
* Copyright 2024 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 | ||
* | ||
* https://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 ( | ||
"sync" | ||
_ "unsafe" | ||
) | ||
|
||
//go:linkname pluginsMu plugin.pluginsMu | ||
var pluginsMu sync.Mutex | ||
|
||
func registerModule(mod *moduledata) { | ||
pluginsMu.Lock() | ||
defer pluginsMu.Unlock() | ||
lastmoduledatap.next = mod | ||
lastmoduledatap = mod | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters