예제 #1
0
        /// <summary>
        /// Desencola el primer objeto de la lista, se bloquea hasta que alguien inserte un elemento
        /// </summary>
        /// <returns>El primer objeto de la cola, null si está vacía</returns>
        public Event draw()
        {
            monitor.Enter();
            while (itemCount == 0)
            {
                monitor.Wait();
            }

            Event eventHandler = queue.Dequeue();

            itemCount--;
            monitor.Exit();
            return(eventHandler);
        }
예제 #2
0
        /// <summary>
        /// Obtiene el primer mensaje en la cola, null si esta vacía
        /// </summary>
        /// <returns>el primer mensaje de la cola o null si está vacía</returns>
        public Message draw()
        {
            monitor.Enter();
            while (itemCount == 0)
            {
                monitor.Wait();
            }

            Message message = queue.Dequeue();

            itemCount--;
            monitor.Exit();
            return(message);
        }
예제 #3
0
 private void AcquireLocks(int fromInclusive, int toExclusive, ref int locksAcquired)
 {
     for (int i = fromInclusive; i < toExclusive; i++)
     {
         bool flag = false;
         try
         {
             Monitor2.Enter(m_locks[i], ref flag);
         }
         finally
         {
             if (flag)
             {
                 locksAcquired++;
             }
         }
     }
 }
예제 #4
0
        /// <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 another thread set the value 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 = m_threadSafeObj;
                bool   lockTaken     = false;
                try
                {
                    if (threadSafeObj != (object)ALREADY_INVOKED_SENTINEL)
                    {
                        Monitor2.Enter(threadSafeObj, ref lockTaken);
                    }
                    else
                    {
                        Debug.Assert(m_boxed != null);
                    }

                    if (m_boxed == null)
                    {
                        boxed           = CreateValue();
                        m_boxed         = boxed;
                        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;
                            Debug.Assert(exHolder != null);
                            exHolder.m_edi.Throw();
                        }
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(threadSafeObj);
                    }
                }
            }
            Debug.Assert(boxed != null);
            return(boxed.m_value);
        }
예제 #5
0
        private bool TryAddInternal(TKey key, TValue value, bool updateIfExists, bool acquireLock, out TValue resultingValue)
        {
            int hashCode = m_comparer.GetHashCode(key);

            ConcurrentDictionary <TKey, TValue> .Node[] local;
            bool flag;

            while (true)
            {
                local = m_buckets;
                int num;
                int num2;
                GetBucketAndLockNo(hashCode, out num, out num2, local.Length);
                flag = false;
                bool flag2 = false;
                try
                {
                    if (acquireLock)
                    {
                        Monitor2.Enter(m_locks[num2], ref flag2);
                    }
                    if (local != m_buckets)
                    {
                        continue;
                    }
                    ConcurrentDictionary <TKey, TValue> .Node node = null;
                    for (ConcurrentDictionary <TKey, TValue> .Node node2 = local[num]; node2 != null; node2 = node2.m_next)
                    {
                        if (m_comparer.Equals(node2.m_key, key))
                        {
                            if (updateIfExists)
                            {
                                ConcurrentDictionary <TKey, TValue> .Node node3 = new ConcurrentDictionary <TKey, TValue> .Node(node2.m_key, value, hashCode, node2.m_next);

                                if (node == null)
                                {
                                    local[num] = node3;
                                }
                                else
                                {
                                    node.m_next = node3;
                                }
                                resultingValue = value;
                            }
                            else
                            {
                                resultingValue = node2.m_value;
                            }
                            return(false);
                        }
                        node = node2;
                    }
                    local[num] = new ConcurrentDictionary <TKey, TValue> .Node(key, value, hashCode, local[num]);

                    m_countPerLock[num2]++;
                    if (m_countPerLock[num2] > local.Length / m_locks.Length)
                    {
                        flag = true;
                    }
                }
                finally
                {
                    if (flag2)
                    {
                        Monitor.Exit(m_locks[num2]);
                    }
                }
                break;
            }
            if (flag)
            {
                GrowTable(local);
            }
            resultingValue = value;
            return(true);
        }