libev

Can you prioritize events in libev?


Let's say I have 2 sockets I am watching for events and I really want socket 1 to be preferred (even at the cost of starving socket 2). How would I do that with libev (I asked the same question about libuv here but it looks like libuv cannot do that) ?

As an example when working with libevent one can use :

int event_priority_set(struct event *event, int priority);

When multiple events of multiple priorities become active, the low-priority events are not run. Instead, Libevent runs the high priority events, then checks for events again. Only when no high-priority events are active are the low-priority events run.


Solution

  • Check libev function ev_set_priority():

    ev_set_priority (ev_TYPE *watcher, int priority)
    

    Set and query the priority of the watcher. The priority is a small integer between EV_MAXPRI (default: 2) and EV_MINPRI (default: -2). Pending watchers with higher priority will be invoked before watchers with lower priority, but priority will not keep watchers from being executed (except for ev_idle watchers).

    http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#GENERIC_WATCHER_FUNCTIONS