stubs.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright 2023 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package loader
  17. import (
  18. "sync/atomic"
  19. "unsafe"
  20. _ `unsafe`
  21. )
  22. //go:linkname lastmoduledatap runtime.lastmoduledatap
  23. //goland:noinspection GoUnusedGlobalVariable
  24. var lastmoduledatap *moduledata
  25. func registerModule(mod *moduledata) {
  26. registerModuleLockFree(&lastmoduledatap, mod)
  27. }
  28. //go:linkname moduledataverify1 runtime.moduledataverify1
  29. func moduledataverify1(_ *moduledata)
  30. func registerModuleLockFree(tail **moduledata, mod *moduledata) {
  31. for {
  32. oldTail := loadModule(tail)
  33. if casModule(tail, oldTail, mod) {
  34. storeModule(&oldTail.next, mod)
  35. break
  36. }
  37. }
  38. }
  39. func loadModule(p **moduledata) *moduledata {
  40. return (*moduledata)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  41. }
  42. func storeModule(p **moduledata, value *moduledata) {
  43. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(value))
  44. }
  45. func casModule(p **moduledata, oldValue *moduledata, newValue *moduledata) bool {
  46. return atomic.CompareAndSwapPointer(
  47. (*unsafe.Pointer)(unsafe.Pointer(p)),
  48. unsafe.Pointer(oldValue),
  49. unsafe.Pointer(newValue),
  50. )
  51. }