2 @comment node-name, next, previous, up
5 None of the following sections apply to SBCL built without package
8 The interface described here is experimental: incompatible changes in
9 future SBCL releases are possible, even expected: most notably the
10 consept of implementation packages and the associated operators may be
14 * Package Lock Concepts::
15 * Package Lock Dictionary::
18 @node Package Lock Concepts
19 @section Package Lock Concepts
22 * Package Lock Overview::
23 * Implementation Packages::
24 * Package Lock Violations::
25 * Package Locks in Compiled Code::
26 * Operations Violating Package Locks::
29 @node Package Lock Overview
30 @comment node-name, next, previous, up
31 @subsection Package Locking Overview
33 Package locks protect against unintentional modifications of a
34 package: they provide similar protection to user packages as is
35 mandated to @code{common-lisp} package by the ANSI specification. They
36 are not, and should not be used as a security measure.
38 Newly created packages are by default unlocked (see the @code{:lock}
39 option to @code{defpackage}).
41 The package @code{common-lisp} and SBCL internal implementation
42 packages are locked by default, including @code{sb-ext}.
44 It may be beneficial to lock @code{common-lisp-user} as well, to
45 ensure that various libraries don't pollute it without asking,
46 but this is not currently done by default.
48 @node Implementation Packages
49 @subsection Implementation Packages
51 Each package has a list of associated implementation packages. A
52 locked package, and the symbols whose home package it is, can be
53 modified without violating package locks only when @code{*package*} is
54 bound to one of the implementation packages of the locked package.
56 Unless explicitly altered by @code{defpackage},
57 @code{sb-ext:add-implementation-package}, or
58 @code{sb-ext:remove-implementation-package} each package is its own
59 (only) implementation package.
61 @node Package Lock Violations
62 @subsection Package Lock Violations
64 If an operation violates a package lock, a continuable error that is
65 of a subtype of @code{sb-ext:package-lock-violation} (subtype of
66 @code{package-error}) is signalled when the operation is attempted.
68 Additional restarts may be established for continuable package lock
69 violations for interactive use.
71 The actual type of the error depends on circumstances that caused the
72 violation: operations on packages signal errors of type
73 @code{sb-ext:package-locked-error}, and operations on symbols signal
74 errors of type @code{sb-ext:symbol-package-locked-error}.
76 @node Package Locks in Compiled Code
77 @subsection Package Locks in Compiled Code
79 @subsubsection Lexical bindings and declarations
81 Compiling lexical binding constructs or lexical declarations that
82 violate package locks causes a compile-time package-lock violation. A
83 complete listing of operators affect by this is: @code{let},
84 @code{let*}, @code{flet}, @code{labels}, @code{macrolet}, and
85 @code{symbol-macrolet}, @code{declare}.
87 Package locks affecting both lexical bindings and declarations can be
88 disabled at compile-time with @code{sb-ext:disable-package-locks}
89 declaration, and re-enabled with @code{sb-ext:enable-package-locks}
90 declaration. Constructs compiled with package locks thusly disabled
91 are guaranteed not to signal package lock violation errors at runtime.
100 (defmacro with-foo (&body body)
101 `(locally (declare (disable-package-locks locked:foo))
103 (declare (enable-package-locks locked:foo)) ; re-enable for body
107 @subsubsection Interned symbols
109 If compiled code contains interned symbols, then loading that code
110 into an image without the said symbols will not cause a package lock
111 violation even if the packages in question are locked.
113 @subsubsection Other limitations on compiled code
115 With the exception of the aforementioned contructs, and interned
116 symbols, behaviour is unspecified if package locks affecting compiled
117 code are not the same during loading of the code or execution.
119 Specifically, code compiled with packages unlocked may or may not fail
120 to signal package-lock-violations even if the packages are locked at
121 runtime, and code compiled with packages locked may or may not signal
122 spurious package-lock-violations at runtime even if the packages are
125 In practise all this means that package-locks have a neglible
126 performance penalty in compiled code as long as they are not violated.
128 @node Operations Violating Package Locks
129 @subsection Operations Violating Package Locks
131 @subsubsection Operations on Packages
133 Following actions cause a package lock violation if the package
134 operated on is locked, and @code{*package*} is not an implementation
135 package of that package, and the action would cause a change in the
136 state of the package (eg. exporting already external symbols is
137 allowed). Package lock violations caused by these operations signal
138 errors of type @code{sb-ext:package-locked-error}.
142 Shadowing a symbol in a package.
145 Importing a symbol to a package.
148 Uninterning a symbol from a package.
151 Exporting a symbol from a package.
154 Unexporting a symbol from a package.
157 Changing the packages used by a package.
167 @subsubsection Operations on Symbols
169 Following actions cause a package lock violation if the home package
170 of the symbol operated on is locked, and @code{*package*} is not an
171 implementation package of that package. Package lock violations caused
172 by these action signal errors of type
173 @code{sb-ext:symbol-package-locked-error}.
175 These actions cause only one package lock violation per lexically
176 apparent violated package.
181 ;; Packages FOO and BAR are locked.
183 ;; Two lexically apparent violated packages: exactly two
184 ;; package-locked-errors will be signalled.
186 (defclass foo:point ()
188 (y :accessor bar:y)))
193 Binding or altering its value lexically or dynamically, or
194 establishing it as a symbol-macro.
200 If the symbol is not defined as a constant, global symbol-macro or a
201 global dynamic variable, it may be lexically bound or established as a
205 If the symbol is defined as a global dynamic variable, it may be
211 Defining, undefining, or binding it, or its setf name as a function.
217 If the symbol is not defined as a function, macro, or special operator
218 it and its setf name may be lexically bound as a function.
223 Defining, undefining, or binding it as a macro or compiler macro.
229 If the symbol is not defined as a function, macro, or special operator
230 it may be lexically bound as a macro.
235 Defining it as a type specifier or structure.
238 Defining it as a declaration with a declaration proclamation.
241 Declaring or proclaiming it special.
244 Declaring or proclaiming its type or ftype.
250 If the symbol may be lexically bound, the type of that binding may be
254 If the symbol may be lexically bound as a function, the ftype of that
255 binding may be declared.
260 Defining a setf expander for it.
263 Defining it as a method combination type.
266 Using it as the class-name argument to setf of find-class.
270 @node Package Lock Dictionary
271 @section Package Lock Dictionary
273 @deftp {Declaration} sb-ext:disable-package-locks
275 Syntax: @code{(sb-ext:disable-package-locks symbol*)}
277 Disables package locks affecting the named symbols during compilation
278 in the lexical scope of the declaration. Disabling locks on symbols
279 whose home package is unlocked, or disabling an already disabled lock,
283 @deftp {Declaration} sb-ext:enable-package-locks
285 Syntax: @code{(sb-ext:enable-package-locks symbol*)}
287 Re-enables package locks affecting the named symbols during
288 compilation in the lexical scope of the declaration. Enabling locks
289 that were not first disabled with @code{sb-ext:disable-package-locks}
290 declararion, or enabling locks that are already enabled has no effect.
293 @include condition-sb-ext-package-lock-violation.texinfo
294 @include condition-sb-ext-package-locked-error.texinfo
295 @include condition-sb-ext-symbol-package-locked-error.texinfo
297 @defun sb-ext:package-locked-error-symbol @var{symbol-package-locked-error}
299 Returns the symbol that caused the @code{symbol-package-locked-error}
303 @include fun-sb-ext-package-locked-p.texinfo
304 @include fun-sb-ext-lock-package.texinfo
305 @include fun-sb-ext-unlock-package.texinfo
306 @include fun-sb-ext-package-implemented-by-list.texinfo
307 @include fun-sb-ext-package-implements-list.texinfo
308 @include fun-sb-ext-add-implementation-package.texinfo
309 @include fun-sb-ext-remove-implementation-package.texinfo
310 @include macro-sb-ext-without-package-locks.texinfo
311 @include macro-sb-ext-with-unlocked-packages.texinfo
313 @defmac defpackage name [[@var{option}]]* @result{} package
315 Options are extended to include the following:
319 @code{:lock} @var{boolean}
321 If the argument to @code{:lock} is @code{t}, the package is initially
322 locked. If @code{:lock} is not provided it defaults to @code{nil}.
325 @code{:implement} @var{package-designator}*
327 The package is added as an implementation package to the packages
328 named. If @code{:implement} is not provided, it defaults to the
335 (defpackage "FOO" (:export "BAR") (:lock t) (:implement))
336 (defpackage "FOO-INT" (:use "FOO") (:implement "FOO" "FOO-INT"))
340 (defpackage "FOO") (:export "BAR"))
342 (remove-implementation-package "FOO" "FOO")
343 (defpackage "FOO-INT" (:use "BAR"))
344 (add-implementation-package "FOO-INT" "FOO")