Photon Server API
Public Member Functions | Protected Member Functions | Properties | List of all members
ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue > Class Template Reference

This class is used to create instances that are unique per key in a multi-threaded environment. It uses a ReaderWriterLockSlim for read and write access to the Instances. Instance creations are synchronized with a Monitor on an object that is unique per key. This approach is designed to minimize the impact of long running instance creations on other threads. More...

Public Member Functions

 SynchronizedSingletonFactory (CreateMethodDelegate< TKey, TValue > defaultCreateMethod, int lockTimeout)
 Initializes a new instance of the SynchronizedSingletonFactory<TKey,TValue> class. More...
 
virtual bool Add (TKey key, TValue value)
 Adds a value if the key has not been added before. More...
 
void ForAll (Action< TValue > action)
 This method iterates over a copy of Instances and executes the action on each item. More...
 
TResult ForAll< TResult > (Func< TValue, TResult > selector, Func< TResult, TResult, TResult > aggregateFunction, TResult seed)
 This method iterates over a copy of Instances. The selector parameter selects a value of each instance. These values are combined with the aggregateFunction . More...
 
virtual TValue Get (TKey key, CreateMethodDelegate< TKey, TValue > createMethod)
 Gets an existing value for a key or creates a new one. More...
 
TValue Get (TKey key)
 Gets an existing value for a key or creates a new one with the default CreateMethod. More...
 
TValue GetBlockingInstance (TKey key, CreateMethodDelegate< TKey, TValue > createMethod)
 Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast. More...
 
TValue GetBlockingInstance (TKey key)
 Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast. More...
 
TValue GetBlockingOverall (TKey key, CreateMethodDelegate< TKey, TValue > createMethod)
 Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast. More...
 
TValue GetBlockingOverall (TKey key)
 Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast. More...
 
TValue GetNonBlocking (TKey key, CreateMethodDelegate< TKey, TValue > createMethod)
 Gets an existing value for a key or creates a new one. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces. More...
 
TValue GetNonBlocking (TKey key)
 Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces. More...
 
virtual bool Remove (TKey key)
 Removes a value from the Instances. More...
 
virtual bool TryGet (TKey key, out TValue value)
 Tries to get an existing value for the key. More...
 

Protected Member Functions

virtual void DoAdd (TKey key, TValue value)
 Adds a value to the Instances. Calling methods need to guard the Instances with a WriterLock. Calling methods are: Add, GetBlockingInstance, GetNonBlocking and GetBlockingOverall. More...
 
IDisposable ReaderLock ()
 Enters a critical read section. Exit the critical section by disposing the return value. More...
 
IDisposable WriterLock ()
 Enters a critical write section. Exit the critical section by disposing the return value. More...
 

Properties

int Count [get]
 Gets the number of added values. More...
 
CreateMethodDelegate< TKey, TValue > CreateMethod [get, set]
 Gets or sets the default creation method for values. More...
 
Dictionary< TKey, TValue > Instances [get]
 Gets a reference to the underlying dictionary that contains all existing instances. More...
 
int LockTimeout [get]
 Gets the maxium timeout for critical sections. More...
 

Detailed Description

This class is used to create instances that are unique per key in a multi-threaded environment. It uses a ReaderWriterLockSlim for read and write access to the Instances. Instance creations are synchronized with a Monitor on an object that is unique per key. This approach is designed to minimize the impact of long running instance creations on other threads.

Template Parameters
TKeyThe type of key.
TValueThe type of value.

Instance members are thread safe unless specified otherwise.

Constructor & Destructor Documentation

ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.SynchronizedSingletonFactory ( CreateMethodDelegate< TKey, TValue >  defaultCreateMethod,
int  lockTimeout 
)
inline

Initializes a new instance of the SynchronizedSingletonFactory<TKey,TValue> class.

Parameters
defaultCreateMethodThe default create method.
lockTimeoutThe max timeout to wait to enter a critical section.

Member Function Documentation

virtual bool ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.Add ( TKey  key,
TValue  value 
)
inlinevirtual

Adds a value if the key has not been added before.

Parameters
keyThe key.
valueThe value.
Returns
True if the value was added.
virtual void ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.DoAdd ( TKey  key,
TValue  value 
)
inlineprotectedvirtual

Adds a value to the Instances. Calling methods need to guard the Instances with a WriterLock. Calling methods are: Add, GetBlockingInstance, GetNonBlocking and GetBlockingOverall.

