1.0.37.57: better DEFMETHOD pretty-printing
[sbcl/pkhuong.git] / src / runtime / dynbind.c
blob9a64a0ab25a0666d2cf43e55b9851c71aa78b1ba
1 /*
2 * support for dynamic binding from C
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
16 #include <stdio.h>
17 #include <stdlib.h>
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"
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
38 void bind_variable(lispobj symbol, lispobj value, void *th)
40 struct binding *binding;
41 struct thread *thread=(struct thread *)th;
42 binding = GetBSP();
43 SetBSP(binding+1);
44 #ifdef LISP_FEATURE_SB_THREAD
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.");
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();
68 #endif
69 binding->value = SymbolTlValue(symbol, thread);
70 binding->symbol = symbol;
71 SetTlSymbolValue(symbol, value, thread);
74 void
75 unbind(void *th)
77 struct thread *thread=(struct thread *)th;
78 struct binding *binding;
79 lispobj symbol;
81 binding = GetBSP() - 1;
83 symbol = binding->symbol;
85 SetTlSymbolValue(symbol, binding->value,thread);
87 binding->symbol = 0;
88 binding->value = 0;
90 SetBSP(binding);
93 void
94 unbind_to_here(lispobj *bsp,void *th)
96 struct thread *thread=(struct thread *)th;
97 struct binding *target = (struct binding *)bsp;
98 struct binding *binding = GetBSP();
99 lispobj symbol;
101 while (target < binding) {
102 binding--;
104 symbol = binding->symbol;
105 if (symbol) {
106 if (symbol != UNBOUND_MARKER_WIDETAG) {
107 SetTlSymbolValue(symbol, binding->value,thread);
109 binding->symbol = 0;
110 binding->value = 0;
113 SetBSP(binding);