From: rain1@openmailbox.org
To: 20087@debbugs.gnu.org
Subject: bug#20087: gensym
Date: Fri, 18 Mar 2016 17:03:27 +0000 [thread overview]
Message-ID: <df0d878daef767b6802adbec4b5ed780@openmailbox.org> (raw)
In-Reply-To: <87a8zjxxpl.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 257 bytes --]
Hello
I agree, this goes against the main assumption people have about gensym.
I was able to reproduce the bug.
Here's a patch to libguile/symbol.c which fixes this behavior by
incrementing the gensym counter in a loop until it creates a fresh
symbol.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: guile-gensym-fix.diff --]
[-- Type: text/x-diff; name=guile-gensym-fix.diff, Size: 1396 bytes --]
diff --git a/libguile/symbols.c b/libguile/symbols.c
index 71d9827..2df7433 100644
--- a/libguile/symbols.c
+++ b/libguile/symbols.c
@@ -394,19 +394,31 @@ SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
SCM suffix, name;
int n, n_digits;
char buf[SCM_INTBUFLEN];
-
+ SCM symbol;
+
if (SCM_UNBNDP (prefix))
prefix = default_gensym_prefix;
- /* mutex in case another thread looks and incs at the exact same moment */
- scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
- n = gensym_counter++;
- scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
-
- n_digits = scm_iint2str (n, 10, buf);
- suffix = scm_from_latin1_stringn (buf, n_digits);
- name = scm_string_append (scm_list_2 (prefix, suffix));
- return scm_string_to_symbol (name);
+ while(1) {
+ /* mutex in case another thread looks and incs at the exact same moment */
+ scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
+ n = gensym_counter++;
+ scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
+
+ n_digits = scm_iint2str (n, 10, buf);
+ suffix = scm_from_latin1_stringn (buf, n_digits);
+ name = scm_string_append (scm_list_2 (prefix, suffix));
+
+ symbol = lookup_interned_symbol (name, scm_i_string_hash (name));
+ if (scm_is_true (symbol))
+ {
+ continue;
+ }
+ else
+ {
+ return scm_string_to_symbol (name);
+ }
+ }
}
#undef FUNC_NAME
next prev parent reply other threads:[~2016-03-18 17:03 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-11 17:15 bug#20087: 'gensym' is not guaranteed to return a fresh symbol Ludovic Courtès
2016-03-18 17:03 ` rain1 [this message]
2016-03-22 5:24 ` bug#20087: gensym Mark H Weaver
2016-03-22 7:58 ` Ludovic Courtès
2016-03-23 17:55 ` Mark H Weaver
2016-03-24 8:45 ` Ludovic Courtès
2016-06-23 13:48 ` Andy Wingo
2016-06-23 14:13 ` Ludovic Courtès
2016-06-23 16:05 ` Andy Wingo
2016-03-22 11:21 ` rain1
2016-03-22 18:06 ` Mark H Weaver
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=df0d878daef767b6802adbec4b5ed780@openmailbox.org \
--to=rain1@openmailbox.org \
--cc=20087@debbugs.gnu.org \
/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).