Parameters
keyThe key.
valueThe value.
void ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.ForAll ( Action< TValue >  action)
inline

This method iterates over a copy of Instances and executes the action on each item.

Parameters
actionThe action.
TResult ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.ForAll< TResult > ( Func< TValue, TResult >  selector,
Func< TResult, TResult, TResult >  aggregateFunction,
TResult  seed 
)
inline

This method iterates over a copy of Instances. The selector parameter selects a value of each instance. These values are combined with the aggregateFunction .

Parameters
selectorThe action that maps a value to each instance.
aggregateFunctionThe function that combines all selector results.
seedThe result value to start with.
Template Parameters
TResultThe type of the result value.
Returns
An aggregegated value from all instances.
virtual TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.Get ( TKey  key,
CreateMethodDelegate< TKey, TValue >  createMethod 
)
inlinevirtual

Gets an existing value for a key or creates a new one.

Parameters
keyThe key.
createMethodThe creation method.
Returns
The value.

The default implementation uses the GetBlockingInstance algorithm. Override to change behavior to GetNonBlocking or GetBlockingOverall

TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.Get ( TKey  key)
inline

Gets an existing value for a key or creates a new one with the default CreateMethod.

Parameters
keyThe key.
Returns
The value for the key.
TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.GetBlockingInstance ( TKey  key,
CreateMethodDelegate< TKey, TValue >  createMethod 
)
inline

Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast.

Parameters
keyThe key.
createMethodThe creation method.
Returns
The value.
TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.GetBlockingInstance ( TKey  key)
inline

Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a sync root that is unique per key. This algorithm is ideal for creation methods that do not return fast.

Parameters
keyThe key.
Returns
The value.
TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.GetBlockingOverall ( TKey  key,
CreateMethodDelegate< TKey, TValue >  createMethod 
)
inline

Gets an existing value for a key or creates a new one. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast.

Parameters
keyThe key.
createMethodThe creation method.
Returns
The value.
TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.GetBlockingOverall ( TKey  key)
inline

Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is guarded with a global WriterLock. This algorithm is ideal for creation methods that return very fast.

Parameters
keyThe key.
Returns
The value.
TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.GetNonBlocking ( TKey  key,
CreateMethodDelegate< TKey, TValue >  createMethod 
)
inline

Gets an existing value for a key or creates a new one. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces.

Parameters
keyThe key.
createMethodThe creation method.
Returns
The value.
TValue ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.GetNonBlocking ( TKey  key)
inline

Gets an existing value for a key or creates a new one with the default CreateMethod. The creation of a new instance is not guarded. This introduces a risk that the creation method is called multiple times for the same key at the same time. Only one of the created values is added. This algorithm is ideal for creation methods that are either not likely to be called multiple times at the same time or that have an unpredictable execution time and a low usage of local reosurces.

Parameters
keyThe key.
Returns
The value.
IDisposable ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.ReaderLock ( )
inlineprotected

Enters a critical read section. Exit the critical section by disposing the return value.

Returns
A disposable read lock.
Exceptions
LockTimeoutExceptionA read lock could not be obtained within the LockTimeout.
virtual bool ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.Remove ( TKey  key)
inlinevirtual

Removes a value from the Instances.

Parameters
keyThe key.
Returns
True if key was found and removed, otherwise false.
virtual bool ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.TryGet ( TKey  key,
out TValue  value 
)
inlinevirtual

Tries to get an existing value for the key.

Parameters
keyThe key.
valueThe value.
Returns
True if a value was found, otherwise false.
IDisposable ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.WriterLock ( )
inlineprotected

Enters a critical write section. Exit the critical section by disposing the return value.

Returns
A disposable write lock.
Exceptions
LockTimeoutExceptionA write lock could not be obtained within the LockTimeout.

Property Documentation

int ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.Count
get

Gets the number of added values.

CreateMethodDelegate<TKey, TValue> ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.CreateMethod
getset

Gets or sets the default creation method for values.

Dictionary<TKey, TValue> ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.Instances
getprotected

Gets a reference to the underlying dictionary that contains all existing instances.

Access to this dictionary needs to be syncronized with ReadLock or WriteLock.

int ExitGames.Threading.SynchronizedSingletonFactory< TKey, TValue >.LockTimeout
getprotected

Gets the maxium timeout for critical sections.