1.0.39.11: fix readdir for :inode64 builds
[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 #if defined(BINDING_STACK_POINTER)
31 #define GetBSP() ((struct binding *)SymbolValue(BINDING_STACK_POINTER,thread))
32 #define SetBSP(value) SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value),thread)
33 #else
34 #define GetBSP() ((struct binding *)current_binding_stack_pointer)
35 #define SetBSP(value) (current_binding_stack_pointer=(lispobj *)(value))
36 #endif
37
38 void bind_variable(lispobj symbol, lispobj value, void *th)
39 {
40     struct binding *binding;
41     struct thread *thread=(struct thread *)th;
42     binding = GetBSP();
43     SetBSP(binding+1);
44 #ifdef LISP_FEATURE_SB_THREAD
45     {
46         struct symbol *sym=(struct symbol *)native_pointer(symbol);
47         if(!sym->tls_index) {
48             lispobj *tls_index_lock=
49                 &((struct symbol *)native_pointer(TLS_INDEX_LOCK))->value;
50             FSHOW_SIGNAL((stderr, "entering dynbind tls alloc\n"));
51             set_pseudo_atomic_atomic(th);
52             get_spinlock(tls_index_lock,(long)th);
53             if(!sym->tls_index) {
54                 sym->tls_index=SymbolValue(FREE_TLS_INDEX,0);
55                 SetSymbolValue(FREE_TLS_INDEX,
56                                make_fixnum(fixnum_value(sym->tls_index)+1),0);
57                 if(fixnum_value(sym->tls_index)>=TLS_SIZE) {
58                     lose("Thread local storage exhausted.");
59                 }
60             }
61             release_spinlock(tls_index_lock);
62             FSHOW_SIGNAL((stderr, "exiting dynbind tls alloc\n"));
63             clear_pseudo_atomic_atomic(th);
64             if (get_pseudo_atomic_interrupted(th))
65                 do_pending_interrupt();
66         }
67     }
68 #endif
69     binding->value = SymbolTlValue(symbol, thread);
70     binding->symbol = symbol;
71     SetTlSymbolValue(symbol, value, thread);
72 }
73
74 void
75 unbind(void *th)
76 {
77     struct thread *thread=(struct thread *)th;
78     struct binding *binding;
79     lispobj symbol;
80
81     binding = GetBSP() - 1;
82
83     symbol = binding->symbol;
84
85     SetTlSymbolValue(symbol, binding->value,thread);
86
87     binding->symbol = 0;
88     binding->value = 0;
89
90     SetBSP(binding);
91 }
92
93 void
94 unbind_to_here(lispobj *bsp,void *th)
95 {
96     struct thread *thread=(struct thread *)th;
97     struct binding *target = (struct binding *)bsp;
98     struct binding *binding = GetBSP();
99     lispobj symbol;
100
101     while (target < binding) {
102         binding--;
103
104         symbol = binding->symbol;
105         if (symbol) {
106             if (symbol != UNBOUND_MARKER_WIDETAG) {
107                 SetTlSymbolValue(symbol, binding->value,thread);
108             }
109             binding->symbol = 0;
110             binding->value = 0;
111         }
112     }
113     SetBSP(binding);
114 }