32f79f0809bfa0893c5874fd0965117919103f08
[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_to_here(lispobj *bsp,void *th)
85 {
86     struct thread *thread=(struct thread *)th;
87     struct binding *target = (struct binding *)bsp;
88     struct binding *binding = (struct binding *)get_binding_stack_pointer(thread);
89     lispobj symbol;
90
91     while (target < binding) {
92         binding--;
93
94         symbol = binding->symbol;
95         if (symbol) {
96             if (symbol != UNBOUND_MARKER_WIDETAG) {
97                 SetTlSymbolValue(symbol, binding->value,thread);
98             }
99             binding->symbol = 0;
100             binding->value = 0;
101         }
102     }
103     set_binding_stack_pointer(thread,binding);
104 }