1.0.11.22: hash-table synchronization support
[sbcl.git] / src / code / class.lisp
index 0390b9f..79dc7ae 100644 (file)
 ;;; Note: This bound is set somewhat less than MOST-POSITIVE-FIXNUM
 ;;; in order to guarantee that several hash values can be added without
 ;;; overflowing into a bignum.
-(def!constant layout-clos-hash-max (ash sb!xc:most-positive-fixnum -3)
+(def!constant layout-clos-hash-limit (1+ (ash sb!xc:most-positive-fixnum -3))
   #!+sb-doc
-  "the inclusive upper bound on LAYOUT-CLOS-HASH values")
+  "the exclusive upper bound on LAYOUT-CLOS-HASH values")
+(def!type layout-clos-hash () '(integer 0 #.layout-clos-hash-limit))
 
 ;;; a list of conses, initialized by genesis
 ;;;
 ;;; type checking and garbage collection. Whenever a class is
 ;;; incompatibly redefined, a new layout is allocated. If two object's
 ;;; layouts are EQ, then they are exactly the same type.
+;;;
+;;; *** IMPORTANT ***
+;;;
+;;; If you change the slots of LAYOUT, you need to alter genesis as
+;;; well, since the initialization of layout slots is hardcoded there.
+;;;
+;;; FIXME: ...it would be better to automate this, of course...
 (def!struct (layout
              ;; KLUDGE: A special hack keeps this from being
              ;; called when building code for the
                                   ;; DEF!STRUCT setup. -- WHN 19990930
                                   #+sb-xc-host
                                   make-load-form-for-layout))
-  ;; hash bits which should be set to constant pseudo-random values
-  ;; for use by CLOS. Sleazily accessed via %INSTANCE-REF, see
-  ;; LAYOUT-CLOS-HASH.
-  ;;
-  ;; FIXME: We should get our story straight on what the type of these
-  ;; values is. (declared INDEX here, described as <=
-  ;; LAYOUT-CLOS-HASH-MAX by the doc string of that constant,
-  ;; generated as strictly positive in RANDOM-LAYOUT-CLOS-HASH..)
-  ;;
-  ;; [ CSR notes, several years later (2005-11-30) that the value 0 is
-  ;;   special for these hash slots, indicating that the wrapper is
-  ;;   obsolete. ]
-  ;;
-  ;; KLUDGE: The fact that the slots here start at offset 1 is known
-  ;; to the LAYOUT-CLOS-HASH function and to the LAYOUT-dumping code
-  ;; in GENESIS.
-  (clos-hash-0 (random-layout-clos-hash) :type index)
-  (clos-hash-1 (random-layout-clos-hash) :type index)
-  (clos-hash-2 (random-layout-clos-hash) :type index)
-  (clos-hash-3 (random-layout-clos-hash) :type index)
-  (clos-hash-4 (random-layout-clos-hash) :type index)
-  (clos-hash-5 (random-layout-clos-hash) :type index)
-  (clos-hash-6 (random-layout-clos-hash) :type index)
-  (clos-hash-7 (random-layout-clos-hash) :type index)
+  ;; a pseudo-random hash value for use by CLOS.  KLUDGE: The fact
+  ;; that this slot is at offset 1 is known to GENESIS.
+  (clos-hash (random-layout-clos-hash) :type layout-clos-hash)
   ;; the class that this is a layout for
   (classoid (missing-arg) :type classoid)
   ;; The value of this slot can be:
   ;; This slot is known to the C runtime support code.
   (n-untagged-slots 0 :type index)
   ;; Definition location
-  (source-location nil))
+  (source-location nil)
+  ;; Information about slots in the class to PCL: this provides fast
+  ;; access to slot-definitions and locations by name, etc.
+  (slot-table #(nil) :type simple-vector)
+  ;; True IFF the layout belongs to a standand-instance or a
+  ;; standard-funcallable-instance -- that is, true only if the layout
+  ;; is really a wrapper.
+  ;;
+  ;; FIXME: If we unify wrappers and layouts this can go away, since
+  ;; it is only used in SB-PCL::EMIT-FETCH-WRAPPERS, which can then
+  ;; use INSTANCE-SLOTS-LAYOUT instead (if there is are no slot
+  ;; layouts, there are no slots for it to pull.)
+  (for-std-class-p nil :type boolean :read-only t))
 
 (def!method print-object ((layout layout) stream)
   (print-unreadable-object (layout stream :type t :identity t)
 \f
 ;;;; support for the hash values used by CLOS when working with LAYOUTs
 
-(def!constant layout-clos-hash-length 8)
-#!-sb-fluid (declaim (inline layout-clos-hash))
-(defun layout-clos-hash (layout i)
-  ;; FIXME: Either this I should be declared to be `(MOD
-  ;; ,LAYOUT-CLOS-HASH-LENGTH), or this is used in some inner loop
-  ;; where we can't afford to check that kind of thing and therefore
-  ;; should have some insane level of optimization. (This is true both
-  ;; of this function and of the SETF function below.)
-  (declare (type layout layout) (type index i))
-  ;; FIXME: LAYOUT slots should have type `(MOD ,LAYOUT-CLOS-HASH-MAX),
-  ;; not INDEX.
-  (truly-the index (%instance-ref layout (1+ i))))
-#!-sb-fluid (declaim (inline (setf layout-clos-hash)))
-(defun (setf layout-clos-hash) (new-value layout i)
-  (declare (type layout layout) (type index new-value i))
-  (setf (%instance-ref layout (1+ i)) new-value))
-
 ;;; a generator for random values suitable for the CLOS-HASH slots of
 ;;; LAYOUTs. We use our own RANDOM-STATE here because we'd like
 ;;; pseudo-random values to come the same way in the target even when
   ;;
   ;; an explanation is provided in Kiczales and Rodriguez, "Efficient
   ;; Method Dispatch in PCL", 1990.  -- CSR, 2005-11-30
-  (1+ (random layout-clos-hash-max
+  (1+ (random (1- layout-clos-hash-limit)
               (if (boundp '*layout-clos-hash-random-state*)
                   *layout-clos-hash-random-state*
                   (setf *layout-clos-hash-random-state*
     (when classoid-layout
       (modify-classoid classoid)
       (when subclasses
-        (dohash (subclass subclass-layout subclasses)
+        (dohash ((subclass subclass-layout) subclasses :locked t)
           (modify-classoid subclass)
           (when invalidate
             (invalidate-layout subclass-layout))))
                   (when (zerop count)
                     (push successor free-objs))))))
        (cond ((endp free-objs)
-              (dohash (obj info obj-info)
+              (dohash ((obj info) obj-info)
                 (unless (zerop (first info))
                   (error "Topological sort failed due to constraint on ~S."
                          obj)))
@@ -850,7 +832,7 @@ NIL is returned when no such class exists."
 (defun update-object-layout-or-invalid (object layout)
   (if (typep (classoid-of object) 'standard-classoid)
       (sb!pcl::check-wrapper-validity object)
-      (%layout-invalid-error object layout)))
+      (sb!c::%layout-invalid-error object layout)))
 
 ;;; Simple methods for TYPE= and SUBTYPEP should never be called when
 ;;; the two classes are equal, since there are EQ checks in those
@@ -876,7 +858,7 @@ NIL is returned when no such class exists."
         (o-sub (classoid-subclasses other)))
     (if (and s-sub o-sub)
         (collect ((res *empty-type* type-union))
-          (dohash (subclass layout s-sub)
+          (dohash ((subclass layout) s-sub :locked t)
             (declare (ignore layout))
             (when (gethash subclass o-sub)
               (res (specifier-type subclass))))
@@ -950,10 +932,10 @@ NIL is returned when no such class exists."
 ;;; FUNCALLABLE-STANDARD-CLASS.
 (def!struct (standard-classoid (:include classoid)
                                (:constructor make-standard-classoid)))
-;;; a metaclass for miscellaneous PCL structure-like objects (at the
-;;; moment, only CTOR objects).
-(def!struct (random-pcl-classoid (:include classoid)
-                                 (:constructor make-random-pcl-classoid)))
+;;; a metaclass for classes which aren't standardlike but will never
+;;; change either.
+(def!struct (static-classoid (:include classoid)
+                             (:constructor make-static-classoid)))
 \f
 ;;;; built-in classes
 
@@ -1096,7 +1078,9 @@ NIL is returned when no such class exists."
       :inherits (array)
       :prototype-form (make-array nil))
      (sequence
-      :translation (or cons (member nil) vector))
+      :translation (or cons (member nil) vector extended-sequence)
+      :state :read-only
+      :depth 2)
      (vector
       :translation vector :codes (#.sb!vm:complex-vector-widetag)
       :direct-superclasses (array sequence)
@@ -1472,8 +1456,7 @@ NIL is returned when no such class exists."
   (declare (type layout layout))
   (setf (layout-invalid layout) t
         (layout-depthoid layout) -1)
-  (dotimes (i layout-clos-hash-length)
-    (setf (layout-clos-hash layout i) 0))
+  (setf (layout-clos-hash layout) 0)
   (let ((inherits (layout-inherits layout))
         (classoid (layout-classoid layout)))
     (modify-classoid classoid)
@@ -1491,7 +1474,7 @@ NIL is returned when no such class exists."
 ;;; late in the build-order.lisp-expr sequence, and be put in
 ;;; !COLD-INIT-FORMS there?
 (defun !class-finalize ()
-  (dohash (name layout *forward-referenced-layouts*)
+  (dohash ((name layout) *forward-referenced-layouts*)
     (let ((class (find-classoid name nil)))
       (cond ((not class)
              (setf (layout-classoid layout) (make-undefined-classoid name)))