LockedResourceMap< HASHTYPE, ENTRY > Class Template Reference 线程

#include <locked_resource_map.h>

Inheritance diagram for LockedResourceMap< HASHTYPE, ENTRY >:

详细描述

template<typename HASHTYPE, typename ENTRY>
class maxon::LockedResourceMap< HASHTYPE, ENTRY >

LockedResourceMap simplifies the creation, calculation or load of cpu expensive resources from different threads. E.g. If the program want to load images from different threads but want to keep it only once in memory this class manages all locking and threading using ConditionVariables for ideal performance with minimum locking. A callback must be provided in the CreateOrGetEntry function to load/calculate the value when the key is created the first time.

范例:

LockedResourceMap<Url, ImageLayerRef> g_lockedResource; Url url = ...;
ImageLayerRef image = g_lockedResource.CreateOrGetEntryCopy(url, []( const Url& url, ImageLayerRef* createResource) -> Result<void> { iferr_scope ; ImageLayerRef img = ImageLayerClasses::RASTER().Create() iferr_return ; img.Load(url) iferr_return ; *createResource = std::move(img); return OK ; }) iferr_return ; ... use 'image' for whatever ...

THREADSAFE.

Template Parameters
HASHTYPE Hash key type of the class. Each entry is stored under this unique hash key.
ENTRY Value to store under the key.

公共成员函数

  LockedResourceMap ()
  LockedResourceMap ( LockedResourceMap && src )
  MAXON_OPERATOR_MOVE_ASSIGNMENT ( LockedResourceMap )
Result < ENTRY & >  CreateOrGetEntry (const HASHTYPE &hashKey, const Delegate < Result < void >(const HASHTYPE &hashKey, ENTRY *createResource)> &createdCallback)
Result < ENTRY >  CreateOrGetEntryCopy (const HASHTYPE &hashKey, const Delegate < Result < void >(const HASHTYPE &hashKey, ENTRY *createResource)> &createdCallback)
Spinlock GetLock ()
Bool   IsChanged () const
void  SetChanged ( Bool changed)
-  Public Member Functions inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
  HashMap ()
  HashMap (const DefaultAllocator &alloc)
  HashMap ( HashMap &&src)
  ~HashMap ()
  MAXON_OPERATOR_MOVE_ASSIGNMENT ( HashMap )
