summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2012-01-26 11:00:55 +0100
committerAndy Wingo <wingo@pobox.com>2012-01-26 12:08:58 +0100
commit4496c9c1e8c5620fd3b4eaacc7f1f8e35829b32d (patch)
treeed73e62dd129c72b45bdc4eb64675b0eda97f8bc
parentd062a8c1eeeb55264c1a2fd8f4fbe765f1dcc6d9 (diff)
downloadguile-4496c9c1e8c5620fd3b4eaacc7f1f8e35829b32d.tar.gz
Revert "Universally-unique gensyms"
This reverts commit ad432bc8317c33899efc29854550b67f3d7babf7. Not all gensyms need to be universally-unique: most of them just need to be unique within some portion of a Guile session. We'll take a different tack on this problem in the next commit.
-rw-r--r--libguile/symbols.c50
-rw-r--r--libguile/threads.c1
-rw-r--r--libguile/threads.h4
3 files changed, 11 insertions, 44 deletions
diff --git a/libguile/symbols.c b/libguile/symbols.c
index 840a458af..59aca002e 100644
--- a/libguile/symbols.c
+++ b/libguile/symbols.c
@@ -31,7 +31,6 @@
#include "libguile/variable.h"
#include "libguile/alist.h"
#include "libguile/fluids.h"
-#include "libguile/threads.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/hashtab.h"
@@ -341,9 +340,7 @@ SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
/* The default prefix for `gensym'd symbols. */
static SCM default_gensym_prefix;
-#define GENSYM_LENGTH 22 /* bytes */
-#define GENSYM_RADIX_BITS 6
-#define GENSYM_RADIX (1 << (GENSYM_RADIX_BITS))
+#define MAX_PREFIX_LENGTH 30
SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
(SCM prefix),
@@ -354,47 +351,22 @@ SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
"resetting the counter.")
#define FUNC_NAME s_scm_gensym
{
- static const char base64[GENSYM_RADIX] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$@";
- static const char base4[4] = "_.-~";
-
- unsigned char *digit_buf = SCM_I_CURRENT_THREAD->gensym_counter;
- char char_buf[GENSYM_LENGTH];
+ static int gensym_counter = 0;
+
SCM suffix, name;
- int i;
+ int n, n_digits;
+ char buf[SCM_INTBUFLEN];
if (SCM_UNBNDP (prefix))
prefix = default_gensym_prefix;
- if (SCM_UNLIKELY (digit_buf == NULL))
- {
- /* This is the first time gensym has been called in this thread.
- Allocate and randomize our new thread-local gensym counter */
- digit_buf = (unsigned char *)
- scm_gc_malloc_pointerless (GENSYM_LENGTH, "gensym-counter");
- scm_i_random_bytes_from_platform (digit_buf, GENSYM_LENGTH);
- for (i = (GENSYM_LENGTH - 1); i >= 0; --i)
- digit_buf[i] &= (GENSYM_RADIX - 1);
- SCM_I_CURRENT_THREAD->gensym_counter = digit_buf;
- }
-
- /* Increment our thread-local gensym_counter. */
- for (i = (GENSYM_LENGTH - 1); i >= 0; --i)
- {
- if (SCM_LIKELY (++(digit_buf[i]) < GENSYM_RADIX))
- break;
- else
- digit_buf[i] = 0;
- }
-
- /* Encode digit_buf as base64, except for the first character where we
- use the sparse glyphs "_.-~" (base 4) to provide some visual
- separation between the prefix and the dense base64 block. */
- for (i = (GENSYM_LENGTH - 1); i > 0; --i)
- char_buf[i] = base64[digit_buf[i]];
- char_buf[0] = base4[digit_buf[0] & 3];
+ /* 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);
- suffix = scm_from_latin1_stringn (char_buf, GENSYM_LENGTH);
+ 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);
}
diff --git a/libguile/threads.c b/libguile/threads.c
index 67834ffc8..5a13e5ccf 100644
--- a/libguile/threads.c
+++ b/libguile/threads.c
@@ -545,7 +545,6 @@ guilify_self_1 (struct GC_stack_base *base)
t.join_queue = SCM_EOL;
t.dynamic_state = SCM_BOOL_F;
t.dynwinds = SCM_EOL;
- t.gensym_counter = NULL;
t.active_asyncs = SCM_EOL;
t.block_asyncs = 1;
t.pending_asyncs = 1;
diff --git a/libguile/threads.h b/libguile/threads.h
index 3660a58d4..ec129bc72 100644
--- a/libguile/threads.h
+++ b/libguile/threads.h
@@ -81,10 +81,6 @@ typedef struct scm_i_thread {
SCM dynamic_state;
SCM dynwinds;
- /* Thread-local gensym counter.
- */
- unsigned char *gensym_counter;
-
/* For system asyncs.
*/
SCM active_asyncs; /* The thunks to be run at the next