From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id CCB08BDDB for ; Sat, 3 Sep 2005 09:54:22 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j837sMi0016760 for ; Sat, 3 Sep 2005 09:54:22 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA20169 for ; Sat, 3 Sep 2005 09:54:21 +0200 (MET DST) Received: from mallaury.nerim.net (smtp-106-saturday.noc.nerim.net [62.4.17.106]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j837sLSO012719 for ; Sat, 3 Sep 2005 09:54:21 +0200 Received: from [192.168.1.2] (xleroy.net1.nerim.net [62.212.116.147]) by mallaury.nerim.net (Postfix) with ESMTP id E033F4F3BE; Sat, 3 Sep 2005 09:54:15 +0200 (CEST) Message-ID: <431956AC.1050008@inria.fr> Date: Sat, 03 Sep 2005 09:54:20 +0200 From: Xavier Leroy User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: yminsky@cs.cornell.edu Cc: Caml Mailing List Subject: Re: [Caml-list] Unix.localtime not threadsafe? References: <891bd339050902103460db1a55@mail.gmail.com> In-Reply-To: <891bd339050902103460db1a55@mail.gmail.com> X-Enigmail-Version: 0.91.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 431956AE.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 431956AD.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml's:01 alloc:01 alloc:01 threads:01 ocaml:01 threads:01 ocaml:01 unix:01 unix:01 standpoint:01 underlying:01 thread:02 thread:02 data:02 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 > I was looking at the Unix.localtime implementation, and it appears to > me that it's not threadsafe. I'm wondering if anyone can confirm. > > First off, the underlying localtime call is definitely not re-entrant. > The tm data structure is shared among all calls, leading to the > possibility of races. As others mentioned, the C library implementation of this function could use thread-local storage to avoid this particular race. I don't think this is guaranteed, though. > If I understand the way ocaml's locking works, another thread could > make a call to localtime where alloc_small is called. No, at least not another Caml thread: GC triggered from C code (e.g. alloc_small()) cannot cause context switches. So, from a Caml standpoint, the call to localtime() and the following allocation of its result are atomic. > If there are mixed C threads and Ocaml threads, then a call by the C > thread at that point could muck up the tm data structure before > switching back to OCaml, thus leading to bad data getting into the > tm data structure. Yes, this is possible in principle if 1- some non-Caml threads call localtime(), and 2- the implementation of that function does not use thread-local storage. - Xavier Leroy