ResultMem   SetCapacityHint ( Int capacity, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
ResultMem   ResizeTable ( Int capacity)
void  重置 ()
void  Flush ()
Int   GetCount () const
Int   GetTableSize () const
Int   GetMemorySize () const
Int   GetNonEmptyBucketCount () const
Entry *  GetNonEmptyBucket ( Int i)
const Entry *  GetNonEmptyBucket ( Int i) const
Int   GetOperationCountForSearch () const
SFINAEHelper < Bool , typename MAP::IsHashMap >::type  IsEqualImpl (const MAP &other, COMPARE &&cmp, OverloadRank1 ) const
Entry *  Find (const KEY &key)
const Entry *  Find (const KEY &key) const
LockedResourceHelper < ENTRY > *  FindValue (const KEY &key)
const LockedResourceHelper < ENTRY > *  FindValue (const KEY &key) const
Result < Entry * >  InsertCtor ( KEY &&key, C &&constructor, Bool &created= BoolLValue ())
ResultRef < Entry >  InsertEntry (const HASHTYPE &key, Bool &created= BoolLValue ())
ResultRef < Entry >  InsertEntry (HASHTYPE &&key, Bool &created= BoolLValue ())
ResultRef < Entry >  InsertEntry ( KEY &&key, Bool &created= BoolLValue ())
ResultRef < LockedResourceHelper < ENTRY > >  InsertKey (const HASHTYPE &key, Bool &created= BoolLValue ())
ResultRef < LockedResourceHelper < ENTRY > >  InsertKey (HASHTYPE &&key, Bool &created= BoolLValue ())
ResultRef < LockedResourceHelper < ENTRY > >  InsertKey ( KEY &&key, Bool &created= BoolLValue ())
Result < Entry * >  InsertLambda ( KEY &&key, LAMBDA &&lambda)
ResultRef < Entry >  Insert ( KEY &&key, const LockedResourceHelper < ENTRY > &value, Bool &created= BoolLValue ())
ResultRef < Entry >  Insert ( KEY &&key, LockedResourceHelper < ENTRY > &&value, Bool &created= BoolLValue ())
ResultRef < Entry >  InsertMultiEntry ( KEY &&key)
ResultMem   InsertMultiEntry (Entry *e, UInt hash)
ResultOk < void >  Erase (const Entry *entry, Bool deleteEntry=true)
ResultOk < void >  Erase (Entry *entry, Bool deleteEntry=true)
ResultOk < void >  Erase (const Entry &entry, Bool deleteEntry=true)
ResultOk < void >  Erase (Entry &entry, Bool deleteEntry=true)
ResultOk < Bool Erase (const KEY &key)
IteratorTemplate< SUPER >  Erase (const IteratorTemplate< SUPER > &it, Bool deleteEntry=true)
void  DeleteEntry (const Entry *e)
Result < void >  IntersectImpl ( SET &&set, OverloadRank0 )
Result < void >  CopyFromImpl (HASHMAP &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank1 )
SFINAEHelper < Result < void >, typename std::remove_reference< S >::type::MapType >::type  AppendAllImpl ( S &&src, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank1 )
MultiEntryIterator< false >  FindAll (const HASHTYPE &key)
MultiEntryIterator< true >  FindAll (const HASHTYPE &key) const
KeyIterator   GetKeys ()
ConstKeyIterator   GetKeys () const
ValueIterator   GetValues ()
ConstValueIterator   GetValues () const
Iterator   Begin ()
ConstIterator   Begin () const
Iterator   End ()
ConstIterator   End () const
Iterator   GetIterator (const Entry *e)
ConstIterator   GetIterator (const Entry *e) const
SFINAEHelper < String , LockedResourceHelper < ENTRY > >::type  ToString (const FormatStatement *formatStatement=nullptr) const
const Entry *  FindEntryImpl ( UInt hash, const KEY &key) const
Entry *  FindEntryImpl ( UInt hash, const KEY &key)
-  Public Member Functions inherited from MapBase< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, HASHTYPE, LockedResourceHelper< ENTRY >, EmptyClass, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE   MapBase (ARGS &&... args)
MapImpl < HashMap < HASHTYPE, LockedResourceHelper < ENTRY >, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 > & >  ToMap ()
MapImpl < const HashMap < HASHTYPE, LockedResourceHelper < ENTRY >, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 > & >  ToMap () const
MAXON_ATTRIBUTE_FORCE_INLINE   operator MapImpl< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 > & > ()
MAXON_ATTRIBUTE_FORCE_INLINE   operator MapImpl< const HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 > & > () const
-  Public Member Functions inherited from MapBase0< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, HASHTYPE, LockedResourceHelper< ENTRY >, EmptyClass, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE   MapBase0 (ARGS &&... args)
MAXON_ATTRIBUTE_FORCE_INLINE Bool   Contains (typename ByValueParam < HASHTYPE >::type key) const
MAXON_ATTRIBUTE_FORCE_INLINE SFINAEHelper < Bool , typename PAIR::KeyType >::type  Contains (const PAIR &pair) const
ResultRef < LockedResourceHelper < ENTRY > >  Append (const HASHTYPE &key)
SFINAEHelper < ResultRef < LockedResourceHelper < ENTRY > >, typename PAIR::KeyType >::type  Append (const PAIR &pair)
Result < void >  添加 (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
Result < void >  AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
Result < void >  AppendAllInverse (COLLECTION2 &&other)
Bool   ContainsAllImpl (COLLECTION2 &&other, OverloadRank0 ) const
Result < void >  SubtractImpl (COLLECTION2 &&other, OverloadRank0 )
Bool   IsEqualImpl (const COLLECTION2 &other, COMPARE &&cmp, OverloadRank0 ) const
HashInt   GetHashCode () const
-  Public Member Functions inherited from BaseCollection< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, EmptyClass >
MAXON_ATTRIBUTE_FORCE_INLINE   BaseCollection (ARGS &&... args)
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type  operator== (const COLLECTION2 &other) const
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type  operator!= (const COLLECTION2 &other) const
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value &&! STD_IS_REPLACEMENT (same, typename std::decay< COMPARE >::type, EQUALITY ), Bool >::type  IsEqual (const COLLECTION2 &other, COMPARE &&cmp=COMPARE()) const
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  CopyFrom (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE )
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  Subtract (COLLECTION2 &&other)
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  Intersect (const COLLECTION2 &other)
Bool   Intersects (const COLLECTION2 &other) const
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0 )
Result < void >  AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0 )
Result < void >  IntersectImpl (COLLECTION2 &&other, OverloadRank0 )
MAXON_ATTRIBUTE_FORCE_INLINE Bool   IsEmpty () const
MAXON_ATTRIBUTE_FORCE_INLINE Bool   IsPopulated () const
String   ToString (const FormatStatement *formatStatement=nullptr) const
MAXON_ATTRIBUTE_FORCE_INLINE Bool   ContainsAll (COLLECTION2 &&other) const
Bool   ContainsAllImpl (COLLECTION2 &&other, OverloadRank0 ) const

