1.0.41.27: ppc: Calling convention fixes for assembly-routines calling static-funs.
[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,
48                                make_fixnum(fixnum_value(sym->tls_index)+1),0);
49                 if(fixnum_value(sym->tls_index)>=TLS_SIZE) {
50                     lose("Thread local storage exhausted.");
51                 }
52             }
53             release_spinlock(tls_index_lock);
54             FSHOW_SIGNAL((stderr, "exiting dynbind tls alloc\n"));
55             clear_pseudo_atomic_atomic(thread);
56             if (get_pseudo_atomic_interrupted(thread))
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 = ((struct binding *)get_binding_stack_pointer(thread)) - 1;
74
75     symbol = binding->symbol;
76
77     SetTlSymbolValue(symbol, binding->value,thread);
78
79     binding->symbol = 0;
80     binding->value = 0;
81
82     set_binding_stack_pointer(thread,binding);
83 }
84
85 void
86 unbind_to_here(lispobj *bsp,void *th)
87 {
88     struct thread *thread=(struct thread *)th;
89     struct binding *target = (struct binding *)bsp;
90     struct binding *binding = (struct binding *)get_binding_stack_pointer(thread);
91     lispobj symbol;
92
93     while (target < binding) {
94         binding--;
95
96         symbol = binding->symbol;
97         if (symbol) {
98             if (symbol != UNBOUND_MARKER_WIDETAG) {
99                 SetTlSymbolValue(symbol, binding->value,thread);
100             }
101             binding->symbol = 0;
102             binding->value = 0;
103         }
104     }
105     set_binding_stack_pointer(thread,binding);
106 }