Optimize special variable binding on sb-thread.
[sbcl.git] / src / runtime / dynbind.c
1 /*
2  * support for dynamic binding from C
3  * See the "Chapter 9: Specials" of the SBCL Internals Manual.
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
10  * This software is derived from the CMU CL system, which was
11  * written at Carnegie Mellon University and released into the
12  * public domain. The software is in the public domain and is
13  * provided with absolutely no warranty. See the COPYING and CREDITS
14  * files for more information.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "sbcl.h"
21 #include "runtime.h"
22 #include "globals.h"
23 #include "dynbind.h"
24 #include "thread.h"
25 #include "pseudo-atomic.h"
26 #include "genesis/symbol.h"
27 #include "genesis/binding.h"
28 #include "genesis/static-symbols.h"
29
30 void bind_variable(lispobj symbol, lispobj value, void *th)
31 {
32     struct binding *binding;
33     struct thread *thread=(struct thread *)th;
34     binding = (struct binding *)get_binding_stack_pointer(thread);
35     set_binding_stack_pointer(thread,binding+1);
36 #ifdef LISP_FEATURE_SB_THREAD
37     {
38         struct symbol *sym=(struct symbol *)native_pointer(symbol);
39         if(!sym->tls_index) {
40             lispobj *tls_index_lock=
41                 &((struct symbol *)native_pointer(TLS_INDEX_LOCK))->value;
42             FSHOW_SIGNAL((stderr, "entering dynbind tls alloc\n"));
43             set_pseudo_atomic_atomic(thread);
44             get_spinlock(tls_index_lock,(uword_t)th);
45             if(!sym->tls_index) {
46                 sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
47                 SetSymbolValue(FREE_TLS_INDEX, sym->tls_index+N_WORD_BYTES, 0);
48                 if((sym->tls_index)>=(TLS_SIZE << WORD_SHIFT)) {
49                     lose("Thread local storage exhausted.");
50                 }
51             }
52             release_spinlock(tls_index_lock);
53             FSHOW_SIGNAL((stderr, "exiting dynbind tls alloc\n"));
54             clear_pseudo_atomic_atomic(thread);
55             if (get_pseudo_atomic_interrupted(thread))
56                 do_pending_interrupt();
57         }
58         binding->symbol = sym->tls_index;
59         binding->value = SymbolTlValue(symbol, thread);
60     }
61 #else
62     binding->symbol = symbol;
63     binding->value = SymbolTlValue(symbol, thread);
64 #endif
65     SetTlSymbolValue(symbol, value, thread);
66 }
67
68 void
69 unbind(void *th)
70 {
71     struct thread *thread=(struct thread *)th;
72     struct binding *binding;
73     lispobj symbol;
74
75     binding = ((struct binding *)get_binding_stack_pointer(thread)) - 1;
76
77     /* On sb-thread, it's actually a tls-index */
78     symbol = binding->symbol;
79
80 #ifdef LISP_FEATURE_SB_THREAD
81
82     ((union per_thread_data *)thread)->dynamic_values[(symbol) >> WORD_SHIFT]
83         = binding->value;
84 #else
85     SetSymbolValue(symbol, binding->value, thread);
86 #endif
87
88     binding->symbol = 0;
89     binding->value = 0;
90
91     set_binding_stack_pointer(thread,binding);
92 }
93
94 void
95 unbind_to_here(lispobj *bsp,void *th)
96 {
97     struct thread *thread=(struct thread *)th;
98     struct binding *target = (struct binding *)bsp;
99     struct binding *binding = (struct binding *)get_binding_stack_pointer(thread);
100     lispobj symbol;
101
102     while (target < binding) {
103         binding--;
104
105         symbol = binding->symbol;
106         if (symbol) {
107             if (symbol != UNBOUND_MARKER_WIDETAG) {
108 #ifdef LISP_FEATURE_SB_THREAD
109                 ((union per_thread_data *)thread)->dynamic_values[(symbol) >> WORD_SHIFT]
110                     = binding->value;
111 #else
112                 SetSymbolValue(symbol, binding->value, thread);
113 #endif
114             }
115             binding->symbol = 0;
116             binding->value = 0;
117         }
118     }
119     set_binding_stack_pointer(thread,binding);
120 }