blob: 72845616128ffcb238143a981132c60e36248551 [file] [log] [blame]
Zesstra3085c662025-08-02 18:31:10 +02001CONCEPT
2 coroutines
3
4INTRODUCTION
5 Regular LPC functions have a short life span. They must finish,
6 before a new event like a player command can be processed.
7 Coroutines are a special type of functions, whose execution can
8 be suspended and continued at a later time. At each suspension
9 point values can be exchanged with another coroutine or the caller.
10
11 Coroutines are passed by reference. When a coroutine finishes
12 it decays to the number zero.
13
14
15DEFINITION
16 Coroutines can be created by calling a function that was declared
17 with the async modifier:
18
19 async void fun()
20 {
21 ...
22 }
23
24 coroutine cr = fun();
25
26 Alternatively inline coroutines are created with the inline closure
27 syntax:
28
29 coroutine cr = async function void() { ... };
30
31 Normal coroutines can get parameters, inline coroutines cannot.
32 (They however can use context variables.)
33
34
35OPERATIONS
36 These operations can be used from within a coroutine:
37
38 await(coroutine[, value])
39
40 Call another coroutine, pass on the given value and wait for
41 this coroutine to finish with a return statement.
42 Until then the current coroutine is suspended. Any operation
43 on this coroutine will be passed to the called coroutine.
44 The result of the await() call is the return value from the
45 called coroutine.
46
47 yield([value])
48
49 Suspend execution of the current coroutine and pass the given
50 value to the caller. The result of the yield() call is the
51 value that will be passed in when the coroutine will be
52 continued.
53
54 yield(value, coroutine)
55
56 Suspend execution of the current coroutine and continue the
57 given coroutine, thereby passing the value in. The result of
58 the yield() call is the value that will be passed in when the
59 coroutine will be continued.
60
61 return [value]
62
63 Destroy the current coroutine. If any coroutine is waiting,
64 continue its execution. Otherwise return to the caller.
65
66
67EFUNS
68 call_coroutine(coroutine[, value])
69
70 Call the given coroutine, pass on the given value.
71
72 In contrast to await() and yield() the current execution will
73 only be suspended for the call (just like any other function
74 call) and be continued when the coroutine suspends execution
75 with yield() or finishes execution with return.
76
77 Therefore this efun can also be used from regular functions.
78
79 The result of the efun call is the value of the yield() or
80 return call that suspended the execution of the called coroutine.
81
82 this_coroutine()
83
84 Returns the current coroutine. If it's not called from a
85 coroutine, the innermost coroutine in the caller stack will
86 be returned. Returns 0 if there is no coroutine in the caller
87 stack.
88
89
90EXAMPLE
91 /* Coroutine that sleeps a given amount of time. */
92 async void sleep(int sec)
93 {
94 /* Start a call_out that will wake this coroutine. */
95 call_out(#'call_coroutine, sec, this_coroutine());
96
97 /* Suspend and wait. */
98 yield();
99 }
100
101 /* Use of the sleep() function. */
102 async void fun()
103 {
104 write("Starting...\n");
105 await(sleep(10));
106 write("Finishing after 10s.\n");
107 }
108
109
110MISCELLANEOUS
111 Support for coroutines is signaled by the macro __LPC_COROUTINES__.
112
113 Only declarative casts to coroutine are possible, there is no
114 conversion of any other type to coroutine available (therefore
115 there is no to_coroutine() efun).
116
117 Coroutines can not be copied or serialized with save_value().
118 Coroutines are bound to their object. If the object is destroyed,
119 the coroutines will be as well (and any awaiting coroutines).
120
121 foreach() can be used to call coroutines repeatedly.
122
123
124HISTORY
125 Coroutines were introduced in LDMud 3.6.5.
126
127
128SEE ALSO
129 async(LPC), await(LPC), yield(LPC), foreach(LPC),
130 call_coroutine(E), this_coroutine(E)