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