0.8.9.18
[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 "runtime.h"
17 #include "sbcl.h"
18 #include "globals.h"
19 #include "dynbind.h"
20 #include "thread.h"
21 #include "genesis/symbol.h"
22 #include "genesis/binding.h"
23 #include "genesis/thread.h"
24
25 #if defined(BINDING_STACK_POINTER)
26 #define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER,thread))
27 #define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value),thread)
28 #else
29 #define GetBSP() ((struct binding *)current_binding_stack_pointer)
30 #define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
31 #endif
32
33 void bind_variable(lispobj symbol, lispobj value, void *th)
34 {
35     lispobj old_tl_value;
36     struct binding *binding;
37     struct thread *thread=(struct thread *)th;
38 #ifdef LISP_FEATURE_SB_THREAD
39     struct symbol *sym=(struct symbol *)native_pointer(symbol);
40 #endif
41     binding = GetBSP();
42     SetBSP(binding+1);
43 #ifdef LISP_FEATURE_SB_THREAD
44     if(!sym->tls_index) {
45         sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
46         SetSymbolValue(FREE_TLS_INDEX,
47                        make_fixnum(fixnum_value(sym->tls_index)+1),0);
48     }
49 #endif
50     old_tl_value=SymbolTlValue(symbol,thread);
51     binding->value = old_tl_value;
52     binding->symbol = symbol;
53     SetTlSymbolValue(symbol, value,thread);
54 }
55
56 void
57 unbind(void *th)
58 {
59     struct thread *thread=(struct thread *)th;
60     struct binding *binding;
61     lispobj symbol;
62         
63     binding = GetBSP() - 1;
64                 
65     symbol = binding->symbol;
66
67     SetTlSymbolValue(symbol, binding->value,thread);
68
69     binding->symbol = 0;
70
71     SetBSP(binding);
72 }
73
74 void
75 unbind_to_here(lispobj *bsp,void *th)
76 {
77     struct thread *thread=(struct thread *)th;
78     struct binding *target = (struct binding *)bsp;
79     struct binding *binding = GetBSP();
80     lispobj symbol;
81
82     while (target < binding) {
83         binding--;
84
85         symbol = binding->symbol;
86         if (symbol) {
87             SetTlSymbolValue(symbol, binding->value,thread);
88             binding->symbol = 0;
89         }
90     }
91     SetBSP(binding);
92 }