Expand description
The execution
module contains state for an instance’s execution, and exposes functions
building that state into something appropriate for safe use externally.
So far as state tracked in this module is concerned, there are two key items: “terminability” and “execution domain”.
§Terminability
This specifically answers the question “is it safe to initiate termination of this instance right now?”. An instance becomes terminable when it begins executing, and stops being terminable when it is terminated, or when it stops executing. Termination does not directly map to the idea of guest code currently executing on a processor, because termination can occur during host code, or while a guest has yielded execution. As a result, termination can only be treated as a best-effort to deschedule a guest, and is typically quick when it occurs during guest code execution, or immediately upon resuming execution of guest code (exiting host code, or resuming a yielded instance).
§Execution Domain
Execution domains allow us to distinguish what an appropriate mechanism to signal termination
is. This means that changing of an execution domain must be atomic - it would be an error to
read the current execution domain, continue with that domain to determine temination, and
simultaneously for execution to continue possibly into a different execution domain. For
example, beginning termination directly at the start of a hostcall, where sending SIGALRM
may
be appropriate, while the domain switches to Hostcall
and is no longer appropriate for
signalling, would be an error.
§Instance Lifecycle and KillState
And now we can enumerate interleavings of execution and timeout, to see the expected state at possible points of interest in an instance’s lifecycle:
Instance created
- terminable:
false
- execution_domain:
Guest
- terminable:
Instance::run called
- terminable:
true
- execution_domain:
Guest
- terminable:
Instance::run executing
- terminable:
true, or false
- execution_domain:
Guest, Hostcall, or Terminated
execution_domain
will only beGuest
when executing guest code, only beHostcall
when executing a hostcall, but may also beTerminated
while in a hostcall to indicate that it should exit when the hostcall completes.terminable
will be false if and only ifexecution_domain
isTerminated
.
- terminable:
Instance::run returns
- terminable:
false
- execution_domain:
Guest, Hostcall, or Terminated
execution_domain
will beGuest
when the initial guest function returns,Hostcall
when terminated bylucet_hostcall_terminate!
, andTerminated
when exiting due to a termination request.
- terminable:
Guest function executing
- terminable:
true
- execution_domain:
Guest
- terminable:
Guest function returns
- terminable:
true
- execution_domain:
Guest
- terminable:
Hostcall called
- terminable:
true
- execution_domain:
Hostcall
- terminable:
Hostcall executing
- terminable:
true
- execution_domain:
Hostcall, or Terminated
execution_domain
will typically beHostcall
, but may beTerminated
if termination of the instance is requested during the hostcall.terminable
will be false if and only ifexecution_domain
isTerminated
.
- terminable:
Hostcall yields
- This is a specific point in “Hostcall executing” and has no further semantics.
Hostcall resumes
- This is a specific point in “Hostcall executing” and has no further semantics.
Hostcall returns
- terminable:
true
- execution_domain:
Guest
execution_domain
may beTerminated
before returning, in which caseterminable
will be false, but the hostcall would then exit. If a hostcall successfully returns to its caller it was not terminated, so the only state an instance will have after returning from a hostcall will be that it’s executing terminable guest code.
- terminable:
Structs§
- Kill
State - All instance state a remote kill switch needs to determine if and how to signal that execution should stop.
- Kill
Switch - An object that can be used to terminate an instance’s execution from a separate thread.