exposing activecnt

Marc Lehmann schmorp at schmorp.de
Wed Nov 24 13:34:41 CET 2010

On Wed, Nov 24, 2010 at 01:28:42AM -0800, Ryan Dahl <ry at tinyclouds.org> wrote:
> I need a function that runs similar to a ev_prepare watcher, in that
> it happens at the end of each iteration. Except I want it to also run
> at the end of the last iteration.

Well, what's the end of an iteration? The end is a check watcher, which is
guaranteed to be called at least once.

And even for prepare watchers, you can do that without activecnt, it seems,
by using a prepare watcher and another iteration.

When ev_run (ev_loop) returns (not due to an ev_break) then the activecnt
is a known constant value, namely 0. For ev_break, you know it's most
likely not zero, and even if, another loop call will tell you without
extra code.

> In my program I queue up writes throughout the iteration and at the
> end I write them to the sockets.  There might be a program that
> doesn't set up any watchers, which just wants to write something out -
> this does not hit an ev_prepare. If the program queues up a lot to
> write out, it may be that while writev-ing this buffer to the socket
> it gets a EAGAIN. So it needs to enable a ev_io watcher to wait until
> it can finish sending the buffer out.

That is a common pattern - but why would you need activecnt for that? As long
as the program computes and doesn't set watchers it will also not wait for
any events, so ev_run will not run, and thus not return.

No activecnt is necessary for that (in fact, the Coro perl module runs into
this pattern often, and doesn't need activecnt).

I suspect that the activecnt usage is actually a bug, and that whatever
problem you have is solved more correctly without it. I don't know for
sure if thats the case though, so please continue to explain why you need
activecnt, and I will continue to try to explain why you don't need it -
or when it is actually needed, expose it (I would be far less opposed to
some ev_has_watchers call that just returns a boolean, but I still think
chances are very high that it will only hide subtle bugs).

The reason I believe that you don't need it is that ev_run already tells
you that the count is 0 _by the act of returning_.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list