Best way to use async library jobs with the event-loop

Marc Lehmann schmorp at schmorp.de
Wed Apr 2 20:38:17 CEST 2014


On Tue, Apr 01, 2014 at 02:10:24PM +0200, Yoran Heling <info at yorhel.nl> wrote:
> On 2014-04-01, Konstantin Olkhovskiy wrote:
> > 2014-04-01 15:51 GMT+04:00 utku genç <utkugenc at gmail.com>:
> > > libuv has a convenience function for using possible blocking library
> > > operations with event-loop paradigm*. What, do you think, is the best way
> > > to implement such functionality for libev?
> > >
> > 
> > Probably best way is to use libeio [1]. Complete example of libev
> > integration is available in the documentation [2].
> 
> Note that libeio isn't suitable as a general purpose thread pool because
> it configures a rather small stack for the worker threads.

Correct, although one can configure the stack size using the EIO_STACKSIZE
symbol, even then libeio isn't a generic threadpool, as mixing cpu or
event-intensive jobs with I/O-intensive jobs in the same threadpool is
bad.

(libeio does come with it's own threadpool called etp, but that isn't yet
separated from libeio).

> I wrote a generic thread pool for libev some time ago and dubbed it
> evtp[1].

Extra points for supporting EV_MULTIPLICITY :)

A problem is there though: your threadpool has the classical problem of
inheriting the signal mask, and thus wreaks havoc with signal handling (by
introducing race conditions with event libs such as libev and by making it
impossible to block signals).

Consider adding something like this around your pthread_create call:

  sigfillset (&fullsigset);
  pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);

  int r = pthread_create(&thread, &attr, evtp_thread, tp);

  pthread_sigmask (SIG_SETMASK, &oldsigset, 0);

-- 
                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