6.4 检查事件状态

    1. #include <stdio.h>
    2. /* Change the callback and callback_arg of 'ev', which must not be
    3. * pending. */
    4. int replace_callback(struct event *ev, event_callback_fn new_callback,
    5. void *new_callback_arg)
    6. {
    7. struct event_base *base;
    8. int pending;
    9. pending = event_pending(ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT,
    10. NULL);
    11. if (pending) {
    12. /* We want to catch this here so that we do not re-assign a
    13. * pending event. That would be very very bad. */
    14. fprintf(stderr,
    15. return -1;
    16. }
    17. event_get_assignment(ev, &base, &fd, &events,
    18. NULL /* ignore old callback */ ,
    19. NULL /* ignore old callback argument */);
    20. event_assign(ev, base, fd, events, new_callback, new_callback_arg);
    21. return 0;