ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/EV/EV.pm
Revision: 1.6
Committed: Wed Nov 7 20:40:30 2007 UTC (16 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-4_21, rel-4_2
Changes since 1.5: +3 -8 lines
Log Message:
support multiple EV event loops without endless looping

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Coro::EV - do events the coro-way
4    
5     =head1 SYNOPSIS
6    
7     use Coro;
8     use Coro::EV;
9    
10 root 1.5 EV::loop;
11    
12 root 1.4 =head1 DESCRIPTION
13 root 1.1
14 root 1.4 This module does two things: First, it offers some utility functions that
15     might be useful for coroutines, and secondly, it integrates Coro into the
16     EV main loop:
17    
18     Before the process blocks (in EV::loop) to wait for events, this module
19     will schedule and run all ready (= runnable) coroutines of the same or
20     higher priority. After that, it will cede once to a coroutine of lower
21     priority, then continue in the event loop.
22    
23     That means that coroutines with the same or higher pripority as the
24     coroutine running the main loop will inhibit event processing, while
25     coroutines of lower priority will get the CPU, but cannot completeley
26     inhibit event processing.
27 root 1.1
28 root 1.5 =head2 RUNNING WITH OR WITHOUT A MAINLOOP
29    
30     In general, you should always run EV::loop, either in your main program,
31     or in a separate coroutine. If you don't do that and all coroutines start
32     waiting for some events, this module will run the event loop once, but
33     this is very inefficient.
34    
35 root 1.1 =head1 FUNCTIONS
36    
37     =over 4
38    
39     =cut
40    
41     package Coro::EV;
42    
43     no warnings;
44    
45     use Carp;
46     no warnings;
47    
48     use Coro;
49     use Coro::Timer;
50    
51     use EV ();
52     use XSLoader;
53    
54     BEGIN {
55     our $VERSION = '2.1';
56    
57     local $^W = 0; # avoid redefine warning for Coro::ready;
58     XSLoader::load __PACKAGE__, $VERSION;
59     }
60    
61 root 1.5 unshift @AnyEvent::REGISTRY, [Coro::EV => "EV::AnyEvent"];
62    
63 root 1.1 $ev->{desc} = "[EV idle process]";
64    
65 root 1.6 $Coro::idle = sub {
66     EV::loop EV::LOOP_ONESHOT;
67     };
68 root 1.1
69 root 1.3 =item $revents = Coro::EV::timed_io_once $fd, $events, $timeout
70    
71     Blocks the coroutine until either the given event set has occured on the
72     fd, or the timeout has been reached (if timeout is zero, there is no
73     timeout). Returns the received flags.
74    
75     =cut
76    
77 root 1.1 sub timed_io_once($$;$) {
78     &_timed_io_once;
79     do { &Coro::schedule } while !$#_;
80     pop
81     }
82    
83 root 1.3 =item Coro::EV::timer_once $after
84    
85     Blocks the coroutine for at least C<$after> seconds.
86    
87     =cut
88    
89 root 1.1 sub timer_once($) {
90     &_timer_once;
91     do { &Coro::schedule } while !$#_;
92     pop
93     }
94    
95     1;
96    
97     =back
98    
99     =head1 AUTHOR
100    
101     Marc Lehmann <schmorp@schmorp.de>
102     http://home.schmorp.de/
103    
104     =cut
105