… | |
… | |
1083 | integer between C<EV_MAXPRI> (default: C<2>) and C<EV_MINPRI> |
1083 | integer 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 |
1085 | before watchers with lower priority, but priority will not keep watchers |
1085 | before watchers with lower priority, but priority will not keep watchers |
1086 | from being executed (except for C<ev_idle> watchers). |
1086 | from being executed (except for C<ev_idle> watchers). |
1087 | |
1087 | |
1088 | This means that priorities are I<only> used for ordering callback |
|
|
1089 | invocation after new events have been received. This is useful, for |
|
|
1090 | example, to reduce latency after idling, or more often, to bind two |
|
|
1091 | watchers on the same event and make sure one is called first. |
|
|
1092 | |
|
|
1093 | If you need to suppress invocation when higher priority events are pending |
1088 | If you need to suppress invocation when higher priority events are pending |
1094 | you need to look at C<ev_idle> watchers, which provide this functionality. |
1089 | you need to look at C<ev_idle> watchers, which provide this functionality. |
1095 | |
1090 | |
1096 | You I<must not> change the priority of a watcher as long as it is active or |
1091 | You I<must not> change the priority of a watcher as long as it is active or |
1097 | pending. |
1092 | pending. |
1098 | |
|
|
1099 | The default priority used by watchers when no priority has been set is |
|
|
1100 | always C<0>, which is supposed to not be too high and not be too low :). |
|
|
1101 | |
1093 | |
1102 | Setting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is |
1094 | Setting a priority outside the range of C<EV_MINPRI> to C<EV_MAXPRI> is |
1103 | fine, as long as you do not mind that the priority value you query might |
1095 | fine, as long as you do not mind that the priority value you query might |
1104 | or might not have been clamped to the valid range. |
1096 | or might not have been clamped to the valid range. |
|
|
1097 | |
|
|
1098 | The default priority used by watchers when no priority has been set is |
|
|
1099 | always C<0>, which is supposed to not be too high and not be too low :). |
|
|
1100 | |
|
|
1101 | See L<WATCHER PRIORITIES>, below, for a more thorough treatment of |
|
|
1102 | priorities. |
1105 | |
1103 | |
1106 | =item ev_invoke (loop, ev_TYPE *watcher, int revents) |
1104 | =item ev_invoke (loop, ev_TYPE *watcher, int revents) |
1107 | |
1105 | |
1108 | Invoke the C<watcher> with the given C<loop> and C<revents>. Neither |
1106 | Invoke the C<watcher> with the given C<loop> and C<revents>. Neither |
1109 | C<loop> nor C<revents> need to be valid as long as the watcher callback |
1107 | C<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 | |
|
|
1190 | Many event loops support I<watcher priorities>, which are usually small |
|
|
1191 | integers that influence the ordering of event callback invocation |
|
|
1192 | between watchers in some way, all else being equal. |
|
|
1193 | |
|
|
1194 | In libev, Watcher priorities can be set using C<ev_set_priority>. See its |
|
|
1195 | description for the more technical details such as the actual priority |
|
|
1196 | range. |
|
|
1197 | |
|
|
1198 | There are two common ways how these these priorities are being interpreted |
|
|
1199 | by event loops: |
|
|
1200 | |
|
|
1201 | In the more common lock-out model, higher priorities "lock out" invocation |
|
|
1202 | of lower priority watchers, which means as long as higher priority |
|
|
1203 | watchers receive events, lower priority watchers are not being invoked. |
|
|
1204 | |
|
|
1205 | The less common only-for-ordering model uses priorities solely to order |
|
|
1206 | callback invocation within a single event loop iteration: Higher priority |
|
|
1207 | watchers are invoked before lower priority ones, but they all get invoked |
|
|
1208 | before polling for new events. |
|
|
1209 | |
|
|
1210 | Libev uses the second (only-for-ordering) model for all its watchers |
|
|
1211 | except for idle watchers (which use the lock-out model). |
|
|
1212 | |
|
|
1213 | The rationale behind this is that implementing the lock-out model for |
|
|
1214 | watchers is not well supported by most kernel interfaces, and most event |
|
|
1215 | libraries will just poll for the same events again and again as long as |
|
|
1216 | their callbacks have not been executed, which is very inefficient in the |
|
|
1217 | common case of one high-priority watcher locking out a mass of lower |
|
|
1218 | priority ones. |
|
|
1219 | |
|
|
1220 | Static (ordering) priorities are most useful when you have two or more |
|
|
1221 | watchers handling the same resource: a typical usage example is having an |
|
|
1222 | C<ev_io> watcher to receive data, and an associated C<ev_timer> to handle |
|
|
1223 | timeouts. Under load, data might be received while the program handles |
|
|
1224 | other jobs, but since timers normally get invoked first, the timeout |
|
|
1225 | handler will be executed before checking for data. In that case, giving |
|
|
1226 | the timer a lower priority than the I/O watcher ensures that I/O will be |
|
|
1227 | handled first even under adverse conditions (which is usually, but not |
|
|
1228 | always, what you want). |
|
|
1229 | |
|
|
1230 | Since idle watchers use the "lock-out" model, meaning that idle watchers |
|
|
1231 | will only be executed when no same or higher priority watchers have |
|
|
1232 | received events, they can be used to implement the "lock-out" model when |
|
|
1233 | required. |
|
|
1234 | |
|
|
1235 | For example, to emulate how many other event libraries handle priorities, |
|
|
1236 | you can associate an C<ev_idle> watcher to each such watcher, and in |
|
|
1237 | the normal watcher callback, you just start the idle watcher. The real |
|
|
1238 | processing is done in the idle watcher callback. This causes libev to |
|
|
1239 | continously poll and process kernel event data for the watcher, but when |
|
|
1240 | the lock-out case is known to be rare (which in turn is rare :), this is |
|
|
1241 | workable. |
|
|
1242 | |
|
|
1243 | Usually, however, the lock-out model implemented that way will perform |
|
|
1244 | miserably under the type of load it was designed to handle. In that case, |
|
|
1245 | it might be preferable to stop the real watcher before starting the |
|
|
1246 | idle watcher, so the kernel will not have to process the event in case |
|
|
1247 | the actual processing will be delayed for considerable time. |
|
|
1248 | |
|
|
1249 | Here is an example of an I/O watcher that should run at a strictly lower |
|
|
1250 | priority than the default, and which should only process data when no |
|
|
1251 | other 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 | |
|
|
1285 | In the "real" world, it might also be beneficial to start a timer, so that |
|
|
1286 | low-priority connections can not be locked out forever under load. This |
|
|
1287 | enables your program to keep a lower latency for important connections |
|
|
1288 | during short periods of high load, while not completely locking out less |
|
|
1289 | important ones. |
1189 | |
1290 | |
1190 | |
1291 | |
1191 | =head1 WATCHER TYPES |
1292 | =head1 WATCHER TYPES |
1192 | |
1293 | |
1193 | This section describes each watcher in detail, but will not repeat |
1294 | This section describes each watcher in detail, but will not repeat |
… | |
… | |
1582 | If the timer is started but non-repeating, stop it (as if it timed out). |
1683 | If the timer is started but non-repeating, stop it (as if it timed out). |
1583 | |
1684 | |
1584 | If the timer is repeating, either start it if necessary (with the |
1685 | If the timer is repeating, either start it if necessary (with the |
1585 | C<repeat> value), or reset the running timer to the C<repeat> value. |
1686 | C<repeat> value), or reset the running timer to the C<repeat> value. |
1586 | |
1687 | |
1587 | This sounds a bit complicated, see "Be smart about timeouts", above, for a |
1688 | This sounds a bit complicated, see L<Be smart about timeouts>, above, for a |
1588 | usage example. |
1689 | usage example. |
1589 | |
1690 | |
1590 | =item ev_tstamp repeat [read-write] |
1691 | =item ev_tstamp repeat [read-write] |
1591 | |
1692 | |
1592 | The current C<repeat> value. Will be used each time the watcher times out |
1693 | The current C<repeat> value. Will be used each time the watcher times out |