1 |
root |
1.1 |
NAME |
2 |
|
|
Coro - coroutine process abstraction |
3 |
|
|
|
4 |
|
|
SYNOPSIS |
5 |
|
|
use Coro; |
6 |
|
|
|
7 |
|
|
async { |
8 |
|
|
# some asynchronous thread of execution |
9 |
|
|
}; |
10 |
|
|
|
11 |
|
|
# alternatively create an async process like this: |
12 |
|
|
|
13 |
|
|
sub some_func : Coro { |
14 |
|
|
# some more async code |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
cede; |
18 |
|
|
|
19 |
|
|
DESCRIPTION |
20 |
|
|
This module collection manages coroutines. Coroutines are similar to |
21 |
|
|
threads but don't run in parallel. |
22 |
|
|
|
23 |
|
|
In this module, coroutines are defined as "callchain + lexical variables |
24 |
|
|
+ @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
25 |
|
|
callchain, it's own set of lexicals and it's own set of perl's most |
26 |
|
|
important global variables. |
27 |
|
|
|
28 |
|
|
$main |
29 |
|
|
This coroutine represents the main program. |
30 |
|
|
|
31 |
|
|
$current (or as function: current) |
32 |
|
|
The current coroutine (the last coroutine switched to). The initial |
33 |
|
|
value is $main (of course). |
34 |
|
|
|
35 |
|
|
$idle |
36 |
|
|
The coroutine to switch to when no other coroutine is running. The |
37 |
|
|
default implementation prints "FATAL: deadlock detected" and exits. |
38 |
|
|
|
39 |
|
|
STATIC METHODS |
40 |
|
|
Static methods are actually functions that operate on the current |
41 |
|
|
process only. |
42 |
|
|
|
43 |
|
|
async { ... } [@args...] |
44 |
|
|
Create a new asynchronous process and return it's process object |
45 |
|
|
(usually unused). When the sub returns the new process is |
46 |
|
|
automatically terminated. |
47 |
|
|
|
48 |
|
|
# create a new coroutine that just prints its arguments |
49 |
|
|
async { |
50 |
|
|
print "@_\n"; |
51 |
|
|
} 1,2,3,4; |
52 |
|
|
|
53 |
|
|
schedule |
54 |
|
|
Calls the scheduler. Please note that the current process will not |
55 |
|
|
be put into the ready queue, so calling this function usually means |
56 |
|
|
you will never be called again. |
57 |
|
|
|
58 |
|
|
cede |
59 |
|
|
"Cede" to other processes. This function puts the current process |
60 |
|
|
into the ready queue and calls "schedule", which has the effect of |
61 |
|
|
giving up the current "timeslice" to other coroutines of the same or |
62 |
|
|
higher priority. |
63 |
|
|
|
64 |
|
|
terminate [arg...] |
65 |
|
|
Terminates the current process with the given status values (see |
66 |
|
|
cancel). |
67 |
|
|
|
68 |
|
|
# dynamic methods |
69 |
|
|
|
70 |
|
|
PROCESS METHODS |
71 |
|
|
These are the methods you can call on process objects. |
72 |
|
|
|
73 |
|
|
new Coro \&sub [, @args...] |
74 |
|
|
Create a new process and return it. When the sub returns the process |
75 |
|
|
automatically terminates as if "terminate" with the returned values |
76 |
|
|
were called. To make the process run you must first put it into the |
77 |
|
|
ready queue by calling the ready method. |
78 |
|
|
|
79 |
|
|
$process->ready |
80 |
|
|
Put the given process into the ready queue. |
81 |
|
|
|
82 |
|
|
$process->cancel (arg...) |
83 |
|
|
Temrinates the given process and makes it return the given arguments |
84 |
|
|
as status (default: the empty list). |
85 |
|
|
|
86 |
|
|
$process->join |
87 |
|
|
Wait until the coroutine terminates and return any values given to |
88 |
|
|
the "terminate" or "cancel" functions. "join" can be called multiple |
89 |
|
|
times from multiple processes. |
90 |
|
|
|
91 |
|
|
$oldprio = $process->prio($newprio) |
92 |
|
|
Sets (or gets, if the argument is missing) the priority of the |
93 |
|
|
process. Higher priority processes get run before lower priority |
94 |
|
|
processes. Priorities are small signed integers (currently -4 .. |
95 |
|
|
+3), that you can refer to using PRIO_xxx constants (use the import |
96 |
|
|
tag :prio to get then): |
97 |
|
|
|
98 |
|
|
PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
99 |
|
|
3 > 1 > 0 > -1 > -3 > -4 |
100 |
|
|
|
101 |
|
|
# set priority to HIGH |
102 |
|
|
current->prio(PRIO_HIGH); |
103 |
|
|
|
104 |
|
|
The idle coroutine ($Coro::idle) always has a lower priority than |
105 |
|
|
any existing coroutine. |
106 |
|
|
|
107 |
|
|
Changing the priority of the current process will take effect |
108 |
|
|
immediately, but changing the priority of processes in the ready |
109 |
|
|
queue (but not running) will only take effect after the next |
110 |
|
|
schedule (of that process). This is a bug that will be fixed in some |
111 |
|
|
future version. |
112 |
|
|
|
113 |
|
|
$newprio = $process->nice($change) |
114 |
|
|
Similar to "prio", but subtract the given value from the priority |
115 |
|
|
(i.e. higher values mean lower priority, just as in unix). |
116 |
|
|
|
117 |
|
|
$olddesc = $process->desc($newdesc) |
118 |
|
|
Sets (or gets in case the argument is missing) the description for |
119 |
|
|
this process. This is just a free-form string you can associate with |
120 |
|
|
a process. |
121 |
|
|
|
122 |
|
|
BUGS/LIMITATIONS |
123 |
|
|
- you must make very sure that no coro is still active on global |
124 |
|
|
destruction. very bad things might happen otherwise (usually segfaults). |
125 |
|
|
|
126 |
|
|
- this module is not thread-safe. You should only ever use this module |
127 |
|
|
from the same thread (this requirement might be losened in the future |
128 |
|
|
to allow per-thread schedulers, but Coro::State does not yet allow |
129 |
|
|
this). |
130 |
|
|
|
131 |
|
|
SEE ALSO |
132 |
root |
1.2 |
Support/Utility: Coro::Cont, Coro::Specific, Coro::State, Coro::Util. |
133 |
|
|
|
134 |
|
|
Locking/IPC: Coro::Signal, Coro::Channel, Coro::Semaphore, |
135 |
|
|
Coro::SemaphoreSet, Coro::RWLock. |
136 |
|
|
|
137 |
|
|
Event/IO: Coro::Timer, Coro::Event, Coro::Handle, Coro::Socket, |
138 |
|
|
Coro::Select. |
139 |
|
|
|
140 |
|
|
Embedding: <Coro:MakeMaker> |
141 |
root |
1.1 |
|
142 |
|
|
AUTHOR |
143 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
144 |
|
|
http://home.schmorp.de/ |
145 |
|
|
|