unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* gensym thread safety
@ 2004-07-27 22:58 Kevin Ryde
  0 siblings, 0 replies; only message in thread
From: Kevin Ryde @ 2004-07-27 22:58 UTC (permalink / 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-07-27 22:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-27 22:58 gensym thread safety Kevin Ryde

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).