...

Source file src/github.com/bytedance/sonic/loader/stubs.go

Documentation: github.com/bytedance/sonic/loader

     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  
    17  package loader
    18  
    19  import (
    20      "sync/atomic"
    21      "unsafe"
    22      _ `unsafe`
    23  )
    24  
    25  //go:linkname lastmoduledatap runtime.lastmoduledatap
    26  //goland:noinspection GoUnusedGlobalVariable
    27  var lastmoduledatap *moduledata
    28  
    29  func registerModule(mod *moduledata) {
    30      registerModuleLockFree(&lastmoduledatap, mod)
    31  }
    32  
    33  //go:linkname moduledataverify1 runtime.moduledataverify1
    34  func moduledataverify1(_ *moduledata)
    35  
    36  func registerModuleLockFree(tail **moduledata, mod *moduledata) {
    37      for {
    38          oldTail := loadModule(tail)
    39          if casModule(tail, oldTail, mod) {
    40              storeModule(&oldTail.next, mod)
    41              break
    42          }
    43      }
    44  }
    45  
    46  func loadModule(p **moduledata) *moduledata {
    47      return (*moduledata)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
    48  }
    49  
    50  func storeModule(p **moduledata, value *moduledata) {
    51      atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(value))
    52  }
    53  
    54  func casModule(p **moduledata, oldValue *moduledata, newValue *moduledata) bool {
    55      return atomic.CompareAndSwapPointer(
    56          (*unsafe.Pointer)(unsafe.Pointer(p)),
    57          unsafe.Pointer(oldValue),
    58          unsafe.Pointer(newValue),
    59      )
    60  }
    61  

View as plain text