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