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

Comparing libev/ev.pod (file contents):
Revision 1.231 by root, Wed Apr 15 19:35:53 2009 UTC vs.
Revision 1.234 by root, Thu Apr 16 07:49:23 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
1088This means that priorities are I<only> used for ordering callback
1089invocation after new events have been received. This is useful, for
1090example, to reduce latency after idling, or more often, to bind two
1091watchers on the same event and make sure one is called first.
1092
1093If you need to suppress invocation when higher priority events are pending 1088If you need to suppress invocation when higher priority events are pending
1094you 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.
1095 1090
1096You 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
1097pending. 1092pending.
1098
1099The default priority used by watchers when no priority has been set is
1100always C<0>, which is supposed to not be too high and not be too low :).
1101 1093
1102Setting 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
1103fine, 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
1104or 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.
1105 1103
1106=item ev_invoke (loop, ev_TYPE *watcher, int revents) 1104=item ev_invoke (loop, ev_TYPE *watcher, int revents)
1107 1105
1108Invoke 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
1109C<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
1184 t2_cb (EV_P_ ev_timer *w, int revents) 1182 t2_cb (EV_P_ ev_timer *w, int revents)
1185 { 1183 {
1186 struct my_biggy big = (struct my_biggy * 1184 struct my_biggy big = (struct my_biggy *
1187 (((char *)w) - offsetof (struct my_biggy, t2)); 1185 (((char *)w) - offsetof (struct my_biggy, t2));
1188 } 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.
1189 1290
1190 1291
1191=head1 WATCHER TYPES 1292=head1 WATCHER TYPES
1192 1293
1193This section describes each watcher in detail, but will not repeat 1294This section describes each watcher in detail, but will not repeat
1582If the timer is started but non-repeating, stop it (as if it timed out). 1683If the timer is started but non-repeating, stop it (as if it timed out).
1583 1684
1584If the timer is repeating, either start it if necessary (with the 1685If the timer is repeating, either start it if necessary (with the
1585C<repeat> value), or reset the running timer to the C<repeat> value. 1686C<repeat> value), or reset the running timer to the C<repeat> value.
1586 1687
1587This sounds a bit complicated, see "Be smart about timeouts", above, for a 1688This sounds a bit complicated, see L<Be smart about timeouts>, above, for a
1588usage example. 1689usage example.
1589 1690
1590=item ev_tstamp repeat [read-write] 1691=item ev_tstamp repeat [read-write]
1591 1692
1592The current C<repeat> value. Will be used each time the watcher times out 1693The current C<repeat> value. Will be used each time the watcher times out
3993involves iterating over all running async watchers or all signal numbers. 4094involves iterating over all running async watchers or all signal numbers.
3994 4095
3995=back 4096=back
3996 4097
3997 4098
4099=head1 GLOSSARY
4100
4101=over 4
4102
4103=item active
4104
4105A watcher is active as long as it has been started (has been attached to
4106an event loop) but not yet stopped (disassociated from the event loop).
4107
4108=item application
4109
4110In this document, an application is whatever is using libev.
4111
4112=item callback
4113
4114The address of a function that is called when some event has been
4115detected. Callbacks are being passed the event loop, the watcher that
4116received the event, and the actual event bitset.
4117
4118=item callback invocation
4119
4120The act of calling the callback associated with a watcher.
4121
4122=item event
4123
4124A change of state of some external event, such as data now being available
4125for reading on a file descriptor, time having passed or simply not having
4126any other events happening anymore.
4127
4128In libev, events are represented as single bits (such as C<EV_READ> or
4129C<EV_TIMEOUT>).
4130
4131=item event library
4132
4133A software package implementing an event model and loop.
4134
4135=item event loop
4136
4137An entity that handles and processes external events and converts them
4138into callback invocations.
4139
4140=item event model
4141
4142The model used to describe how an event loop handles and processes
4143watchers and events.
4144
4145=item pending
4146
4147A watcher is pending as soon as the corresponding event has been detected,
4148and stops being pending as soon as the watcher will be invoked or its
4149pending status is explicitly cleared by the application.
4150
4151A watcher can be pending, but not active. Stopping a watcher also clears
4152its pending status.
4153
4154=item real time
4155
4156The physical time that is observed. It is apparently strictly monotonic :)
4157
4158=item wall-clock time
4159
4160The time and date as shown on clocks. Unlike real time, it can actually
4161be wrong and jump forwards and backwards, e.g. when the you adjust your
4162clock.
4163
4164=item watcher
4165
4166A data structure that describes interest in certain events. Watchers need
4167to be started (attached to an event loop) before they can receive events.
4168
4169=item watcher invocation
4170
4171The act of calling the callback associated with a watcher.
4172
4173=back
4174
3998=head1 AUTHOR 4175=head1 AUTHOR
3999 4176
4000Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. 4177Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson.
4001 4178

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines