Example #1
0
        public void TestsEnqueue()
        {
            if (LinkedQueue.IsEmpty != true)
            {
                throw new System.Exception("IsEmpty Not True");
            }

            if (LinkedQueue.Count != 0)
            {
                throw new System.Exception("Count Not 0");
            }

            LinkedQueue.Enqueue(LastInputOutput++);

            if (LinkedQueue.IsEmpty != false)
            {
                throw new System.Exception("IsEmpty Not False");
            }

            if (LinkedQueue.Count != 1)
            {
                throw new System.Exception("Count Not 1");
            }

            if (LinkedQueue.First == null)
            {
                throw new System.Exception("First is null");
            }

            if (LinkedQueue.Last == null)
            {
                throw new System.Exception("Last is null");
            }

            if (false == LinkedQueue.TryEnqueue(ref LastInputOutput))
            {
                throw new System.Exception("TryEnqueue Not True");
            }

            if (LinkedQueue.IsEmpty != false)
            {
                throw new System.Exception("IsEmpty Not False");
            }

            if (LinkedQueue.Count != 2)
            {
                throw new System.Exception("Count Not 2");
            }

            if (LinkedQueue.First == null)
            {
                throw new System.Exception("First is null");
            }

            if (LinkedQueue.Last == null)
            {
                throw new System.Exception("Last is null");
            }
        }
Example #2
0
        void Count()
        {
            //Todo, make JumpExpression ... along with TypedReferenceExpression
            //System.Action ShouldStop = () => if (false.Equals(m_Enabled.Equals(uint.MinValue))) goto Approximate; System.Threading.Thread.ResetAbort();

            System.Threading.Thread Event = null;

            Event = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                System.Threading.Thread.BeginCriticalRegion();

                long sample = 0;

                AfterSample:

                try
                {
                    Top:
                    System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

                    /*do */
                    if (Producer.TryDequeue(out sample))
                    {
                        Tick(ref sample);
                    }
                    //while (m_Enabled && Producer.Count >= 0);

                    System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Lowest;

                    if (m_Enabled.Equals(uint.MinValue))
                    {
                        return;
                    }
                    else if (Producer.Count.Equals(uint.MinValue))
                    {
                        if (System.Threading.Thread.CurrentThread.Equals(m_Counter))
                        {
                            //Wait for other threads and if no other thread wanted time join the event thread indefinitely.
                            if (false.Equals(System.Threading.Thread.Yield()))
                            {
                                //If not already waiting or sleeping or joining
                                if (false.Equals(Event.ThreadState.HasFlag(System.Threading.ThreadState.WaitSleepJoin)))
                                {
                                    Event.Join(System.Threading.Timeout.InfiniteTimeSpan);  //++m_Ops;
                                }
                            }
                        }
                        else //Not the counter so return
                        {
                            return;

                            //m_Counter.Join(m_Frequency);  //++m_Ops;
                        }
                    }

                    goto Top;
                }
                catch { if (m_Enabled.Equals(uint.MinValue))
                        {
                            return;
                        }
                        goto AfterSample; }
                finally { System.Threading.Thread.EndCriticalRegion(); }
            }))
            {
                Priority = System.Threading.ThreadPriority.AboveNormal
            };

            Event.TrySetApartmentState(System.Threading.ApartmentState.MTA);

            Event.Start();

            //to ensure the slice offset is different, e.g. more bias
            //m_Clock.NanoSleep(0);

Approximate:

            ulong approximate = (ulong)Common.Binary.Clamp((m_Clock.AverageOperationsPerTick / (Frequency.Ticks + 1)), 1, ulong.MaxValue), x = 0;

            try
            {
                m_Started = m_Clock.Now;

                System.Threading.Thread.BeginCriticalRegion();

                unchecked
                {
Start:

                    if (IsDisposed)
                    {
                        return;
                    }

                    switch (++m_Ops)
                    {
                    default:
                    {
                        if (false.Equals(m_Enabled.Equals(uint.MinValue)) && m_Bias + ++m_Ops >= approximate)
                        {
                            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

                            Producer.Enqueue((long)m_Ticks++);

                            x = (ulong)Common.Binary.Clamp((m_Bias = m_Ops / approximate), 0, m_Bias);

                            while (false.Equals(m_Enabled.Equals(uint.MinValue)) && 1 > --x /*&& Producer.Count <= m_Frequency.Ticks*/)
                            {
                                Producer.Enqueue((long)++m_Ticks);
                            }

                            m_Ops += m_Bias;

                            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Lowest;
                        }

                        if (false.Equals(Event == null) && Event.IsAlive)
                        {
                            //If this is the counter thread
                            if (System.Threading.Thread.CurrentThread.Equals(m_Counter))
                            {
                                //If not event is not sleeping or joined then
                                if (false.Equals(Event.ThreadState.HasFlag(System.Threading.ThreadState.WaitSleepJoin)))
                                {
                                    //Join the event thread for the frequency
                                    Event.Join(m_Frequency);
                                }

                                //Time and Stack..
                                //Event.Interrupt();
                            }
                        }
                        else         //Not the counter so return
                        {
                            //Option for delay

                            //Time and Stack..
                            //m_Counter.Interrupt();

                            //if (false.Equals(System.Threading.Thread.Yield())) ; //m_Counter.Interrupt();

                            return;
                        }

                        goto Start;
                    }
                    }
                }
            }
            catch (System.SystemException se)
            {
                if (se is System.Threading.ThreadAbortException)
                {
                    System.Threading.Thread.ResetAbort();
                }
                else if (se is System.Threading.ThreadInterruptedException | false.Equals(m_Enabled.Equals(uint.MinValue)))
                {
                    goto Approximate;
                }
                else if (se is System.OutOfMemoryException)
                {
                    if ((ulong)Producer.Count > approximate)
                    {
                        Producer.Clear();
                    }

                    if (false.Equals(m_Enabled.Equals(uint.MinValue)))
                    {
                        goto Approximate;
                    }
                }
            }
            catch
            {
                if (false.Equals(m_Enabled.Equals(uint.MinValue)))
                {
                    goto Approximate;
                }
            }
            finally
            {
                Event = null;

                System.Threading.Thread.EndCriticalRegion();
            }
        }