caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Romain Beauxis <toots@rastageeks.org>
Cc: caml-list@yquem.inria.fr, Goswin von Brederlow <goswin-v-b@web.de>
Subject: Re: [Caml-list] Smart ways to implement worker threads
Date: Sat, 17 Jul 2010 17:52:40 +0200	[thread overview]
Message-ID: <87vd8eqf5j.fsf@frosties.localdomain> (raw)
In-Reply-To: <201007171020.58902.toots@rastageeks.org> (Romain Beauxis's message of "Sat, 17 Jul 2010 10:20:58 -0400")

Romain Beauxis <toots@rastageeks.org> writes:

> Le samedi 17 juillet 2010 05:52:31, Goswin von Brederlow a écrit :
>> Now I have a queue that I can include in select. The take function can
>> be used by the worker threads. The main thread can use either take_all
>> or process to save on syscalls or stick with take to ensure the queue
>> does not starve the other FDs.
>> 
>> Also I think I can use the EventFD to terminate a queue and wake up all
>> listeners. Closing the EventFD should wake up any thread stuck in read
>> and prevent any threads from becoming stuck in the future. Haven't
>> tested that yet though.
>
> I don't see why you want to use EventFD and not Condition.wait/signal. In 
> particular, Condition.broadcast does exactly the last thing you mention.

Not quite. With a Condition.wait I can only wait on exactly one
condition. With the EventFD I can use the Unix.file_descr together with
other Unix.file_descr in Unix.select. That means I can wait for both new
input to come in over the sockets or for results to become available in
the queue with a single thread, which gets rid of quite some Mutexes.

> Now, I don't understand why you asked for related modules and and the end 
> discard all of them and post your own implementation. Your issue is not that 
> complex you could have done that in the first place...

I was fishing for solutions. There were some good ones and tanks for all
the replies. They were not wasted. But none of them seem like a really
good fit to the code as I have it in mind. Some reasons why that is so I
only see now, after tasting the waters and testing things out.

For example, and the reason why EventFD seem like the way to go NOW, I
noticed that replies to requests are sometimes too large to be written
out in a single go or simply come too fast to be written out without
blocking. That means I have to write them out in multiple chunks and
then sockets need to be protected so chunks from multiple replies don't
get mixed together from different threads and so on.

If I use a queue with EventFD instead of Condition.t then I can add the
queue to the select loop and any thread can drop a reply job into that
queue. The select thread will wake up, take the reply job, add the data
to the sockets outgoing buffer and add the sockets FD to the select call
and wait for it to be ready for writing. All without needing Mutexes to
protect the socket or signals to wake up and restart the select since
the select thread and only the select thread will be handling the
sockets.

MfG
        Goswin

PS: I still haven't 100% decided yet what the best solution is but
EventFD seems to lead currently. So if anyone has other ideas keep them
coming.


      reply	other threads:[~2010-07-17 15:52 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-14 16:09 Goswin von Brederlow
2010-07-15 15:58 ` [Caml-list] " Rich Neswold
2010-07-15 16:19   ` David McClain
2010-07-15 17:16   ` Ashish Agarwal
2010-07-15 18:24   ` Goswin von Brederlow
2010-07-15 18:37     ` David McClain
2010-07-15 18:40     ` David McClain
2010-07-15 19:56     ` Rich Neswold
2010-07-16  4:02       ` Goswin von Brederlow
2010-07-16  4:23         ` Rich Neswold
2010-07-16 13:02           ` Goswin von Brederlow
2010-07-16 14:40             ` Dawid Toton
2010-07-16 16:18             ` [Caml-list] " Rich Neswold
2010-07-17 17:53               ` Eray Ozkural
2010-07-20  4:54             ` Satoshi Ogasawara
2010-07-17 18:34         ` Eray Ozkural
2010-07-17 19:35           ` Goswin von Brederlow
2010-07-17 22:00             ` Eray Ozkural
2010-07-15 16:32 ` Romain Beauxis
2010-07-15 17:46   ` Goswin von Brederlow
2010-07-15 18:44     ` Romain Beauxis
2010-07-16  3:52       ` Goswin von Brederlow
2010-07-16  4:19         ` Romain Beauxis
2010-07-16 13:05           ` Goswin von Brederlow
2010-07-16 13:20             ` Romain Beauxis
2010-07-17  9:07               ` Goswin von Brederlow
2010-07-17 13:51                 ` Romain Beauxis
2010-07-17 14:08                   ` Goswin von Brederlow
2010-07-17  9:52 ` Goswin von Brederlow
2010-07-17 14:20   ` Romain Beauxis
2010-07-17 15:52     ` Goswin von Brederlow [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=87vd8eqf5j.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=toots@rastageeks.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;
as well as URLs for NNTP newsgroup(s).