vxx / vlib / x / executor / examples / synthetic_ffi_owner_state.v
36 lines · 31 sloc · 619 bytes · e80386b6af8a071a2e13fd654ebcdb2105fb9202
Raw
1import x.executor
2
3struct FakeHandle {
4mut:
5 calls int
6}
7
8fn main() {
9 mut owner := executor.new(queue_size: 4)!
10 result := chan int{cap: 1}
11 mut handle := FakeHandle{}
12
13 owner.try_post(fn [mut handle, result] () ! {
14 handle.calls++
15 handle.calls++
16 result <- handle.calls
17 })!
18
19 if !owner.run_one()! {
20 eprintln('owner job did not run')
21 exit(1)
22 }
23 calls := <-result
24 if calls != 2 {
25 eprintln('unexpected handle call count: ${calls}')
26 exit(1)
27 }
28
29 println('synthetic FFI handle calls=${calls}')
30 owner.stop()
31 if owner.run_one()! {
32 eprintln('unexpected owner job after stop')
33 exit(1)
34 }
35 owner.wait()!
36}
37