wiki:Internals/DynamicBinding

Version 1 (modified by rme, 6 years ago) (diff)

--

Per-thread bindings of special variables

In the thread-context record (TCR), there is a vector (tcr.tlb_pointer) that contains thread-local special binding values. (The "vector" is actually allocated with malloc(), but it always contains tagged lisp objects.)

Each symbol that's used in a special-binding construct is assigned (either at compile/load time or during PROGV pre-processing) a unique integer, called the binding index (symbol.binding_index). This integer (a fixnum) is used as an index into the vector mentioned above.

An attempt to dynamically bind a symbol whose binding index is outside the bounds of the current thread's tlb table (tcr.tlb_limit) will trap and grow the table.

Referencing

The value of a special variable may be found in the tlb table, indexed by the symbol's binding index. If there is no thread local binding for the symbol, the value in the tlb table will be a no-thread-local-binding marker. The symbol's vcell will be returned in this case.

Binding

Another field in the TCR, called tcr.db_link, is the dynamic binding chain (a linked list of binding records).

When dynamically binding a symbol, a binding record (link, binding_index, value) representing the current binding is created on the stack, and becomes the new head of the chain. The tlb table is then updated with the new value.

Dynamic binding information is always pushed onto the lisp stack, and therefore pointers to any of that information (link, etc.) will always be fixnum-tagged.

Note that the symbol's vcell is never affected.

Unbinding

Unbinding is just a matter of updating the tlb table using the data in the binding record at the front of the dynamic binding chain, and then popping off said record.