私有成员函数

  MAXON_DISALLOW_COPY_AND_ASSIGN ( LockedResourceMap )

Private Attributes

Spinlock   _hashLock
Bool   _changed

Additional Inherited Members

-  Public Types inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
using  Super = MapBase < HashMap , HASHTYPE, LockedResourceHelper < ENTRY >, EmptyClass , DefaultCompare >
using  HashType = DefaultCompare
using  IsHashMap = std::true_type
using  Iterator = IteratorTemplate< EntryIteratorBase >
using  ConstIterator = ConstIteratorTemplate< EntryIteratorBase >
using  KeyIterator = IteratorTemplate< KeyIteratorBase >
using  ConstKeyIterator = ConstIteratorTemplate< KeyIteratorBase >
using  ValueIterator = IteratorTemplate< ValueIteratorBase >
using  ConstValueIterator = ConstIteratorTemplate< ValueIteratorBase >
-  Public Types inherited from MapBase0< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, HASHTYPE, LockedResourceHelper< ENTRY >, EmptyClass, DefaultCompare >
using  MapType = HashMap < HASHTYPE, LockedResourceHelper < ENTRY >, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 >
using  Super = BaseCollection < HashMap < HASHTYPE, LockedResourceHelper < ENTRY >, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 >, EmptyClass >
using  KeyType = HASHTYPE
using  ValueType = LockedResourceHelper < ENTRY >
-  Public Types inherited from BaseCollection< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, EmptyClass >
using  IsCollection = std::true_type
-  Static Public Member Functions inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
static const Entry *  GetEntry (const LockedResourceHelper < ENTRY > *value)
static Entry *  GetEntry (typename std::remove_const< LockedResourceHelper < ENTRY > >::type *value)
-  Static Public Member Functions inherited from MapBase0< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, HASHTYPE, LockedResourceHelper< ENTRY >, EmptyClass, DefaultCompare >
static const HASHTYPE &  GetMapKey (const HASHTYPE &key)
static const HASHTYPE &  GetMapKey (const PAIR &pair)
-  Static Public Attributes inherited from MapBase0< HashMap< HASHTYPE, LockedResourceHelper< ENTRY >, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, HASHTYPE, LockedResourceHelper< ENTRY >, EmptyClass, DefaultCompare >
static const COLLECTION_KIND   KIND
-  Protected Types inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
using  Bucket = typename std::conditional< HASHMAP_MODE::DEFAULT==HASHMAP_MODE::DEFAULT, DefaultBucket, SimpleBucket >::type
-  Protected Member Functions inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
Bool   ResizeTableImpl ( Int length)
Entry *  AddEntryImpl (Entry *e, Entry *prev, Bool &created, Bool multi, void *)
Entry *  AddEntryImpl (Entry *e, Entry *prev, Bool &created, Bool multi, Char *)
  MAXON_DISALLOW_COPY_AND_ASSIGN ( HashMap )
