ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.pm
Revision: 1.3
Committed: Fri Aug 17 03:33:00 2001 UTC (22 years, 10 months ago) by root
Branch: MAIN
Changes since 1.2: +6 -34 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::Event - do events the coro-way
4
5 =head1 SYNOPSIS
6
7 use Coro;
8 use Coro::Event;
9
10 sub keyboard : Coro {
11 my $w = Coro::Event->io(fd => *STDIN, poll => 'r');
12 while() {
13 print "cmd> ";
14 my $ev = $w->next; my $cmd = <STDIN>;
15 unloop unless $cmd ne "";
16 print "data> ";
17 my $ev = $w->next; my $data = <STDIN>;
18 }
19 }
20
21 &loop;
22
23 =head1 DESCRIPTION
24
25 This module enables you to create programs using the powerful Event model
26 (and module), while retaining the linear style known from simple or
27 threaded programs.
28
29 This module provides a method and a function for every watcher type
30 (I<flavour>) (see L<Event>). The only difference between these and the
31 watcher constructors from Event is that you do not specify a callback
32 function - it will be managed by this module.
33
34 Your application should just create all necessary coroutines and then call
35 Coro::Event->main.
36
37 =over 4
38
39 =cut
40
41 package Coro::Event;
42
43 no warnings;
44
45 use Carp;
46
47 use Coro;
48 use Event qw(unloop); # we are re-exporting this, cooool!
49
50 use base 'Exporter';
51
52 @EXPORT = qw(loop unloop sweep);
53
54 BEGIN {
55 $VERSION = 0.45;
56
57 require XSLoader;
58 XSLoader::load Coro::Event, $VERSION;
59 }
60
61 =item $w = Coro::Event->flavour(args...)
62
63 Create and return a watcher of the given type.
64
65 Examples:
66
67 my $reader = Coro::Event->io(fd => $filehandle, poll => 'r');
68 $reader->next;
69
70 =cut
71
72 =item $w->next
73
74 Return the next event of the event queue of the watcher.
75
76 =cut
77
78 =item do_flavour(args...)
79
80 Create a watcher of the given type and immediately call it's next
81 method. This is less efficient then calling the constructor once and the
82 next method often, but it does save typing sometimes.
83
84 =cut
85
86 for my $flavour (qw(idle var timer io signal)) {
87 push @EXPORT, "do_$flavour";
88 my $new = \&{"Event::$flavour"};
89 my $class = "Coro::Event::$flavour";
90 my $type = $flavour eq "io" ? 1 : 0;
91 @{"${class}::ISA"} = (Coro::Event::, "Event::$flavour");
92 my $coronew = sub {
93 # how does one do method-call-by-name?
94 # my $w = $class->SUPER::$flavour(@_);
95
96 $_[0] eq Coro::Event::
97 or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method";
98
99 my $q = []; # [$coro, $event]
100 my $w = $new->(
101 desc => $flavour,
102 @_,
103 parked => 1,
104 );
105 _install_std_cb($w, $type);
106 bless $w, $class; # reblessing due to broken Event
107 };
108 *{ $flavour } = $coronew;
109 *{"do_$flavour"} = sub {
110 unshift @_, Coro::Event::;
111 my $e = (&$coronew)->next;
112 $e->w->cancel;
113 $e;
114 };
115 }
116
117 # double calls to avoid stack-cloning ;()
118 # is about 10% slower, though.
119 sub next($) {
120 &Coro::schedule if &_next; $_[0];
121 }
122
123 sub Coro::Event::w { $_[0]{Coro::Event}[2] }
124 sub Coro::Event::got { $_[0]{Coro::Event}[3] }
125 sub Coro::Event::prio { croak "prio not supported yet, please mail to pcg\@goof.com" }
126 sub Coro::Event::hits { croak "prio not supported yet, please mail to pcg\@goof.com" }
127
128 =item sweep
129
130 Similar to Event::one_event and Event::sweep: The idle task is called once
131 (this has the effect of jumping back into the Event loop once to serve new
132 events).
133
134 The reason this function exists is that you sometimes want to serve events
135 while doing other work. Calling C<Coro::cede> does not work because
136 C<cede> implies that the current coroutine is runnable and does not call
137 into the Event dispatcher.
138
139 =cut
140
141 sub sweep {
142 one_event(0); # for now
143 }
144
145 =item $result = loop([$timeout])
146
147 This is the version of C<loop> you should use instead of C<Event::loop>
148 when using this module - it will ensure correct scheduling in the presence
149 of events.
150
151 =begin comment
152
153 Unlike loop's counterpart it is not an error when no watchers are active -
154 loop silently returns in this case, as if unloop(undef) were called.
155
156 =end comment
157
158 =cut
159
160 sub loop(;$) {
161 local $Coro::idle = $Coro::current;
162 Coro::schedule; # become idle task, which is implicitly ready
163 &Event::loop;
164 }
165
166 =item unloop([$result])
167
168 Same as Event::unloop (provided here for your convinience only).
169
170 =cut
171
172 $Coro::idle = new Coro sub {
173 while () {
174 Event::one_event; # inefficient
175 Coro::schedule;
176 }
177 };
178
179 1;
180
181 =head1 AUTHOR
182
183 Marc Lehmann <pcg@goof.com>
184 http://www.goof.com/pcg/marc/
185
186 =cut
187