+ be removed.
+ :SYNCHRONIZED -- If NIL (the default), the hash-table may have
+ multiple concurrent readers, but results are undefined if a
+ thread writes to the hash-table concurrently with another
+ reader or writer. If T, all concurrent accesses are safe, but
+ note that CLHS 3.6 (Traversal Rules and Side Effects) remains
+ in force. See also: SB-EXT:WITH-LOCKED-HASH-TABLE. This keyword
+ argument is experimental, and may change incompatibly or be
+ removed in the future."
(declaim (inline update-hash-table-cache))
(defun update-hash-table-cache (hash-table index)
(unless (hash-table-weakness hash-table)
(setf (hash-table-cache hash-table) index)))
(declaim (inline update-hash-table-cache))
(defun update-hash-table-cache (hash-table index)
(unless (hash-table-weakness hash-table)
(setf (hash-table-cache hash-table) index)))
- `(with-concurrent-access-check ,hash-table
- ;; Inhibit GC for the duration of BODY if the GC might mutate the
- ;; HASH-TABLE in some way (currently true only if the table is
- ;; weak). We also need to lock the table to ensure that two
- ;; concurrent writers can't create a cyclical vector that would
- ;; cause scav_weak_hash_table_chain to loop.
- ;;
- ;; Otherwise we can avoid the 2x-3x overhead, and just pin the key.
- (if (hash-table-weakness ,hash-table)
- (sb!thread::with-recursive-system-spinlock
- ((hash-table-spinlock hash-table) :without-gcing t)
- ,@body)
- (with-pinned-objects ,pin-objects
- (locally
- ;; Inline the implementation function on the fast path
- ;; only. (On the slow path it'll just bloat the
- ;; generated code with no benefit).
- (declare (inline ,@inline))
- ,@body)))))
+ (with-unique-names (body-fun)
+ `(with-concurrent-access-check ,hash-table
+ (flet ((,body-fun ()
+ (locally (declare (inline ,@inline))
+ ,@body)))
+ (if (hash-table-weakness ,hash-table)
+ (sb!thread::with-recursive-system-spinlock
+ ((hash-table-spinlock ,hash-table) :without-gcing t)
+ (,body-fun))
+ (with-pinned-objects ,pin
+ (if ,synchronized
+ ;; We use a "system" spinlock here because it is very
+ ;; slightly faster, as it doesn't re-enable interrupts.
+ (sb!thread::with-recursive-system-spinlock
+ ((hash-table-spinlock ,hash-table))
+ (,body-fun))
+ (,body-fun))))))))
- ;; Search for key in the hash table.
- (multiple-value-bind (hashing eq-based)
- (funcall (hash-table-hash-fun hash-table) key)
- (declare (type hash hashing))
- (let* ((index-vector (hash-table-index-vector hash-table))
- (length (length index-vector))
+ ;; Search for key in the hash table.
+ (multiple-value-bind (hashing eq-based)
+ (funcall (hash-table-hash-fun hash-table) key)
+ (declare (type hash hashing))
+ (let* ((index-vector (hash-table-index-vector hash-table))
+ (length (length index-vector))
- (next (aref index-vector index))
- (next-vector (hash-table-next-vector hash-table))
- (hash-vector (hash-table-hash-vector hash-table))
- (test-fun (hash-table-test-fun hash-table)))
- (declare (type index index))
- ;; Search next-vector chain for a matching key.
- (if (or eq-based (not hash-vector))
+ (next (aref index-vector index))
+ (next-vector (hash-table-next-vector hash-table))
+ (hash-vector (hash-table-hash-vector hash-table))
+ (test-fun (hash-table-test-fun hash-table)))
+ (declare (type index index))
+ ;; Search next-vector chain for a matching key.
+ (if (or eq-based (not hash-vector))
- ;; Search for key in the hash table.
- (multiple-value-bind (hashing eq-based)
- (funcall (hash-table-hash-fun hash-table) key)
- (declare (type hash hashing))
- (let* ((index-vector (hash-table-index-vector hash-table))
- (length (length index-vector))
+ ;; Search for key in the hash table.
+ (multiple-value-bind (hashing eq-based)
+ (funcall (hash-table-hash-fun hash-table) key)
+ (declare (type hash hashing))
+ (let* ((index-vector (hash-table-index-vector hash-table))
+ (length (length index-vector))
- (next (aref index-vector index))
- (kv-vector (hash-table-table hash-table))
- (next-vector (hash-table-next-vector hash-table))
- (hash-vector (hash-table-hash-vector hash-table))
- (test-fun (hash-table-test-fun hash-table)))
- (declare (type index index next))
- (when (hash-table-weakness hash-table)
- (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype))
- (cond ((or eq-based (not hash-vector))
- (when eq-based
- (set-header-data kv-vector
- sb!vm:vector-valid-hashing-subtype))
- ;; Search next-vector chain for a matching key.
+ (next (aref index-vector index))
+ (kv-vector (hash-table-table hash-table))
+ (next-vector (hash-table-next-vector hash-table))
+ (hash-vector (hash-table-hash-vector hash-table))
+ (test-fun (hash-table-test-fun hash-table)))
+ (declare (type index index next))
+ (when (hash-table-weakness hash-table)
+ (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype))
+ (cond ((or eq-based (not hash-vector))
+ (when eq-based
+ (set-header-data kv-vector
+ sb!vm:vector-valid-hashing-subtype))
+ ;; Search next-vector chain for a matching key.
- ;; Pop a KV slot off the free list
- (let ((free-kv-slot (hash-table-next-free-kv hash-table)))
- (declare (type index/2 free-kv-slot))
- ;; Double-check for overflow.
- (aver (not (zerop free-kv-slot)))
- (setf (hash-table-next-free-kv hash-table)
- (aref next-vector free-kv-slot))
- (incf (hash-table-number-entries hash-table))
+ ;; Pop a KV slot off the free list
+ (let ((free-kv-slot (hash-table-next-free-kv hash-table)))
+ (declare (type index/2 free-kv-slot))
+ ;; Double-check for overflow.
+ (aver (not (zerop free-kv-slot)))
+ (setf (hash-table-next-free-kv hash-table)
+ (aref next-vector free-kv-slot))
+ (incf (hash-table-number-entries hash-table))
- (setf (aref kv-vector (* 2 free-kv-slot)) key)
- (setf (aref kv-vector (1+ (* 2 free-kv-slot))) value)
- ;; Setup the hash-vector if necessary.
- (when hash-vector
- (if (not eq-based)
- (setf (aref hash-vector free-kv-slot) hashing)
- (aver (= (aref hash-vector free-kv-slot)
- +magic-hash-vector-value+))))
- ;; Push this slot into the next chain.
- (setf (aref next-vector free-kv-slot) next)
+ (setf (aref kv-vector (* 2 free-kv-slot)) key)
+ (setf (aref kv-vector (1+ (* 2 free-kv-slot))) value)
+ ;; Setup the hash-vector if necessary.
+ (when hash-vector
+ (if (not eq-based)
+ (setf (aref hash-vector free-kv-slot) hashing)
+ (aver (= (aref hash-vector free-kv-slot)
+ +magic-hash-vector-value+))))
+ ;; Push this slot into the next chain.
+ (setf (aref next-vector free-kv-slot) next)
(setf (aref index-vector index) free-kv-slot)))
value))
(defun %puthash (key hash-table value)
(declare (type hash-table hash-table))
(aver (hash-table-index-vector hash-table))
(setf (aref index-vector index) free-kv-slot)))
value))
(defun %puthash (key hash-table value)
(declare (type hash-table hash-table))
(aver (hash-table-index-vector hash-table))
- (with-hash-table-locks (hash-table (%%puthash) key)
- (%%puthash key hash-table value)))))
+ ,(if lockedp
+ '(%%puthash key hash-table value)
+ '(with-hash-table-locks
+ (hash-table :inline (%%puthash) :pin (key)
+ :synchronized nil)
+ (%%puthash key hash-table value)))))))
+ (if (hash-table-synchronized-p hash-table)
+ (with-hash-table-locks (hash-table :pin (key) :synchronized t)
+ (put-it t))
+ (put-it nil))))
- ;; Search for key in the hash table.
- (multiple-value-bind (hashing eq-based)
- (funcall (hash-table-hash-fun hash-table) key)
- (declare (type hash hashing))
- (let* ((index-vector (hash-table-index-vector hash-table))
- (length (length index-vector))
+ ;; Search for key in the hash table.
+ (multiple-value-bind (hashing eq-based)
+ (funcall (hash-table-hash-fun hash-table) key)
+ (declare (type hash hashing))
+ (let* ((index-vector (hash-table-index-vector hash-table))
+ (length (length index-vector))
- (next (aref index-vector index))
- (table (hash-table-table hash-table))
- (next-vector (hash-table-next-vector hash-table))
- (hash-vector (hash-table-hash-vector hash-table))
- (test-fun (hash-table-test-fun hash-table)))
- (declare (type index index)
- (type index/2 next))
- (flet ((clear-slot (chain-vector prior-slot-location slot-location)
- (declare (type index/2 slot-location))
- ;; Mark slot as empty.
- (setf (aref table (* 2 slot-location)) +empty-ht-slot+
- (aref table (1+ (* 2 slot-location))) +empty-ht-slot+)
- ;; Update the prior pointer in the chain to skip this.
- (setf (aref chain-vector prior-slot-location)
- (aref next-vector slot-location))
- ;; Push KV slot onto free chain.
- (setf (aref next-vector slot-location)
- (hash-table-next-free-kv hash-table))
- (setf (hash-table-next-free-kv hash-table) slot-location)
- (when hash-vector
- (setf (aref hash-vector slot-location)
- +magic-hash-vector-value+))
- (decf (hash-table-number-entries hash-table))
- t))
- (cond ((zerop next)
- nil)
- ((if (or eq-based (not hash-vector))
- (eq key (aref table (* 2 next)))
- (and (= hashing (aref hash-vector next))
- (funcall test-fun key (aref table (* 2 next)))))
- (clear-slot index-vector index next))
- ;; Search next-vector chain for a matching key.
- ((or eq-based (not hash-vector))
- ;; EQ based
- (do ((prior next next)
+ (next (aref index-vector index))
+ (table (hash-table-table hash-table))
+ (next-vector (hash-table-next-vector hash-table))
+ (hash-vector (hash-table-hash-vector hash-table))
+ (test-fun (hash-table-test-fun hash-table)))
+ (declare (type index index)
+ (type index/2 next))
+ (flet ((clear-slot (chain-vector prior-slot-location slot-location)
+ (declare (type index/2 slot-location))
+ ;; Mark slot as empty.
+ (setf (aref table (* 2 slot-location)) +empty-ht-slot+
+ (aref table (1+ (* 2 slot-location))) +empty-ht-slot+)
+ ;; Update the prior pointer in the chain to skip this.
+ (setf (aref chain-vector prior-slot-location)
+ (aref next-vector slot-location))
+ ;; Push KV slot onto free chain.
+ (setf (aref next-vector slot-location)
+ (hash-table-next-free-kv hash-table))
+ (setf (hash-table-next-free-kv hash-table) slot-location)
+ (when hash-vector
+ (setf (aref hash-vector slot-location)
+ +magic-hash-vector-value+))
+ (decf (hash-table-number-entries hash-table))
+ t))
+ (cond ((zerop next)
+ nil)
+ ((if (or eq-based (not hash-vector))
+ (eq key (aref table (* 2 next)))
+ (and (= hashing (aref hash-vector next))
+ (funcall test-fun key (aref table (* 2 next)))))
+ (clear-slot index-vector index next))
+ ;; Search next-vector chain for a matching key.
+ ((or eq-based (not hash-vector))
+ ;; EQ based
+ (do ((prior next next)
-the key and value of the entry. Return NIL."
+the key and value of the entry. Return NIL.
+
+Consequences are undefined if HASH-TABLE is mutated during the call to
+MAPHASH, except for changing or removing elements corresponding to the
+current key. The applies to all threads, not just the current one --
+even for synchronized hash-tables. If the table may be mutated by
+another thread during iteration, use eg. SB-EXT:WITH-LOCKED-HASH-TABLE
+to protect the MAPHASH call."