1.0.23.9: extend pa_alloc to accept a page_type_flag
[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 "pseudo-atomic.h"
22 #include "genesis/symbol.h"
23 #include "genesis/binding.h"
24 #include "genesis/thread.h"
25 #include "genesis/static-symbols.h"
26
27 #if defined(BINDING_STACK_POINTER)
28 #define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER,thread))
29 #define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value),thread)
30 #else
31 #define GetBSP() ((struct binding *)current_binding_stack_pointer)
32 #define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
33 #endif
34
35 void bind_variable(lispobj symbol, lispobj value, void *th)
36 {
37     struct binding *binding;
38     struct thread *thread=(struct thread *)th;
39     binding = GetBSP();
40     SetBSP(binding+1);
41 #ifdef LISP_FEATURE_SB_THREAD
42     {
43         struct symbol *sym=(struct symbol *)native_pointer(symbol);
44         if(!sym->tls_index) {
45             lispobj *tls_index_lock=
46                 &((struct symbol *)native_pointer(TLS_INDEX_LOCK))->value;
47             clear_pseudo_atomic_interrupted(th);
48             set_pseudo_atomic_atomic(th);
49             get_spinlock(tls_index_lock,(long)th);
50             if(!sym->tls_index) {
51                 sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
52                 SetSymbolValue(FREE_TLS_INDEX,
53                                make_fixnum(fixnum_value(sym->tls_index)+1),0);
54                 if(fixnum_value(sym->tls_index)>=TLS_SIZE) {
55                     lose("Thread local storage exhausted.");
56                 }
57             }
58             release_spinlock(tls_index_lock);
59             clear_pseudo_atomic_atomic(th);
60             if (get_pseudo_atomic_interrupted(th))
61                 do_pending_interrupt();
62         }
63     }
64 #endif
65     binding->value = SymbolTlValue(symbol, thread);
66     binding->symbol = symbol;
67     SetTlSymbolValue(symbol, value, thread);
68 }
69
70 void
71 unbind(void *th)
72 {
73     struct thread *thread=(struct thread *)th;
74     struct binding *binding;
75     lispobj symbol;
76
77     binding = GetBSP() - 1;
78
79     symbol = binding->symbol;
80
81     SetTlSymbolValue(symbol, binding->value,thread);
82
83     binding->symbol = 0;
84
85     SetBSP(binding);
86 }
87
88 void
89 unbind_to_here(lispobj *bsp,void *th)
90 {
91     struct thread *thread=(struct thread *)th;
92     struct binding *target = (struct binding *)bsp;
93     struct binding *binding = GetBSP();
94     lispobj symbol;
95
96     while (target < binding) {
97         binding--;
98
99         symbol = binding->symbol;
100         if (symbol) {
101             if (symbol != UNBOUND_MARKER_WIDETAG) {
102                 SetTlSymbolValue(symbol, binding->value,thread);
103             }
104             binding->symbol = 0;
105         }
106     }
107     SetBSP(binding);
108 }