unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: gensym thread safety
Date: Wed, 28 Jul 2004 08:58:20 +1000	[thread overview]
Message-ID: <87smbd12ib.fsf@zip.com.au> (raw)

[-- Attachment #1: Type: text/plain, Size: 383 bytes --]

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.


[-- Attachment #2: symbols.c.gensym.diff --]
[-- Type: text/plain, Size: 1501 bytes --]

--- 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"
 }
 

[-- Attachment #3: Type: text/plain, Size: 143 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel

                 reply	other threads:[~2004-07-27 22:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87smbd12ib.fsf@zip.com.au \
    --to=user42@zip.com.au \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).