ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.232 by root, Thu Apr 16 06:17:26 2009 UTC vs.
Revision 1.233 by root, Thu Apr 16 07:32:51 2009 UTC

1083integer between C<EV_MAXPRI> (default: C<2>) and C<EV_MINPRI> 1083integer between C<EV_MAXPRI> (default: C<2>) and C<EV_MINPRI>
1084(default: C<-2>). Pending watchers with higher priority will be invoked 1084(default: C<-2>). Pending watchers with higher priority will be invoked
1085before watchers with lower priority, but priority will not keep watchers 1085before watchers with lower priority, but priority will not keep watchers
1086from being executed (except for C<ev_idle> watchers). 1086from being executed (except for C<ev_idle> watchers).
1087 1087
1088See L<
1089
1090This means that priorities are I<only> used for ordering callback
1091invocation after new events have been received. This is useful, for
1092example, to reduce latency after idling, or more often, to bind two
1093watchers on the same event and make sure one is called first.
1094
1095If you need to suppress invocation when higher priority events are pending 1088If you need to suppress invocation when higher priority events are pending
1096you need to look at C<ev_idle> watchers, which provide this functionality. 1089you need to look at C<ev_idle> watchers, which provide this functionality.
1097 1090
1098You I<must not> change the priority of a watcher as long as it is active or 1091You I<must not> change the priority of a watcher as long as it is active or
1099pending. 1092pending.
1100
1101The default priority used by watchers when no priority has been set is
1102always C<0>, which is supposed to not be too high and not be too low :).
1103 1093
1104Setting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is 1094Setting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is
1105fine, as long as you do not mind that the priority value you query might 1095fine, as long as you do not mind that the priority value you query might
1106or might not have been clamped to the valid range. 1096or might not have been clamped to the valid range.
1097
1098The default priority used by watchers when no priority has been set is
1099always C<0>, which is supposed to not be too high and not be too low :).
1100
1101See L<WATCHER PRIORITIES>, below, for a more thorough treatment of
1102priorities.
1107 1103
1108=item ev_invoke (loop, ev_TYPE *watcher, int revents) 1104=item ev_invoke (loop, ev_TYPE *watcher, int revents)
1109 1105
1110Invoke the C<watcher> with the given C<loop> and C<revents>. Neither 1106Invoke the C<watcher> with the given C<loop> and C<revents>. Neither
1111C<loop> nor C<revents> need to be valid as long as the watcher callback 1107C<loop> nor C<revents> need to be valid as long as the watcher callback
1186 t2_cb (EV_P_ ev_timer *w, int revents) 1182 t2_cb (EV_P_ ev_timer *w, int revents)
1187 { 1183 {
1188 struct my_biggy big = (struct my_biggy * 1184 struct my_biggy big = (struct my_biggy *
1189 (((char *)w) - offsetof (struct my_biggy, t2)); 1185 (((char *)w) - offsetof (struct my_biggy, t2));
1190 } 1186 }
1187
1188=head2 WATCHER PRIORITY MODELS
1189
1190Many event loops support I<watcher priorities>, which are usually small
1191integers that influence the ordering of event callback invocation
1192between watchers in some way, all else being equal.
1193
1194In libev, Watcher priorities can be set using C<ev_set_priority>. See its
1195description for the more technical details such as the actual priority
1196range.
1197
1198There are two common ways how these these priorities are being interpreted
1199by event loops:
1200
1201In the more common lock-out model, higher priorities "lock out" invocation
1202of lower priority watchers, which means as long as higher priority
1203watchers receive events, lower priority watchers are not being invoked.
1204
1205The less common only-for-ordering model uses priorities solely to order
1206callback invocation within a single event loop iteration: Higher priority
1207watchers are invoked before lower priority ones, but they all get invoked
1208before polling for new events.
1209
1210Libev uses the second (only-for-ordering) model for all its watchers
1211except for idle watchers (which use the lock-out model).
1212
1213The rationale behind this is that implementing the lock-out model for
1214watchers is not well supported by most kernel interfaces, and most event
1215libraries will just poll for the same events again and again as long as
1216their callbacks have not been executed, which is very inefficient in the
1217common case of one high-priority watcher locking out a mass of lower
1218priority ones.
1219
1220Static (ordering) priorities are most useful when you have two or more
1221watchers handling the same resource: a typical usage example is having an
1222C<ev_io> watcher to receive data, and an associated C<ev_timer> to handle
1223timeouts. Under load, data might be received while the program handles
1224other jobs, but since timers normally get invoked first, the timeout
1225handler will be executed before checking for data. In that case, giving
1226the timer a lower priority than the I/O watcher ensures that I/O will be
1227handled first even under adverse conditions (which is usually, but not
1228always, what you want).
1229
1230Since idle watchers use the "lock-out" model, meaning that idle watchers
1231will only be executed when no same or higher priority watchers have
1232received events, they can be used to implement the "lock-out" model when
1233required.
1234
1235For example, to emulate how many other event libraries handle priorities,
1236you can associate an C<ev_idle> watcher to each such watcher, and in
1237the normal watcher callback, you just start the idle watcher. The real
1238processing is done in the idle watcher callback. This causes libev to
1239continously poll and process kernel event data for the watcher, but when
1240the lock-out case is known to be rare (which in turn is rare :), this is
1241workable.
1242
1243Usually, however, the lock-out model implemented that way will perform
1244miserably under the type of load it was designed to handle. In that case,
1245it might be preferable to stop the real watcher before starting the
1246idle watcher, so the kernel will not have to process the event in case
1247the actual processing will be delayed for considerable time.
1248
1249Here is an example of an I/O watcher that should run at a strictly lower
1250priority than the default, and which should only process data when no
1251other events are pending:
1252
1253 ev_idle idle; // actual processing watcher
1254 ev_io io; // actual event watcher
1255
1256 static void
1257 io_cb (EV_P_ ev_io *w, int revents)
1258 {
1259 // stop the I/O watcher, we received the event, but
1260 // are not yet ready to handle it.
1261 ev_io_stop (EV_A_ w);
1262
1263 // start the idle watcher to ahndle the actual event.
1264 // it will not be executed as long as other watchers
1265 // with the default priority are receiving events.
1266 ev_idle_start (EV_A_ &idle);
1267 }
1268
1269 static void
1270 idle-cb (EV_P_ ev_idle *w, int revents)
1271 {
1272 // actual processing
1273 read (STDIN_FILENO, ...);
1274
1275 // have to start the I/O watcher again, as
1276 // we have handled the event
1277 ev_io_start (EV_P_ &io);
1278 }
1279
1280 // initialisation
1281 ev_idle_init (&idle, idle_cb);
1282 ev_io_init (&io, io_cb, STDIN_FILENO, EV_READ);
1283 ev_io_start (EV_DEFAULT_ &io);
1284
1285In the "real" world, it might also be beneficial to start a timer, so that
1286low-priority connections can not be locked out forever under load. This
1287enables your program to keep a lower latency for important connections
1288during short periods of high load, while not completely locking out less
1289important ones.
1191 1290
1192 1291
1193=head1 WATCHER TYPES 1292=head1 WATCHER TYPES
1194 1293
1195This section describes each watcher in detail, but will not repeat 1294This section describes each watcher in detail, but will not repeat

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines