@@ -76,7 +76,8 @@ type VirtualMachine struct {
76
76
77
77
type (
78
78
machineStatus struct {
79
- state VirtualMachineState
79
+ state VirtualMachineState
80
+ stateNotify chan VirtualMachineState
80
81
81
82
mu sync.RWMutex
82
83
}
@@ -104,7 +105,8 @@ func NewVirtualMachine(config *VirtualMachineConfiguration) *VirtualMachine {
104
105
cs := charWithGoString (id )
105
106
defer cs .Free ()
106
107
statuses [id ] = & machineStatus {
107
- state : VirtualMachineState (0 ),
108
+ state : VirtualMachineState (0 ),
109
+ stateNotify : make (chan VirtualMachineState ),
108
110
}
109
111
handlers [id ] = & machineHandlers {
110
112
start : func (error ) {},
@@ -137,7 +139,10 @@ func changeStateOnObserver(state C.int, cID *C.char) {
137
139
// if caused panic, that's unexpected behavior.
138
140
v , _ := statuses [id .String ()]
139
141
v .mu .Lock ()
140
- v .state = VirtualMachineState (state )
142
+ newState := VirtualMachineState (state )
143
+ v .state = newState
144
+ // for non-blocking
145
+ go func () { v .stateNotify <- newState }()
141
146
statuses [id .String ()] = v
142
147
v .mu .Unlock ()
143
148
}
@@ -152,6 +157,16 @@ func (v *VirtualMachine) State() VirtualMachineState {
152
157
return val .state
153
158
}
154
159
160
+ // StateChangedNotify gets notification is changed execution state of the virtual machine.
161
+ func (v * VirtualMachine ) StateChangedNotify () <- chan VirtualMachineState {
162
+ // I expected it will not cause panic.
163
+ // if caused panic, that's unexpected behavior.
164
+ val , _ := statuses [v .id ]
165
+ val .mu .RLock ()
166
+ defer val .mu .RUnlock ()
167
+ return val .stateNotify
168
+ }
169
+
155
170
// CanStart returns true if the machine is in a state that can be started.
156
171
func (v * VirtualMachine ) CanStart () bool {
157
172
return bool (C .vmCanStart (v .Ptr (), v .dispatchQueue ))
0 commit comments