Codebase list libcoro-perl / bee858f
[svn-upgrade] new version libcoro-perl (6.000) Alessandro Ghedini 12 years ago
41 changed file(s) with 2003 addition(s) and 638 deletion(s). Raw diff Collapse all Expand all
22 TODO: should explore PerlIO::coroaio (perl leaks like hell).
33 TODO: channel->maxsize(newsize)?
44 TODO: http://www.microsoft.com/msj/archive/s2ce.aspx
5
6 6.0 Wed Jun 29 19:43:35 CEST 2011
7 - INCOMPATIBLE CHANGE: unreferenced coro objects will now be
8 destroyed and cleaned up automatically (e.g. async { schedule }).
9 - implement a JIT compiler for part of the thread switch code,
10 which gives a 50% speed improvement on threaded perls, and
11 about 4% on non-thraeded perls (so threaded perls now finally
12 reach about half the speed of non-threaded perls).
13 - slightly modernise Coro::Intro, add section about rouse functions.
14 - avoid DEFSV and ERRSV, giving another 10% improvement
15 in thread switching.
16 - Coro::State->is_destroyed is now called is_zombie.
17 - implement a Coro->safe_cancel method that might fail, but
18 cancels in a "safer" way if it succeeds.
19 - add preliminary support for DEBUGGING perls.
20 - get rid of two hash-accesses when initialising a new Coro - this
21 speeds up coro creation by almost a factor of two.
22 - croak when a coro that is being cancelled tries to block
23 (e.g. while executing a guard block), instead of crashing or
24 deadlocking.
25 - use a more robust and also faster method to identify Coro::State
26 objects - speeds up everything a bit.
27 - implement Coro->cancel in XS for a 20% speed improvement, and to
28 be able to implement mutual cancellation.
29 - speed up context switches by a percent or two by more efficiently
30 allocating context stack entries.
31 - implement Coro->join and Coro->on_destroy in XS for a speedup and
32 a reduction in memory use.
33 - cancelling a coro while it itself is cancelling another coro is
34 now supported and working, instead of triggering an assertion.
35 - be a bit more crash-resistant when calling (buggy) on_destroy
36 callbacks (best effort).
37 - move on_destroy into the slf_frame, to allow extension slf
38 functions to have destructors.
39 - get rid if coro refcounting - simply crash in other interpreter
40 threads by nulling the pointers on clone.
41 - simplify warn/die hook handling when loading Coro - the convoluted
42 logic seems to be no longer neccessary.
43 - use libecb instead of our own home-grown gcc hacks.
44 - document alternatives to Coro::LWP. Please use them :)
45 - work around another mindless idiotic NEEDLESS bug in openbsd/mirbsds
46 sigaltstack. Really. wine suffers from it, erlang suffers from it,
47 and it's known since at least 2006.
548
649 5.372 Wed Feb 23 06:14:30 CET 2011
750 - apparently mingw doesn't provide a working gettimeofday, try to
6868
6969 use base Exporter::;
7070
71 our $VERSION = 5.372;
71 our $VERSION = 6.0;
7272
7373 our @EXPORT = (@IO::AIO::EXPORT, qw(aio_wait));
7474 our @EXPORT_OK = @IO::AIO::EXPORT_OK;
163163 use Coro;
164164 use AnyEvent ();
165165
166 our $VERSION = 5.372;
166 our $VERSION = 6.0;
167167
168168 #############################################################################
169169 # idle handler
239239 =item Coro::AnyEvent::poll
240240
241241 This call will block the current thread until the event loop has polled
242 for new events and instructs the event loop to poll for new events once,
243 without blocking.
244
245 Note that this call will not actually execute the poll, just block until
246 new events have been polled, so other threads will have a chance to run.
242 for potential new events and instructs the event loop to poll for new
243 events once, without blocking.
244
245 Note that this call will not actually execute the poll, nor will it wait
246 until there are some events, just block until the event loop has polled
247 for new events, so other threads will have a chance to run.
247248
248249 This is useful when you have a thread that does some computations, but you
249250 still want to poll for new events from time to time. Simply call C<poll>
4646
4747 use base Exporter::;
4848
49 our $VERSION = 5.372;
49 our $VERSION = 6.0;
5050 our $WATCHER;
5151
5252 BDB::set_sync_prepare {
3434 use Coro ();
3535 use Coro::Semaphore ();
3636
37 our $VERSION = 5.372;
37 our $VERSION = 6.0;
3838
3939 sub DATA (){ 0 }
4040 sub SGET (){ 1 }
2727 {
2828 void (*prepare) (pTHX_ struct coro_transfer_args *ta); /* 0 means not yet initialised */
2929 int (*check) (pTHX_ struct CoroSLF *frame);
30 void *data; /* for use by prepare/check */
30 void *data; /* for use by prepare/check/destroy */
31 void (*destroy) (pTHX_ struct CoroSLF *frame);
3132 };
3233
3334 /* needs to fill in the *frame */
3940 /* private */
4041 I32 ver;
4142 I32 rev;
42 #define CORO_API_VERSION 7
43 #define CORO_API_REVISION 0
43 #define CORO_API_VERSION 7 /* reorder CoroSLF on change */
44 #define CORO_API_REVISION 1
4445
4546 /* Coro */
4647 int nready;
120120 use Coro::AnyEvent ();
121121 use Coro::Timer ();
122122
123 our $VERSION = 5.372;
123 our $VERSION = 6.0;
124124
125125 our %log;
126126 our $SESLOGLEVEL = exists $ENV{PERL_CORO_DEFAULT_LOGLEVEL} ? $ENV{PERL_CORO_DEFAULT_LOGLEVEL} : -1;
4444
4545 use base 'Exporter';
4646
47 our $VERSION = 5.372;
47 our $VERSION = 6.0;
4848 our @EXPORT = qw(unblock);
4949
5050 =item $fh = new_from_fh Coro::Handle $fhandle [, arg => value...]
1414 allowing you to return at any time, as kind of non-local jump, not unlike
1515 C's C<setjmp>/C<longjmp>. This is nowadays known as a L<Coro::State>.
1616
17 The natural application for these is to include a scheduler, resulting in
17 One natural application for these is to include a scheduler, resulting in
1818 cooperative threads, which is the main use case for Coro today. Still,
1919 much of the documentation and custom refers to these threads as
2020 "coroutines" or often just "coros".
2727 variable or location.
2828
2929 Cooperative means that these threads must cooperate with each other, when
30 it coems to CPU usage - only one thread ever has the CPU, and if another
30 it comes to CPU usage - only one thread ever has the CPU, and if another
3131 thread wants the CPU, the running thread has to give it up. The latter
3232 is either explicitly, by calling a function to do so, or implicity, when
3333 waiting on a resource (such as a Semaphore, or the completion of some I/O
34 request).
34 request). This threading model is popular in scripting languages (such as
35 python or ruby), and this implementation is typically far more efficient
36 than threads implemented in other languages.
3537
3638 Perl itself uses rather confusing terminilogy - what perl calls a "thread"
37 is actually called a "process" everywhere else: The so-called "perl
38 threads" are actually artifacts of the unix process emulation code used
39 on Windows, which is consequently why they are actually processes and not
40 threads. The biggest difference is that neither variables (nor code) are
41 shared between processes.
39 (or "ithread") is actually called a "process" everywhere else: The
40 so-called "perl threads" are actually artifacts of the unix process
41 emulation code used on Windows, which is consequently why they are
42 actually processes and not threads. The biggest difference is that neither
43 variables (nor code) are shared between processes or ithreads.
4244
4345
4446 =head1 Cooperative Threads
4547
46 Cooperative threads is what the Coro module gives you:
48 Cooperative threads is what the Coro module gives you. Obviously, you have
49 to C<use> it first:
4750
4851 use Coro;
4952
5558 };
5659
5760 Async expects a code block as first argument (in indirect object
58 notation). You can pass it more arguments, and these will end up in C<@_>
59 when executing the codeblock, but since it is a closure, you can also just
60 refer to any lexical variables that are currently visible.
61
62 If you save the above lines in a file and execute it as a perl program,
63 you will not get any output.
61 notation). You can actually pass it extra arguments, and these will end up
62 in C<@_> when executing the codeblock, but since it is a closure, you can
63 also just refer to any lexical variables that are currently visible.
64
65 The above lines create a thread, but if you save them in a file and
66 execute it as a perl program, you will not get any output.
6467
6568 The reasons is that, although you created a thread, and the thread is
6669 ready to execute (because C<async> puts it into the so-called I<ready
6770 queue>), it never gets any CPU time to actually execute, as the main
6871 program - which also is a thread almost like any other - never gives up
69 the CPU but instead exits the whole program, by running off the end of the
70 file.
72 the CPU but instead exits the whole program, by running off the end of
73 the file. Since Coro threads are cooperative, the main thread has to
74 cooperate, and give up the CPU.
7175
7276 To explicitly give up the CPU, use the C<cede> function (which is often
7377 called C<yield> in other thread implementations):
8286
8387 Running the above prints C<hello> and exits.
8488
85 This is not very interetsing, so let's try a slightly more interesting
86 program:
89 Now, this is not very interesting, so let's try a slightly more
90 interesting program:
8791
8892 use Coro;
8993
111115 "async 1", and itself yields the CPU. Since the only other thread
112116 available is the main program, it continues running and so on.
113117
114 In more detail, C<async> creates a new thread. All new threads
115 start in a suspended state. To make them run, they need to be put into
116 the ready queue, which is the second thing that C<async> does. Each time
117 a thread gives up the CPU, Coro runs a so-called I<scheduler>. The
118 scheduler selects the next thread from the ready queue, removes it from
119 the queue, and runs it.
118 Let's look at the example in more detail: C<async> first creates a new
119 thread. All new threads start in a suspended state. To make them run,
120 they need to be put into the ready queue, which is the second thing that
121 C<async> does. Each time a thread gives up the CPU, Coro runs a so-called
122 I<scheduler>. The scheduler selects the next thread from the ready queue,
123 removes it from the queue, and runs it.
120124
121125 C<cede> also does two things: first it puts the running thread into the
122126 ready queue, and then it jumps into the scheduler. This has the effect of
123 giving up the CPU, but also ensures that, eventually, the thread gets
124 run again.
127 giving up the CPU, but also ensures that, eventually, the thread gets run
128 again.
125129
126130 In fact, C<cede> could be implemented like this:
127131
130134 schedule;
131135 }
132136
133 This works because C<$Coro::current> always contains the currently running
134 thread, and the scheduler itself can be called via C<Coro::schedule>.
135
136 What's the effect of just calling C<schedule>? Simple, the scheduler
137 selects the next ready thread and runs it - the current thread, as
138 it hasn't been put into the ready queue, will go to sleep until something
139 wakes it up.
137 This works because C<$Coro::current> always contains the currently
138 running thread, and the scheduler itself can be called directly via
139 C<Coro::schedule>.
140
141 What is the effect of just calling C<schedule> without putting the current
142 thread into the ready queue first? Simple: the scheduler selects the
143 next ready thread and runs it. And the current thread, as it hasn't been
144 put into the ready queue, will go to sleep until something wakes it
145 up. If. Ever.
140146
141147 The following example remembers the current thread in a variable,
142148 creates a thread and then puts the main program to sleep.
149155 my $wakeme = $Coro::current;
150156
151157 async {
152 $wakeme->ready if 0.5 < rand;
158 $wakeme->ready if 0.5 > rand;
153159 };
154160
155161 schedule;
156162
157 Now, when you run it, one of two things happen: Either the C<async>
158 thread wakes up main again, in which case the program silently exits,
159 or it doesn't, in which case you get:
160
161 FATAL: deadlock detected at - line 0
162
163 Why is that? When the C<async> thread falls of the end, it will be
164 terminated (via a call to C<Coro::terminate>) and the scheduler gets run
165 again. Since the C<async> thread hasn't woken up the main thread,
166 and there aren't any other threads, there is nothing to wake up,
167 and the program cannot continue. Since there I<are> threads that
163 Now, when you run it, one of two things happen: Either the C<async> thread
164 wakes up the main thread again, in which case the program silently exits,
165 or it doesn't, in which case you get something like this:
166
167 FATAL: deadlock detected.
168 PID SC RSS USES Description Where
169 31976480 -C 19k 0 [main::] [program:9]
170 32223768 UC 12k 1 [Coro.pm:691]
171 32225088 -- 2068 1 [coro manager] [Coro.pm:691]
172 32225184 N- 216 0 [unblock_sub scheduler] -
173
174 Why is that? Well, when the C<async> thread runs into the end of its
175 block, it will be terminated (via a call to C<Coro::terminate>) and the
176 scheduler is called again. Since the C<async> thread hasn't woken up the
177 main thread, and there aren't any other threads, there is nothing to wake
178 up, and the program cannot continue. Since there I<are> threads that
168179 I<could> be running (main) but none are I<ready> to do so, Coro signals a
169 I<deadlock> - no progress is possible.
170
171 In fact, there is an important case where progress I<is>, in fact,
172 possible - namely in an event-based program. In such a case, the program
173 could well wait for I<external> events, such as a timeout, or some data to
174 arrive on a socket.
180 I<deadlock> - no progress is possible. Usually you also get a listing of
181 all threads, which might help you track down the problem.
182
183 However, there is an important case where progress I<is>, in fact,
184 possible, despite no threads being ready - namely in an event-based
185 program. In such a program, some threads could wait for I<external>
186 events, such as a timeout, or some data to arrive on a socket.
175187
176188 Since a deadlock in such a case would not be very useful, there is a
177189 module named L<Coro::AnyEvent> that integrates threads into an event
178 loop. It configures Coro in a way that instead of dieing with an error
190 loop. It configures Coro in a way that, instead of C<die>ing with an error
179191 message, it instead runs the event loop in the hope of receiving an event
180192 that will wake up some thread.
181193
188200 ways. The first such primitives is L<Coro::Semaphore>, which implements
189201 counting semaphores (binary semaphores are available as L<Coro::Signal>,
190202 and there are L<Coro::SemaphoreSet> and L<Coro::RWLock> primitives as
191 well):
203 well).
204
205 Counting semaphores, in a sense, store a count of resources. You can
206 remove/allocate/reserve a resource by calling the C<< ->down >> method,
207 which decrements the counter, and you can add or free a resource by
208 calling the C<< ->up >> method, which increments the counter. If the
209 counter is C<0>, then C<< ->down >> cannot decrement the semaphore - it is
210 locked - and the thread will wait until a count becomes available again.
211
212 Here is an example:
192213
193214 use Coro;
194215
204225 print "we got it!\n";
205226
206227 This program creates a I<locked> semaphore (a semaphore with count C<0>)
207 and tries to lock it. Since the semaphore is already locked, this will
208 block the main thread until the semaphore becomes available.
209
210 This yields the CPU to the C<async> thread, which unlocks the semaphore
211 (and instantly terminates itself by returning).
228 and tries to lock it (by trying to decrement it's counter in the C<down>
229 method). Since the semaphore count is already exhausted, this will block
230 the main thread until the semaphore becomes available.
231
232 This yields the CPU to the only other read thread in the process,t he
233 one created with C<async>, which unlocks the semaphore (and instantly
234 terminates itself by returning).
212235
213236 Since the semaphore is now available, the main program locks it and
214 continues.
215
216 Semaphores are most often used to lock resources, or to exclude other
217 threads from accessing or using a resource. For example, consider
237 continues: "we got it!".
238
239 Counting semaphores are most often used to lock resources, or to exclude
240 other threads from accessing or using a resource. For example, consider
218241 a very costly function (that temporarily allocates a lot of ram, for
219 example). You wouldn't want to have many threads calling this function,
220 so you use a semaphore:
221
222 my $lock = new Coro::Semaphore; # unlocked initially
242 example). You wouldn't want to have many threads calling this function at
243 the same time, so you use a semaphore:
244
245 my $lock = new Coro::Semaphore; # unlocked initially - default is 1
223246
224247 sub costly_function {
225248 $lock->down; # acquire semaphore
230253 }
231254
232255 No matter how many threads call C<costly_function>, only one will run
233 the body of it, all others will wait in the C<down> call.
234
235 Why does the comment mention "operation the blocks"? That's because coro's
236 threads are cooperative: unless C<costly_function> willingly gives up the
237 CPU, other threads of control will simply not run. This makes locking
238 superfluous in cases where the fucntion itself never gives up the CPU, but
239 when dealing with the outside world, this is rare.
256 the body of it, all others will wait in the C<down> call. If you want to
257 limit the number of concurrent executions to five, you could create the
258 semaphore with an initial count of C<5>.
259
260 Why does the comment mention an "operation the blocks"? Again, that's
261 because coro's threads are cooperative: unless C<costly_function>
262 willingly gives up the CPU, other threads of control will simply not
263 run. This makes locking superfluous in cases where the function itself
264 never gives up the CPU, but when dealing with the outside world, this is
265 rare.
240266
241267 Now consider what happens when the code C<die>s after executing C<down>,
242268 but before C<up>. This will leave the semaphore in a locked state, which
243 usually isn't what you want: normally you would want to free the lock
244 again.
245
246 This is what the C<guard> method is for:
269 often isn't what you want - imagine the caller expecting a failure and
270 wrapping the call into an C<eval {}>.
271
272 So normally you would want to free the lock again if execution somehow
273 leaves the function, whether "normally" or via an exception. Here the
274 C<guard> method proves useful:
247275
248276 my $lock = new Coro::Semaphore; # unlocked initially
249277
253281 # do costly operation that blocks
254282 }
255283
256 This method C<down>s the semaphore and returns a so-called guard
257 object. Nothing happens as long as there are references to it, but when
258 all references are gone, for example, when C<costly_function> returns or
259 throws an exception, it will automatically call C<up> on the semaphore,
260 no way to forget it. Even when the thread gets C<cancel>ed by another
261 thread will the guard object ensure that the lock is freed.
262
263 Apart from L<Coro::Semaphore> and L<Coro::Signal>, there is
264 also a reader-writer lock (L<Coro::RWLock>) and a semaphore set
265 (L<Coro::SemaphoreSet>).
284 The C<guard> method C<down>s the semaphore and returns a so-called guard
285 object. Nothing happens as long as there are references to it (i.e. it is
286 in scope somehow), but when all references are gone, for example, when
287 C<costly_function> returns or throws an exception, it will automatically
288 call C<up> on the semaphore, no way to forget it. Even when the thread
289 gets C<cancel>ed by another thread will the guard object ensure that the
290 lock is freed.
291
292 This concludes this introduction to semaphores and locks. Apart from
293 L<Coro::Semaphore> and L<Coro::Signal>, there is also a reader-writer lock
294 (L<Coro::RWLock>) and a semaphore set (L<Coro::SemaphoreSet>). All of
295 these come with their own manpage.
266296
267297
268298 =head2 Channels
269299
270300 Semaphores are fine, but usually you want to communicate by exchanging
271 data as well. This is where L<Coro::Channel> comes in useful: Channels
272 are the Coro equivalent of a unix pipe (and very similar to amiga message
273 ports :) - you can put stuff into it on one side, and read data from it on
274 the other.
275
276 Here is a simple example that creates a thread and sends it
277 numbers. The thread calculates the square of the number, and puts it
278 into another channel, which the main thread reads the result from:
301 data as well. Of course, you can just use some locks, and array of sorts
302 and use that to communicate, but there is a useful abstraction for
303 communicaiton between threads: L<Coro::Channel>. Channels are the Coro
304 equivalent of a unix pipe (and very similar to AmigaOS message ports :) -
305 you can put stuff into it on one side, and read data from it on the other.
306
307 Here is a simple example that creates a thread and sends numbers to
308 it. The thread calculates the square of each number and puts that into
309 another channel, which the main thread reads the result from:
279310
280311 use Coro;
281312
304335 10 ** 2 = 100
305336 77 ** 2 = 5929
306337
307 Both C<get> and C<put> methods can block the current thread: C<get>
308 first checks whether there I<is> some data available, and if not, it block
309 the current thread until some data arrives. C<put> can also block, as
310 each Channel has a "maximum buffering capacity", i.e. you cannot store
311 more than a specific number of items, which can be configured when the
312 Channel gets created.
338 Both C<get> and C<put> methods can block the current thread: C<get> first
339 checks whether there I<is> some data available, and if not, it block the
340 current thread until some data arrives. C<put> can also block, as each
341 Channel has a "maximum item capacity", i.e. you cannot store more than a
342 specific number of items, which can be configured when the Channel gets
343 created.
313344
314345 In the above example, C<put> never blocks, as the default capacity
315 of a Channel is very high. So the for loop first puts data into the
346 of a Channel is very high. So the for loop first puts data into the
316347 channel, then tries to C<get> the result. Since the async thread hasn't
317 put anything in there yet (on the firts iteration it hasn't even run
348 put anything in there yet (on the first iteration it hasn't even run
318349 yet), the result Channel is still empty, so the main thread blocks.
319350
320351 Since the only other runnable/ready thread at this point is the squaring
327358 calculate channel will it block (because nothing is there yet) and the
328359 main thread will continue running. And so on.
329360
330 In general, Coro will I<only ever block> a thread when it has to: Neither
331 the Coro module itself nor any of its submodules will ever give up the
332 CPU unless they have to, because they wait for some event to happen.
361 This illustrates a general principle used by Coro: a thread will I<only
362 ever block> when it has to. Neither the Coro module itself nor any of its
363 submodules will ever give up the CPU unless they have to, because they
364 wait for some event to happen.
333365
334366 Be careful, however: when multiple threads put numbers into C<$calculate>
335367 and read from C<$result>, they won't know which result is theirs. The
336368 solution for this is to either use a semaphore, or send not just the
337369 number, but also your own private result channel.
338370
339 L<Coro::Channel> can buffer some amount of items.
340
341371
342372 =head2 What is mine, what is ours?
343373
344374 What, exactly, constitutes a thread? Obviously it contains the current
345 point of execution. Not so obviously, it also has to include all
375 point of execution. Not so obviously, it also has to include all
346376 lexical variables, that means, every thread has its own set of lexical
347 variables. To see why this is necessary, consider this program:
377 variables.
378
379 To see why this is necessary, consider this program:
348380
349381 use Coro;
350382
366398 C<World!\nWorld\n>, which is rather unexpected, and would make it very
367399 difficult to make good use of threads.
368400
369 There are quite a number of other things that are per-thread:
401 To make things run smoothly, there are quite a number of other things that
402 are per-thread:
370403
371404 =over 4
372405
383416 =item $/ and the default output file handle
384417
385418 Threads most often block when doing I/O. Since C<$/> is used when reading
386 lines, it would be very inconvenient if it were a shared variable, so it is per-thread.
419 lines, it would be very inconvenient if it were a shared variable, so it
420 is per-thread.
387421
388422 The default output handle (see C<select>) is a difficult case: sometimes
389423 being global is preferable, sometimes per-thread is preferable. Since
399433 };
400434
401435 Would not allow coroutine switching. Since exception-handling is
402 per-thread, those variables should be per-thread too.
436 per-thread, those variables should be per-thread as well.
403437
404438 =item Lots of other esoteric stuff
405439
406440 For example, C<$^H> is per-thread. Most of the additional per-thread state
407 is not directly visible to perl, but required to make the interpreter
441 is not directly visible to Perl, but required to make the interpreter
408442 work. You won't normally notice these.
409443
410444 =back
426460
427461 =head2 Debugging
428462
429 Sometimes it can be useful to find out what each thread is doing (or
430 which threads exist in the first place). The L<Coro::Debug> module has
431 (among other goodies), a function that allows you to print a "ps"-like
432 listing:
463 Sometimes it can be useful to find out what each thread is doing (or which
464 threads exist in the first place). The L<Coro::Debug> module has (among
465 other goodies), a function that allows you to print a "ps"-like listing -
466 you have seen it in action earlier when Coro detected a deadlock.
467
468 You use it like this:
433469
434470 use Coro::Debug;
435471
436472 Coro::Debug::command "ps";
437473
438 Running it just after C<< $calculate->get >> outputs something similar to this:
474 Remember the example with the two channels and a worker thread that
475 squared numbers? Running "ps" just after C<< $calculate->get >> outputs
476 something similar to this:
439477
440478 PID SC RSS USES Description Where
441479 8917312 -C 22k 0 [main::] [introscript:20]
464502 =head1 The Real World - Event Loops
465503
466504 Coro really wants to run in a program using some event loop. In fact, most
467 real-world programs using Coro's threads are written in a combination of
505 real-world programs using Coro threads are written with a combination of
468506 event-based and thread-based techniques, as it is easy to get the best of
469507 both worlds with Coro.
470508
471 Coro integrates well into any event loop supported by L<AnyEvent>, simply
472 by C<use>ing L<Coro::AnyEvent>, but can take special advantage of the
509 Coro integrates automatically into any event loop supported by L<AnyEvent>
510 (see L<Coro::AnyEvent> for details), but can take special advantage of the
473511 L<EV> and L<Event> modules.
474512
475513 Here is a simple finger client, using whatever event loop L<AnyEvent>
476 comes up with (L<Coro::Socket> automatically initialises all the event
477 stuff):
514 comes up with:
478515
479516 use Coro;
480517 use Coro::Socket;
500537 $_->join; # wait for the result
501538 }
502539
503 There are quite a few new things here. First of all, there
504 is L<Coro::Socket>. This module works much the same way as
540 There are a few new things here. First of all, there is
541 L<Coro::Socket>. This module works much the same way as
505542 L<IO::Socket::INET>, except that it is coroutine-aware. This means that
506543 L<IO::Socket::INET>, when waiting for the network, will block the whole
507544 process - that means all threads, which is clearly undesirable.
508545
509546 On the other hand, L<Coro::Socket> knows how to give up the CPU to other
510 threads when it waits for the network, which makes parallel processing
547 threads when it waits for the network, which makes parallel execution
511548 possible.
512549
513550 The other new thing is the C<join> method: All we want to do in this
516553 much simpler to synchronously wait for them to C<terminate>, which is
517554 exactly what the C<join> method does.
518555
519 It doesn't matter that the three async's will probably finish in a
520 different order then the for loop C<join>s them - when the thread
521 is still running, C<join> simply waits. If the thread has already
522 terminated, it will simply fetch its return status.
556 It doesn't matter that the three C<async>s will probably finish in a
557 different order then the for loop C<join>s them - when the thread is still
558 running, C<join> simply waits. If the thread has already terminated, it
559 will simply fetch its return status.
523560
524561 If you are experienced in event-based programming, you will see that the
525562 above program doesn't quite follow the normal pattern, where you start
535572
536573 EV::loop; # and loop
537574
538 In fact, for debugging, you often do something like this:
575 And in fact, for debugging, you often do something like this:
539576
540577 use EV;
541578 use Coro::Debug;
583620
584621 Fortunately, the L<IO::AIO> module on CPAN allows you to move these
585622 I/O calls into the background, letting you do useful work in the
586 foreground. It is event-/callback-based, but Coro has a nice interface to
587 it, called L<Coro::AIO>, which let's you use its functions naturally from
588 within threads:
623 foreground. It is event-/callback-based, but Coro has a nice wrapper
624 around it, called L<Coro::AIO>, which lets you use its functions
625 naturally from within threads:
589626
590627 use Fcntl;
591628 use Coro::AIO;
602639 and atomically replaces a base file with a new copy.
603640
604641
642 =head2 Inversion of control - rouse functions
643
644 Last not least, me talk about inversion of control. The "control" refers
645 to "who calls whom", who is in control of the program. In this program,
646 the main program is in control and passes this to all functions it calls:
647
648 use LWP;
649
650 # pass control to get
651 my $res = get "http://example.org/";
652 # control returned to us
653
654 print $res;
655
656 When switching to event-based programs, instead of "us calling them",
657 "they call us" - this is the inversion of control form the title:
658
659 use AnyEvent::HTTP;
660
661 # do not pass control for long - http_get immediately returns
662 http_get "http://example.org/", sub {
663 print $_[0];
664 };
665
666 # we stay in control and can do other things
667
668 Event based programming can be nice, but sometimes it's just easier to
669 write down some processing in "linear" fashion, without callbacks. Coro
670 provides some special functions to reduce typing:
671
672 use AnyEvent::HTTP;
673
674 # do not pass control for long - http_get immediately returns
675 http_get "http://example.org/", Coro::rouse_cb;
676
677 # we stay in control and can do other things...
678 # ...such as wait for the result
679 my ($res) = Coro::rouse_wait;
680
681 C<Coro::rouse_cb> creates and returns a special callback. You can pass
682 this callback to any function that would expect a callback.
683
684 C<Coro::rouse_wait> waits (block the current thread) until the most
685 recently created callback has been called, and returns whatever was passed
686 to it.
687
688 These two functions allow you to I<mechanically> invert the control from
689 "callback based style" used by most event-based libraries to "blocking
690 style", whenever you wish to.
691
692 The pattern is simple: instead of...
693
694 some_func ..., sub {
695 my @res = @_;
696 ...
697 };
698
699 ... you write:
700
701 some_func ..., Coro::rouse_cb;
702 my @res = Coro::rouse_wait;
703 ...
704
705 Callback-based interfaces are plenty, and the rouse functions allow you to
706 use them in an often more convenient way.
707
708
605709 =head1 Other Modules
606710
607 This introduction only mentions a very few methods and modules, Coro has
608 many other functions (see the L<Coro> manpage) and modules (documented in
609 the C<SEE ALSO> section of the L<Coro> manpage).
711 This introduction only mentions a few methods and modules, Coro has many
712 other functions (see the L<Coro> manpage) and modules (documented in the
713 C<SEE ALSO> section of the L<Coro> manpage).
610714
611715 Noteworthy modules are L<Coro::LWP> (for parallel LWP requests, but see
612716 L<AnyEvent::HTTP> for a better HTTP-only alternative), L<Coro::BDB>, for
615719 C<STDOUT>) and L<Coro::EV>, the optimised interface to L<EV> (which gets
616720 used automatically by L<Coro::AnyEvent>).
617721
722 There are a number of Coro-related moduels that might be useful for your problem
723 (see L<http://search.cpan.org/search?query=Coro&mode=module>). And since Coro
724 integrates so well into AnyEvent, it's often easy to adapt existing AnyEvent modules
725 (see L<http://search.cpan.org/search?query=AnyEvent&mode=module>).
726
618727
619728 =head1 AUTHOR
620729
44 =head1 SYNOPSIS
55
66 use Coro::LWP; # afterwards LWP should not block
7
8 =head1 ALTERNATIVES
9
10 Over the years, a number of less-invasive alternatives have popped up,
11 which you might find more acceptable than this rather invasive and fragile
12 module. All of them only support HTTP (and sometimes HTTPS).
13
14 =over 4
15
16 =item L<AnyEvent::HTTP>
17
18 Works fine without Coro. Requires using a very different API than
19 LWP. Probably the best choice I<iff> you can do with a completely
20 different event-based API.
21
22 =item L<LWP::Protocol::AnyEvent::http>
23
24 Makes LWP use L<AnyEvent::HTTP>. Does not make LWP event-based, but allows
25 Coro threads to schedule unimpeded through its AnyEvent integration.
26
27 Let's you use the LWP API normally.
28
29 =item L<LWP::Protocol::Coro::http>
30
31 Basically the same as above, distinction unclear. :)
32
33 =item L<AnyEvent::HTTP::LWP::UserAgent>
34
35 A different user agent implementation, not completely transparent to
36 users, requires Coro.
37
38 =back
739
840 =head1 DESCRIPTION
941
93125 use Net::FTP ();
94126 use Net::NNTP ();
95127
96 our $VERSION = 5.372;
128 our $VERSION = 6.0;
97129
98130 *Socket::inet_aton = \&Coro::Util::inet_aton;
99131
66
77 our $installsitearch;
88
9 our $VERSION = 5.372;
9 our $VERSION = 6.0;
1010 our @EXPORT_OK = qw(&coro_args $installsitearch);
1111
1212 my %opt;
8686 }
8787
8888 } elsif ($^O =~ /(openbsd|mirbsd)/) {
89 # openbsd:
90 # asm seems to work, setjmp might, ucontext is missing, threads lets not talk about
91 # try setjmp/longjmp on 4.4, but pthread on earlier
92 # mirbsd:
93 # seems to be bug-to-bug compatible openbsd fork,
89 # mirbsd seems to be bug-to-bug compatible openbsd fork,
9490 # with the name change being the biggest difference.
95 $iface = $iface_asm || ($Config{osvers} >= 4.4 ? "s" : "p");
91 if ($Config{libs} !~ "-lpthread") {
92 # asm seems to work, setjmp might, ucontext is missing,
93 # threads lets not talk about
94 # try setjmp/longjmp on 4.4, but pthread on earlier
95 $iface = $iface_asm || ($Config{osvers} >= 4.4 ? "s" : "p");
96 } else {
97 # seems newer openbsd platforms have marginally working pthreads, but
98 # their pthreads break sigaltstack - reading the sigaltstack sources
99 # again shows how fundamentally clueless those people are (if no thread
100 # has ever been created, then the program is bound to a kernel-scheduled
101 # entity. get that? GET THAT?)
102 $iface = "p";
103 }
96104
97105 } elsif ($^O =~ /solaris/) {
98106 # setjmp, ucontext seem to work, as well as asm
126134
127135 u The unix 'ucontext.h' functions are relatively new and not implemented
128136 or well-tested in older unices. They allow very fast coroutine creation
129 and reasonably fast switching. They are, however, usually slower than
137 and reasonably fast switching. They are, however, usually slower than
130138 the other alternatives due to an extra syscall done by swapcontext. And
131139 while nominally most portable (it's the only POSIX-standardised
132140 interface for coroutines), ucontext functions are, as usual, broken on
321329
322330 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
323331
332 Coro can use a simple JIT compiler to compile a part of the thread switch
333 function at runtime. On perls with windows process emulation (most!),
334 this results in a 50% speed improvement. On sane perls, the gain is much
335 less, usually around 5%. If you enable this option, then the JIT will
336 be enabled, on compatible operating systems and CPUs (currently only
337 x86/amd64 on certain unix clones). Otherwise, it will be disabled. It
338 should be safe to leave on - this setting is only here so you can switch
339 it off in case of problems.
340
341 EOF
342
343 my $orgasm = $ENV{CORO_JIT} || "y";
344 $orgasm = prompt ("Try to use the JIT compiler, if available?", $orgasm);
345 $DEFINE .= " -DCORO_JIT=1" if $orgasm =~ /[yY]/;
346
347 print <<EOF;
348
349 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
350
324351 Coro has experimental support for cloning states. This can be used
325352 to implement a scheme-like call/cc. However, this doesn't add to the
326353 expressiveness in general, and is likely perl-version specific (and perl
348375 LIBS => @LIBS,
349376 DIR => [],
350377 depend => {
351 "State.c" => "state.h clone.c libcoro/coro.h libcoro/coro.c",
378 "State.c" => "state.h clone.c ecb.h libcoro/coro.h libcoro/coro.c",
352379 },
353380 );
354381
3737
3838 use Coro ();
3939
40 our $VERSION = 5.372;
40 our $VERSION = 6.0;
4141
4242 =item $l = new Coro::RWLock;
4343
6666
6767 use base Exporter::;
6868
69 our $VERSION = 5.372;
69 our $VERSION = 6.0;
7070 our @EXPORT_OK = "select";
7171
7272 sub import {
3939
4040 use Coro ();
4141
42 our $VERSION = 5.372;
42 our $VERSION = 6.0;
4343
4444 =item new [inital count]
4545
3434
3535 use common::sense;
3636
37 our $VERSION = 5.372;
37 our $VERSION = 6.0;
3838
3939 use Coro::Semaphore ();
4040
3737
3838 use Coro::Semaphore ();
3939
40 our $VERSION = 5.372;
40 our $VERSION = 6.0;
4141
4242 =item $sig = new Coro::Signal;
4343
7272
7373 use base qw(Coro::Handle IO::Socket::INET);
7474
75 our $VERSION = 5.372;
75 our $VERSION = 6.0;
7676
7777 our (%_proto, %_port);
7878
3535
3636 use common::sense;
3737
38 our $VERSION = 5.372;
38 our $VERSION = 6.0;
3939
4040 =item new
4141
8989 use XSLoader;
9090
9191 BEGIN {
92 our $VERSION = 5.372;
92 our $VERSION = 6.0;
9393
9494 # must be done here because the xs part expects it to exist
9595 # it might exist already because Coro::Specific created it.
9696 $Coro::current ||= { };
9797
98 {
99 # save/restore the handlers before/after overwriting %SIG magic
100 local $SIG{__DIE__};
101 local $SIG{__WARN__};
102
103 XSLoader::load __PACKAGE__, $VERSION;
104 }
98 XSLoader::load __PACKAGE__, $VERSION;
10599
106100 # need to do it after overwriting the %SIG magic
107101 $SIG{__DIE__} ||= \&diehook;
230224 The "state" of a subroutine includes the scope, i.e. lexical variables and
231225 the current execution state (subroutine, stack).
232226
227 =item $state->throw ([$scalar])
228
233229 =item $state->is_new
234230
235 Returns true iff this Coro::State object is "new", i.e. has never been run
236 yet. Those states basically consist of only the code reference to call and
237 the arguments, but consumes very little other resources. New states will
238 automatically get assigned a perl interpreter when they are transfered to.
239
240 =item $state->is_destroyed
241
242 Returns true iff the Coro::State object has been destroyed (by
243 C<cancel>), i.e. it's resources freed because they were C<cancel>'d (or
244 C<terminate>'d).
231 =item $state->is_zombie
232
233 See the corresponding method for L<Coro> objects.
245234
246235 =item $state->cancel
247236
248237 Forcefully destructs the given Coro::State. While you can keep the
249238 reference, and some memory is still allocated, the Coro::State object is
250 effecticely dead, destructors have been freed, it cannot be transfered to
251 anymore.
252
253 =item $state->throw ([$scalar])
254
255 See L<< Coro->throw >>.
239 effectively dead, destructors have been freed, it cannot be transfered to
240 anymore, it's pushing up the daisies.
256241
257242 =item $state->call ($coderef)
258243
1313 #include "perliol.h"
1414
1515 #include "schmorp.h"
16
16 #include "ecb.h"
17
18 #include <stddef.h>
1719 #include <stdio.h>
1820 #include <errno.h>
1921 #include <assert.h>
2224 # define SVs_PADSTALE 0
2325 #endif
2426
25 #ifdef WIN32
27 #if defined(_WIN32)
2628 # undef HAS_GETTIMEOFDAY
2729 # undef setjmp
2830 # undef longjmp
3234 # include <inttypes.h> /* most portable stdint.h */
3335 #endif
3436
35 #ifdef HAVE_MMAP
37 #if HAVE_MMAP
3638 # include <unistd.h>
3739 # include <sys/mman.h>
3840 # ifndef MAP_ANONYMOUS
6264 /* the maximum number of idle cctx that will be pooled */
6365 static int cctx_max_idle = 4;
6466
67 #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
68 # define HAS_SCOPESTACK_NAME 1
69 #endif
70
6571 #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
6672 # undef CORO_STACKGUARD
6773 #endif
8793
8894 #define IN_DESTRUCT PL_dirty
8995
90 #if __GNUC__ >= 3
91 # define attribute(x) __attribute__(x)
92 # define expect(expr,value) __builtin_expect ((expr), (value))
93 # define INLINE static inline
94 #else
95 # define attribute(x)
96 # define expect(expr,value) (expr)
97 # define INLINE static
98 #endif
99
100 #define expect_false(expr) expect ((expr) != 0, 0)
101 #define expect_true(expr) expect ((expr) != 0, 1)
102
103 #define NOINLINE attribute ((noinline))
104
10596 #include "CoroAPI.h"
10697 #define GCoroAPI (&coroapi) /* very sneaky */
10798
110101 static void *coro_thx;
111102 # endif
112103 #endif
104
105 /* used in state.h */
106 #define VAR(name,type) VARx(name, PL_ ## name, type)
113107
114108 #ifdef __linux
115109 # include <time.h> /* for timespec */
127121 /* we hijack an hopefully unused CV flag for our purposes */
128122 #define CVf_SLF 0x4000
129123 static OP *pp_slf (pTHX);
124 static void slf_destroy (pTHX_ struct coro *coro);
130125
131126 static U32 cctx_gen;
132127 static size_t cctx_stacksize = CORO_STACKSIZE;
166161 static struct coro_cctx *cctx_first;
167162 static int cctx_count, cctx_idle;
168163
169 enum {
164 enum
165 {
170166 CC_MAPPED = 0x01,
171167 CC_NOREUSE = 0x02, /* throw this away after tracing */
172168 CC_TRACE = 0x04,
197193 unsigned char flags;
198194 } coro_cctx;
199195
200 coro_cctx *cctx_current; /* the currently running cctx */
196 static coro_cctx *cctx_current; /* the currently running cctx */
201197
202198 /*****************************************************************************/
203199
204 enum {
200 static MGVTBL coro_state_vtbl;
201
202 enum
203 {
205204 CF_RUNNING = 0x0001, /* coroutine is running */
206205 CF_READY = 0x0002, /* coroutine is ready */
207206 CF_NEW = 0x0004, /* has never been switched to */
208 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
207 CF_ZOMBIE = 0x0008, /* coroutine data has been freed */
209208 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
209 CF_NOCANCEL = 0x0020, /* cannot cancel, set slf_frame.data to 1 (hackish) */
210210 };
211211
212212 /* the structure where most of the perl state is stored, overlaid on the cxstack */
213213 typedef struct
214214 {
215 SV *defsv;
216 AV *defav;
217 SV *errsv;
218 SV *irsgv;
219 HV *hinthv;
220 #define VAR(name,type) type name;
215 #define VARx(name,expr,type) type name;
221216 # include "state.h"
222 #undef VAR
217 #undef VARx
223218 } perl_slots;
224219
220 /* how many context stack entries do we need for perl_slots */
225221 #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
226222
227223 /* this is a structure representing a perl-level coroutine */
228 struct coro {
224 struct coro
225 {
229226 /* the C coroutine allocated to this perl coroutine, if any */
230227 coro_cctx *cctx;
231228
237234 AV *mainstack;
238235 perl_slots *slot; /* basically the saved sp */
239236
240 CV *startcv; /* the CV to execute */
241 AV *args; /* data associated with this coroutine (initial args) */
242 int refcnt; /* coroutines are refcounted, yes */
243 int flags; /* CF_ flags */
244 HV *hv; /* the perl hash associated with this coro, if any */
245 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
237 CV *startcv; /* the CV to execute */
238 AV *args; /* data associated with this coroutine (initial args) */
239 int flags; /* CF_ flags */
240 HV *hv; /* the perl hash associated with this coro, if any */
246241
247242 /* statistics */
248243 int usecount; /* number of transfers to this coro */
249244
250245 /* coro process data */
251246 int prio;
252 SV *except; /* exception to be thrown */
253 SV *rouse_cb;
247 SV *except; /* exception to be thrown */
248 SV *rouse_cb; /* last rouse callback */
249 AV *on_destroy; /* callbacks or coros to notify on destroy */
250 AV *status; /* the exit status list */
254251
255252 /* async_pool */
256253 SV *saved_deffh;
276273
277274 /* the following variables are effectively part of the perl context */
278275 /* and get copied between struct coro and these variables */
279 /* the mainr easonw e don't support windows process emulation */
276 /* the main reason we don't support windows process emulation */
280277 static struct CoroSLF slf_frame; /* the current slf frame */
281278
282279 /** Coro ********************************************************************/
292289 static SV *coro_current;
293290 static SV *coro_readyhook;
294291 static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
295 static CV *cv_coro_run, *cv_coro_terminate;
292 static CV *cv_coro_run;
296293 static struct coro *coro_first;
297294 #define coro_nready coroapi.nready
295
296 /** JIT *********************************************************************/
297
298 #if CORO_JIT
299 /* APPLE doesn't have HAVE_MMAP though */
300 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
301 #ifndef CORO_JIT_TYPE
302 #if __x86_64 && CORO_JIT_UNIXY
303 #define CORO_JIT_TYPE "amd64-unix"
304 #elif __i386 && CORO_JIT_UNIXY
305 #define CORO_JIT_TYPE "x86-unix"
306 #endif
307 #endif
308 #endif
309
310 #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
311 #undef CORO_JIT
312 #endif
313
314 #if CORO_JIT
315 typedef void (*load_save_perl_slots_type)(perl_slots *);
316 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
317 #endif
298318
299319 /** Coro::Select ************************************************************/
300320
320340
321341 #ifdef HAS_GETTIMEOFDAY
322342
323 static void
343 ecb_inline void
324344 coro_u2time (pTHX_ UV ret[2])
325345 {
326346 struct timeval tv;
330350 ret [1] = tv.tv_usec;
331351 }
332352
333 static double
334 coro_nvtime ()
353 ecb_inline double
354 coro_nvtime (void)
335355 {
336356 struct timeval tv;
337357 gettimeofday (&tv, 0);
339359 return tv.tv_sec + tv.tv_usec * 1e-6;
340360 }
341361
342 static void
362 ecb_inline void
343363 time_init (pTHX)
344364 {
345365 nvtime = coro_nvtime;
348368
349369 #else
350370
351 static void
371 ecb_inline void
352372 time_init (pTHX)
353373 {
354374 SV **svp;
357377
358378 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
359379
360 if (!svp) croak ("Time::HiRes is required, but missing.");
361 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
380 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
381 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
362382
363383 nvtime = INT2PTR (double (*)(), SvIV (*svp));
364384
370390
371391 /** lowlevel stuff **********************************************************/
372392
373 static SV *
393 static SV * ecb_noinline
374394 coro_get_sv (pTHX_ const char *name, int create)
375395 {
376396 #if PERL_VERSION_ATLEAST (5,10,0)
380400 return get_sv (name, create);
381401 }
382402
383 static AV *
403 static AV * ecb_noinline
384404 coro_get_av (pTHX_ const char *name, int create)
385405 {
386406 #if PERL_VERSION_ATLEAST (5,10,0)
390410 return get_av (name, create);
391411 }
392412
393 static HV *
413 static HV * ecb_noinline
394414 coro_get_hv (pTHX_ const char *name, int create)
395415 {
396416 #if PERL_VERSION_ATLEAST (5,10,0)
400420 return get_hv (name, create);
401421 }
402422
403 INLINE void
404 coro_times_update ()
423 ecb_inline void
424 coro_times_update (void)
405425 {
406426 #ifdef coro_clock_gettime
407427 struct timespec ts;
423443 #endif
424444 }
425445
426 INLINE void
446 ecb_inline void
427447 coro_times_add (struct coro *c)
428448 {
429449 c->t_real [1] += time_real [1];
435455 c->t_cpu [0] += time_cpu [0];
436456 }
437457
438 INLINE void
458 ecb_inline void
439459 coro_times_sub (struct coro *c)
440460 {
441461 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
453473 #define CORO_MAGIC_type_cv 26
454474 #define CORO_MAGIC_type_state PERL_MAGIC_ext
455475
456 #define CORO_MAGIC_NN(sv, type) \
457 (expect_true (SvMAGIC (sv)->mg_type == type) \
458 ? SvMAGIC (sv) \
476 #define CORO_MAGIC_NN(sv, type) \
477 (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \
478 ? SvMAGIC (sv) \
459479 : mg_find (sv, type))
460480
461 #define CORO_MAGIC(sv, type) \
462 (expect_true (SvMAGIC (sv)) \
463 ? CORO_MAGIC_NN (sv, type) \
481 #define CORO_MAGIC(sv, type) \
482 (ecb_expect_true (SvMAGIC (sv)) \
483 ? CORO_MAGIC_NN (sv, type) \
464484 : 0)
465485
466486 #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
467487 #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
468488
469 INLINE struct coro *
489 ecb_inline MAGIC *
490 SvSTATEhv_p (pTHX_ SV *coro)
491 {
492 MAGIC *mg;
493
494 if (ecb_expect_true (
495 SvTYPE (coro) == SVt_PVHV
496 && (mg = CORO_MAGIC_state (coro))
497 && mg->mg_virtual == &coro_state_vtbl
498 ))
499 return mg;
500
501 return 0;
502 }
503
504 ecb_inline struct coro *
470505 SvSTATE_ (pTHX_ SV *coro)
471506 {
472 HV *stash;
473507 MAGIC *mg;
474508
475509 if (SvROK (coro))
476510 coro = SvRV (coro);
477511
478 if (expect_false (SvTYPE (coro) != SVt_PVHV))
512 mg = SvSTATEhv_p (aTHX_ coro);
513 if (!mg)
479514 croak ("Coro::State object required");
480515
481 stash = SvSTASH (coro);
482 if (expect_false (stash != coro_stash && stash != coro_state_stash))
483 {
484 /* very slow, but rare, check */
485 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
486 croak ("Coro::State object required");
487 }
488
489 mg = CORO_MAGIC_state (coro);
490516 return (struct coro *)mg->mg_ptr;
491517 }
492518
499525 /*****************************************************************************/
500526 /* padlist management and caching */
501527
502 static AV *
528 ecb_inline AV *
503529 coro_derive_padlist (pTHX_ CV *cv)
504530 {
505531 AV *padlist = CvPADLIST (cv);
521547 return newpadlist;
522548 }
523549
524 static void
550 ecb_inline void
525551 free_padlist (pTHX_ AV *padlist)
526552 {
527553 /* may be during global destruction */
574600 };
575601
576602 /* the next two functions merely cache the padlists */
577 static void
603 ecb_inline void
578604 get_padlist (pTHX_ CV *cv)
579605 {
580606 MAGIC *mg = CORO_MAGIC_cv (cv);
581607 AV *av;
582608
583 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
609 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
584610 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
585611 else
586612 {
597623 }
598624 }
599625
600 static void
626 ecb_inline void
601627 put_padlist (pTHX_ CV *cv)
602628 {
603629 MAGIC *mg = CORO_MAGIC_cv (cv);
604630 AV *av;
605631
606 if (expect_false (!mg))
632 if (ecb_expect_false (!mg))
607633 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
608634
609635 av = (AV *)mg->mg_obj;
610636
611 if (expect_false (AvFILLp (av) >= AvMAX (av)))
637 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
612638 av_extend (av, AvFILLp (av) + 1);
613639
614640 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
663689 }
664690 }
665691
666 #define SWAP_SVS(coro) \
667 if (expect_false ((coro)->swap_sv)) \
692 #define SWAP_SVS(coro) \
693 if (ecb_expect_false ((coro)->swap_sv)) \
668694 swap_svs (aTHX_ (coro))
669695
670696 static void
678704
679705 PL_mainstack = c->mainstack;
680706
681 GvSV (PL_defgv) = slot->defsv;
682 GvAV (PL_defgv) = slot->defav;
683 GvSV (PL_errgv) = slot->errsv;
684 GvSV (irsgv) = slot->irsgv;
685 GvHV (PL_hintgv) = slot->hinthv;
686
687 #define VAR(name,type) PL_ ## name = slot->name;
707 #if CORO_JIT
708 load_perl_slots (slot);
709 #else
710 #define VARx(name,expr,type) expr = slot->name;
688711 # include "state.h"
689 #undef VAR
712 #undef VARx
713 #endif
690714
691715 {
692716 dSP;
694718 CV *cv;
695719
696720 /* now do the ugly restore mess */
697 while (expect_true (cv = (CV *)POPs))
721 while (ecb_expect_true (cv = (CV *)POPs))
698722 {
699723 put_padlist (aTHX_ cv); /* mark this padlist as available */
700724 CvDEPTH (cv) = PTR2IV (POPs);
707731 slf_frame = c->slf_frame;
708732 CORO_THROW = c->except;
709733
710 if (expect_false (enable_times))
711 {
712 if (expect_false (!times_valid))
734 if (ecb_expect_false (enable_times))
735 {
736 if (ecb_expect_false (!times_valid))
713737 coro_times_update ();
714738
715739 coro_times_sub (c);
716740 }
717741
718 if (expect_false (c->on_enter))
742 if (ecb_expect_false (c->on_enter))
719743 {
720744 int i;
721745
731755 {
732756 SWAP_SVS (c);
733757
734 if (expect_false (c->on_leave))
758 if (ecb_expect_false (c->on_leave))
735759 {
736760 int i;
737761
741765
742766 times_valid = 0;
743767
744 if (expect_false (enable_times))
768 if (ecb_expect_false (enable_times))
745769 {
746770 coro_times_update (); times_valid = 1;
747771 coro_times_add (c);
765789 /* this loop was inspired by pp_caller */
766790 for (;;)
767791 {
768 while (expect_true (cxix >= 0))
792 while (ecb_expect_true (cxix >= 0))
769793 {
770794 PERL_CONTEXT *cx = &ccstk[cxix--];
771795
772 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
796 if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT))
773797 {
774798 CV *cv = cx->blk_sub.cv;
775799
776 if (expect_true (CvDEPTH (cv)))
800 if (ecb_expect_true (CvDEPTH (cv)))
777801 {
778802 EXTEND (SP, 3);
779803 PUSHs ((SV *)CvPADLIST (cv));
786810 }
787811 }
788812
789 if (expect_true (top_si->si_type == PERLSI_MAIN))
813 if (ecb_expect_true (top_si->si_type == PERLSI_MAIN))
790814 break;
791815
792816 top_si = top_si->si_prev;
798822 }
799823
800824 /* allocate some space on the context stack for our purposes */
801 /* we manually unroll here, as usually 2 slots is enough */
802 if (SLOT_COUNT >= 1) CXINC;
803 if (SLOT_COUNT >= 2) CXINC;
804 if (SLOT_COUNT >= 3) CXINC;
805 {
806 unsigned int i;
807 for (i = 3; i < SLOT_COUNT; ++i)
808 CXINC;
809 }
810 cxstack_ix -= SLOT_COUNT; /* undo allocation */
825 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
826 {
827 unsigned int i;
828
829 for (i = 0; i < SLOT_COUNT; ++i)
830 CXINC;
831
832 cxstack_ix -= SLOT_COUNT; /* undo allocation */
833 }
811834
812835 c->mainstack = PL_mainstack;
813836
814837 {
815838 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
816839
817 slot->defav = GvAV (PL_defgv);
818 slot->defsv = DEFSV;
819 slot->errsv = ERRSV;
820 slot->irsgv = GvSV (irsgv);
821 slot->hinthv = GvHV (PL_hintgv);
822
823 #define VAR(name,type) slot->name = PL_ ## name;
840 #if CORO_JIT
841 save_perl_slots (slot);
842 #else
843 #define VARx(name,expr,type) slot->name = expr;
824844 # include "state.h"
825 #undef VAR
845 #undef VARx
846 #endif
826847 }
827848 }
828849
838859 static void
839860 coro_init_stacks (pTHX)
840861 {
841 PL_curstackinfo = new_stackinfo(32, 8);
862 PL_curstackinfo = new_stackinfo(32, 4 + SLOT_COUNT); /* 3 is minimum due to perl rounding down in scope.c:GROW() */
842863 PL_curstackinfo->si_type = PERLSI_MAIN;
843864 PL_curstack = PL_curstackinfo->si_stack;
844865 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
863884 New(54,PL_scopestack,8,I32);
864885 PL_scopestack_ix = 0;
865886 PL_scopestack_max = 8;
887 #if HAS_SCOPESTACK_NAME
888 New(54,PL_scopestack_name,8,const char*);
889 #endif
866890
867891 New(54,PL_savestack,24,ANY);
868892 PL_savestack_ix = 0;
900924 Safefree (PL_tmps_stack);
901925 Safefree (PL_markstack);
902926 Safefree (PL_scopestack);
927 #if HAS_SCOPESTACK_NAME
928 Safefree (PL_scopestack_name);
929 #endif
903930 Safefree (PL_savestack);
904931 #if !PERL_VERSION_ATLEAST (5,10,0)
905932 Safefree (PL_retstack);
955982 /*
956983 * This overrides the default magic get method of %SIG elements.
957984 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
958 * and instead of trying to save and restore the hash elements, we just provide
959 * readback here.
985 * and instead of trying to save and restore the hash elements (extremely slow),
986 * we just provide our own readback here.
960987 */
961 static int
988 static int ecb_cold
962989 coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
963990 {
964991 const char *s = MgPV_nolen_const (mg);
9801007 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
9811008 }
9821009
983 static int
1010 static int ecb_cold
9841011 coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
9851012 {
9861013 const char *s = MgPV_nolen_const (mg);
10041031 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
10051032 }
10061033
1007 static int
1034 static int ecb_cold
10081035 coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
10091036 {
10101037 const char *s = MgPV_nolen_const (mg);
10491076
10501077 static UNOP init_perl_op;
10511078
1052 static void NOINLINE /* noinline to keep it out of the transfer fast path */
1079 ecb_noinline static void /* noinline to keep it out of the transfer fast path */
10531080 init_perl (pTHX_ struct coro *coro)
10541081 {
10551082 /*
10741101 PL_hints = 0;
10751102
10761103 /* recreate the die/warn hooks */
1077 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
1078 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
1104 PL_diehook = SvREFCNT_inc (rv_diehook);
1105 PL_warnhook = SvREFCNT_inc (rv_warnhook);
10791106
10801107 GvSV (PL_defgv) = newSV (0);
10811108 GvAV (PL_defgv) = coro->args; coro->args = 0;
11061133 */
11071134 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
11081135 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
1136 slf_frame.destroy = 0;
11091137
11101138 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
11111139 init_perl_op.op_next = PL_op;
11201148
11211149 SWAP_SVS (coro);
11221150
1123 if (expect_false (enable_times))
1151 if (ecb_expect_false (enable_times))
11241152 {
11251153 coro_times_update ();
11261154 coro_times_sub (coro);
11541182 SV *svf [9];
11551183
11561184 {
1157 struct coro *current = SvSTATE_current;
1185 SV *old_current = SvRV (coro_current);
1186 struct coro *current = SvSTATE (old_current);
11581187
11591188 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
11601189
11611190 save_perl (aTHX_ current);
1191
1192 /* this will cause transfer_check to croak on block*/
1193 SvRV_set (coro_current, (SV *)coro->hv);
1194
11621195 load_perl (aTHX_ coro);
11631196
11641197 coro_unwind_stacks (aTHX);
1165 coro_destruct_stacks (aTHX);
11661198
11671199 /* restore swapped sv's */
11681200 SWAP_SVS (coro);
11691201
1170 // now save some sv's to be free'd later
1202 coro_destruct_stacks (aTHX);
1203
1204 /* now save some sv's to be free'd later */
11711205 svf [0] = GvSV (PL_defgv);
11721206 svf [1] = (SV *)GvAV (PL_defgv);
11731207 svf [2] = GvSV (PL_errgv);
11791213 svf [8] = PL_warnhook;
11801214 assert (9 == sizeof (svf) / sizeof (*svf));
11811215
1216 SvRV_set (coro_current, old_current);
1217
11821218 load_perl (aTHX_ current);
11831219 }
11841220
11951231 }
11961232 }
11971233
1198 INLINE void
1234 ecb_inline void
11991235 free_coro_mortal (pTHX)
12001236 {
1201 if (expect_true (coro_mortal))
1202 {
1203 SvREFCNT_dec (coro_mortal);
1237 if (ecb_expect_true (coro_mortal))
1238 {
1239 SvREFCNT_dec ((SV *)coro_mortal);
12041240 coro_mortal = 0;
12051241 }
12061242 }
13431379 }
13441380
13451381 /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1346 static void NOINLINE
1382 static void ecb_noinline
13471383 cctx_prepare (pTHX)
13481384 {
13491385 PL_top_env = &PL_start_env;
13641400 }
13651401
13661402 /* the tail of transfer: execute stuff we can only do after a transfer */
1367 INLINE void
1403 ecb_inline void
13681404 transfer_tail (pTHX)
13691405 {
13701406 free_coro_mortal (aTHX);
14181454 }
14191455
14201456 static coro_cctx *
1421 cctx_new ()
1457 cctx_new (void)
14221458 {
14231459 coro_cctx *cctx;
14241460
14341470
14351471 /* create a new cctx only suitable as source */
14361472 static coro_cctx *
1437 cctx_new_empty ()
1473 cctx_new_empty (void)
14381474 {
14391475 coro_cctx *cctx = cctx_new ();
14401476
14461482
14471483 /* create a new cctx suitable as destination/running a perl interpreter */
14481484 static coro_cctx *
1449 cctx_new_run ()
1485 cctx_new_run (void)
14501486 {
14511487 coro_cctx *cctx = cctx_new ();
14521488 void *stack_start;
14551491 #if HAVE_MMAP
14561492 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
14571493 /* mmap supposedly does allocate-on-write for us */
1458 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1494 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0);
14591495
14601496 if (cctx->sptr != (void *)-1)
14611497 {
14971533 if (!cctx)
14981534 return;
14991535
1500 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1536 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
15011537
15021538 --cctx_count;
15031539 coro_destroy (&cctx->cctx);
15261562 static coro_cctx *
15271563 cctx_get (pTHX)
15281564 {
1529 while (expect_true (cctx_first))
1565 while (ecb_expect_true (cctx_first))
15301566 {
15311567 coro_cctx *cctx = cctx_first;
15321568 cctx_first = cctx->next;
15331569 --cctx_idle;
15341570
1535 if (expect_true (!CCTX_EXPIRED (cctx)))
1571 if (ecb_expect_true (!CCTX_EXPIRED (cctx)))
15361572 return cctx;
15371573
15381574 cctx_destroy (cctx);
15471583 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
15481584
15491585 /* free another cctx if overlimit */
1550 if (expect_false (cctx_idle >= cctx_max_idle))
1586 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
15511587 {
15521588 coro_cctx *first = cctx_first;
15531589 cctx_first = first->next;
15681604 {
15691605 /* TODO: throwing up here is considered harmful */
15701606
1571 if (expect_true (prev != next))
1572 {
1573 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1607 if (ecb_expect_true (prev != next))
1608 {
1609 if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
15741610 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
15751611
1576 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1612 if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED)))
15771613 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
15781614
15791615 #if !PERL_VERSION_ATLEAST (5,10,0)
1580 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1616 if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING))
15811617 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
15821618 #endif
15831619 }
15841620 }
15851621
15861622 /* always use the TRANSFER macro */
1587 static void NOINLINE /* noinline so we have a fixed stackframe */
1623 static void ecb_noinline /* noinline so we have a fixed stackframe */
15881624 transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
15891625 {
15901626 dSTACKLEVEL;
15911627
15921628 /* sometimes transfer is only called to set idle_sp */
1593 if (expect_false (!prev))
1629 if (ecb_expect_false (!prev))
15941630 {
15951631 cctx_current->idle_sp = STACKLEVEL;
15961632 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
15971633 }
1598 else if (expect_true (prev != next))
1634 else if (ecb_expect_true (prev != next))
15991635 {
16001636 coro_cctx *cctx_prev;
16011637
1602 if (expect_false (prev->flags & CF_NEW))
1638 if (ecb_expect_false (prev->flags & CF_NEW))
16031639 {
16041640 /* create a new empty/source context */
16051641 prev->flags &= ~CF_NEW;
16121648 /* first get rid of the old state */
16131649 save_perl (aTHX_ prev);
16141650
1615 if (expect_false (next->flags & CF_NEW))
1651 if (ecb_expect_false (next->flags & CF_NEW))
16161652 {
16171653 /* need to start coroutine */
16181654 next->flags &= ~CF_NEW;
16231659 load_perl (aTHX_ next);
16241660
16251661 /* possibly untie and reuse the cctx */
1626 if (expect_true (
1662 if (ecb_expect_true (
16271663 cctx_current->idle_sp == STACKLEVEL
16281664 && !(cctx_current->flags & CC_TRACE)
16291665 && !force_cctx
16341670
16351671 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
16361672 /* without this the next cctx_get might destroy the running cctx while still in use */
1637 if (expect_false (CCTX_EXPIRED (cctx_current)))
1638 if (expect_true (!next->cctx))
1673 if (ecb_expect_false (CCTX_EXPIRED (cctx_current)))
1674 if (ecb_expect_true (!next->cctx))
16391675 next->cctx = cctx_get (aTHX);
16401676
16411677 cctx_put (cctx_current);
16461682 ++next->usecount;
16471683
16481684 cctx_prev = cctx_current;
1649 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1685 cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
16501686
16511687 next->cctx = 0;
16521688
1653 if (expect_false (cctx_prev != cctx_current))
1689 if (ecb_expect_false (cctx_prev != cctx_current))
16541690 {
16551691 cctx_prev->top_env = PL_top_env;
16561692 PL_top_env = cctx_current->top_env;
16661702
16671703 /** high level stuff ********************************************************/
16681704
1669 static int
1705 /* this function is actually Coro, not Coro::State, but we call it from here */
1706 /* because it is convenient - but it hasn't been declared yet for that reason */
1707 static void
1708 coro_call_on_destroy (pTHX_ struct coro *coro);
1709
1710 static void
16701711 coro_state_destroy (pTHX_ struct coro *coro)
16711712 {
1672 if (coro->flags & CF_DESTROYED)
1673 return 0;
1674
1675 if (coro->on_destroy && !PL_dirty)
1676 coro->on_destroy (aTHX_ coro);
1677
1678 coro->flags |= CF_DESTROYED;
1713 if (coro->flags & CF_ZOMBIE)
1714 return;
1715
1716 slf_destroy (aTHX_ coro);
1717
1718 coro->flags |= CF_ZOMBIE;
16791719
16801720 if (coro->flags & CF_READY)
16811721 {
16851725 }
16861726 else
16871727 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1728
1729 if (coro->next) coro->next->prev = coro->prev;
1730 if (coro->prev) coro->prev->next = coro->next;
1731 if (coro == coro_first) coro_first = coro->next;
16881732
16891733 if (coro->mainstack
16901734 && coro->mainstack != main_mainstack
16921736 && !PL_dirty)
16931737 destroy_perl (aTHX_ coro);
16941738
1695 if (coro->next) coro->next->prev = coro->prev;
1696 if (coro->prev) coro->prev->next = coro->next;
1697 if (coro == coro_first) coro_first = coro->next;
1698
16991739 cctx_destroy (coro->cctx);
17001740 SvREFCNT_dec (coro->startcv);
17011741 SvREFCNT_dec (coro->args);
17021742 SvREFCNT_dec (coro->swap_sv);
17031743 SvREFCNT_dec (CORO_THROW);
17041744
1705 return 1;
1745 coro_call_on_destroy (aTHX_ coro);
1746
1747 /* more destruction mayhem in coro_state_free */
17061748 }
17071749
17081750 static int
17111753 struct coro *coro = (struct coro *)mg->mg_ptr;
17121754 mg->mg_ptr = 0;
17131755
1714 coro->hv = 0;
1715
1716 if (--coro->refcnt < 0)
1717 {
1718 coro_state_destroy (aTHX_ coro);
1719 Safefree (coro);
1720 }
1756 coro_state_destroy (aTHX_ coro);
1757 SvREFCNT_dec (coro->on_destroy);
1758 SvREFCNT_dec (coro->status);
1759
1760 Safefree (coro);
17211761
17221762 return 0;
17231763 }
17241764
1725 static int
1765 static int ecb_cold
17261766 coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
17271767 {
1728 struct coro *coro = (struct coro *)mg->mg_ptr;
1729
1730 ++coro->refcnt;
1768 /* called when perl clones the current process the slow way (windows process emulation) */
1769 /* WE SIMply nuke the pointers in the copy, causing perl to croak */
1770 mg->mg_ptr = 0;
1771 mg->mg_virtual = 0;
17311772
17321773 return 0;
17331774 }
17621803
17631804 /** Coro ********************************************************************/
17641805
1765 INLINE void
1806 ecb_inline void
17661807 coro_enq (pTHX_ struct coro *coro)
17671808 {
17681809 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
17741815 ready [1] = coro;
17751816 }
17761817
1777 INLINE struct coro *
1818 ecb_inline struct coro *
17781819 coro_deq (pTHX)
17791820 {
17801821 int prio;
18371878 }
18381879
18391880 /* expects to own a reference to next->hv */
1840 INLINE void
1881 ecb_inline void
18411882 prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
18421883 {
18431884 SV *prev_sv = SvRV (coro_current);
18601901 {
18611902 struct coro *next = coro_deq (aTHX);
18621903
1863 if (expect_true (next))
1904 if (ecb_expect_true (next))
18641905 {
18651906 /* cannot transfer to destroyed coros, skip and look for next */
1866 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1907 if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED)))
18671908 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
18681909 else
18691910 {
19061947 }
19071948 }
19081949
1909 INLINE void
1950 ecb_inline void
19101951 prepare_cede (pTHX_ struct coro_transfer_args *ta)
19111952 {
19121953 api_ready (aTHX_ coro_current);
19131954 prepare_schedule (aTHX_ ta);
19141955 }
19151956
1916 INLINE void
1957 ecb_inline void
19171958 prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
19181959 {
19191960 SV *prev = SvRV (coro_current);
19531994
19541995 prepare_cede (aTHX_ &ta);
19551996
1956 if (expect_true (ta.prev != ta.next))
1997 if (ecb_expect_true (ta.prev != ta.next))
19571998 {
19581999 TRANSFER (ta, 1);
19592000 return 1;
20062047 }
20072048
20082049 static void
2050 coro_push_av (pTHX_ AV *av, I32 gimme_v)
2051 {
2052 if (AvFILLp (av) >= 0 && gimme_v != G_VOID)
2053 {
2054 dSP;
2055
2056 if (gimme_v == G_SCALAR)
2057 XPUSHs (AvARRAY (av)[AvFILLp (av)]);
2058 else
2059 {
2060 int i;
2061 EXTEND (SP, AvFILLp (av) + 1);
2062
2063 for (i = 0; i <= AvFILLp (av); ++i)
2064 PUSHs (AvARRAY (av)[i]);
2065 }
2066
2067 PUTBACK;
2068 }
2069 }
2070
2071 static void
2072 coro_push_on_destroy (pTHX_ struct coro *coro, SV *cb)
2073 {
2074 if (!coro->on_destroy)
2075 coro->on_destroy = newAV ();
2076
2077 av_push (coro->on_destroy, cb);
2078 }
2079
2080 static void
2081 slf_destroy_join (pTHX_ struct CoroSLF *frame)
2082 {
2083 SvREFCNT_dec ((SV *)((struct coro *)frame->data)->hv);
2084 }
2085
2086 static int
2087 slf_check_join (pTHX_ struct CoroSLF *frame)
2088 {
2089 struct coro *coro = (struct coro *)frame->data;
2090
2091 if (!coro->status)
2092 return 1;
2093
2094 frame->destroy = 0;
2095
2096 coro_push_av (aTHX_ coro->status, GIMME_V);
2097
2098 SvREFCNT_dec ((SV *)coro->hv);
2099
2100 return 0;
2101 }
2102
2103 static void
2104 slf_init_join (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2105 {
2106 struct coro *coro = SvSTATE (items > 0 ? arg [0] : &PL_sv_undef);
2107
2108 if (items > 1)
2109 croak ("join called with too many arguments");
2110
2111 if (coro->status)
2112 frame->prepare = prepare_nop;
2113 else
2114 {
2115 coro_push_on_destroy (aTHX_ coro, SvREFCNT_inc_NN (SvRV (coro_current)));
2116 frame->prepare = prepare_schedule;
2117 }
2118
2119 frame->check = slf_check_join;
2120 frame->destroy = slf_destroy_join;
2121 frame->data = (void *)coro;
2122 SvREFCNT_inc (coro->hv);
2123 }
2124
2125 static void
20092126 coro_call_on_destroy (pTHX_ struct coro *coro)
20102127 {
2011 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
2012 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
2013
2014 if (on_destroyp)
2015 {
2016 AV *on_destroy = (AV *)SvRV (*on_destroyp);
2017
2018 while (AvFILLp (on_destroy) >= 0)
2128 AV *od = coro->on_destroy;
2129
2130 if (!od)
2131 return;
2132
2133 while (AvFILLp (od) >= 0)
2134 {
2135 SV *cb = sv_2mortal (av_pop (od));
2136
2137 /* coro hv's (and only hv's at the moment) are supported as well */
2138 if (SvSTATEhv_p (aTHX_ cb))
2139 api_ready (aTHX_ cb);
2140 else
20192141 {
20202142 dSP; /* don't disturb outer sp */
2021 SV *cb = av_pop (on_destroy);
2022
20232143 PUSHMARK (SP);
20242144
2025 if (statusp)
2145 if (coro->status)
20262146 {
2027 int i;
2028 AV *status = (AV *)SvRV (*statusp);
2029 EXTEND (SP, AvFILLp (status) + 1);
2030
2031 for (i = 0; i <= AvFILLp (status); ++i)
2032 PUSHs (AvARRAY (status)[i]);
2147 PUTBACK;
2148 coro_push_av (aTHX_ coro->status, G_ARRAY);
2149 SPAGAIN;
20332150 }
20342151
20352152 PUTBACK;
2036 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
2153 call_sv (cb, G_VOID | G_DISCARD);
20372154 }
20382155 }
20392156 }
20402157
20412158 static void
2042 slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2043 {
2044 int i;
2045 HV *hv = (HV *)SvRV (coro_current);
2046 AV *av = newAV ();
2159 coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2160 {
2161 AV *av;
2162
2163 if (coro->status)
2164 {
2165 av = coro->status;
2166 av_clear (av);
2167 }
2168 else
2169 av = coro->status = newAV ();
20472170
20482171 /* items are actually not so common, so optimise for this case */
20492172 if (items)
20502173 {
2174 int i;
2175
20512176 av_extend (av, items - 1);
20522177
20532178 for (i = 0; i < items; ++i)
20542179 av_push (av, SvREFCNT_inc_NN (arg [i]));
20552180 }
2056
2057 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
2058
2059 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
2181 }
2182
2183 static void
2184 slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2185 {
2186 av_push (av_destroy, (SV *)newRV_inc ((SV *)coro_hv)); /* RVinc for perl */
20602187 api_ready (aTHX_ sv_manager);
20612188
20622189 frame->prepare = prepare_schedule;
20652192 /* as a minor optimisation, we could unwind all stacks here */
20662193 /* but that puts extra pressure on pp_slf, and is not worth much */
20672194 /*coro_unwind_stacks (aTHX);*/
2195 }
2196
2197 static void
2198 slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2199 {
2200 HV *coro_hv = (HV *)SvRV (coro_current);
2201
2202 coro_set_status (aTHX_ SvSTATE ((SV *)coro_hv), arg, items);
2203 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2204 }
2205
2206 static void
2207 slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2208 {
2209 HV *coro_hv;
2210 struct coro *coro;
2211
2212 if (items <= 0)
2213 croak ("Coro::cancel called without coro object,");
2214
2215 coro = SvSTATE (arg [0]);
2216 coro_hv = coro->hv;
2217
2218 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2219
2220 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2221 {
2222 /* coro currently busy cancelling something, so just notify it */
2223 coro->slf_frame.data = (void *)coro;
2224
2225 frame->prepare = prepare_nop;
2226 frame->check = slf_check_nop;
2227 }
2228 else if (coro_hv == (HV *)SvRV (coro_current))
2229 {
2230 /* cancelling the current coro is allowed, and equals terminate */
2231 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2232 }
2233 else
2234 {
2235 struct coro *self = SvSTATE_current;
2236
2237 /* otherwise we cancel directly, purely for speed reasons
2238 * unfortunately, this requires some magic trickery, as
2239 * somebody else could cancel us, so we have to fight the cancellation.
2240 * this is ugly, and hopefully fully worth the extra speed.
2241 * besides, I can't get the slow-but-safe version working...
2242 */
2243 slf_frame.data = 0;
2244 self->flags |= CF_NOCANCEL;
2245 coro_state_destroy (aTHX_ coro);
2246 self->flags &= ~CF_NOCANCEL;
2247
2248 if (slf_frame.data)
2249 {
2250 /* while we were busy we have been cancelled, so terminate */
2251 slf_init_terminate_cancel_common (aTHX_ frame, self->hv);
2252 }
2253 else
2254 {
2255 frame->prepare = prepare_nop;
2256 frame->check = slf_check_nop;
2257 }
2258 }
2259 }
2260
2261 static int
2262 slf_check_safe_cancel (pTHX_ struct CoroSLF *frame)
2263 {
2264 frame->prepare = 0;
2265 coro_unwind_stacks (aTHX);
2266
2267 slf_init_terminate_cancel_common (aTHX_ frame, (HV *)SvRV (coro_current));
2268
2269 return 1;
2270 }
2271
2272 static int
2273 safe_cancel (pTHX_ struct coro *coro, SV **arg, int items)
2274 {
2275 if (coro->cctx)
2276 croak ("coro inside C callback, unable to cancel at this time, caught");
2277
2278 if (coro->flags & CF_NEW)
2279 {
2280 coro_set_status (aTHX_ coro, arg, items);
2281 coro_state_destroy (aTHX_ coro);
2282 }
2283 else
2284 {
2285 if (!coro->slf_frame.prepare)
2286 croak ("coro outside an SLF function, unable to cancel at this time, caught");
2287
2288 slf_destroy (aTHX_ coro);
2289
2290 coro_set_status (aTHX_ coro, arg, items);
2291 coro->slf_frame.prepare = prepare_nop;
2292 coro->slf_frame.check = slf_check_safe_cancel;
2293
2294 api_ready (aTHX_ (SV *)coro->hv);
2295 }
2296
2297 return 1;
20682298 }
20692299
20702300 /*****************************************************************************/
21052335 HV *hv = (HV *)SvRV (coro_current);
21062336 struct coro *coro = SvSTATE_hv ((SV *)hv);
21072337
2108 if (expect_true (coro->saved_deffh))
2338 if (ecb_expect_true (coro->saved_deffh))
21092339 {
21102340 /* subsequent iteration */
21112341 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
21142344 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
21152345 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
21162346 {
2117 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
2118 coro->invoke_av = newAV ();
2119
2120 frame->prepare = prepare_nop;
2347 slf_init_terminate_cancel_common (aTHX_ frame, hv);
2348 return;
21212349 }
21222350 else
21232351 {
23502578 {
23512579 frame->prepare = prepare_cede_notself;
23522580 frame->check = slf_check_nop;
2581 }
2582
2583 /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */
2584 static void
2585 slf_destroy (pTHX_ struct coro *coro)
2586 {
2587 /* this callback is reserved for slf functions needing to do cleanup */
2588 if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty)
2589 coro->slf_frame.destroy (aTHX_ &coro->slf_frame);
2590
2591 /*
2592 * The on_destroy above most likely is from an SLF call.
2593 * Since by definition the SLF call will not finish when we destroy
2594 * the coro, we will have to force-finish it here, otherwise
2595 * cleanup functions cannot call SLF functions.
2596 */
2597 coro->slf_frame.prepare = 0;
23532598 }
23542599
23552600 /*
23662611 /* set up the slf frame, unless it has already been set-up */
23672612 /* the latter happens when a new coro has been started */
23682613 /* or when a new cctx was attached to an existing coroutine */
2369 if (expect_true (!slf_frame.prepare))
2614 if (ecb_expect_true (!slf_frame.prepare))
23702615 {
23712616 /* first iteration */
23722617 dSP;
24172662 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
24182663
24192664 /* exception handling */
2420 if (expect_false (CORO_THROW))
2665 if (ecb_expect_false (CORO_THROW))
24212666 {
24222667 SV *exception = sv_2mortal (CORO_THROW);
24232668
24282673
24292674 /* return value handling - mostly like entersub */
24302675 /* make sure we put something on the stack in scalar context */
2431 if (GIMME_V == G_SCALAR)
2676 if (GIMME_V == G_SCALAR
2677 && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1))
24322678 {
24332679 dSP;
24342680 SV **bot = PL_stack_base + checkmark;
24352681
24362682 if (sp == bot) /* too few, push undef */
24372683 bot [1] = &PL_sv_undef;
2438 else if (sp != bot + 1) /* too many, take last one */
2684 else /* too many, take last one */
24392685 bot [1] = *sp;
24402686
24412687 SP = bot + 1;
25522798 NV next, every;
25532799 } PerlIOCede;
25542800
2555 static IV
2801 static IV ecb_cold
25562802 PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
25572803 {
25582804 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
25632809 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
25642810 }
25652811
2566 static SV *
2812 static SV * ecb_cold
25672813 PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
25682814 {
25692815 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
26852931 }
26862932
26872933 static void
2688 coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2934 coro_semaphore_destroy (pTHX_ struct CoroSLF *frame)
26892935 {
26902936 /* call $sem->adjust (0) to possibly wake up some other waiters */
2691 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2937 coro_semaphore_adjust (aTHX_ (AV *)frame->data, 0);
26922938 }
26932939
26942940 static int
26962942 {
26972943 AV *av = (AV *)frame->data;
26982944 SV *count_sv = AvARRAY (av)[0];
2945 SV *coro_hv = SvRV (coro_current);
26992946
27002947 /* if we are about to throw, don't actually acquire the lock, just throw */
27012948 if (CORO_THROW)
27022949 return 0;
27032950 else if (SvIVX (count_sv) > 0)
27042951 {
2705 SvSTATE_current->on_destroy = 0;
2952 frame->destroy = 0;
27062953
27072954 if (acquire)
27082955 SvIVX (count_sv) = SvIVX (count_sv) - 1;
27172964 /* if we were woken up but can't down, we look through the whole */
27182965 /* waiters list and only add us if we aren't in there already */
27192966 /* this avoids some degenerate memory usage cases */
2720
2721 for (i = 1; i <= AvFILLp (av); ++i)
2722 if (AvARRAY (av)[i] == SvRV (coro_current))
2967 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2968 if (AvARRAY (av)[i] == coro_hv)
27232969 return 1;
27242970
2725 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2971 av_push (av, SvREFCNT_inc (coro_hv));
27262972 return 1;
27272973 }
27282974 }
27553001
27563002 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
27573003 frame->prepare = prepare_schedule;
2758
27593004 /* to avoid race conditions when a woken-up coro gets terminated */
27603005 /* we arrange for a temporary on_destroy that calls adjust (0) */
2761 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
3006 frame->destroy = coro_semaphore_destroy;
27623007 }
27633008 }
27643009
29863231 static SV *prio_cv;
29873232 static SV *prio_sv;
29883233
2989 if (expect_false (!prio_cv))
3234 if (ecb_expect_false (!prio_cv))
29903235 {
29913236 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0);
29923237 prio_sv = newSViv (0);
31023347 return coro_sv;
31033348 }
31043349
3350 #ifndef __cplusplus
3351 ecb_cold XS(boot_Coro__State);
3352 #endif
3353
3354 #if CORO_JIT
3355
3356 static void ecb_noinline ecb_cold
3357 pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3358 {
3359 dSP;
3360 AV *av = newAV ();
3361
3362 av_store (av, 3, newSVuv (d));
3363 av_store (av, 2, newSVuv (c));
3364 av_store (av, 1, newSVuv (b));
3365 av_store (av, 0, newSVuv (a));
3366
3367 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3368
3369 PUTBACK;
3370 }
3371
3372 static void ecb_noinline ecb_cold
3373 jit_init (pTHX)
3374 {
3375 dSP;
3376 SV *load, *save;
3377 char *map_base;
3378 char *load_ptr, *save_ptr;
3379 STRLEN load_len, save_len, map_len;
3380 int count;
3381
3382 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3383
3384 PUSHMARK (SP);
3385 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3386 # include "state.h"
3387 #undef VARx
3388 count = call_pv ("Coro::State::_jit", G_ARRAY);
3389 SPAGAIN;
3390
3391 save = POPs; save_ptr = SvPVbyte (save, save_len);
3392 load = POPs; load_ptr = SvPVbyte (load, load_len);
3393
3394 map_len = load_len + save_len + 16;
3395
3396 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3397
3398 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3399
3400 load_perl_slots = (load_save_perl_slots_type)map_base;
3401 memcpy (map_base, load_ptr, load_len);
3402
3403 map_base += (load_len + 15) & ~15;
3404
3405 save_perl_slots = (load_save_perl_slots_type)map_base;
3406 memcpy (map_base, save_ptr, save_len);
3407
3408 /* we are good citizens and try to make the page read-only, so the evil evil */
3409 /* hackers might have it a bit more difficult */
3410 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3411
3412 PUTBACK;
3413 eval_pv ("undef &Coro::State::_jit", 1);
3414 }
3415
3416 #endif
3417
31053418 MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
31063419
31073420 PROTOTYPES: DISABLE
31143427 # endif
31153428 #endif
31163429 BOOT_PAGESIZE;
3430
3431 /* perl defines these to check for existance first, but why it doesn't */
3432 /* just create them one at init time is not clear to me, except for */
3433 /* programs trying to delete them, but... */
3434 /* anyway, we declare this as invalid and make sure they are initialised here */
3435 DEFSV;
3436 ERRSV;
31173437
31183438 cctx_current = cctx_new_empty ();
31193439
31663486 time_init (aTHX);
31673487
31683488 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3489 #if CORO_JIT
3490 PUTBACK;
3491 jit_init (aTHX);
3492 SPAGAIN;
3493 #endif
31693494 }
31703495
31713496 SV *
31823507 PROTOTYPE: $$
31833508 CODE:
31843509 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3185
3186 bool
3187 _destroy (SV *coro_sv)
3188 CODE:
3189 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3190 OUTPUT:
3191 RETVAL
31923510
31933511 void
31943512 _exit (int code)
32743592 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
32753593 {
32763594 struct coro *current = SvSTATE_current;
3595 struct CoroSLF slf_save;
32773596
32783597 if (current != coro)
32793598 {
32803599 PUTBACK;
32813600 save_perl (aTHX_ current);
32823601 load_perl (aTHX_ coro);
3602 /* the coro is most likely in an active SLF call.
3603 * while not strictly required (the code we execute is
3604 * not allowed to call any SLF functions), it's cleaner
3605 * to reinitialise the slf_frame and restore it later.
3606 * This might one day allow us to actually do SLF calls
3607 * from code executed here.
3608 */
3609 slf_save = slf_frame;
3610 slf_frame.prepare = 0;
32833611 SPAGAIN;
32843612 }
32853613
32993627 if (current != coro)
33003628 {
33013629 PUTBACK;
3630 slf_frame = slf_save;
33023631 save_perl (aTHX_ coro);
33033632 load_perl (aTHX_ current);
33043633 SPAGAIN;
33133642 is_ready = CF_READY
33143643 is_running = CF_RUNNING
33153644 is_new = CF_NEW
3316 is_destroyed = CF_DESTROYED
3645 is_destroyed = CF_ZOMBIE
3646 is_zombie = CF_ZOMBIE
33173647 is_suspended = CF_SUSPENDED
33183648 CODE:
33193649 RETVAL = boolSV (coro->flags & ix);
33943724 cancel (Coro::State self)
33953725 CODE:
33963726 coro_state_destroy (aTHX_ self);
3397 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3398
33993727
34003728 SV *
34013729 enable_times (int enabled = enable_times)
34203748 {
34213749 struct coro *current = SvSTATE (coro_current);
34223750
3423 if (expect_false (current == self))
3751 if (ecb_expect_false (current == self))
34243752 {
34253753 coro_times_update ();
34263754 coro_times_add (SvSTATE (coro_current));
34303758 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
34313759 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
34323760
3433 if (expect_false (current == self))
3761 if (ecb_expect_false (current == self))
34343762 coro_times_sub (SvSTATE (coro_current));
34353763 }
34363764
34613789 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
34623790 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
34633791 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3464 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
34653792 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
34663793 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
34673794 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
35103837 RETVAL
35113838
35123839 void
3840 _destroy (Coro::State coro)
3841 CODE:
3842 /* used by the manager thread */
3843 coro_state_destroy (aTHX_ coro);
3844
3845 void
3846 on_destroy (Coro::State coro, SV *cb)
3847 CODE:
3848 coro_push_on_destroy (aTHX_ coro, newSVsv (cb));
3849
3850 void
3851 join (...)
3852 CODE:
3853 CORO_EXECUTE_SLF_XS (slf_init_join);
3854
3855 void
35133856 terminate (...)
35143857 CODE:
35153858 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3859
3860 void
3861 cancel (...)
3862 CODE:
3863 CORO_EXECUTE_SLF_XS (slf_init_cancel);
3864
3865 int
3866 safe_cancel (Coro::State self, ...)
3867 C_ARGS: aTHX_ self, &ST (1), items - 1
35163868
35173869 void
35183870 schedule (...)
37984150 MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
37994151
38004152 void
3801 _may_delete (SV *sem, int count, int extra_refs)
4153 _may_delete (SV *sem, int count, unsigned int extra_refs)
38024154 PPCODE:
38034155 {
38044156 AV *av = (AV *)SvRV (sem);
8383 use Storable;
8484 use base "Exporter";
8585
86 our $VERSION = 5.372;
86 our $VERSION = 6.0;
8787 our @EXPORT = qw(thaw freeze nfreeze blocking_thaw blocking_freeze blocking_nfreeze);
8888
8989 our $GRANULARITY = 0.01;
2929 use Coro ();
3030 use Coro::AnyEvent ();
3131
32 our $VERSION = 5.372;
32 our $VERSION = 6.0;
3333 our @EXPORT_OK = qw(timeout sleep);
3434
3535 # compatibility with older programs
4040 our @EXPORT = qw(gethostbyname gethostbyaddr);
4141 our @EXPORT_OK = qw(inet_aton fork_eval);
4242
43 our $VERSION = 5.372;
43 our $VERSION = 6.0;
4444
4545 our $MAXPARALLEL = 16; # max. number of parallel jobs
4646
0 /*
1 * libecb - http://software.schmorp.de/pkg/libecb
2 *
3 * Copyright (©) 2009-2011 Marc Alexander Lehmann <libecb@schmorp.de>
4 * Copyright (©) 2011 Emanuele Giaquinta
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef ECB_H
30 #define ECB_H
31
32 #ifdef _WIN32
33 typedef signed char int8_t;
34 typedef unsigned char uint8_t;
35 typedef signed short int16_t;
36 typedef unsigned short uint16_t;
37 typedef signed int int32_t;
38 typedef unsigned int uint32_t;
39 #if __GNUC__
40 typedef signed long long int64_t;
41 typedef unsigned long long uint64_t;
42 #else /* _MSC_VER || __BORLANDC__ */
43 typedef signed __int64 int64_t;
44 typedef unsigned __int64 uint64_t;
45 #endif
46 #else
47 #include <inttypes.h>
48 #endif
49
50 /* many compilers define _GNUC_ to some versions but then only implement
51 * what their idiot authors think are the "more important" extensions,
52 * causing enourmous grief in return for some better fake benchmark numbers.
53 * or so.
54 * we try to detect these and simply assume they are not gcc - if they have
55 * an issue with that they should have done it right in the first place.
56 */
57 #ifndef ECB_GCC_VERSION
58 #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__)
59 #define ECB_GCC_VERSION(major,minor) 0
60 #else
61 #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
62 #endif
63 #endif
64
65 #define ECB_C99 (__STDC_VERSION__ >= 199901L)
66
67 #if __cplusplus
68 #define ecb_inline static inline
69 #elif ECB_GCC_VERSION(2,5)
70 #define ecb_inline static __inline__
71 #elif ECB_C99
72 #define ecb_inline static inline
73 #else
74 #define ecb_inline static
75 #endif
76
77 #if ECB_GCC_VERSION(3,3)
78 #define ecb_restrict __restrict__
79 #elif ECB_C99
80 #define ecb_restrict restrict
81 #else
82 #define ecb_restrict
83 #endif
84
85 typedef int ecb_bool;
86
87 #define ECB_CONCAT_(a, b) a ## b
88 #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
89 #define ECB_STRINGIFY_(a) # a
90 #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
91
92 #define ecb_function_ ecb_inline
93
94 #if ECB_GCC_VERSION(3,1)
95 #define ecb_attribute(attrlist) __attribute__(attrlist)
96 #define ecb_is_constant(expr) __builtin_constant_p (expr)
97 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
98 #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
99 #else
100 #define ecb_attribute(attrlist)
101 #define ecb_is_constant(expr) 0
102 #define ecb_expect(expr,value) (expr)
103 #define ecb_prefetch(addr,rw,locality)
104 #endif
105
106 /* no emulation for ecb_decltype */
107 #if ECB_GCC_VERSION(4,5)
108 #define ecb_decltype(x) __decltype(x)
109 #elif ECB_GCC_VERSION(3,0)
110 #define ecb_decltype(x) __typeof(x)
111 #endif
112
113 #define ecb_noinline ecb_attribute ((__noinline__))
114 #define ecb_noreturn ecb_attribute ((__noreturn__))
115 #define ecb_unused ecb_attribute ((__unused__))
116 #define ecb_const ecb_attribute ((__const__))
117 #define ecb_pure ecb_attribute ((__pure__))
118
119 #if ECB_GCC_VERSION(4,3)
120 #define ecb_artificial ecb_attribute ((__artificial__))
121 #define ecb_hot ecb_attribute ((__hot__))
122 #define ecb_cold ecb_attribute ((__cold__))
123 #else
124 #define ecb_artificial
125 #define ecb_hot
126 #define ecb_cold
127 #endif
128
129 /* put around conditional expressions if you are very sure that the */
130 /* expression is mostly true or mostly false. note that these return */
131 /* booleans, not the expression. */
132 #define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
133 #define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
134 /* for compatibility to the rest of the world */
135 #define ecb_likely(expr) ecb_expect_true (expr)
136 #define ecb_unlikely(expr) ecb_expect_false (expr)
137
138 /* count trailing zero bits and count # of one bits */
139 #if ECB_GCC_VERSION(3,4)
140 /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
141 #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
142 #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
143 #define ecb_ctz32(x) __builtin_ctz (x)
144 #define ecb_ctz64(x) __builtin_ctzll (x)
145 #define ecb_popcount32(x) __builtin_popcount (x)
146 /* no popcountll */
147 #else
148 ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const;
149 ecb_function_ int
150 ecb_ctz32 (uint32_t x)
151 {
152 int r = 0;
153
154 x &= ~x + 1; /* this isolates the lowest bit */
155
156 #if ECB_branchless_on_i386
157 r += !!(x & 0xaaaaaaaa) << 0;
158 r += !!(x & 0xcccccccc) << 1;
159 r += !!(x & 0xf0f0f0f0) << 2;
160 r += !!(x & 0xff00ff00) << 3;
161 r += !!(x & 0xffff0000) << 4;
162 #else
163 if (x & 0xaaaaaaaa) r += 1;
164 if (x & 0xcccccccc) r += 2;
165 if (x & 0xf0f0f0f0) r += 4;
166 if (x & 0xff00ff00) r += 8;
167 if (x & 0xffff0000) r += 16;
168 #endif
169
170 return r;
171 }
172
173 ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const;
174 ecb_function_ int
175 ecb_ctz64 (uint64_t x)
176 {
177 int shift = x & 0xffffffffU ? 0 : 32;
178 return ecb_ctz32 (x >> shift) + shift;
179 }
180
181 ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const;
182 ecb_function_ int
183 ecb_popcount32 (uint32_t x)
184 {
185 x -= (x >> 1) & 0x55555555;
186 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
187 x = ((x >> 4) + x) & 0x0f0f0f0f;
188 x *= 0x01010101;
189
190 return x >> 24;
191 }
192
193 /* you have the choice beetween something with a table lookup, */
194 /* something using lots of bit arithmetic and a simple loop */
195 /* we went for the loop */
196 ecb_function_ int ecb_ld32 (uint32_t x) ecb_const;
197 ecb_function_ int ecb_ld32 (uint32_t x)
198 {
199 int r = 0;
200
201 if (x >> 16) { x >>= 16; r += 16; }
202 if (x >> 8) { x >>= 8; r += 8; }
203 if (x >> 4) { x >>= 4; r += 4; }
204 if (x >> 2) { x >>= 2; r += 2; }
205 if (x >> 1) { r += 1; }
206
207 return r;
208 }
209
210 ecb_function_ int ecb_ld64 (uint64_t x) ecb_const;
211 ecb_function_ int ecb_ld64 (uint64_t x)
212 {
213 int r = 0;
214
215 if (x >> 32) { x >>= 32; r += 32; }
216
217 return r + ecb_ld32 (x);
218 }
219 #endif
220
221 /* popcount64 is only available on 64 bit cpus as gcc builtin */
222 /* so for this version we are lazy */
223 ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const;
224 ecb_function_ int
225 ecb_popcount64 (uint64_t x)
226 {
227 return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
228 }
229
230 ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const;
231 ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const;
232 ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const;
233 ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const;
234 ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const;
235 ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const;
236 ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const;
237 ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const;
238
239 ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
240 ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
241 ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
242 ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
243 ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
244 ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
245 ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
246 ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
247
248 #if ECB_GCC_VERSION(4,3)
249 #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
250 #define ecb_bswap32(x) __builtin_bswap32 (x)
251 #define ecb_bswap64(x) __builtin_bswap64 (x)
252 #else
253 ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const;
254 ecb_function_ uint16_t
255 ecb_bswap16 (uint16_t x)
256 {
257 return ecb_rotl16 (x, 8);
258 }
259
260 ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const;
261 ecb_function_ uint32_t
262 ecb_bswap32 (uint32_t x)
263 {
264 return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
265 }
266
267 ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const;
268 ecb_function_ uint64_t
269 ecb_bswap64 (uint64_t x)
270 {
271 return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
272 }
273 #endif
274
275 #if ECB_GCC_VERSION(4,5)
276 #define ecb_unreachable() __builtin_unreachable ()
277 #else
278 /* this seems to work fine, but gcc always emits a warning for it :/ */
279 ecb_function_ void ecb_unreachable (void) ecb_noreturn;
280 ecb_function_ void ecb_unreachable (void) { }
281 #endif
282
283 /* try to tell the compiler that some condition is definitely true */
284 #define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0)
285
286 ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const;
287 ecb_function_ unsigned char
288 ecb_byteorder_helper (void)
289 {
290 const uint32_t u = 0x11223344;
291 return *(unsigned char *)&u;
292 }
293
294 ecb_function_ ecb_bool ecb_big_endian (void) ecb_const;
295 ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; }
296 ecb_function_ ecb_bool ecb_little_endian (void) ecb_const;
297 ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; }
298
299 #if ECB_GCC_VERSION(3,0) || ECB_C99
300 #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
301 #else
302 #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
303 #endif
304
305 #if ecb_cplusplus_does_not_suck
306 /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
307 template<typename T, int N>
308 static inline int ecb_array_length (const T (&arr)[N])
309 {
310 return N;
311 }
312 #else
313 #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
314 #endif
315
316 #endif
317
0 #!/opt/bin/perl
1
2 {
3 package Coro::State;
4
5 use common::sense;
6
7 my @insn;
8
9 $insn[0][1] = "\x0f\xb6"; # movzbl mem -> rax
10 $insn[0][2] = "\x0f\xb7"; # movzwl mem -> rax
11 $insn[0][4] = "\x8b"; # movl mem -> rax
12 $insn[0][8] = "\x48\x8b"; # movq mem -> rax
13 $insn[1][1] = "\x88"; # movb al -> mem
14 $insn[1][2] = "\x66\x89"; # movw ax -> mem
15 $insn[1][4] = "\x89"; # movl eax -> mem
16 $insn[1][8] = "\x48\x89"; # movq rax -> mem
17
18 my $modrm_disp8 = 0x40;
19 my $modrm_disp32 = 0x80;
20 my $modrm_rsi = 0x06;
21 my $modrm_rdi = 0x07;
22
23 my @vars;
24
25 my $mov_ind = sub {
26 my ($size, $mod_rm, $store, $offset) = @_;
27
28 if ($offset < -128 || $offset > 127) {
29 $mod_rm |= $modrm_disp32;
30 $offset = pack "V", $offset;
31 } elsif ($offset) {
32 $mod_rm |= $modrm_disp8;
33 $offset = pack "c", $offset;
34 } else {
35 $offset = "";
36 }
37
38 $insn[$store][$size] . (chr $mod_rm) . $offset
39 };
40
41 my $gencopy = sub {
42 my ($save) = shift;
43
44 # all perl variables must be within 32-bits of this address
45 my $curbase = $vars[$#vars >> 1][0];
46
47 my $code = "\x48\xbe" . pack "Q", $curbase; # mov imm64, %rsi
48
49 my $curslot = 0;
50
51 for (@vars) {
52 my ($addr, $asize, $slot, $ssize) = @$_;
53
54 my $slotofs = $slot - $curslot;
55
56 # the sort ensures that this condition and adjustment suffices
57 if ($slotofs > 127) {
58 my $adj = 256;
59 $code .= "\x48\x81\xc7" . pack "i", $adj; # addq imm32, %rdi
60 $curslot += $adj;
61 $slotofs -= $adj;
62 }
63
64 if ($save) {
65 $code .= $mov_ind->($asize, $modrm_rsi, 0, $addr - $curbase);
66 $code .= $mov_ind->($ssize, $modrm_rdi, 1, $slotofs);
67 } else {
68 $code .= $mov_ind->($ssize, $modrm_rdi, 0, $slotofs);
69 $code .= $mov_ind->($asize, $modrm_rsi, 1, $addr - $curbase);
70 }
71 }
72
73 $code .= "\xc3"; # retq
74
75 $code
76 };
77
78 sub _jit {
79 @vars = @_;
80
81 # sort all variables into 256 byte blocks, biased by -128
82 # so gencopy can += 256 occasionally. within those blocks,
83 # sort by address so we can play further tricks.
84 @vars = sort {
85 (($a->[2] + 128) & ~255) <=> (($b->[2] + 128) & ~255)
86 or $a->[0] <=> $b->[0]
87 } @vars;
88
89 # we *could* combine adjacent vars, but this is not very common
90
91 $vars[-1][0] - $vars[0][0] <= 0x7fffffff
92 or die "JIT failed, perl var spread >31 bit\n";
93
94 my $load = $gencopy->(0);
95 my $save = $gencopy->(1);
96
97 #open my $fh, ">dat"; syswrite $fh, $save; system "objdump -b binary -m i386 -M x86-64 -D dat";
98 #warn length $load;
99 #warn length $save;
100
101 ($load, $save)
102 }
103 }
104
105 1
0 #!/opt/bin/perl
1
2 {
3 package Coro::State;
4
5 use common::sense;
6
7 my @insn;
8
9 $insn[0][1] = "\x0f\xb6"; # movzbl mem -> rax
10 $insn[0][2] = "\x0f\xb7"; # movzwl mem -> rax
11 $insn[0][4] = "\x8b"; # movl mem -> rax
12 $insn[1][1] = "\x88"; # movb al -> mem
13 $insn[1][2] = "\x66\x89"; # movw eax -> mem
14 $insn[1][4] = "\x89"; # movl rax -> mem
15
16 my $modrm_abs = 0x05;
17 my $modrm_disp8 = 0x40;
18 my $modrm_disp32 = 0x80;
19 my $modrm_edx = 0x02;
20
21 my @vars;
22
23 my $mov = sub {
24 my ($size, $mod_rm, $store, $offset) = @_;
25
26 if ($mod_rm == $modrm_abs) {
27 $offset = pack "V", $offset;
28 } else {
29 if ($offset < -128 || $offset > 127) {
30 $mod_rm |= $modrm_disp32;
31 $offset = pack "V", $offset;
32 } elsif ($offset) {
33 $mod_rm |= $modrm_disp8;
34 $offset = pack "c", $offset;
35 } else {
36 $offset = "";
37 }
38 }
39
40 my $insn = $insn[$store][$size] . (chr $mod_rm) . $offset;
41
42 # some instructions have shorter sequences
43
44 $insn =~ s/^\x8b\x05/\xa1/;
45 $insn =~ s/^\x88\x05/\xa2/;
46 $insn =~ s/^\x66\x89\x05/\x66\xa3/;
47 $insn =~ s/^\x89\x05/\xa3/;
48
49 $insn
50 };
51
52 my $gencopy = sub {
53 my ($save) = shift;
54
55 my $code = "\x8b\x54\x24\x04"; # mov 4(%esp),%edx
56
57 my $curslot = 0;
58
59 for (@vars) {
60 my ($addr, $asize, $slot, $ssize) = @$_;
61
62 my $slotofs = $slot - $curslot;
63
64 # the sort ensures that this condition and adjustment suffices
65 if ($slotofs > 127) {
66 my $adj = 256;
67 $code .= "\x81\xc2" . pack "V", $adj; # add imm32, %edi
68 $curslot += $adj;
69 $slotofs -= $adj;
70 }
71
72 if ($save) {
73 $code .= $mov->($asize, $modrm_abs, 0, $addr);
74 $code .= $mov->($ssize, $modrm_edx, 1, $slotofs);
75 } else {
76 $code .= $mov->($ssize, $modrm_edx, 0, $slotofs);
77 $code .= $mov->($asize, $modrm_abs, 1, $addr);
78 }
79 }
80
81 $code .= "\xc3"; # retl
82
83 $code
84 };
85
86 sub _jit {
87 @vars = @_;
88
89 # split 8-byte accesses into two 4-byte accesses
90 # not needed even for 64 bit perls, but you never know
91 for (@vars) {
92 if ($_->[1] == 8) {
93 die "Coro: FATAL - cannot handle size mismatch between 8 and $_->[3] byte slots.\n";
94
95 $_->[1] =
96 $_->[3] = 4;
97
98 push @vars,
99 [$_->[0] + 4, 4,
100 $_->[1] + 4, 4];
101 }
102 }
103
104 # sort by slot offset, required by gencopy to work
105 @vars = sort { $a->[2] <=> $b->[2] } @vars;
106
107 # we *could* combine adjacent vars, but this is not very common
108
109 my $load = $gencopy->(0);
110 my $save = $gencopy->(1);
111
112 #open my $fh, ">dat"; syswrite $fh, $save; system "objdump -b binary -m i386 -D dat";
113 #warn length $load;
114 #warn length $save;
115
116 ($load, $save)
117 }
118 }
119
120 1
00 /*
1 * Copyright (c) 2001-2009 Marc Alexander Lehmann <schmorp@schmorp.de>
1 * Copyright (c) 2001-2011 Marc Alexander Lehmann <schmorp@schmorp.de>
22 *
33 * Redistribution and use in source and binary forms, with or without modifica-
44 * tion, are permitted provided that the following conditions are met:
8787
8888 coro_transfer (new_coro, create_coro);
8989
90 #if __linux && __amd64
91 /* we blindly assume on any __linux with __amd64 we have a new enough gas with .cfi_undefined support */
92 asm (".cfi_undefined rip");
93 #endif
94
9095 func ((void *)arg);
9196
9297 /* the new coro returned. bad. just abort() for now */
111116
112117 # if CORO_ASM
113118
119 #if _WIN32
120 #define CORO_WIN_TIB 1
121 #endif
122
114123 asm (
115 ".text\n"
116 ".globl coro_transfer\n"
117 ".type coro_transfer, @function\n"
124 "\t.text\n"
125 "\t.globl coro_transfer\n"
118126 "coro_transfer:\n"
119127 /* windows, of course, gives a shit on the amd64 ABI and uses different registers */
120128 /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */
121129 #if __amd64
122 #define NUM_SAVED 6
123 "\tpush %rbp\n"
124 "\tpush %rbx\n"
125 "\tpush %r12\n"
126 "\tpush %r13\n"
127 "\tpush %r14\n"
128 "\tpush %r15\n"
129 #if CORO_WIN_TIB
130 "\tpush %gs:0x0\n"
131 "\tpush %gs:0x8\n"
132 "\tpush %gs:0xc\n"
130 #ifdef WIN32
131 /* TODO: xmm6..15 also would need to be saved. sigh. */
132 #define NUM_SAVED 8
133 "\tpushq %rsi\n"
134 "\tpushq %rdi\n"
135 "\tpushq %rbp\n"
136 "\tpushq %rbx\n"
137 "\tpushq %r12\n"
138 "\tpushq %r13\n"
139 "\tpushq %r14\n"
140 "\tpushq %r15\n"
141 #if CORO_WIN_TIB
142 "\tpushq %fs:0x0\n"
143 "\tpushq %fs:0x8\n"
144 "\tpushq %fs:0xc\n"
145 #endif
146 "\tmovq %rsp, (%rcx)\n"
147 "\tmovq (%rdx), %rsp\n"
148 #if CORO_WIN_TIB
149 "\tpopq %fs:0xc\n"
150 "\tpopq %fs:0x8\n"
151 "\tpopq %fs:0x0\n"
152 #endif
153 "\tpopq %r15\n"
154 "\tpopq %r14\n"
155 "\tpopq %r13\n"
156 "\tpopq %r12\n"
157 "\tpopq %rbx\n"
158 "\tpopq %rbp\n"
159 "\tpopq %rdi\n"
160 "\tpopq %rsi\n"
161 #else
162 #define NUM_SAVED 6
163 "\tpushq %rbp\n"
164 "\tpushq %rbx\n"
165 "\tpushq %r12\n"
166 "\tpushq %r13\n"
167 "\tpushq %r14\n"
168 "\tpushq %r15\n"
169 "\tmovq %rsp, (%rdi)\n"
170 "\tmovq (%rsi), %rsp\n"
171 "\tpopq %r15\n"
172 "\tpopq %r14\n"
173 "\tpopq %r13\n"
174 "\tpopq %r12\n"
175 "\tpopq %rbx\n"
176 "\tpopq %rbp\n"
133177 #endif
134 "\tmov %rsp, (%rdi)\n"
135 "\tmov (%rsi), %rsp\n"
136 #if CORO_WIN_TIB
137 "\tpop %gs:0xc\n"
138 "\tpop %gs:0x8\n"
139 "\tpop %gs:0x0\n"
140 #endif
141 "\tpop %r15\n"
142 "\tpop %r14\n"
143 "\tpop %r13\n"
144 "\tpop %r12\n"
145 "\tpop %rbx\n"
146 "\tpop %rbp\n"
147178 #elif __i386
148179 #define NUM_SAVED 4
149 "\tpush %ebp\n"
150 "\tpush %ebx\n"
151 "\tpush %esi\n"
152 "\tpush %edi\n"
180 "\tpushl %ebp\n"
181 "\tpushl %ebx\n"
182 "\tpushl %esi\n"
183 "\tpushl %edi\n"
153184 #if CORO_WIN_TIB
154 "\tpush %fs:0\n"
155 "\tpush %fs:4\n"
156 "\tpush %fs:8\n"
185 "\tpushl %fs:0\n"
186 "\tpushl %fs:4\n"
187 "\tpushl %fs:8\n"
157188 #endif
158 "\tmov %esp, (%eax)\n"
159 "\tmov (%edx), %esp\n"
189 "\tmovl %esp, (%eax)\n"
190 "\tmovl (%edx), %esp\n"
160191 #if CORO_WIN_TIB
161 "\tpop %fs:8\n"
162 "\tpop %fs:4\n"
163 "\tpop %fs:0\n"
192 "\tpopl %fs:8\n"
193 "\tpopl %fs:4\n"
194 "\tpopl %fs:0\n"
164195 #endif
165 "\tpop %edi\n"
166 "\tpop %esi\n"
167 "\tpop %ebx\n"
168 "\tpop %ebp\n"
196 "\tpopl %edi\n"
197 "\tpopl %esi\n"
198 "\tpopl %ebx\n"
199 "\tpopl %ebp\n"
169200 #else
170201 #error unsupported architecture
171202 #endif
306337 #endif
307338
308339 ctx->sp -= NUM_SAVED;
340 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED);
309341
310342 # elif CORO_UCONTEXT
311343
6969 * 2008-11-19 define coro_*jmp symbols for easier porting.
7070 * 2009-06-23 tentative win32-backend support for mingw32 (Yasuhiro Matsumoto).
7171 * 2010-12-03 tentative support for uclibc (which lacks all sorts of things).
72 * 2011-05-30 set initial callee-saved-registers to zero with CORO_ASM.
73 * use .cfi_undefined rip on linux-amd64 for better backtraces.
74 * 2011-06-08 maybe properly implement weird windows amd64 calling conventions.
7275 */
7376
7477 #ifndef CORO_H
203206 && !defined(CORO_SJLJ) && !defined(CORO_LINUX) \
204207 && !defined(CORO_IRIX) && !defined(CORO_ASM) \
205208 && !defined(CORO_PTHREAD)
206 # if defined(WINDOWS)
209 # if defined(WINDOWS) || defined(_WIN32)
207210 # define CORO_LOSER 1 /* you don't win with windoze */
208211 # elif defined(__linux) && (defined(__x86) || defined (__amd64))
209212 # define CORO_ASM 1
77 #include <signal.h>
88 #include <errno.h>
99
10 #if defined(WIN32 ) || defined(_MINIX)
10 #if defined(_WIN32) || defined(_MINIX)
1111 # define SCHMORP_H_PREFER_SELECT 1
1212 #endif
1313
00 /* list the interpreter variables that need to be saved/restored */
1
2 VARx(defsv, GvSV (PL_defgv), SV *)
3 VARx(defav, GvAV (PL_defgv), AV *)
4 VARx(errsv, GvSV (PL_errgv), SV *)
5 VARx(irsgv, GvSV (irsgv), SV *)
6 VARx(hinthv, GvHV (PL_hintgv), HV *)
7
18 /* mostly copied from thrdvar.h */
29
3 VAR(defoutgv, GV *) /* default FH for output */
410 VAR(stack_sp, SV **) /* the main stack */
511 #ifdef OP_IN_REGISTER
612 VAR(opsave, OP *) /* probably not necessary */
1521 VAR(scopestack, I32 *) /* scopes we've ENTERed */
1622 VAR(scopestack_ix, I32)
1723 VAR(scopestack_max,I32)
24 #if HAS_SCOPESTACK_NAME
25 VAR(scopestack_name,const char **)
26 #endif
1827
1928 VAR(savestack, ANY *) /* items that need to be restored
2029 when LEAVEing scopes we've ENTERed */
3645 VAR(retstack_max, I32)
3746 #endif
3847
39 VAR(tainted, bool) /* using variables controlled by $< */
4048 VAR(curpm, PMOP *) /* what to do \ interps in REs from */
4149 VAR(rs, SV *) /* input record separator $/ */
50 VAR(defoutgv, GV *) /* default FH for output */
4251 VAR(curcop, COP *)
43
44 VAR(in_eval, int) /* trap "fatal" errors? */
45 VAR(localizing, int) /* are we processing a local() list? */
4652
4753 VAR(curstack, AV *) /* THE STACK */
4854 VAR(curstackinfo, PERL_SI *) /* current stack + context */
5359 VAR(sortcxix, I32) /* from pp_ctl.c */
5460 #endif
5561
62 VAR(localizing, U8) /* are we processing a local() list? */
63 VAR(in_eval, U8) /* trap "fatal" errors? */
64 VAR(tainted, bool) /* using variables controlled by $< */
65
66 VAR(diehook, SV *)
67 VAR(warnhook, SV *)
68
69 /* compcv is intrpvar, but seems to be thread-specific to me */
70 /* but, well, I thoroughly misunderstand what thrdvar and intrpvar is. still. */
71 VAR(compcv, CV *) /* currently compiling subroutine */
72
5673 VAR(comppad, AV *) /* storage for lexically scoped temporaries */
5774 VAR(comppad_name, AV *) /* variable names for "my" variables */
5875 VAR(comppad_name_fill, I32) /* last "introduced" variable offset */
5976 VAR(comppad_name_floor, I32) /* start of vars in innermost block */
6077
61 /* compcv is intrpvar, but seems to be thread-specific to me */
62 /* but, well, I thoroughly misunderstand what thrdvar and intrpvar is. still. */
63 VAR(compcv, CV *) /* currently compiling subroutine */
78 VAR(runops, runops_proc_t) /* for tracing support */
6479
65 VAR(diehook, SV *)
66 VAR(warnhook, SV *)
67 VAR(runops, runops_proc_t) /* for tracing support */
80 VAR(hints, U32) /* pragma-tic compile-time flags */
6881
6982 #if PERL_VERSION_ATLEAST (5,10,0)
7083 VAR(parser, yy_parser *)
7184 #endif
7285
73 VAR(hints, U32) /* pragma-tic compile-time flags */
74
1717 cede; # and again
1818
1919 # use locking
20 use Coro::Semaphore;
2120 my $lock = new Coro::Semaphore;
2221 my $locked;
2322
9190 This creates a new coro thread and puts it into the ready queue, meaning
9291 it will run as soon as the CPU is free for it.
9392
94 C<async> will return a coro object - you can store this for future
95 reference or ignore it, the thread itself will keep a reference to it's
96 thread object - threads are alive on their own.
93 C<async> will return a Coro object - you can store this for future
94 reference or ignore it - a thread that is running, ready to run or waiting
95 for some event is alive on it's own.
9796
9897 Another way to create a thread is to call the C<new> constructor with a
9998 code-reference:
132131 instead), but it will give up the CPU regularly because it waits for
133132 external events.
134133
135 As long as a coro thread runs, it's coro object is available in the global
134 As long as a coro thread runs, its Coro object is available in the global
136135 variable C<$Coro::current>.
137136
138137 The low-level way to give up the CPU is to call the scheduler, which
197196 Coro::terminate "return value 1", "return value 2";
198197 };
199198
200 And yet another way is to C<< ->cancel >> the coro thread from another
201 thread:
199 And yet another way is to C<< ->cancel >> (or C<< ->safe_cancel >>) the
200 coro thread from another thread:
202201
203202 my $coro = async {
204203 exit 1;
205204 };
206205
207 $coro->cancel; # an also accept values for ->join to retrieve
206 $coro->cancel; # also accepts values for ->join to retrieve
208207
209208 Cancellation I<can> be dangerous - it's a bit like calling C<exit> without
210209 actually exiting, and might leave C libraries and XS modules in a weird
211210 state. Unlike other thread implementations, however, Coro is exceptionally
212211 safe with regards to cancellation, as perl will always be in a consistent
213 state.
212 state, and for those cases where you want to do truly marvellous things
213 with your coro while it is being cancelled - that is, make sure all
214 cleanup code is executed from the thread being cancelled - there is even a
215 C<< ->safe_cancel >> method.
214216
215217 So, cancelling a thread that runs in an XS event loop might not be the
216218 best idea, but any other combination that deals with perl only (cancelling
217219 when a thread is in a C<tie> method or an C<AUTOLOAD> for example) is
218220 safe.
219221
222 Lastly, a coro thread object that isn't referenced is C<< ->cancel >>'ed
223 automatically - just like other objects in Perl. This is not such a common
224 case, however - a running thread is referencedy b C<$Coro::current>, a
225 thread ready to run is referenced by the ready queue, a thread waiting
226 on a lock or semaphore is referenced by being in some wait list and so
227 on. But a thread that isn't in any of those queues gets cancelled:
228
229 async {
230 schedule; # cede to other coros, don't go into the ready queue
231 };
232
233 cede;
234 # now the async above is destroyed, as it is not referenced by anything.
235
220236 =item 5. Cleanup
221237
222238 Threads will allocate various resources. Most but not all will be returned
249265 };
250266
251267 The C<Guard::guard> function comes in handy for any custom cleanup you
252 might want to do:
268 might want to do (but you cannot switch to other coroutines form those
269 code blocks):
253270
254271 async {
255272 my $window = new Gtk2::Window "toplevel";
272289
273290 =item 6. Viva La Zombie Muerte
274291
275 Even after a thread has terminated and cleaned up it's resources, the coro
276 object still is there and stores the return values of the thread. Only in
277 this state will the coro object be "reference counted" in the normal perl
278 sense: the thread code keeps a reference to it when it is active, but not
279 after it has terminated.
280
281 The means the coro object gets freed automatically when the thread has
292 Even after a thread has terminated and cleaned up its resources, the Coro
293 object still is there and stores the return values of the thread.
294
295 The means the Coro object gets freed automatically when the thread has
282296 terminated and cleaned up and there arenot other references.
283297
284 If there are, the coro object will stay around, and you can call C<<
298 If there are, the Coro object will stay around, and you can call C<<
285299 ->join >> as many times as you wish to retrieve the result values:
286300
287301 async {
329343 our $main; # main coro
330344 our $current; # current coro
331345
332 our $VERSION = 5.372;
346 our $VERSION = '6.0';
333347
334348 our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait);
335349 our %EXPORT_TAGS = (
400414
401415 $manager = new Coro sub {
402416 while () {
403 Coro::State::cancel shift @destroy
417 _destroy shift @destroy
404418 while @destroy;
405419
406420 &schedule;
544558
545559 =item terminate [arg...]
546560
547 Terminates the current coro with the given status values (see L<cancel>).
561 Terminates the current coro with the given status values (see
562 L<cancel>). The values will not be copied, but referenced directly.
548563
549564 =item Coro::on_enter BLOCK, Coro::on_leave BLOCK
550565
710725 against spurious wakeups, and the one in the Coro family certainly do
711726 that.
712727
728 =item $state->is_new
729
730 Returns true iff this Coro object is "new", i.e. has never been run
731 yet. Those states basically consist of only the code reference to call and
732 the arguments, but consumes very little other resources. New states will
733 automatically get assigned a perl interpreter when they are transfered to.
734
735 =item $state->is_zombie
736
737 Returns true iff the Coro object has been cancelled, i.e.
738 it's resources freed because they were C<cancel>'ed, C<terminate>'d,
739 C<safe_cancel>'ed or simply went out of scope.
740
741 The name "zombie" stems from UNIX culture, where a process that has
742 exited and only stores and exit status and no other resources is called a
743 "zombie".
744
713745 =item $is_ready = $coro->is_ready
714746
715747 Returns true iff the Coro object is in the ready queue. Unless the Coro
728760
729761 =item $coro->cancel (arg...)
730762
731 Terminates the given Coro and makes it return the given arguments as
732 status (default: the empty list). Never returns if the Coro is the
763 Terminates the given Coro thread and makes it return the given arguments as
764 status (default: an empty list). Never returns if the Coro is the
733765 current Coro.
734766
735 =cut
736
737 sub cancel {
738 my $self = shift;
739
740 if ($current == $self) {
741 terminate @_;
742 } else {
743 $self->{_status} = [@_];
744 Coro::State::cancel $self;
767 This is a rather brutal way to free a coro, with some limitations - if
768 the thread is inside a C callback that doesn't expect to be canceled,
769 bad things can happen, or if the cancelled thread insists on running
770 complicated cleanup handlers that rely on its thread context, things will
771 not work.
772
773 Any cleanup code being run (e.g. from C<guard> blocks) will be run without
774 a thread context, and is not allowed to switch to other threads. On the
775 plus side, C<< ->cancel >> will always clean up the thread, no matter
776 what. If your cleanup code is complex or you want to avoid cancelling a
777 C-thread that doesn't know how to clean up itself, it can be better to C<<
778 ->throw >> an exception, or use C<< ->safe_cancel >>.
779
780 The arguments to C<< ->cancel >> are not copied, but instead will
781 be referenced directly (e.g. if you pass C<$var> and after the call
782 change that variable, then you might change the return values passed to
783 e.g. C<join>, so don't do that).
784
785 The resources of the Coro are usually freed (or destructed) before this
786 call returns, but this can be delayed for an indefinite amount of time, as
787 in some cases the manager thread has to run first to actually destruct the
788 Coro object.
789
790 =item $coro->safe_cancel ($arg...)
791
792 Works mostly like C<< ->cancel >>, but is inherently "safer", and
793 consequently, can fail with an exception in cases the thread is not in a
794 cancellable state.
795
796 This method works a bit like throwing an exception that cannot be caught
797 - specifically, it will clean up the thread from within itself, so
798 all cleanup handlers (e.g. C<guard> blocks) are run with full thread
799 context and can block if they wish. The downside is that there is no
800 guarantee that the thread can be cancelled when you call this method, and
801 therefore, it might fail. It is also considerably slower than C<cancel> or
802 C<terminate>.
803
804 A thread is in a safe-cancellable state if it either hasn't been run yet,
805 or it has no C context attached and is inside an SLF function.
806
807 The latter two basically mean that the thread isn't currently inside a
808 perl callback called from some C function (usually via some XS modules)
809 and isn't currently executing inside some C function itself (via Coro's XS
810 API).
811
812 This call returns true when it could cancel the thread, or croaks with an
813 error otherwise (i.e. it either returns true or doesn't return at all).
814
815 Why the weird interface? Well, there are two common models on how and
816 when to cancel things. In the first, you have the expectation that your
817 coro thread can be cancelled when you want to cancel it - if the thread
818 isn't cancellable, this would be a bug somewhere, so C<< ->safe_cancel >>
819 croaks to notify of the bug.
820
821 In the second model you sometimes want to ask nicely to cancel a thread,
822 but if it's not a good time, well, then don't cancel. This can be done
823 relatively easy like this:
824
825 if (! eval { $coro->safe_cancel }) {
826 warn "unable to cancel thread: $@";
745827 }
746 }
828
829 However, what you never should do is first try to cancel "safely" and
830 if that fails, cancel the "hard" way with C<< ->cancel >>. That makes
831 no sense: either you rely on being able to execute cleanup code in your
832 thread context, or you don't. If you do, then C<< ->safe_cancel >> is the
833 only way, and if you don't, then C<< ->cancel >> is always faster and more
834 direct.
747835
748836 =item $coro->schedule_to
749837
772860
773861 Coro will check for the exception each time a schedule-like-function
774862 returns, i.e. after each C<schedule>, C<cede>, C<< Coro::Semaphore->down
775 >>, C<< Coro::Handle->readable >> and so on. Most of these functions
776 detect this case and return early in case an exception is pending.
863 >>, C<< Coro::Handle->readable >> and so on. Most of those functions (all
864 that are part of Coro itself) detect this case and return early in case an
865 exception is pending.
777866
778867 The exception object will be thrown "as is" with the specified scalar in
779868 C<$@>, i.e. if it is a string, no line number or newline will be appended
780869 (unlike with C<die>).
781870
782 This can be used as a softer means than C<cancel> to ask a coro to
783 end itself, although there is no guarantee that the exception will lead to
784 termination, and if the exception isn't caught it might well end the whole
785 program.
871 This can be used as a softer means than either C<cancel> or C<safe_cancel
872 >to ask a coro to end itself, although there is no guarantee that the
873 exception will lead to termination, and if the exception isn't caught it
874 might well end the whole program.
786875
787876 You might also think of C<throw> as being the moral equivalent of
788877 C<kill>ing a coro with a signal (in this case, a scalar).
791880
792881 Wait until the coro terminates and return any values given to the
793882 C<terminate> or C<cancel> functions. C<join> can be called concurrently
794 from multiple coro, and all will be resumed and given the status
883 from multiple threads, and all will be resumed and given the status
795884 return once the C<$coro> terminates.
796885
797 =cut
798
799 sub join {
800 my $self = shift;
801
802 unless ($self->{_status}) {
803 my $current = $current;
804
805 push @{$self->{_on_destroy}}, sub {
806 $current->ready;
807 undef $current;
808 };
809
810 &schedule while $current;
811 }
812
813 wantarray ? @{$self->{_status}} : $self->{_status}[0];
814 }
815
816886 =item $coro->on_destroy (\&cb)
817887
818888 Registers a callback that is called when this coro thread gets destroyed,
819 but before it is joined. The callback gets passed the terminate arguments,
820 if any, and I<must not> die, under any circumstances.
821
822 There can be any number of C<on_destroy> callbacks per coro.
823
824 =cut
825
826 sub on_destroy {
827 my ($self, $cb) = @_;
828
829 push @{ $self->{_on_destroy} }, $cb;
830 }
889 that is, after it's resources have been freed but before it is joined. The
890 callback gets passed the terminate/cancel arguments, if any, and I<must
891 not> die, under any circumstances.
892
893 There can be any number of C<on_destroy> callbacks per coro, and there is
894 no way currently to remove a callback once added.
831895
832896 =item $oldprio = $coro->prio ($newprio)
833897
11091173 the windows process emulation enabled under unix roughly halves perl
11101174 performance, even when not used.
11111175
1176 Attempts to use threads created in another emulated process will crash
1177 ("cleanly", with a null pointer exception).
1178
11121179 =item coro switching is not signal safe
11131180
11141181 You must not switch to another coro from within a signal handler (only
5959 use XSLoader;
6060
6161 BEGIN {
62 our $VERSION = 5.372;
62 our $VERSION = 6.0;
6363
6464 local $^W = 0; # avoid redefine warning for Coro::ready;
6565 XSLoader::load __PACKAGE__, $VERSION;
9191 our @EXPORT = qw(loop unloop sweep);
9292
9393 BEGIN {
94 our $VERSION = 5.372;
94 our $VERSION = 6.0;
9595
9696 local $^W = 0; # avoid redefine warning for Coro::ready;
9797 XSLoader::load __PACKAGE__, $VERSION;
88 Coro.pm
99 Coro/Makefile.PL
1010 Coro/typemap
11 Coro/ecb.h
1112 Coro/schmorp.h
1213 Coro/State.pm
1314 Coro/State.xs
1415 Coro/clone.c
1516 Coro/state.h
17 Coro/jit-amd64-unix.pl
18 Coro/jit-x86-unix.pl
1619 Coro/Signal.pm
1720 Coro/Semaphore.pm
1821 Coro/SemaphoreSet.pm
6063 t/17_rouse.t
6164 t/18_winder.t
6265 t/19_handle.t
66 t/20_mutual_cancel.t
6367
6468 EV/Makefile.PL
6569 EV/EV.pm
1010 },
1111 "generated_by" : "ExtUtils::MakeMaker::JSONMETA version 7.000",
1212 "distribution_type" : "module",
13 "version" : "5.372",
13 "version" : "6.0",
1414 "name" : "Coro",
1515 "author" : [],
1616 "license" : "unknown",
3535 print "\nEvent version $Event::VERSION found, building Event support.\n\n";
3636 }
3737 } else {
38 print "\n*** Event not found, not build Event support.\n\n";
38 print "\n*** Event not found, not building Event support.\n\n";
3939 }
4040
4141 if (eval { require EV }) {
4343 print <<EOF
4444
4545 ***
46 *** WARNING: Event version $EV::VERSION found, NOT building EV support.
46 *** WARNING: EV version $EV::VERSION found, NOT building EV support.
4747 ***
4848 *** This version is ABI-incompatible with Coro, please upgrade to at least 3.3.
4949 ***
5555 print "\nEV version $EV::VERSION found, building EV support.\n\n";
5656 }
5757 } else {
58 print "\n*** EV not found, not build EV support.\n\n";
58 print "\n*** EV not found, not building EV support.\n\n";
5959 }
6060
6161 WriteMakefile(
107107 'Coro.pm' => '$(INST_LIBDIR)/Coro.pm',
108108
109109 'Coro/State.pm' => '$(INST_LIBDIR)/Coro/State.pm',
110 'Coro/jit-amd64-unix.pl' => '$(INST_LIBDIR)/Coro/jit-amd64-unix.pl',
111 'Coro/jit-x86-unix.pl' => '$(INST_LIBDIR)/Coro/jit-x86-unix.pl',
110112
111113 'Coro/MakeMaker.pm' => '$(INST_LIBDIR)/Coro/MakeMaker.pm',
112114 'Coro/CoroAPI.h' => '$(INST_LIBDIR)/Coro/CoroAPI.h',
+140
-34
README less more
1515 cede; # and again
1616
1717 # use locking
18 use Coro::Semaphore;
1918 my $lock = new Coro::Semaphore;
2019 my $locked;
2120
8584 This creates a new coro thread and puts it into the ready queue,
8685 meaning it will run as soon as the CPU is free for it.
8786
88 "async" will return a coro object - you can store this for future
89 reference or ignore it, the thread itself will keep a reference to
90 it's thread object - threads are alive on their own.
87 "async" will return a Coro object - you can store this for future
88 reference or ignore it - a thread that is running, ready to run or
89 waiting for some event is alive on it's own.
9190
9291 Another way to create a thread is to call the "new" constructor with
9392 a code-reference:
124123 a function instead), but it will give up the CPU regularly because
125124 it waits for external events.
126125
127 As long as a coro thread runs, it's coro object is available in the
126 As long as a coro thread runs, its Coro object is available in the
128127 global variable $Coro::current.
129128
130129 The low-level way to give up the CPU is to call the scheduler, which
188187 Coro::terminate "return value 1", "return value 2";
189188 };
190189
191 And yet another way is to "->cancel" the coro thread from another
192 thread:
190 And yet another way is to "->cancel" (or "->safe_cancel") the coro
191 thread from another thread:
193192
194193 my $coro = async {
195194 exit 1;
196195 };
197196
198 $coro->cancel; # an also accept values for ->join to retrieve
197 $coro->cancel; # also accepts values for ->join to retrieve
199198
200199 Cancellation *can* be dangerous - it's a bit like calling "exit"
201200 without actually exiting, and might leave C libraries and XS modules
202201 in a weird state. Unlike other thread implementations, however, Coro
203202 is exceptionally safe with regards to cancellation, as perl will
204 always be in a consistent state.
203 always be in a consistent state, and for those cases where you want
204 to do truly marvellous things with your coro while it is being
205 cancelled - that is, make sure all cleanup code is executed from the
206 thread being cancelled - there is even a "->safe_cancel" method.
205207
206208 So, cancelling a thread that runs in an XS event loop might not be
207209 the best idea, but any other combination that deals with perl only
208210 (cancelling when a thread is in a "tie" method or an "AUTOLOAD" for
209211 example) is safe.
210212
213 Lastly, a coro thread object that isn't referenced is "->cancel"'ed
214 automatically - just like other objects in Perl. This is not such a
215 common case, however - a running thread is referencedy b
216 $Coro::current, a thread ready to run is referenced by the ready
217 queue, a thread waiting on a lock or semaphore is referenced by
218 being in some wait list and so on. But a thread that isn't in any of
219 those queues gets cancelled:
220
221 async {
222 schedule; # cede to other coros, don't go into the ready queue
223 };
224
225 cede;
226 # now the async above is destroyed, as it is not referenced by anything.
227
211228 5. Cleanup
212229 Threads will allocate various resources. Most but not all will be
213230 returned when a thread terminates, during clean-up.
239256 };
240257
241258 The "Guard::guard" function comes in handy for any custom cleanup
242 you might want to do:
259 you might want to do (but you cannot switch to other coroutines form
260 those code blocks):
243261
244262 async {
245263 my $window = new Gtk2::Window "toplevel";
261279 }
262280
263281 6. Viva La Zombie Muerte
264 Even after a thread has terminated and cleaned up it's resources,
265 the coro object still is there and stores the return values of the
266 thread. Only in this state will the coro object be "reference
267 counted" in the normal perl sense: the thread code keeps a reference
268 to it when it is active, but not after it has terminated.
269
270 The means the coro object gets freed automatically when the thread
282 Even after a thread has terminated and cleaned up its resources, the
283 Coro object still is there and stores the return values of the
284 thread.
285
286 The means the Coro object gets freed automatically when the thread
271287 has terminated and cleaned up and there arenot other references.
272288
273 If there are, the coro object will stay around, and you can call
289 If there are, the Coro object will stay around, and you can call
274290 "->join" as many times as you wish to retrieve the result values:
275291
276292 async {
439455
440456 terminate [arg...]
441457 Terminates the current coro with the given status values (see
442 cancel).
458 cancel). The values will not be copied, but referenced directly.
443459
444460 Coro::on_enter BLOCK, Coro::on_leave BLOCK
445461 These function install enter and leave winders in the current scope.
581597 protect itself against spurious wakeups, and the one in the Coro
582598 family certainly do that.
583599
600 $state->is_new
601 Returns true iff this Coro object is "new", i.e. has never been run
602 yet. Those states basically consist of only the code reference to
603 call and the arguments, but consumes very little other resources.
604 New states will automatically get assigned a perl interpreter when
605 they are transfered to.
606
607 $state->is_zombie
608 Returns true iff the Coro object has been cancelled, i.e. it's
609 resources freed because they were "cancel"'ed, "terminate"'d,
610 "safe_cancel"'ed or simply went out of scope.
611
612 The name "zombie" stems from UNIX culture, where a process that has
613 exited and only stores and exit status and no other resources is
614 called a "zombie".
615
584616 $is_ready = $coro->is_ready
585617 Returns true iff the Coro object is in the ready queue. Unless the
586618 Coro object gets destroyed, it will eventually be scheduled by the
596628 Coros will not ever be scheduled.
597629
598630 $coro->cancel (arg...)
599 Terminates the given Coro and makes it return the given arguments as
600 status (default: the empty list). Never returns if the Coro is the
601 current Coro.
631 Terminates the given Coro thread and makes it return the given
632 arguments as status (default: an empty list). Never returns if the
633 Coro is the current Coro.
634
635 This is a rather brutal way to free a coro, with some limitations -
636 if the thread is inside a C callback that doesn't expect to be
637 canceled, bad things can happen, or if the cancelled thread insists
638 on running complicated cleanup handlers that rely on its thread
639 context, things will not work.
640
641 Any cleanup code being run (e.g. from "guard" blocks) will be run
642 without a thread context, and is not allowed to switch to other
643 threads. On the plus side, "->cancel" will always clean up the
644 thread, no matter what. If your cleanup code is complex or you want
645 to avoid cancelling a C-thread that doesn't know how to clean up
646 itself, it can be better to "->throw" an exception, or use
647 "->safe_cancel".
648
649 The arguments to "->cancel" are not copied, but instead will be
650 referenced directly (e.g. if you pass $var and after the call change
651 that variable, then you might change the return values passed to
652 e.g. "join", so don't do that).
653
654 The resources of the Coro are usually freed (or destructed) before
655 this call returns, but this can be delayed for an indefinite amount
656 of time, as in some cases the manager thread has to run first to
657 actually destruct the Coro object.
658
659 $coro->safe_cancel ($arg...)
660 Works mostly like "->cancel", but is inherently "safer", and
661 consequently, can fail with an exception in cases the thread is not
662 in a cancellable state.
663
664 This method works a bit like throwing an exception that cannot be
665 caught - specifically, it will clean up the thread from within
666 itself, so all cleanup handlers (e.g. "guard" blocks) are run with
667 full thread context and can block if they wish. The downside is that
668 there is no guarantee that the thread can be cancelled when you call
669 this method, and therefore, it might fail. It is also considerably
670 slower than "cancel" or "terminate".
671
672 A thread is in a safe-cancellable state if it either hasn't been run
673 yet, or it has no C context attached and is inside an SLF function.
674
675 The latter two basically mean that the thread isn't currently inside
676 a perl callback called from some C function (usually via some XS
677 modules) and isn't currently executing inside some C function itself
678 (via Coro's XS API).
679
680 This call returns true when it could cancel the thread, or croaks
681 with an error otherwise (i.e. it either returns true or doesn't
682 return at all).
683
684 Why the weird interface? Well, there are two common models on how
685 and when to cancel things. In the first, you have the expectation
686 that your coro thread can be cancelled when you want to cancel it -
687 if the thread isn't cancellable, this would be a bug somewhere, so
688 "->safe_cancel" croaks to notify of the bug.
689
690 In the second model you sometimes want to ask nicely to cancel a
691 thread, but if it's not a good time, well, then don't cancel. This
692 can be done relatively easy like this:
693
694 if (! eval { $coro->safe_cancel }) {
695 warn "unable to cancel thread: $@";
696 }
697
698 However, what you never should do is first try to cancel "safely"
699 and if that fails, cancel the "hard" way with "->cancel". That makes
700 no sense: either you rely on being able to execute cleanup code in
701 your thread context, or you don't. If you do, then "->safe_cancel"
702 is the only way, and if you don't, then "->cancel" is always faster
703 and more direct.
602704
603705 $coro->schedule_to
604706 Puts the current coro to sleep (like "Coro::schedule"), but instead
625727 Coro will check for the exception each time a schedule-like-function
626728 returns, i.e. after each "schedule", "cede",
627729 "Coro::Semaphore->down", "Coro::Handle->readable" and so on. Most of
628 these functions detect this case and return early in case an
629 exception is pending.
730 those functions (all that are part of Coro itself) detect this case
731 and return early in case an exception is pending.
630732
631733 The exception object will be thrown "as is" with the specified
632734 scalar in $@, i.e. if it is a string, no line number or newline will
633735 be appended (unlike with "die").
634736
635 This can be used as a softer means than "cancel" to ask a coro to
636 end itself, although there is no guarantee that the exception will
637 lead to termination, and if the exception isn't caught it might well
638 end the whole program.
737 This can be used as a softer means than either "cancel" or
738 "safe_cancel "to ask a coro to end itself, although there is no
739 guarantee that the exception will lead to termination, and if the
740 exception isn't caught it might well end the whole program.
639741
640742 You might also think of "throw" as being the moral equivalent of
641743 "kill"ing a coro with a signal (in this case, a scalar).
643745 $coro->join
644746 Wait until the coro terminates and return any values given to the
645747 "terminate" or "cancel" functions. "join" can be called concurrently
646 from multiple coro, and all will be resumed and given the status
748 from multiple threads, and all will be resumed and given the status
647749 return once the $coro terminates.
648750
649751 $coro->on_destroy (\&cb)
650752 Registers a callback that is called when this coro thread gets
651 destroyed, but before it is joined. The callback gets passed the
652 terminate arguments, if any, and *must not* die, under any
653 circumstances.
654
655 There can be any number of "on_destroy" callbacks per coro.
753 destroyed, that is, after it's resources have been freed but before
754 it is joined. The callback gets passed the terminate/cancel
755 arguments, if any, and *must not* die, under any circumstances.
756
757 There can be any number of "on_destroy" callbacks per coro, and
758 there is no way currently to remove a callback once added.
656759
657760 $oldprio = $coro->prio ($newprio)
658761 Sets (or gets, if the argument is missing) the priority of the coro
849952 processes, as having the windows process emulation enabled under
850953 unix roughly halves perl performance, even when not used.
851954
955 Attempts to use threads created in another emulated process will
956 crash ("cleanly", with a null pointer exception).
957
852958 coro switching is not signal safe
853959 You must not switch to another coro from within a signal handler
854960 (only relevant with %SIG - most event libraries provide safe
2626 };
2727
2828 cede;
29
30 $main = $Coro::main;
3129
3230 *transfer = \&Coro::State::transfer;
3331
6260 #$c0->save (0);
6361 #$c1->save (-1);
6462
65 transfer($main, $c0);
66 transfer($main, $c1);
67
6863 #Coro::State::enable_times 1;
6964 #use Coro::Debug; Coro::Debug::command "ps";#d#
7065 #(async {
71 timethese 5000000, {
72 function => 'a(5); a(6)',
73 method => '$a->b(5); $a->b(6)',
74 cede => 'cede',
75 transfer0 => 'transfer($main, $c0)',
76 transfer1 => 'transfer($main, $c1)',
77 };
66 $main = $Coro::current;
67
68 transfer($main, $c0);
69 transfer($main, $c1);
70
71 timethese 5000000, {
72 function => 'a(5); a(6)',
73 method => '$a->b(5); $a->b(6)',
74 cede => 'cede',
75 transfer0 => 'transfer($main, $c0)',
76 transfer1 => 'transfer($main, $c1)',
77 };
7878 #})->join;
7979 #use Coro::Debug; Coro::Debug::command "ps";#d#
8080
8181
82
1111
1212 $p2 = async {
1313 print "ok 4\n";
14 ();
14 ()
1515 };
1616
1717 $p3 = async {
1818 print "ok 5\n";
19 (0,1,2);
19 (0,1,2)
2020 };
2121
2222 print "ok 2\n";
2424 print 0 == ($p3->join)[0] ? "ok " : "not ok ", "7\n";
2525 print 1 == ($p3->join)[1] ? "ok " : "not ok ", "8\n";
2626 print 2 == ($p3->join)[2] ? "ok " : "not ok ", "9\n";
27 print 5 == $p1->join ? "ok " : "not ok ", "10\n";
27 print 5 == $p1->join ? "ok " : "not ok ", "10\n";
2828
0 $|=1;
1 print "1..10\n";
2
3 # when two coros cancel each other mutually,
4 # the slf function currently being executed needs to
5 # be cleaned up, otherwise the next slf call in the cleanup code
6 # will simply resume the previous call.
7 # in addition, mutual cancellation must be specially handled
8 # as currently, we sometimes cancel coros from another coro
9 # which must not be interrupted (see slf_init_cancel).
10
11 use Coro;
12
13 print "ok 1\n";
14
15 my ($a, $b);
16
17 sub xyz::DESTROY {
18 print "ok 7\n";
19 $b->cancel;
20 print "ok 8\n";
21 }
22
23 $b = async {
24 print "ok 3\n";
25 cede;
26 print "ok 6\n";
27 $a->cancel;
28 print "not ok 7\n";
29 };
30
31 $a = async {
32 print "ok 4\n";
33 my $x = bless \my $dummy, "xyz";
34 cede;
35 print "not ok 5\n";
36 };
37
38 print "ok 2\n";
39 cede;
40 print "ok 5\n";
41 cede;
42 print "ok 9\n";
43 cede;
44 print "ok 10\n";
45