diff options
| author | David Crawshaw <crawshaw@golang.org> | 2016-10-30 20:30:38 -0400 |
|---|---|---|
| committer | David Crawshaw <crawshaw@golang.org> | 2016-11-01 16:04:12 +0000 |
| commit | 54ec7b072e017f7351f889f9f5b31bbf53a44119 (patch) | |
| tree | 8e1520eca08765335b77ef904f4ad0f9a2d865b8 /src/runtime/symtab.go | |
| parent | 807a7ebd5107b44ca93849f2b12b61bc4cacca10 (diff) | |
| download | go-54ec7b072e017f7351f889f9f5b31bbf53a44119.tar.xz | |
runtime: access modules via a slice
The introduction of -buildmode=plugin means modules can be added to a
Go program while it is running. This means there exists some time
while the program is running with the module is on the moduledata
linked list, but it has not been initialized to the satisfaction of
other parts of the runtime. Notably, the GC.
This CL adds a new way of access modules, an activeModules function.
It returns a slice of modules that is built in the background and
atomically swapped in. The parts of the runtime that need to wait on
module initialization can use this slice instead of the linked list.
Fixes #17455
Change-Id: I04790fd07e40c7295beb47cea202eb439206d33d
Reviewed-on: https://go-review.googlesource.com/32357
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/symtab.go')
| -rw-r--r-- | src/runtime/symtab.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/runtime/symtab.go b/src/runtime/symtab.go index d69f610ebb..9ec95d7a0c 100644 --- a/src/runtime/symtab.go +++ b/src/runtime/symtab.go @@ -5,6 +5,7 @@ package runtime import ( + "runtime/internal/atomic" "runtime/internal/sys" "unsafe" ) @@ -233,6 +234,52 @@ var pinnedTypemaps []map[typeOff]*_type var firstmoduledata moduledata // linker symbol var lastmoduledatap *moduledata // linker symbol +var modulesSlice unsafe.Pointer // see activeModules + +// activeModules returns a slice of active modules. +// +// A module is active once its gcdatamask and gcbssmask have been +// assembled and it is usable by the GC. +func activeModules() []*moduledata { + p := (*[]*moduledata)(atomic.Loadp(unsafe.Pointer(&modulesSlice))) + if p == nil { + return nil + } + return *p +} + +// modulesinit creates the active modules slice out of all loaded modules. +// +// When a module is first loaded by the dynamic linker, an .init_array +// function (written by cmd/link) is invoked to call addmoduledata, +// appending to the module to the linked list that starts with +// firstmoduledata. +// +// There are two times this can happen in the lifecycle of a Go +// program. First, if compiled with -linkshared, a number of modules +// built with -buildmode=shared can be loaded at program initialization. +// Second, a Go program can load a module while running that was built +// with -buildmode=plugin. +// +// After loading, this function is called which initializes the +// moduledata so it is usable by the GC and creates a new activeModules +// list. +// +// Only one goroutine may call modulesinit at a time. +func modulesinit() { + oldNum := len(activeModules()) + modules := new([]*moduledata) + num := 0 + for md := &firstmoduledata; md != nil; md = md.next { + *modules = append(*modules, md) + num++ + if num > oldNum { + md.gcdatamask = progToPointerMask((*byte)(unsafe.Pointer(md.gcdata)), md.edata-md.data) + md.gcbssmask = progToPointerMask((*byte)(unsafe.Pointer(md.gcbss)), md.ebss-md.bss) + } + } + atomicstorep(unsafe.Pointer(&modulesSlice), unsafe.Pointer(modules)) +} type functab struct { entry uintptr |
