From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kevin Ryde Newsgroups: gmane.lisp.guile.devel Subject: gensym thread safety Date: Wed, 28 Jul 2004 08:58:20 +1000 Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: <87smbd12ib.fsf@zip.com.au> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: sea.gmane.org 1090969202 1251 80.91.224.253 (27 Jul 2004 23:00:02 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 27 Jul 2004 23:00:02 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Jul 28 00:59:50 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BpavF-00084P-00 for ; Wed, 28 Jul 2004 00:59:49 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BpayL-0001Ej-T6 for guile-devel@m.gmane.org; Tue, 27 Jul 2004 19:03:01 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1BpaxL-00013c-M8 for guile-devel@gnu.org; Tue, 27 Jul 2004 19:01:59 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1BpaxK-00013B-Hg for guile-devel@gnu.org; Tue, 27 Jul 2004 19:01:58 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BpaxJ-00012e-G4 for guile-devel@gnu.org; Tue, 27 Jul 2004 19:01:57 -0400 Original-Received: from [61.8.0.85] (helo=mailout2.pacific.net.au) by monty-python.gnu.org with esmtp (Exim 4.34) id 1Bpatz-00070W-IQ for guile-devel@gnu.org; Tue, 27 Jul 2004 18:58:32 -0400 Original-Received: from mailproxy2.pacific.net.au (mailproxy2.pacific.net.au [61.8.0.87]) by mailout2.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i6RMwUje022207 for ; Wed, 28 Jul 2004 08:58:30 +1000 Original-Received: from localhost (ppp20F1.dyn.pacific.net.au [61.8.32.241]) by mailproxy2.pacific.net.au (8.12.3/8.12.3/Debian-6.6) with ESMTP id i6RMwTgH031199 for ; Wed, 28 Jul 2004 08:58:29 +1000 Original-Received: from gg by localhost with local (Exim 3.36 #1 (Debian)) id 1Bpato-0000xP-00; Wed, 28 Jul 2004 08:58:20 +1000 Original-To: guile-devel@gnu.org Mail-Copies-To: never User-Agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3 (gnu/linux) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:3898 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:3898 --=-=-= The chance of two threads hitting the read+increment at the same time is obviously small, but a mutex will guarantee it. * symbols.c (scm_gensym): Use scm_i_misc_mutex around gensym_counter update, for thread safety. (gensym_counter): Move into scm_gensym which is its only user. (scm_init_symbols): No need to explicitly initialize gensym_counter. --=-=-= Content-Disposition: attachment; filename=symbols.c.gensym.diff --- symbols.c.~1.112.~ 2004-07-24 09:09:00.000000000 +1000 +++ symbols.c 2004-07-24 16:53:36.000000000 +1000 @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -264,8 +264,6 @@ #define MAX_PREFIX_LENGTH 30 -static int gensym_counter; - SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0, (SCM prefix), "Create a new symbol with a name constructed from a prefix and\n" @@ -275,6 +273,8 @@ "resetting the counter.") #define FUNC_NAME s_scm_gensym { + static int gensym_counter = 0; + char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN]; char *name = buf; size_t len; @@ -293,7 +293,14 @@ memcpy (name, SCM_STRING_CHARS (prefix), len); } { - int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]); + int n, n_digits; + + /* mutex in case another thread looks and incs at the exact same moment */ + scm_mutex_lock (&scm_i_misc_mutex); + n = gensym_counter++; + scm_mutex_unlock (&scm_i_misc_mutex); + + n_digits = scm_iint2str (count, 10, &name[len]); SCM res = scm_mem2symbol (name, len + n_digits); if (name != buf) free (name); @@ -414,7 +421,6 @@ void scm_init_symbols () { - gensym_counter = 0; #include "libguile/symbols.x" } --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://lists.gnu.org/mailman/listinfo/guile-devel --=-=-=--