diff options
author | Kevin Ryde <user42@zip.com.au> | 2004-07-31 01:08:20 +0000 |
---|---|---|
committer | Kevin Ryde <user42@zip.com.au> | 2004-07-31 01:08:20 +0000 |
commit | 7426a638b7017d7d67b647ae8b75f8ee26c17d80 (patch) | |
tree | 7c2a881044a12edccad91083f7627f7c3a746a11 /libguile/symbols.c | |
parent | da1e6e6710d6a803709214ccb32bff4dda652519 (diff) | |
download | guile-7426a638b7017d7d67b647ae8b75f8ee26c17d80.tar.gz |
(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.
Diffstat (limited to 'libguile/symbols.c')
-rw-r--r-- | libguile/symbols.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/libguile/symbols.c b/libguile/symbols.c index 1783a47bd..01261e2f1 100644 --- a/libguile/symbols.c +++ b/libguile/symbols.c @@ -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 @@ SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0, #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 @@ SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0, "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 @@ SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0, 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 (n, 10, &name[len]); SCM res = scm_mem2symbol (name, len + n_digits); if (name != buf) free (name); @@ -414,7 +421,6 @@ scm_symbols_prehistory () void scm_init_symbols () { - gensym_counter = 0; #include "libguile/symbols.x" } |