Skip to content

Commit

Permalink
use go-infinity-channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Apr 15, 2023
1 parent 62d5966 commit 0cea712
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/Code-Hex/vz/v3
go 1.19

require (
github.com/Code-Hex/go-infinity-channel v1.0.0
golang.org/x/crypto v0.1.0
golang.org/x/mod v0.6.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Code-Hex/go-infinity-channel v1.0.0 h1:M8BWlfDOxq9or9yvF9+YkceoTkDI1pFAqvnP87Zh0Nw=
github.com/Code-Hex/go-infinity-channel v1.0.0/go.mod h1:5yUVg/Fqao9dAjcpzoQ33WwfdMWmISOrQloDRn3bsvY=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
Expand Down
10 changes: 5 additions & 5 deletions virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"unsafe"

infinity "github.com/Code-Hex/go-infinity-channel"
"github.com/Code-Hex/vz/v3/internal/objc"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ type VirtualMachine struct {

type machineState struct {
state VirtualMachineState
stateNotify chan VirtualMachineState
stateNotify *infinity.Channel[VirtualMachineState]

mu sync.RWMutex
}
Expand All @@ -104,7 +105,7 @@ func NewVirtualMachine(config *VirtualMachineConfiguration) (*VirtualMachine, er

stateHandle := cgo.NewHandle(&machineState{
state: VirtualMachineState(0),
stateNotify: make(chan VirtualMachineState),
stateNotify: infinity.NewChannel[VirtualMachineState](),
})

v := &VirtualMachine{
Expand Down Expand Up @@ -160,8 +161,7 @@ func changeStateOnObserver(newStateRaw C.int, cgoHandlerPtr unsafe.Pointer) {
v.mu.Lock()
newState := VirtualMachineState(newStateRaw)
v.state = newState
// for non-blocking
go func() { v.stateNotify <- newState }()
v.stateNotify.In() <- newState
v.mu.Unlock()
}

Expand All @@ -188,7 +188,7 @@ func (v *VirtualMachine) StateChangedNotify() <-chan VirtualMachineState {
val, _ := v.stateHandle.Value().(*machineState)
val.mu.RLock()
defer val.mu.RUnlock()
return val.stateNotify
return val.stateNotify.Out()
}

// CanStart returns true if the machine is in a state that can be started.
Expand Down

0 comments on commit 0cea712

Please sign in to comment.