0.9.6.1:
[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     struct binding *binding;
37     struct thread *thread=(struct thread *)th;
38     binding = GetBSP();
39     SetBSP(binding+1);
40 #ifdef LISP_FEATURE_SB_THREAD
41     {
42         struct symbol *sym=(struct symbol *)native_pointer(symbol);
43         if(!sym->tls_index) {
44             lispobj *tls_index_lock=
45                 &((struct symbol *)native_pointer(TLS_INDEX_LOCK))->value;
46             SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),th);
47             SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1),th);
48             get_spinlock(tls_index_lock,(long)th);
49             if(!sym->tls_index) {
50                 sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
51                 SetSymbolValue(FREE_TLS_INDEX,
52                                make_fixnum(fixnum_value(sym->tls_index)+1),0);
53             }
54             release_spinlock(tls_index_lock);
55             SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0),th);
56             if (fixnum_value(SymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th)))
57                 do_pending_interrupt();
58         }
59     }
60 #endif
61     binding->value = SymbolTlValue(symbol, thread);
62     binding->symbol = symbol;
63     SetTlSymbolValue(symbol, value, thread);
64 }
65
66 void
67 unbind(void *th)
68 {
69     struct thread *thread=(struct thread *)th;
70     struct binding *binding;
71     lispobj symbol;
72
73     binding = GetBSP() - 1;
74
75     symbol = binding->symbol;
76
77     SetTlSymbolValue(symbol, binding->value,thread);
78
79     binding->symbol = 0;
80
81     SetBSP(binding);
82 }
83
84 void
85 unbind_to_here(lispobj *bsp,void *th)
86 {
87     struct thread *thread=(struct thread *)th;
88     struct binding *target = (struct binding *)bsp;
89     struct binding *binding = GetBSP();
90     lispobj symbol;
91
92     while (target < binding) {
93         binding--;
94
95         symbol = binding->symbol;
96         if (symbol) {
97             SetTlSymbolValue(symbol, binding->value,thread);
98             binding->symbol = 0;
99         }
100     }
101     SetBSP(binding);
102 }