/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (m_valueFactory == PUBLICATION_ONLY_OR_ALREADY_INITIALIZED) { throw new InvalidOperationException("Lazy_Value_RecursiveCallsToValue"); } Func <T> factory = m_valueFactory; m_valueFactory = PUBLICATION_ONLY_OR_ALREADY_INITIALIZED; boxed = new Boxed(factory()); } catch (Exception ex) { m_boxed = new LazyInternalExceptionHolder(ex); throw; } } else { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } return(boxed); }
private T LazyInitValue() { Boxed boxed = null; if (_lock == null) { boxed = new Boxed(_value()); } else { lock (_lock) { if (_boxed == null) { _boxed = boxed = new Boxed(_value()); } } //bool locked = false; //try //{ // Monitor.Enter(_lock, ref locked); // if(_boxed == null) // _boxed = boxed = new Boxed(_value()); //} //finally //{ // if(locked) // Monitor.Exit(_lock); //} } return(boxed.value); }
/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && _valueFactory == s_ALREADY_INVOKED_SENTINEL) { throw new InvalidOperationException(SR.Lazy_Value_RecursiveCallsToValue); } Func <T> factory = _valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { _valueFactory = s_ALREADY_INVOKED_SENTINEL; } else if (factory == s_ALREADY_INVOKED_SENTINEL) { // Another thread raced with us and beat us to successfully invoke the factory. return(null); } boxed = new Boxed(factory()); } catch (Exception ex) { if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { _boxed = new LazyInternalExceptionHolder(ex); } throw; } } else { try { boxed = new Boxed(Activator.CreateInstance <T>()); } catch (MissingMethodException) { Exception ex = new MissingMethodException(SR.Lazy_CreateValue_NoParameterlessCtorForT); if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { _boxed = new LazyInternalExceptionHolder(ex); } throw ex; } } return(boxed); }
/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == ALREADY_INVOKED_SENTINEL) { throw new InvalidOperationException("Should not try to create the value more than once"); } Func2 <T> factory = m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { m_valueFactory = ALREADY_INVOKED_SENTINEL; } else if (factory == ALREADY_INVOKED_SENTINEL) { // Another thread raced to successfully invoke the factory. return(null); } boxed = new Boxed(factory()); } catch (Exception ex) { if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw; } } else { try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (MissingMethodException) { Exception ex = new MissingMemberException(string.Format("The type {0} does not have parameterless constructor", typeof(T).FullName)); if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw ex; } } return(boxed); }
/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == ALREADY_INVOKED_SENTINEL) { throw new InvalidOperationException(Environment.GetResourceString("Lazy_Value_RecursiveCallsToValue")); } Func <T> factory = m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { m_valueFactory = ALREADY_INVOKED_SENTINEL; } else if (factory == ALREADY_INVOKED_SENTINEL) { // Another thread ----d with us and beat us to successfully invoke the factory. return(null); } boxed = new Boxed(factory()); } catch (Exception ex) { if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw; } } else { try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (System.MissingMethodException) { Exception ex = new System.MissingMemberException(Environment.GetResourceString("Lazy_CreateValue_NoParameterlessCtorForT")); if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw ex; } } return(boxed); }
/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == PUBLICATION_ONLY_OR_ALREADY_INITIALIZED) { throw new InvalidOperationException(Environment2.GetResourceString("Lazy_Value_RecursiveCallsToValue")); } Func <T> factory = m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { m_valueFactory = PUBLICATION_ONLY_OR_ALREADY_INITIALIZED; } boxed = new Boxed(factory()); } catch (Exception ex) { if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { #if PFX_LEGACY_3_5 m_boxed = new LazyInternalExceptionHolder(ex); #else m_boxed = new LazyInternalExceptionHolder(ex.PrepForRemoting());// copy the call stack by calling the internal method PrepForRemoting #endif } throw; } } else { try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (System.MissingMethodException) { Exception ex = new System.MissingMemberException(Environment2.GetResourceString("Lazy_CreateValue_NoParameterlessCtorForT")); if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw ex; } } return(boxed); }
private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == PUBLICATION_ONLY_OR_ALREADY_INITIALIZED) { throw new InvalidOperationException(); } Func <T> factory = m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { m_valueFactory = PUBLICATION_ONLY_OR_ALREADY_INITIALIZED; } boxed = new Boxed(factory()); } catch (Exception ex) { if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw; } } else { try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (System.MissingMethodException) { Exception ex = new System.MissingMemberException("No parameterless constructor found for " + typeof(T).FullName); if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw ex; } } return(boxed); }
private Boxed CreateValue() { Boxed boxed; LazyThreadSafetyMode mode = Mode; if (m_ValueFactory != null) { try { if ((mode != LazyThreadSafetyMode.PublicationOnly) && (m_ValueFactory == s_PublicationOnlyOrAlreadyInitialized)) { throw new InvalidOperationException("Lazy_Value_RecursiveCallsToValue"); } Func <T> valueFactory = m_ValueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) { m_ValueFactory = s_PublicationOnlyOrAlreadyInitialized; } return(new Boxed(valueFactory())); } catch (Exception exception) { if (mode != LazyThreadSafetyMode.PublicationOnly) { m_Boxed = new LazyInternalExceptionHolder(exception); } throw; } } try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (MissingMethodException) { Exception ex = new MissingMemberException("Lazy_CreateValue_NoParameterlessCtorForT"); if (mode != LazyThreadSafetyMode.PublicationOnly) { m_Boxed = new LazyInternalExceptionHolder(ex); } throw ex; } return(boxed); }
/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == PUBLICATION_ONLY_OR_ALREADY_INITIALIZED) { throw new InvalidOperationException("ValueFactory attempted to access the Value property of this instance."); } Func <T> factory = m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { m_valueFactory = PUBLICATION_ONLY_OR_ALREADY_INITIALIZED; } boxed = new Boxed(factory()); } catch (Exception ex) { if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { #if PFX_LEGACY_3_5 m_boxed = new LazyInternalExceptionHolder(ex); #else m_boxed = new LazyInternalExceptionHolder(ex.PrepForRemoting());// copy the call stack by calling the internal method PrepForRemoting #endif } throw; } } else { try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (System.MissingMethodException) { Exception ex = new System.MissingMemberException("The lazily-initialized type does not have a public, parameterless constructor."); if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode { m_boxed = new LazyInternalExceptionHolder(ex); } throw ex; } } return(boxed); }
/// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary> /// <returns>An instance of Boxed.</returns> private Boxed CreateValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_valueFactory != null) { try { // check for recursion if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == ALREADY_INVOKED_SENTINEL) { throw new InvalidOperationException(); } Func <T> factory = m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes { m_valueFactory = ALREADY_INVOKED_SENTINEL; } else if (factory == ALREADY_INVOKED_SENTINEL) { // Another thread ----d with us and beat us to successfully invoke the factory. return(null); } boxed = new Boxed(factory()); } catch (Exception ex) { throw; } } else { try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); } catch (System.MissingMethodException) { Exception ex = new System.MissingMemberException(); throw ex; } } return(boxed); }
private Boxed <T> CreateValue() { Boxed <T> boxed = null; LazyThreadSafetyMode mode = this.Mode; if (this.m_valueFactory != null) { try { if ((mode != LazyThreadSafetyMode.PublicationOnly) && (this.m_valueFactory == Lazy <T> .PUBLICATION_ONLY_OR_ALREADY_INITIALIZED)) { throw new InvalidOperationException(Environment.GetResourceString("Lazy_Value_RecursiveCallsToValue")); } Func <T> valueFactory = this.m_valueFactory; if (mode != LazyThreadSafetyMode.PublicationOnly) { this.m_valueFactory = Lazy <T> .PUBLICATION_ONLY_OR_ALREADY_INITIALIZED; } return(new Boxed <T>(valueFactory())); } catch (Exception exception) { if (mode != LazyThreadSafetyMode.PublicationOnly) { this.m_boxed = new LazyInternalExceptionHolder <T>(exception.PrepForRemoting()); } throw; } } try { boxed = new Boxed <T>((T)Activator.CreateInstance(typeof(T))); } catch (MissingMethodException) { Exception ex = new MissingMemberException(Environment.GetResourceString("Lazy_CreateValue_NoParameterlessCtorForT")); if (mode != LazyThreadSafetyMode.PublicationOnly) { this.m_boxed = new LazyInternalExceptionHolder <T>(ex); } throw ex; } return(boxed); }
private T LazyInitValue() { Boxed boxed = null; switch (Mode) { case LazyThreadSafetyMode.None: boxed = CreateValue(); _boxed = boxed; break; case LazyThreadSafetyMode.PublicationOnly: boxed = CreateValue(); #pragma warning disable 420 if (Interlocked.CompareExchange(ref _boxed, boxed, null) != null) { boxed = (Boxed)_boxed; } #pragma warning restore 420 break; default: lock (_threadSafeObj) { if (_boxed == null) { boxed = CreateValue(); _boxed = boxed; } else { boxed = (_boxed as Boxed); if (boxed == null) { var holder = (_boxed as LazyInternalExceptionHolder); throw holder._exception; } } } break; } return(boxed._value); }
/// <summary> /// local helper method to initialize the value /// </summary> /// <returns>The inititialized T value</returns> private T LazyInitValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (mode == LazyThreadSafetyMode.None) { boxed = CreateValue(); m_boxed = boxed; } else if (mode == LazyThreadSafetyMode.PublicationOnly) { boxed = CreateValue(); if (Interlocked.CompareExchange(ref m_boxed, boxed, null) != null) { boxed = (Boxed)m_boxed; // set the boxed value to the succeeded thread value } } else { lock (m_threadSafeObj) { if (m_boxed == null) { boxed = CreateValue(); m_boxed = boxed; } else // got the lock but the value is not null anymore, check if it is created by another thread or faulted and throw if so { boxed = m_boxed as Boxed; if (boxed == null) // it is not Boxed, so it is a LazyInternalExceptionHolder { LazyInternalExceptionHolder exHolder = m_boxed as LazyInternalExceptionHolder; Contract.Assert(exHolder != null); throw exHolder.m_exception; } } } } Contract.Assert(boxed != null); return(boxed.m_value); }
private T LazyInitValue() { Boxed <T> boxed = null; switch (this.Mode) { case LazyThreadSafetyMode.None: boxed = this.CreateValue(); this.m_boxed = boxed; break; case LazyThreadSafetyMode.PublicationOnly: boxed = this.CreateValue(); if (Interlocked.CompareExchange(ref this.m_boxed, boxed, null) != null) { boxed = (Boxed <T>) this.m_boxed; } break; default: lock (this.m_threadSafeObj) { if (this.m_boxed == null) { boxed = this.CreateValue(); this.m_boxed = boxed; } else { boxed = this.m_boxed as Boxed <T>; if (boxed == null) { LazyInternalExceptionHolder <T> holder = this.m_boxed as LazyInternalExceptionHolder <T>; throw holder.m_exception; } } } break; } return(boxed.m_value); }
private T LazyInitValue() { Boxed boxed = null; object threadSafeObj = m_threadSafeObj; bool lockTaken = false; try { if (threadSafeObj != (object)ALREADY_INVOKED_SENTINEL) { Monitor.Enter(threadSafeObj); lockTaken = true; } if (m_boxed == null) { boxed = CreateValue(); m_boxed = boxed; m_threadSafeObj = ALREADY_INVOKED_SENTINEL; } else { boxed = m_boxed as Boxed; if (boxed == null) { var exc = m_boxed as Exception; exc.Throw(); } } } finally { if (lockTaken) { Monitor.Exit(threadSafeObj); } } return(boxed.m_value); }
/// <summary> /// local helper method to initialize the value /// </summary> /// <returns>The inititialized T value</returns> private T LazyInitValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (m_boxed == null) { boxed = CreateValue(); m_boxed = boxed; } else // got the lock but the value is not null anymore, check if it is created by another thread or faulted and throw if so { boxed = m_boxed as Boxed; if (boxed == null) // it is not Boxed, so it is a LazyInternalExceptionHolder { LazyInternalExceptionHolder exHolder = m_boxed as LazyInternalExceptionHolder; System.Diagnostics.Debug.Assert(exHolder != null); throw exHolder.m_exception; } } System.Diagnostics.Debug.Assert(boxed != null); return(boxed.m_value); }
private Boxed CreateValue() { Boxed boxed = null; try { if (m_valueFactory == ALREADY_INVOKED_SENTINEL) { throw new InvalidOperationException(); } Func <T> factory = m_valueFactory; m_valueFactory = ALREADY_INVOKED_SENTINEL; boxed = new Boxed(factory()); } catch (Exception ex) { m_boxed = ex; throw; } return(boxed); }
/// <summary> /// local helper method to initialize the value /// </summary> /// <returns>The inititialized T value</returns> private T LazyInitValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (mode == LazyThreadSafetyMode.None) { boxed = CreateValue(); m_boxed = boxed; } else if (mode == LazyThreadSafetyMode.PublicationOnly) { boxed = CreateValue(); if (boxed == null || Interlocked.CompareExchange(ref m_boxed, boxed, null) != null) { // If CreateValue returns null, it means another thread successfully invoked the value factory // and stored the result, so we should just take what was stored. If CreateValue returns non-null // but we lose the ---- to store the single value, again we should just take what was stored. boxed = (Boxed)m_boxed; } else { // We successfully created and stored the value. At this point, the value factory delegate is // no longer needed, and we don't want to hold onto its resources. m_valueFactory = ALREADY_INVOKED_SENTINEL; } } else { object threadSafeObj = Volatile.Read(ref m_threadSafeObj); bool lockTaken = false; try { if (threadSafeObj != (object)ALREADY_INVOKED_SENTINEL) { Monitor.Enter(threadSafeObj, ref lockTaken); } else { Contract.Assert(m_boxed != null); } if (m_boxed == null) { boxed = CreateValue(); m_boxed = boxed; Volatile.Write(ref m_threadSafeObj, ALREADY_INVOKED_SENTINEL); } else // got the lock but the value is not null anymore, check if it is created by another thread or faulted and throw if so { boxed = m_boxed as Boxed; if (boxed == null) // it is not Boxed, so it is a LazyInternalExceptionHolder { LazyInternalExceptionHolder exHolder = m_boxed as LazyInternalExceptionHolder; Contract.Assert(exHolder != null); exHolder.m_edi.Throw(); } } } finally { if (lockTaken) { Monitor.Exit(threadSafeObj); } } } Contract.Assert(boxed != null); return(boxed.m_value); }