Skip to content

Commit

Permalink
implemented around stop
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Aug 29, 2022
1 parent 481c580 commit 09db4f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const (
// VirtualMachineStateResuming The virtual machine is being resumed.
// This is the intermediate state between VirtualMachineStatePaused and VirtualMachineStateRunning.
VirtualMachineStateResuming

// VZVirtualMachineStateStopping The virtual machine is being stopped.
// This is the intermediate state between VZVirtualMachineStateRunning and VZVirtualMachineStateStop.
VirtualMachineStateStopping
)

// VirtualMachine represents the entire state of a single virtual machine.
Expand Down Expand Up @@ -195,6 +199,11 @@ func (v *VirtualMachine) CanRequestStop() bool {
return (bool)(C.vmCanRequestStop(v.Ptr(), v.dispatchQueue))
}

// CanStop returns whether the machine is in a state that can be stopped.
func (v *VirtualMachine) CanStop() bool {
return (bool)(C.vmCanStop(v.Ptr(), v.dispatchQueue))
}

//export virtualMachineCompletionHandler
func virtualMachineCompletionHandler(cgoHandlerPtr, errPtr unsafe.Pointer) {
cgoHandler := *(*cgo.Handle)(cgoHandlerPtr)
Expand Down Expand Up @@ -266,6 +275,21 @@ func (v *VirtualMachine) RequestStop() (bool, error) {
return ret, nil
}

// Stop stops a VM that’s in either a running or paused state.
//
// The completion handler returns an error object when the VM fails to stop,
// or nil if the stop was successful.
//
// Warning: This is a destructive operation. It stops the VM without
// giving the guest a chance to stop cleanly.
func (v *VirtualMachine) Stop(fn func(error)) {
h, done := makeHandler(fn)
handler := cgo.NewHandle(h)
defer handler.Delete()
C.stopWithCompletionHandler(v.Ptr(), v.dispatchQueue, unsafe.Pointer(&handler))
<-done
}

// StartGraphicApplication starts an application to display graphics of the VM.
//
// You must to call runtime.LockOSThread before calling this method.
Expand Down
2 changes: 2 additions & 0 deletions virtualization.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ bool requestStopVirtualMachine(void *machine, void *queue, void **error);
void startWithCompletionHandler(void *machine, void *queue, void *completionHandler);
void pauseWithCompletionHandler(void *machine, void *queue, void *completionHandler);
void resumeWithCompletionHandler(void *machine, void *queue, void *completionHandler);
void stopWithCompletionHandler(void *machine, void *queue, void *completionHandler);
bool vmCanStart(void *machine, void *queue);
bool vmCanPause(void *machine, void *queue);
bool vmCanResume(void *machine, void *queue);
bool vmCanRequestStop(void *machine, void *queue);
bool vmCanStop(void *machine, void *queue);

void *makeDispatchQueue(const char *label);

Expand Down
18 changes: 18 additions & 0 deletions virtualization.m
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,15 @@ void resumeWithCompletionHandler(void *machine, void *queue, void *completionHan
});
}

void stopWithCompletionHandler(void *machine, void *queue, void *completionHandler)
{
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine stopWithCompletionHandler:^(NSError *err) {
virtualMachineCompletionHandler(completionHandler, err);
}];
});
}

// TODO(codehex): use KVO
bool vmCanStart(void *machine, void *queue)
{
Expand Down Expand Up @@ -887,6 +896,15 @@ bool vmCanRequestStop(void *machine, void *queue)
});
return (bool)result;
}

bool vmCanStop(void *machine, void *queue)
{
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{
result = ((VZVirtualMachine *)machine).canStop;
});
return (bool)result;
}
// --- TODO end

void sharedApplication()
Expand Down

0 comments on commit 09db4f3

Please sign in to comment.