File tree Expand file tree Collapse file tree 3 files changed +38
-2
lines changed
src/hyperlight_host/examples Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -89,17 +89,29 @@ fn main() -> Result<()> {
8989 let no_op = Noop :: < UninitializedSandbox , MultiUseSandbox > :: default ( ) ;
9090
9191 let mut multiuse_sandbox = usandbox. evolve ( no_op) ?;
92+ let interrupt_handle = multiuse_sandbox. interrupt_handle ( ) ;
93+
94+ const NUM_CALLS : i32 = 5 ;
95+ let thread = std:: thread:: spawn ( move || {
96+ for _ in 0 ..NUM_CALLS {
97+ // Sleep for a short time to allow the guest function to run.
98+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 500 ) ) ;
99+ // Cancel the host function call.
100+ interrupt_handle. kill ( ) ;
101+ }
102+ } ) ;
92103
93104 // Call a function that gets cancelled by the host function 5 times to generate some log entries.
94105
95- for _ in 0 ..5 {
106+ for _ in 0 ..NUM_CALLS {
96107 let mut ctx = multiuse_sandbox. new_call_context ( ) ;
97108
98109 let result = ctx. call ( "Spin" , ReturnType :: Void , None ) ;
99110 assert ! ( result. is_err( ) ) ;
100111 let result = ctx. finish ( ) ;
101112 multiuse_sandbox = result. unwrap ( ) ;
102113 }
114+ thread. join ( ) . unwrap ( ) ;
103115
104116 Ok ( ( ) )
105117}
Original file line number Diff line number Diff line change @@ -102,10 +102,22 @@ fn do_hyperlight_stuff() {
102102 let no_op = Noop :: < UninitializedSandbox , MultiUseSandbox > :: default ( ) ;
103103
104104 let mut multiuse_sandbox = usandbox. evolve ( no_op) . expect ( "Failed to evolve sandbox" ) ;
105+ let interrupt_handle = multiuse_sandbox. interrupt_handle ( ) ;
106+
107+ const NUM_CALLS : i32 = 5 ;
108+
109+ let thread = std:: thread:: spawn ( move || {
110+ for _ in 0 ..NUM_CALLS {
111+ // Sleep for a short time to allow the guest function to run.
112+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 500 ) ) ;
113+ // Cancel the host function call.
114+ interrupt_handle. kill ( ) ;
115+ }
116+ } ) ;
105117
106118 // Call a function that gets cancelled by the host function 5 times to generate some metrics.
107119
108- for _ in 0 ..5 {
120+ for _ in 0 ..NUM_CALLS {
109121 let mut ctx = multiuse_sandbox. new_call_context ( ) ;
110122
111123 let result = ctx. call ( "Spin" , ReturnType :: Void , None ) ;
@@ -119,6 +131,7 @@ fn do_hyperlight_stuff() {
119131 let result = join_handle. join ( ) ;
120132 assert ! ( result. is_ok( ) ) ;
121133 }
134+ thread. join ( ) . unwrap ( ) ;
122135}
123136
124137fn fn_writer ( _msg : String ) -> Result < i32 > {
Original file line number Diff line number Diff line change @@ -117,8 +117,18 @@ fn run_example() -> Result<()> {
117117 let no_op = Noop :: < UninitializedSandbox , MultiUseSandbox > :: default ( ) ;
118118
119119 let mut multiuse_sandbox = usandbox. evolve ( no_op) ?;
120+ let interrupt_handle = multiuse_sandbox. interrupt_handle ( ) ;
120121
121122 // Call a function that gets cancelled by the host function 5 times to generate some log entries.
123+ const NUM_CALLS : i32 = 5 ;
124+ let thread = std:: thread:: spawn ( move || {
125+ for _ in 0 ..NUM_CALLS {
126+ // Sleep for a short time to allow the guest function to run.
127+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 500 ) ) ;
128+ // Cancel the host function call.
129+ interrupt_handle. kill ( ) ;
130+ }
131+ } ) ;
122132
123133 for i in 0 ..5 {
124134 let id = Uuid :: new_v4 ( ) ;
@@ -143,6 +153,7 @@ fn run_example() -> Result<()> {
143153 let result = join_handle. join ( ) ;
144154 assert ! ( result. is_ok( ) ) ;
145155 }
156+ thread. join ( ) . unwrap ( ) ;
146157
147158 Ok ( ( ) )
148159}
You can’t perform that action at this time.
0 commit comments