-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrol_transfer.tex
58 lines (45 loc) · 2.66 KB
/
control_transfer.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
\abschnitt{control transfer mechanism}
According to the literature\cite{Moura2009}, coroutine-like control-transfer
operations can be distinguished into the concepts of \sym and \asym
operations.
\uabschnitt{symmetric fiber} A symmetric fiber provides a single
control-transfer operation. This single operation requires that the control is
passed explicitly between the fibers.
\graphc{symm}
\cppfl{symmetric_op}
In the pseudo-code example above, a chain of fibers is created.
Control is transferred to fiber \cpp{f1} at line 15 and the lambda
passed to constructor of \cpp{f1} is entered. Control is transferred from
fiber \cpp{f1} to \cpp{f2} at line 12 and from \cpp{f2} to \cpp{f3} (line 9) and
so on. Fiber \cpp{f4} itself transfers control directly back to
fiber \cpp{f1} at line 3.
\uabschnitt{asymmetric fiber} Two control-transfer operations are part of
asymmetric fiber's interface: one operation for resuming (\resume) and one for
suspending (\cpp{suspend()}) the fiber. The suspending operation returns
control back to the calling fiber.
\graphc{asymm}
\cppfl{asymmetric_op}
In the pseudo code above execution control is transferred to fiber \cpp{f1} at
line 16. Fiber \cpp{f1} resumes fiber \cpp{f2} at line 13 and so on. At line 2
fiber \cpp{f4} calls its suspend operation \cpp{self::suspend()}. Fiber \cpp{f4}
is suspended and \cpp{f3} resumed. Inside the lambda, \cpp{f3} returns from
\cpp{f4.resume()} and calls \cpp{self::suspend()} (line 6). Fiber \cpp{f3} gets
suspended while \cpp{f2} will be resumed and so on ...
The asymmetric version needs \bfs{N-1 more} fiber switches than the variant
using symmetric fibers.
\zs{While asymmetric fibers establish a caller-callee relationship (strongly
coupled), symmetric fibers operate as siblings (loosely coupled).}
Symmetric fibers represent independent flows of control, making symmetric
fibers a suitable mechanism for concurrent programming. Additionally,
constructs that produce sequences of values (\emph{generators}) are easily
constructed out of two symmetric fibers (one represents the caller, the other
the callee).
Asymmetric fibers incorporate additional fiber switches as shown in the pseudo
code above. It is obvious that for a broad range of use cases, asymmetric
fibers are less efficient than their symmetric counterparts.
Additionally, the calling fiber must be kept alive until the called fiber
terminates. Otherwise the call of \cpp{suspend()} will be undefined behaviour
(where to transfer execution control to?).
\zs{Symmetric fibers are more efficient, have fewer restrictions (no
caller-callee relationship) and can be used to create a wider set of
applications (generators, cooperative multitasking, backtracking ...).}