… | |
… | |
37 | Please note that even programs or modules (such as |
37 | Please note that even programs or modules (such as |
38 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
38 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
39 | event-based/continuation style will run more efficient with this module |
39 | event-based/continuation style will run more efficient with this module |
40 | then when using only Event. |
40 | then when using only Event. |
41 | |
41 | |
|
|
42 | =head1 WARNING |
|
|
43 | |
|
|
44 | Please note that Event does not support coroutines or threads. That |
|
|
45 | means that you B<MUST NOT> block in an event callback. Again: In Event |
|
|
46 | callbacks, you I<must never ever> call a Coroutine fucntion that blocks |
|
|
47 | the current coroutine. |
|
|
48 | |
|
|
49 | While this seems to work superficially, it will eventually cause memory |
|
|
50 | corruption. |
|
|
51 | |
|
|
52 | =head1 FUNCTIONS |
|
|
53 | |
42 | =over 4 |
54 | =over 4 |
43 | |
55 | |
44 | =cut |
56 | =cut |
45 | |
57 | |
46 | package Coro::Event; |
58 | package Coro::Event; |
47 | |
59 | |
48 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
60 | no warnings; |
49 | |
61 | |
50 | use Carp; |
62 | use Carp; |
51 | no warnings; |
63 | no warnings; |
52 | |
64 | |
53 | use Coro; |
65 | use Coro; |
… | |
… | |
65 | |
77 | |
66 | local $^W = 0; # avoid redefine warning for Coro::ready; |
78 | local $^W = 0; # avoid redefine warning for Coro::ready; |
67 | XSLoader::load __PACKAGE__, $VERSION; |
79 | XSLoader::load __PACKAGE__, $VERSION; |
68 | } |
80 | } |
69 | |
81 | |
70 | =item $w = Coro::Event->flavour(args...) |
82 | =item $w = Coro::Event->flavour (args...) |
71 | |
83 | |
72 | Create and return a watcher of the given type. |
84 | Create and return a watcher of the given type. |
73 | |
85 | |
74 | Examples: |
86 | Examples: |
75 | |
87 | |
… | |
… | |
82 | |
94 | |
83 | Return the next event of the event queue of the watcher. |
95 | Return the next event of the event queue of the watcher. |
84 | |
96 | |
85 | =cut |
97 | =cut |
86 | |
98 | |
87 | =item do_flavour(args...) |
99 | =item do_flavour args... |
88 | |
100 | |
89 | Create a watcher of the given type and immediately call it's next |
101 | Create a watcher of the given type and immediately call it's next |
90 | method. This is less efficient then calling the constructor once and the |
102 | method. This is less efficient then calling the constructor once and the |
91 | next method often, but it does save typing sometimes. |
103 | next method often, but it does save typing sometimes. |
92 | |
104 | |
… | |
… | |
104 | |
116 | |
105 | shift eq Coro::Event:: |
117 | shift eq Coro::Event:: |
106 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
118 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
107 | |
119 | |
108 | my $w = $new->($class, |
120 | my $w = $new->($class, |
109 | desc => $flavour, |
121 | desc => $flavour, |
110 | @_, |
122 | @_, |
111 | parked => 1, |
123 | parked => 1, |
112 | ); |
124 | ); |
|
|
125 | |
113 | _install_std_cb($w, $type); |
126 | _install_std_cb $w, $type; |
114 | bless $w, $class; # reblessing due to broken Event |
127 | |
|
|
128 | # reblessing due to Event being broken |
|
|
129 | bless $w, $class |
115 | }; |
130 | }; |
116 | *{ $flavour } = $coronew; |
131 | *{ $flavour } = $coronew; |
117 | *{"do_$flavour"} = sub { |
132 | *{"do_$flavour"} = sub { |
118 | unshift @_, Coro::Event::; |
133 | unshift @_, Coro::Event::; |
119 | my $e = (&$coronew)->next; |
134 | my $e = &$coronew->next; |
120 | $e->cancel; # $e === $e->w |
135 | $e->cancel; # $e === $e->w |
121 | $e; |
136 | $e |
122 | }; |
137 | }; |
123 | } |
138 | } |
124 | |
139 | |
125 | # double calls to avoid stack-cloning ;() |
140 | # do schedule in perl to avoid forcign a stack allocation. |
126 | # is about 10% slower, though. |
141 | # this is about 10% slower, though. |
127 | sub next($) { |
142 | sub next($) { |
128 | &Coro::schedule while &_next; |
143 | &Coro::schedule while &_next; |
129 | |
144 | |
130 | $_[0] |
145 | $_[0] |
131 | } |
146 | } |
… | |
… | |
147 | into the Event dispatcher. |
162 | into the Event dispatcher. |
148 | |
163 | |
149 | =cut |
164 | =cut |
150 | |
165 | |
151 | sub sweep { |
166 | sub sweep { |
152 | Event::one_event(0); # for now |
167 | Event::one_event 0; # for now |
153 | } |
168 | } |
154 | |
169 | |
155 | =item $result = loop([$timeout]) |
170 | =item $result = loop([$timeout]) |
156 | |
171 | |
157 | This is the version of C<loop> you should use instead of C<Event::loop> |
172 | This is the version of C<loop> you should use instead of C<Event::loop> |