… | |
… | |
989 | See also the locking example in the C<THREADS> section later in this |
989 | See also the locking example in the C<THREADS> section later in this |
990 | document. |
990 | document. |
991 | |
991 | |
992 | =item ev_set_userdata (loop, void *data) |
992 | =item ev_set_userdata (loop, void *data) |
993 | |
993 | |
994 | =item ev_userdata (loop) |
994 | =item void *ev_userdata (loop) |
995 | |
995 | |
996 | Set and retrieve a single C<void *> associated with a loop. When |
996 | Set and retrieve a single C<void *> associated with a loop. When |
997 | C<ev_set_userdata> has never been called, then C<ev_userdata> returns |
997 | C<ev_set_userdata> has never been called, then C<ev_userdata> returns |
998 | C<0>. |
998 | C<0>. |
999 | |
999 | |
… | |
… | |
3367 | loop!). |
3367 | loop!). |
3368 | |
3368 | |
3369 | =back |
3369 | =back |
3370 | |
3370 | |
3371 | |
3371 | |
|
|
3372 | =head1 COMMON OR USEFUL IDIOMS (OR BOTH) |
|
|
3373 | |
|
|
3374 | This section explains some common idioms that are not immediately |
|
|
3375 | obvious. Note that examples are sprinkled over the whole manual, and this |
|
|
3376 | section only contains stuff that wouldn't fit anywhere else. |
|
|
3377 | |
|
|
3378 | =over 4 |
|
|
3379 | |
|
|
3380 | =item Model/nested event loop invocations and exit conditions. |
|
|
3381 | |
|
|
3382 | Often (especially in GUI toolkits) there are places where you have |
|
|
3383 | I<modal> interaction, which is most easily implemented by recursively |
|
|
3384 | invoking C<ev_run>. |
|
|
3385 | |
|
|
3386 | This brings the problem of exiting - a callback might want to finish the |
|
|
3387 | main C<ev_run> call, but not the nested one (e.g. user clicked "Quit", but |
|
|
3388 | a modal "Are you sure?" dialog is still waiting), or just the nested one |
|
|
3389 | and not the main one (e.g. user clocked "Ok" in a modal dialog), or some |
|
|
3390 | other combination: In these cases, C<ev_break> will not work alone. |
|
|
3391 | |
|
|
3392 | The solution is to maintain "break this loop" variable for each C<ev_run> |
|
|
3393 | invocation, and use a loop around C<ev_run> until the condition is |
|
|
3394 | triggered, using C<EVRUN_ONCE>: |
|
|
3395 | |
|
|
3396 | // main loop |
|
|
3397 | int exit_main_loop = 0; |
|
|
3398 | |
|
|
3399 | while (!exit_main_loop) |
|
|
3400 | ev_run (EV_DEFAULT_ EVRUN_ONCE); |
|
|
3401 | |
|
|
3402 | // in a model watcher |
|
|
3403 | int exit_nested_loop = 0; |
|
|
3404 | |
|
|
3405 | while (!exit_nested_loop) |
|
|
3406 | ev_run (EV_A_ EVRUN_ONCE); |
|
|
3407 | |
|
|
3408 | To exit from any of these loops, just set the corresponding exit variable: |
|
|
3409 | |
|
|
3410 | // exit modal loop |
|
|
3411 | exit_nested_loop = 1; |
|
|
3412 | |
|
|
3413 | // exit main program, after modal loop is finished |
|
|
3414 | exit_main_loop = 1; |
|
|
3415 | |
|
|
3416 | // exit both |
|
|
3417 | exit_main_loop = exit_nested_loop = 1; |
|
|
3418 | |
|
|
3419 | =back |
|
|
3420 | |
|
|
3421 | |
3372 | =head1 LIBEVENT EMULATION |
3422 | =head1 LIBEVENT EMULATION |
3373 | |
3423 | |
3374 | Libev offers a compatibility emulation layer for libevent. It cannot |
3424 | Libev offers a compatibility emulation layer for libevent. It cannot |
3375 | emulate the internals of libevent, so here are some usage hints: |
3425 | emulate the internals of libevent, so here are some usage hints: |
3376 | |
3426 | |
3377 | =over 4 |
3427 | =over 4 |
|
|
3428 | |
|
|
3429 | =item * Only the libevent-1.4.1-beta API is being emulated. |
|
|
3430 | |
|
|
3431 | This was the newest libevent version available when libev was implemented, |
|
|
3432 | and is still mostly uncanged in 2010. |
3378 | |
3433 | |
3379 | =item * Use it by including <event.h>, as usual. |
3434 | =item * Use it by including <event.h>, as usual. |
3380 | |
3435 | |
3381 | =item * The following members are fully supported: ev_base, ev_callback, |
3436 | =item * The following members are fully supported: ev_base, ev_callback, |
3382 | ev_arg, ev_fd, ev_res, ev_events. |
3437 | ev_arg, ev_fd, ev_res, ev_events. |
… | |
… | |
3417 | Care has been taken to keep the overhead low. The only data member the C++ |
3472 | Care has been taken to keep the overhead low. The only data member the C++ |
3418 | classes add (compared to plain C-style watchers) is the event loop pointer |
3473 | classes add (compared to plain C-style watchers) is the event loop pointer |
3419 | that the watcher is associated with (or no additional members at all if |
3474 | that the watcher is associated with (or no additional members at all if |
3420 | you disable C<EV_MULTIPLICITY> when embedding libev). |
3475 | you disable C<EV_MULTIPLICITY> when embedding libev). |
3421 | |
3476 | |
3422 | Currently, functions, and static and non-static member functions can be |
3477 | Currently, functions, static and non-static member functions and classes |
3423 | used as callbacks. Other types should be easy to add as long as they only |
3478 | with C<operator ()> can be used as callbacks. Other types should be easy |
3424 | need one additional pointer for context. If you need support for other |
3479 | to add as long as they only need one additional pointer for context. If |
3425 | types of functors please contact the author (preferably after implementing |
3480 | you need support for other types of functors please contact the author |
3426 | it). |
3481 | (preferably after implementing it). |
3427 | |
3482 | |
3428 | Here is a list of things available in the C<ev> namespace: |
3483 | Here is a list of things available in the C<ev> namespace: |
3429 | |
3484 | |
3430 | =over 4 |
3485 | =over 4 |
3431 | |
3486 | |