37b83a1d0d6812ff2ff7f70c177729cf8ddfd84d
[sbcl.git] / src / code / weak.lisp
1 ;;;; weak pointer support
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!IMPL")
13
14 (defun make-weak-pointer (object)
15   #!+sb-doc
16   "Allocate and return a weak pointer which points to OBJECT."
17   (declare (values weak-pointer))
18   (make-weak-pointer object))
19
20 #!-sb-fluid (declaim (inline weak-pointer-value))
21 (defun weak-pointer-value (weak-pointer)
22   #!+sb-doc
23   "If WEAK-POINTER is valid, return the value of WEAK-POINTER and T.
24    If the referent of WEAK-POINTER has been garbage collected, returns
25    the values NIL and NIL."
26   (declare (type weak-pointer weak-pointer)
27            (values t (member t nil)))
28   ;; We don't need to wrap this with a WITHOUT-GCING, because once we
29   ;; have extracted the value, our reference to it will keep the weak
30   ;; pointer from becoming broken. We just have to make sure the
31   ;; compiler won't reorder these primitives.
32   ;;
33   ;; FIXME: Might it be a good idea to tweak the DEFKNOWNs for
34   ;; %WEAK-POINTER-VALUE and %WEAK-POINTER-BROKEN, so that the
35   ;; compiler will never try to reorder them even in code where we
36   ;; neglect to frame them in a LET?
37   (let ((value (sb!c::%weak-pointer-value weak-pointer))
38         (broken (sb!c::%weak-pointer-broken weak-pointer)))
39     (values value (not broken))))