@@ -5,17 +5,20 @@ use std::fmt::{self, Debug};
55use std:: hash:: { BuildHasher , Hash } ;
66use std:: mem:: replace;
77use std:: ops:: { Deref , DerefMut , RangeInclusive } ;
8- use std :: pin :: pin ;
8+ # [ cfg ( not ( feature = "loom" ) ) ]
99use std:: sync:: atomic:: AtomicUsize ;
1010use std:: sync:: atomic:: Ordering :: Relaxed ;
1111
12+ #[ cfg( feature = "loom" ) ]
13+ use loom:: sync:: atomic:: AtomicUsize ;
1214use sdd:: { AtomicShared , Guard , Shared , Tag } ;
1315
1416use super :: Equivalent ;
17+ use super :: async_helper:: fake_guard;
18+ use super :: hash_table:: MAXIMUM_CAPACITY_LIMIT ;
1519use super :: hash_table:: bucket:: { CACHE , DoublyLinkedList , EntryPtr } ;
1620use super :: hash_table:: bucket_array:: BucketArray ;
1721use super :: hash_table:: { HashTable , LockedBucket } ;
18- use crate :: async_helper:: AsyncGuard ;
1922
2023/// Scalable concurrent 32-way associative cache backed by [`HashMap`](super::HashMap).
2124///
@@ -189,7 +192,7 @@ where
189192 let maximum_capacity = maximum_capacity
190193 . max ( minimum_capacity. load ( Relaxed ) )
191194 . max ( BucketArray :: < K , V , DoublyLinkedList , CACHE > :: minimum_capacity ( ) )
192- . min ( 1_usize << ( usize :: BITS - 1 ) )
195+ . min ( MAXIMUM_CAPACITY_LIMIT )
193196 . next_power_of_two ( ) ;
194197 HashCache {
195198 bucket_array : array,
@@ -219,10 +222,9 @@ where
219222 #[ inline]
220223 pub async fn entry_async ( & self , key : K ) -> Entry < ' _ , K , V , H > {
221224 let hash = self . hash ( & key) ;
222- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
223- let locked_bucket = self . writer_async ( hash, & async_guard) . await ;
224- let prolonged_guard = self . prolonged_guard_ref ( async_guard. guard ( ) ) ;
225- let entry_ptr = locked_bucket. search ( & key, hash, prolonged_guard) ;
225+ let locked_bucket = self . writer_async ( hash) . await ;
226+ let fake_guard = fake_guard ( ) ;
227+ let entry_ptr = locked_bucket. search ( & key, hash, fake_guard) ;
226228 if entry_ptr. is_valid ( ) {
227229 Entry :: Occupied ( OccupiedEntry {
228230 hashcache : self ,
@@ -341,14 +343,13 @@ where
341343 #[ inline]
342344 pub async fn put_async ( & self , key : K , val : V ) -> Result < EvictedEntry < K , V > , ( K , V ) > {
343345 let hash = self . hash ( & key) ;
344- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
345- let locked_bucket = self . writer_async ( hash, & async_guard) . await ;
346- let guard = async_guard. guard ( ) ;
347- if locked_bucket. search ( & key, hash, guard) . is_valid ( ) {
346+ let locked_bucket = self . writer_async ( hash) . await ;
347+ let fake_guard = fake_guard ( ) ;
348+ if locked_bucket. search ( & key, hash, fake_guard) . is_valid ( ) {
348349 Err ( ( key, val) )
349350 } else {
350351 let evicted = locked_bucket. evict_lru_head ( locked_bucket. data_block ) ;
351- let entry_ptr = locked_bucket. insert ( hash, ( key, val) , guard ) ;
352+ let entry_ptr = locked_bucket. insert ( hash, ( key, val) , fake_guard ) ;
352353 locked_bucket. update_lru_tail ( & entry_ptr) ;
353354 Ok ( evicted)
354355 }
@@ -441,10 +442,9 @@ where
441442 #[ inline]
442443 pub async fn replace_async ( & self , key : K ) -> ReplaceResult < ' _ , K , V , H > {
443444 let hash = self . hash ( & key) ;
444- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
445- let locked_bucket = self . writer_async ( hash, & async_guard) . await ;
446- let prolonged_guard = self . prolonged_guard_ref ( async_guard. guard ( ) ) ;
447- let mut entry_ptr = locked_bucket. search ( & key, hash, prolonged_guard) ;
445+ let locked_bucket = self . writer_async ( hash) . await ;
446+ let fake_guard = fake_guard ( ) ;
447+ let mut entry_ptr = locked_bucket. search ( & key, hash, fake_guard) ;
448448 if entry_ptr. is_valid ( ) {
449449 let prev_key = replace (
450450 & mut entry_ptr
@@ -610,10 +610,9 @@ where
610610 Q : Equivalent < K > + Hash + ?Sized ,
611611 {
612612 let hash = self . hash ( key) ;
613- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
614- let locked_bucket = self . optional_writer_async ( hash, & async_guard) . await ?;
615- let prolonged_guard = self . prolonged_guard_ref ( async_guard. guard ( ) ) ;
616- let entry_ptr = locked_bucket. search ( key, hash, prolonged_guard) ;
613+ let locked_bucket = self . optional_writer_async ( hash) . await ?;
614+ let fake_guard = fake_guard ( ) ;
615+ let entry_ptr = locked_bucket. search ( key, hash, fake_guard) ;
617616 if entry_ptr. is_valid ( ) {
618617 locked_bucket. writer . update_lru_tail ( & entry_ptr) ;
619618 return Some ( OccupiedEntry {
@@ -686,8 +685,7 @@ where
686685 Q : Equivalent < K > + Hash + ?Sized ,
687686 {
688687 let hash = self . hash ( key) ;
689- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
690- self . reader_async ( key, hash, reader, & async_guard) . await
688+ self . reader_async ( key, hash, reader) . await
691689 }
692690
693691 /// Reads a key-value pair.
@@ -731,7 +729,8 @@ where
731729 where
732730 Q : Equivalent < K > + Hash + ?Sized ,
733731 {
734- self . read_async ( key, |_, _| ( ) ) . await . is_some ( )
732+ let hash = self . hash ( key) ;
733+ self . reader_async ( key, hash, |_, _| ( ) ) . await . is_some ( )
735734 }
736735
737736 /// Returns `true` if the [`HashCache`] contains a value for the specified key.
@@ -778,11 +777,11 @@ where
778777 Q : Equivalent < K > + Hash + ?Sized ,
779778 {
780779 let hash = self . hash ( key) ;
781- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
782- let mut locked_bucket = self . optional_writer_async ( hash , & async_guard ) . await ? ;
783- let mut entry_ptr = locked_bucket. search ( key, hash, async_guard . guard ( ) ) ;
780+ let mut locked_bucket = self . optional_writer_async ( hash ) . await ? ;
781+ let fake_guard = fake_guard ( ) ;
782+ let mut entry_ptr = locked_bucket. search ( key, hash, fake_guard ) ;
784783 if entry_ptr. is_valid ( ) && condition ( & mut locked_bucket. entry_mut ( & mut entry_ptr) . 1 ) {
785- Some ( locked_bucket. remove ( self , & mut entry_ptr, async_guard . guard ( ) ) )
784+ Some ( locked_bucket. remove ( self , & mut entry_ptr, & Guard :: new ( ) ) )
786785 } else {
787786 None
788787 }
@@ -845,12 +844,11 @@ where
845844 /// ```
846845 #[ inline]
847846 pub async fn iter_async < F : FnMut ( & K , & V ) -> bool > ( & self , mut f : F ) -> bool {
848- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
849847 let mut result = true ;
850- self . for_each_reader_async ( & async_guard , |reader, data_block| {
851- let guard = async_guard . guard ( ) ;
852- let mut entry_ptr = EntryPtr :: new ( guard ) ;
853- while entry_ptr. move_to_next ( & reader, guard ) {
848+ self . for_each_reader_async ( |reader, data_block| {
849+ let fake_guard = fake_guard ( ) ;
850+ let mut entry_ptr = EntryPtr :: new ( fake_guard ) ;
851+ while entry_ptr. move_to_next ( & reader, fake_guard ) {
854852 let ( k, v) = entry_ptr. get ( data_block) ;
855853 if !f ( k, v) {
856854 result = false ;
@@ -937,17 +935,16 @@ where
937935 & self ,
938936 mut f : F ,
939937 ) -> bool {
940- let async_guard = pin ! ( AsyncGuard :: default ( ) ) ;
941938 let mut result = true ;
942- self . for_each_writer_async ( 0 , 0 , & async_guard , |mut locked_bucket, removed| {
943- let guard = async_guard . guard ( ) ;
944- let mut entry_ptr = EntryPtr :: new ( guard ) ;
945- while entry_ptr. move_to_next ( & locked_bucket. writer , guard ) {
939+ self . for_each_writer_async ( 0 , 0 , |mut locked_bucket, removed| {
940+ let fake_guard = fake_guard ( ) ;
941+ let mut entry_ptr = EntryPtr :: new ( fake_guard ) ;
942+ while entry_ptr. move_to_next ( & locked_bucket. writer , fake_guard ) {
946943 let consumable_entry = ConsumableEntry {
947944 locked_bucket : & mut locked_bucket,
948945 entry_ptr : & mut entry_ptr,
949946 remove_probe : removed,
950- guard,
947+ guard : fake_guard ,
951948 } ;
952949 if !f ( consumable_entry) {
953950 result = false ;
@@ -1329,12 +1326,12 @@ where
13291326 }
13301327
13311328 #[ inline]
1332- fn bucket_array ( & self ) -> & AtomicShared < BucketArray < K , V , DoublyLinkedList , CACHE > > {
1329+ fn bucket_array_var ( & self ) -> & AtomicShared < BucketArray < K , V , DoublyLinkedList , CACHE > > {
13331330 & self . bucket_array
13341331 }
13351332
13361333 #[ inline]
1337- fn minimum_capacity ( & self ) -> & AtomicUsize {
1334+ fn minimum_capacity_var ( & self ) -> & AtomicUsize {
13381335 & self . minimum_capacity
13391336 }
13401337
0 commit comments