Redefine symbol TLS slot indices.
[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/thread.h"
28 #include "genesis/static-symbols.h"
29
30 void bind_variable(lispobj symbol, lispobj value, void *th)
31 {
32     struct binding *binding;
33     struct thread *thread=(struct thread *)th;
34     binding = (struct binding *)get_binding_stack_pointer(thread);
35     set_binding_stack_pointer(thread,binding+1);
36 #ifdef LISP_FEATURE_SB_THREAD
37     {
38         struct symbol *sym=(struct symbol *)native_pointer(symbol);
39         if(!sym->tls_index) {
40             lispobj *tls_index_lock=
41                 &((struct symbol *)native_pointer(TLS_INDEX_LOCK))->value;
42             FSHOW_SIGNAL((stderr, "entering dynbind tls alloc\n"));
43             set_pseudo_atomic_atomic(thread);
44             get_spinlock(tls_index_lock,(long)th);
45             if(!sym->tls_index) {
46                 sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
47                 SetSymbolValue(FREE_TLS_INDEX, sym->tls_index+N_WORD_BYTES, 0);
48                 if((sym->tls_index)>=(TLS_SIZE << WORD_SHIFT)) {
49                     lose("Thread local storage exhausted.");
50                 }
51             }
52             release_spinlock(tls_index_lock);
53             FSHOW_SIGNAL((stderr, "exiting dynbind tls alloc\n"));
54             clear_pseudo_atomic_atomic(thread);
55             if (get_pseudo_atomic_interrupted(thread))
56                 do_pending_interrupt();
57         }
58     }
59 #endif
60     binding->value = SymbolTlValue(symbol, thread);
61     binding->symbol = symbol;
62     SetTlSymbolValue(symbol, value, thread);
63 }
64
65 void
66 unbind(void *th)
67 {
68     struct thread *thread=(struct thread *)th;
69     struct binding *binding;
70     lispobj symbol;
71
72     binding = ((struct binding *)get_binding_stack_pointer(thread)) - 1;
73
74     symbol = binding->symbol;
75
76     SetTlSymbolValue(symbol, binding->value,thread);
77
78     binding->symbol = 0;
79     binding->value = 0;
80
81     set_binding_stack_pointer(thread,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 = (struct binding *)get_binding_stack_pointer(thread);
90     lispobj symbol;
91
92     while (target < binding) {
93         binding--;
94
95         symbol = binding->symbol;
96         if (symbol) {
97             if (symbol != UNBOUND_MARKER_WIDETAG) {
98                 SetTlSymbolValue(symbol, binding->value,thread);
99             }
100             binding->symbol = 0;
101             binding->value = 0;
102         }
103     }
104     set_binding_stack_pointer(thread,binding);
105 }