mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Davide Libenzi <davidel@xmailserver.org>
To: Paul P Komkoff Jr <i@stingr.net>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: How to implement multithreaded event loop ?
Date: Mon, 27 Dec 2004 23:05:29 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.58.0412272206510.25362@bigblue.dev.mdolabs.com> (raw)
In-Reply-To: <20041227200509.GD1035@stingr.sgu.ru>

On Mon, 27 Dec 2004, Paul P Komkoff Jr wrote:

> I am trying to implement a multithreaded event loop using epoll. So, I
> have 2 kind of events. First, there are conditions on fds I have
> (listener sockets, client connections, my connections to other
> clients). Second, I need to have some kind of priority queue into
> which I can push forthcoming timed events.
> 
> In case of single-threaded server, this is fairly trivial. I just need
> to make heap queue and add its 1st (i.e. minimal) element as timeout
> to each next epoll_wait call. When some condition breaks the wait, I
> can always do find_min and wait again with new timeout.
> 
> Things become complicated when I need to scale. So, without this
> timeout cruft, I can just add proper locking around my data structures
> but main epoll_wait loop is multithread-aware, e.g. it will retrieve
> different events for different waiting threads (to be absolutely fair
> with you, I did not implemented this part yet, but I assume that some
> combination of edge triggered + one shot epoll will do the trick).
> But, if using heap priority queue to manage timed events, I need to
> wake up each waiting thread when any event was added to the heap
> before one that was minimal.

One solution (if you really cannot scale using multiple processes) is the 
one that you're describing, where you have many threads doing epoll_wait. 
You have to be careful to not fetch too many event on a single wait 
(preferably one), otherwise you can end up with period of time with 
pending events on a single thread, while others are free to spin. If the 
timers that you are using are simply to timeout pending operations, the 
timer resolution does not need to be that high, so you could use a 
maximum timeout of a second or so for epoll_wait (and handle timers like 
you planned). Another solution would be to have a single epoll_wait loop 
thread that feeds your threads that are waiting on a mutex, protecting an 
event queue from which threads pull events. This will make timeout 
handling somehow easier, but delivering an event would mean a ctx switch 
to wake the epoll_wait thread, plus another one waking a processing 
thread.



> Another solution proposed by my poor brain is - to have alive thread
> which will handle this priority queue, and have one fifo fd in my
> epoll set dedicated to this purpose. Priority queue management thread
> will write single char to that fd when some timed event needs to be
> processed.
> 
> Doing some google search, I've found this message:
> http://www.uwsg.iu.edu/hypermail/linux/kernel/0210.3/2416.html
> Things can be much easier if there was timer kernel object (or its
> equivalent). Can anyone give me some advice - how I should solve this
> problem?

Like lingering IRQs, nobody cared :-) Linus also proposed a "signal fd", 
that could have been used for timers, but nobody showed interest either.


- Davide


      reply	other threads:[~2004-12-28  7:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-27 20:05 Paul P Komkoff Jr
2004-12-28  7:05 ` Davide Libenzi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0412272206510.25362@bigblue.dev.mdolabs.com \
    --to=davidel@xmailserver.org \
    --cc=i@stingr.net \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®