Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Mark Shinwell <mshinwell@janestreet.com>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] How does the Thread module work with the Gc?
Date: Thu, 17 Apr 2014 11:57:11 +0100	[thread overview]
Message-ID: <CAM3Ki74LD0oN6LPD0Wqbz45Sdi3ZVrd3YF03zEr994QCqdFtZA@mail.gmail.com> (raw)
In-Reply-To: <20140417085226.GA18497@frosties>

On 17 April 2014 09:52, Goswin von Brederlow <goswin-v-b@web.de> wrote:
> How does ocaml switch threads when a signal occurs? What if a thread
> never enters a blocking section? Isn't there some other point where
> tasks can be switched other than enter/leave_blocking_section?
>
> I looked at the implementation for signals and they seem to set the
> allocation limit for the thread to 0 so the next allocation will
> trigger a Gc run. But how does that lead to a thread switch? What am I
> missing here?

Gah, this is a complicated part of the world.

It works as follows when there is no explicit call to release the
OCaml lock.  The ticker thread records a "preemption signal" and sets
the minor heap limit such that the next minor allocation goes to
[caml_garbage_collection].  At this point, pending signals are
processed; and then here is the piece I think you've missed: the
systhreads library has already registered a handler on that signal
(see otherlibs/systhreads/thread.ml) and that handler will cause
[Thread.yield] to be called.  This in turn calls the "yield" operation
in st_posix.h.  On Linux this does nothing; see the comment.  However,
that call (see otherlibs/systhreads/st_stubs.c) is also wrapped in an
enter/leave blocking section, which causes the mutex to be dropped.
At this time, if the stars align and the operating system scheduler is
willing, a thread switch may occur.  There are no guarantees about
fairness made in the runtime at this point (see Xavier's comment on
mantis 5373), so you may in fact find that sometimes a thread does not
yield.  (Also, code that never allocates will not yield unless it
explicitly drops the lock for some other reason.)

Mark

  reply	other threads:[~2014-04-17 10:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-17  8:52 Goswin von Brederlow
2014-04-17 10:57 ` Mark Shinwell [this message]
2014-04-17 22:10   ` Goswin von Brederlow

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=CAM3Ki74LD0oN6LPD0Wqbz45Sdi3ZVrd3YF03zEr994QCqdFtZA@mail.gmail.com \
    --to=mshinwell@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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