gencgc: More precise conservatism for pointers to boxed pages.
[sbcl.git] / src / runtime / dynbind.c
index 7b410c3..d89bc7d 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * support for dynamic binding from C
+ * See the "Chapter 9: Specials" of the SBCL Internals Manual.
  */
 
 /*
  * files for more information.
  */
 
-#include "runtime.h"
+#include <stdio.h>
+#include <stdlib.h>
+
 #include "sbcl.h"
+#include "runtime.h"
 #include "globals.h"
 #include "dynbind.h"
 #include "thread.h"
+#include "pseudo-atomic.h"
 #include "genesis/symbol.h"
 #include "genesis/binding.h"
-#include "genesis/thread.h"
-
-#if defined(LISP_FEATURE_X86)
-#define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER,thread))
-#define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value),thread)
-#else
-#define GetBSP() ((struct binding *)current_binding_stack_pointer)
-#define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
-#endif
+#include "genesis/static-symbols.h"
 
 void bind_variable(lispobj symbol, lispobj value, void *th)
 {
-    lispobj old_tl_value;
     struct binding *binding;
     struct thread *thread=(struct thread *)th;
-    struct symbol *sym=(struct symbol *)native_pointer(symbol);
-    binding = GetBSP();
-    SetBSP(binding+1);
+    binding = (struct binding *)get_binding_stack_pointer(thread);
+    set_binding_stack_pointer(thread,binding+1);
 #ifdef LISP_FEATURE_SB_THREAD
-    if(!sym->tls_index) {
-       sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
-       SetSymbolValue(FREE_TLS_INDEX,
-                      make_fixnum(fixnum_value(sym->tls_index)+1),0);
+    {
+        struct symbol *sym=(struct symbol *)native_pointer(symbol);
+        if(!sym->tls_index) {
+            lispobj *tls_index_lock=
+                &((struct symbol *)native_pointer(TLS_INDEX_LOCK))->value;
+            FSHOW_SIGNAL((stderr, "entering dynbind tls alloc\n"));
+            set_pseudo_atomic_atomic(thread);
+            get_spinlock(tls_index_lock,(uword_t)th);
+            if(!sym->tls_index) {
+                sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
+                SetSymbolValue(FREE_TLS_INDEX, sym->tls_index+N_WORD_BYTES, 0);
+                if((sym->tls_index)>=(TLS_SIZE << WORD_SHIFT)) {
+                    lose("Thread local storage exhausted.");
+                }
+            }
+            release_spinlock(tls_index_lock);
+            FSHOW_SIGNAL((stderr, "exiting dynbind tls alloc\n"));
+            clear_pseudo_atomic_atomic(thread);
+            if (get_pseudo_atomic_interrupted(thread))
+                do_pending_interrupt();
+        }
+        binding->symbol = sym->tls_index;
+        binding->value = SymbolTlValue(symbol, thread);
     }
-#endif
-    old_tl_value=SymbolTlValue(symbol,thread);
-    binding->value = old_tl_value;
+#else
     binding->symbol = symbol;
-    SetTlSymbolValue(symbol, value,thread);
+    binding->value = SymbolTlValue(symbol, thread);
+#endif
+    SetTlSymbolValue(symbol, value, thread);
 }
 
 void
@@ -57,16 +71,24 @@ unbind(void *th)
     struct thread *thread=(struct thread *)th;
     struct binding *binding;
     lispobj symbol;
-       
-    binding = GetBSP() - 1;
-               
+
+    binding = ((struct binding *)get_binding_stack_pointer(thread)) - 1;
+
+    /* On sb-thread, it's actually a tls-index */
     symbol = binding->symbol;
 
-    SetTlSymbolValue(symbol, binding->value,thread);
+#ifdef LISP_FEATURE_SB_THREAD
+
+    ((union per_thread_data *)thread)->dynamic_values[(symbol) >> WORD_SHIFT]
+        = binding->value;
+#else
+    SetSymbolValue(symbol, binding->value, thread);
+#endif
 
     binding->symbol = 0;
+    binding->value = 0;
 
-    SetBSP(binding);
+    set_binding_stack_pointer(thread,binding);
 }
 
 void
@@ -74,17 +96,25 @@ unbind_to_here(lispobj *bsp,void *th)
 {
     struct thread *thread=(struct thread *)th;
     struct binding *target = (struct binding *)bsp;
-    struct binding *binding = GetBSP();
+    struct binding *binding = (struct binding *)get_binding_stack_pointer(thread);
     lispobj symbol;
 
     while (target < binding) {
-       binding--;
+        binding--;
 
-       symbol = binding->symbol;
-       if (symbol) {
-           SetTlSymbolValue(symbol, binding->value,thread);
-           binding->symbol = 0;
-       }
+        symbol = binding->symbol;
+        if (symbol) {
+            if (symbol != UNBOUND_MARKER_WIDETAG) {
+#ifdef LISP_FEATURE_SB_THREAD
+                ((union per_thread_data *)thread)->dynamic_values[(symbol) >> WORD_SHIFT]
+                    = binding->value;
+#else
+                SetSymbolValue(symbol, binding->value, thread);
+#endif
+            }
+            binding->symbol = 0;
+            binding->value = 0;
+        }
     }
-    SetBSP(binding);
+    set_binding_stack_pointer(thread,binding);
 }