wasm: move ModuleInstance.Closed to the top (#1321)

ModuleInstance.Closed is an atomic variable meant to be loaded and
swapped with sync/atomic. Closed, being a 64 bit integer, requires 64
bit alignment. The simplest way we can get alignment is to place these
atomic fields at the top of their struct.

Closed can be moved to a more logical place once support for Go 1.18 is
dropped and its type changed to atomic.Uint64.

Signed-off-by: Tristan Willy <tristan.willy@gmail.com>
This commit is contained in:
Tristan Willy
2023-03-30 16:13:44 -07:00
committed by GitHub
parent 8887799da7
commit f3516a656f
2 changed files with 18 additions and 15 deletions

View File

@@ -348,13 +348,13 @@ const (
functionSize = 56
// Offsets for wasm.ModuleInstance.
moduleInstanceGlobalsOffset = 24
moduleInstanceMemoryOffset = 48
moduleInstanceTablesOffset = 56
moduleInstanceEngineOffset = 80
moduleInstanceTypeIDsOffset = 96
moduleInstanceDataInstancesOffset = 120
moduleInstanceElementInstancesOffset = 144
moduleInstanceGlobalsOffset = 32
moduleInstanceMemoryOffset = 56
moduleInstanceTablesOffset = 64
moduleInstanceEngineOffset = 88
moduleInstanceTypeIDsOffset = 104
moduleInstanceDataInstancesOffset = 128
moduleInstanceElementInstancesOffset = 152
// Offsets for wasm.TableInstance.
tableInstanceTableOffset = 0

View File

@@ -58,6 +58,17 @@ type (
//
// This implements api.Module.
ModuleInstance struct {
// Closed is used both to guard moduleEngine.CloseWithExitCode and to store the exit code.
//
// The update value is closedType + exitCode << 32. This ensures an exit code of zero isn't mistaken for never closed.
//
// Note: Exclusively reading and updating this with atomics guarantees cross-goroutine observations.
// See /RATIONALE.md
//
// TODO: Retype this to atomic.Unit64 when Go 1.18 is no longer supported. Until then, keep Closed at the top of
// this struct. See PR #1299 for an implementation and discussion.
Closed uint64
ModuleName string
Exports map[string]*Export
Globals []*GlobalInstance
@@ -94,14 +105,6 @@ type (
// security implications.
Sys *internalsys.Context
// closed is the pointer used both to guard moduleEngine.CloseWithExitCode and to store the exit code.
//
// The update value is closedType + exitCode << 32. This ensures an exit code of zero isn't mistaken for never closed.
//
// Note: Exclusively reading and updating this with atomics guarantees cross-goroutine observations.
// See /RATIONALE.md
Closed uint64
// CodeCloser is non-nil when the code should be closed after this module.
CodeCloser api.Closer