From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id C94057F720 for ; Thu, 17 Apr 2014 12:58:22 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of mshinwell@janestreet.com) identity=pra; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mshinwell@janestreet.com"; x-sender="mshinwell@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of mshinwell@janestreet.com designates 38.105.200.229 as permitted sender) identity=mailfrom; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mshinwell@janestreet.com"; x-sender="mshinwell@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mxout3.mail.janestreet.com) identity=helo; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="mshinwell@janestreet.com"; x-sender="postmaster@mxout3.mail.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Al8BAImzT1MmacjlnGdsb2JhbABZhBjDSoEdHg4BAQEBAQYWCTyCJQEBBUABATcBDwsLDS4iEgEFARwGEwiHYAMRAwKbQIsYhFUBBZguIoZdEQaOC1cHhDiYbpBcGCmEYYFp X-IPAS-Result: Al8BAImzT1MmacjlnGdsb2JhbABZhBjDSoEdHg4BAQEBAQYWCTyCJQEBBUABATcBDwsLDS4iEgEFARwGEwiHYAMRAwKbQIsYhFUBBZguIoZdEQaOC1cHhDiYbpBcGCmEYYFp X-IronPort-AV: E=Sophos;i="4.97,878,1389740400"; d="scan'208";a="57546449" Received: from mx5.janestreet.com (HELO mxout3.mail.janestreet.com) ([38.105.200.229]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 17 Apr 2014 12:58:15 +0200 Received: from tot-oib-smtp1.delacy.com ([172.27.22.15] helo=tot-smtp) by mxout3.mail.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1Wak0F-0004K0-Qz for caml-list@inria.fr; Thu, 17 Apr 2014 06:57:11 -0400 Received: from tot-dmz-mxgoog1.delacy.com ([172.27.224.14] helo=mxgoog2.janestreet.com) by tot-smtp with esmtps (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1Wak0F-0007LI-Py for caml-list@inria.fr; Thu, 17 Apr 2014 06:57:11 -0400 Received: from mail-ve0-f171.google.com ([209.85.128.171]) by mxgoog2.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1Wak0F-0008T1-NS for caml-list@inria.fr; Thu, 17 Apr 2014 06:57:11 -0400 Received: by mail-ve0-f171.google.com with SMTP id jy13so299584veb.30 for ; Thu, 17 Apr 2014 03:57:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=BmTcMNVFaliFXt+uhKaffvLO24Nvmu/fwAqe/4nGPwE=; b=1JFr1o6vJmIf4RlGSQheeFEBTd7mUC7MP017bDtBWHINPAvx3bLPYfDITq29F0hIAu j5wRVCWhDYNZSOma9W+nrYSwwu0DONBGzSHQvHox+5y8J9mkpu3yibMoMr6mgmP6fsaI VG6m2zIRokHmQ3QpCCHj+j2vvhesJoPAUaCcQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=BmTcMNVFaliFXt+uhKaffvLO24Nvmu/fwAqe/4nGPwE=; b=fLL382P553iYH44eZaUYb3HaxB1GQKiOXaWxzSRLTxDuLiinGCurYev2VfBLD5Rcia 7F5FFWZR3U6dthG1gqeNuUphaylRFA5mB1BM0ovG/GqA4fVMuiWY2s6hE0sh6wZvdry2 LyMFkURd9sLxhQa/Jb+stR/UC301YZ0AAQI115JuYJjjpoOG/Mr3apbnsSGY8NhmGxbz ppIzo8Fg/zezfWwXQbiifz6bDldO0nXplmEG7Ovq5Itx0Ku18ibcnEVtnyifCGbxk2iH M+xCHWK1cdkCHs2chJA6gy/yFF1GVHDl9QUPBUwj7m3J+55an6OTySZS42cop3quGk+J XxJw== X-Gm-Message-State: ALoCoQlswZmMgGQLFWBOhzS2ZVpqSRPSWWgyQjlA/HQH1laTCEhh3B+yixuWfuzVZzPLBjhwIOyHodXFRRCtSmIHdOX55zfzgL4f/I40xzIgTpOa2kuFq76kz5ZKaKVhs1tIUFUS1fgC X-Received: by 10.52.164.237 with SMTP id yt13mr5995744vdb.18.1397732231553; Thu, 17 Apr 2014 03:57:11 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.52.164.237 with SMTP id yt13mr5995738vdb.18.1397732231456; Thu, 17 Apr 2014 03:57:11 -0700 (PDT) Received: by 10.58.218.9 with HTTP; Thu, 17 Apr 2014 03:57:11 -0700 (PDT) In-Reply-To: <20140417085226.GA18497@frosties> References: <20140417085226.GA18497@frosties> Date: Thu, 17 Apr 2014 11:57:11 +0100 Message-ID: From: Mark Shinwell To: Goswin von Brederlow Cc: "caml-list@inria.fr" Content-Type: text/plain; charset=ISO-8859-1 X-Validation-by: mshinwell@janestreet.com Subject: Re: [Caml-list] How does the Thread module work with the Gc? On 17 April 2014 09:52, Goswin von Brederlow 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