Wed, 11 Dec 2019 18:45:34 +0200
Add Channel comms helper module
5 | 1 | ######################################### |
2 | # Helpers for communication via channels | |
3 | ######################################### | |
4 | ||
5 | module Comms | |
6 | ||
7 | __precompile__() | |
8 | ||
9 | ############## | |
10 | # Our exports | |
11 | ############## | |
12 | ||
13 | export process_channel, | |
14 | put_onlylatest! | |
15 | ||
16 | #################### | |
17 | # Channel iteration | |
18 | #################### | |
19 | ||
20 | function process_channel(fn, rc) | |
21 | while true | |
22 | d=take!(rc) | |
23 | # Take only the latest image to visualise | |
24 | while isready(rc) | |
25 | d=take!(rc) | |
26 | end | |
27 | # We're done if we were fed nothing | |
28 | if isnothing(d) | |
29 | break | |
30 | end | |
31 | try | |
32 | fn(d) | |
33 | catch ex | |
34 | error("Exception in process_channel handler. Terminating.\n$(ex)") | |
35 | throw(ex) | |
36 | end | |
37 | end | |
38 | end | |
39 | ||
40 | ############################################# | |
41 | # Ensure only latest data is in a Channel(1) | |
42 | ############################################# | |
43 | ||
44 | function put_onlylatest!(rc, d) | |
45 | while isready(rc) | |
46 | take!(rc) | |
47 | end | |
48 | put!(rc, d) | |
49 | end | |
50 | ||
51 | end # Module |