unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#38534: fluid-ref doesn't return defaults for thread local fluids
@ 2019-12-08 17:41 Rob Browning
  2020-01-12 21:06 ` Andy Wingo
  0 siblings, 1 reply; 2+ messages in thread
From: Rob Browning @ 2019-12-08 17:41 UTC (permalink / raw)
  To: 38534

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


For example, in 2.2.6:

  scheme@(guile-user)> (define x (make-thread-local-fluid 'default))
  scheme@(guile-user)> (fluid-ref x)
  $1 = #f

Here's a possible fix and some (trivial) tests:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Respect-thread-local-fluid-defaults.patch --]
[-- Type: text/x-diff, Size: 3098 bytes --]

From 31fa1050340271ca2f68ac5a6c66322912f915e0 Mon Sep 17 00:00:00 2001
From: Rob Browning <rlb@defaultvalue.org>
Date: Sun, 8 Dec 2019 11:35:37 -0600
Subject: [PATCH 1/1] Respect thread local fluid defaults

Previously (fluid-ref (make-thread-local-fluid #t)) would return #f via
scm_fluid_ref because the internal scm_hashq_ref would return #f when
the fluid had not been set, and that was interpreted as an actual value
for the fluid.

Instead, just pass the fluid default as the default for the hash table
lookups so that we don't need a second step to determine if the fluid
was set.

Thanks to Andrew Gierth for tracking down the problem.
---

 These changes might not be OK if SCM_I_FLUID_DEFAULT can ever be very
 expensive, i.e. we wouldn't want to have to pay that cost for every
 lookup.

 libguile/fluids.c            | 23 +++++++++--------------
 test-suite/tests/fluids.test | 14 ++++++++++++--
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/libguile/fluids.c b/libguile/fluids.c
index c3dd1c9ea..472f92a06 100644
--- a/libguile/fluids.c
+++ b/libguile/fluids.c
@@ -343,22 +343,17 @@ fluid_ref (scm_t_dynamic_state *dynamic_state, SCM fluid)
 
   entry = scm_cache_lookup (&dynamic_state->cache, fluid);
   if (scm_is_eq (SCM_PACK (entry->key), fluid))
-    val = SCM_PACK (entry->value);
-  else
-    {
-      if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
-        val = scm_hashq_ref (dynamic_state->thread_local_values, fluid,
-                             SCM_UNDEFINED);
-      else
-        val = scm_weak_table_refq (dynamic_state->values, fluid,
-                                   SCM_UNDEFINED);
+    return SCM_PACK (entry->value);
 
-      if (SCM_UNBNDP (val))
-        val = SCM_I_FLUID_DEFAULT (fluid);
+  if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
+    val = scm_hashq_ref (dynamic_state->thread_local_values, fluid,
+                         SCM_I_FLUID_DEFAULT (fluid));
+  else
+    val = scm_weak_table_refq (dynamic_state->values, fluid,
+                         SCM_I_FLUID_DEFAULT (fluid));
 
-      /* Cache this lookup.  */
-      fluid_set_x (dynamic_state, fluid, val);
-    }
+  /* Cache this lookup.  */
+  fluid_set_x (dynamic_state, fluid, val);
 
   return val;
 }
diff --git a/test-suite/tests/fluids.test b/test-suite/tests/fluids.test
index a5ca8857e..949d50410 100644
--- a/test-suite/tests/fluids.test
+++ b/test-suite/tests/fluids.test
@@ -49,8 +49,18 @@
 	  (interaction-environment))))
 
 (with-test-prefix "initial fluid values"
-  (pass-if "fluid-ref uninitialized fluid is #f"
-    (not (fluid-ref a)))
+
+  (pass-if "fluid-ref returns #f for uninitialized fluid"
+    (eq? #f (fluid-ref (make-fluid))))
+
+  (pass-if "fluid-ref returns #f for uninitialized thread local fluid"
+    (eq? #f (fluid-ref (make-thread-local-fluid))))
+
+  (pass-if "fluid-ref returns default"
+    (eq? #t (fluid-ref (make-fluid #t))))
+
+  (pass-if "fluid-ref returns thread local default"
+    (eq? #t (fluid-ref (make-thread-local-fluid #t))))
 
   (pass-if "initial value is inherited from parent thread"
     (if (provided? 'threads)
-- 
2.24.0


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


Thanks
-- 
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#38534: fluid-ref doesn't return defaults for thread local fluids
  2019-12-08 17:41 bug#38534: fluid-ref doesn't return defaults for thread local fluids Rob Browning
@ 2020-01-12 21:06 ` Andy Wingo
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Wingo @ 2020-01-12 21:06 UTC (permalink / raw)
  To: Rob Browning; +Cc: 38534-done

On Sun 08 Dec 2019 18:41, Rob Browning <rlb@defaultvalue.org> writes:

> For example, in 2.2.6:
>
>   scheme@(guile-user)> (define x (make-thread-local-fluid 'default))
>   scheme@(guile-user)> (fluid-ref x)
>   $1 = #f

Applied your patch on both branches; thank you!

Andy





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-01-12 21:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-08 17:41 bug#38534: fluid-ref doesn't return defaults for thread local fluids Rob Browning
2020-01-12 21:06 ` Andy Wingo

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