const Char GetSignature (void *) const
const Char GetSignature ( Char *) const
-  Static Protected Member Functions inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
static Entry *  LoadRelaxed ( AtomicPtr < Entry > &e)
static Entry *  LoadRelaxed (Entry *e)
static Entry *  LoadAcquire ( AtomicPtr < Entry > &e)
static Entry *  LoadAcquire (Entry *e)
static void  StoreRelaxed ( AtomicPtr < Entry > &e, Entry *newValue)
static void  StoreRelaxed (Entry *&e, Entry *newValue)
static Bool   TryCompareAndSwap ( AtomicPtr < Entry > &e, Entry *newValue, Entry *compare)
static Bool   TryCompareAndSwap (Entry *&e, Entry *newValue, Entry *compare)
-  Protected Attributes inherited from HashMap< HASHTYPE, LockedResourceHelper< ENTRY > >
DefaultAllocator   _allocator
Bucket _table
Int   _tableLengthM1
Bucket **  _nonemptyBuckets
Int   _nonemptyBucketCount
Int   _size
Int   _resizeThreshold
const Float   _obsoleteLoadFactor

构造函数 & 析构函数文档编制

◆  LockedResourceMap() [1/2]

LockedResourceMap ( )

◆  LockedResourceMap() [2/2]

LockedResourceMap ( LockedResourceMap < HASHTYPE, ENTRY > &&  src )

成员函数文档编制

◆  MAXON_DISALLOW_COPY_AND_ASSIGN()

MAXON_DISALLOW_COPY_AND_ASSIGN ( LockedResourceMap < HASHTYPE, ENTRY >  )
private

◆  MAXON_OPERATOR_MOVE_ASSIGNMENT()

MAXON_OPERATOR_MOVE_ASSIGNMENT ( LockedResourceMap < HASHTYPE, ENTRY >  )

◆  CreateOrGetEntry()

Result <ENTRY&> CreateOrGetEntry ( const HASHTYPE &  hashKey ,
const Delegate < Result < void >(const HASHTYPE &hashKey, ENTRY *createResource)> &  createdCallback  
)

Creates the value if the key is not created yet or gets the value for the given hash key.

参数
[in] hashKey Key under which a resource should be created and returned.
[in] createdCallback If the key is accessed the first time this callback will be called. The calculation/loading is lock free. All other threads that ask for the same resource in the meanwhile will put on hold using a ConditionVariable.
返回
Returns the reference to the entry for the given hashKey. The map is still locked. You must call GetLock() . Unlock() as soon as you don't need access to the element anymore. Keep the lock as short as possible. The best way is to copy the data you need and unlock it. In case of error the hash map lock is already released.

◆  CreateOrGetEntryCopy()

Result <ENTRY> CreateOrGetEntryCopy ( const HASHTYPE &  hashKey ,
const Delegate < Result < void >(const HASHTYPE &hashKey, ENTRY *createResource)> &  createdCallback  
)

Creates the value if the key is not created yet or gets a copy of the value for the given hash key. This function is ideal to use with reference counted objects (e.g. ImageLayerRef, ObjectRef).

参数
[in] hashKey Key under which a resource should be created and returned.
[in] createdCallback If the key is accessed the first time this callback will be called. The calculation/loading is lock free. All other threads that ask for the same resource in the meanwhile will put on hold using a ConditionVariable.
返回
Returns a copy of the entry for the given hashKey. The map is NOT locked after calling this function.

◆  GetLock()

Spinlock & GetLock ( )

◆  IsChanged()

Bool IsChanged ( ) const

◆  SetChanged()

void SetChanged ( Bool   changed )

Member Data Documentation

◆  _hashLock

Spinlock _hashLock
private

◆  _changed

Bool _changed
private
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
iferr_scope
#define iferr_scope
定义: resultbase.h:1343