예제 #1
0
        public async Task MoveToState(TState state, StateTransitionOption option = StateTransitionOption.Default)
        {
            if (!IsEnabled)
            {
                return;
            }

            var flag = true;

            queueMonitor.Enter();
            if (machine.Monitor.TryEnter())
            {
                if (queueCount == 0)
                {
                    queueMonitor.Exit();
                    flag = false;

                    try
                    {
                        await machine.MoveToStateInternal(state, option);
                    }
                    finally
                    {
                        queueMonitor.Enter();
                        if (queueCount > 0)
                        {
                            queueMonitor.Exit();
                            var _ = StartQueueIfNecessaryAsync(true);
                            // Should not exit monitor here. Its handled by the process queue.
                        }
                        else
                        {
                            queueMonitor.Exit();
                            machine.Monitor.Exit();
                        }
                    }
                }
            }

            if (flag)
            {
                var tcs = new TaskCompletionSource <bool>();
                actionsQueue = actionsQueue.Enqueue(async() =>
                {
                    try
                    {
                        await machine.MoveToStateInternal(state, option);
                        tcs.TrySetResult(true);
                    }
                    catch (Exception ex)
                    {
                        tcs.SetException(ex);
                    }
                });
                queueCount++;
                queueMonitor.Exit();
                var _ = StartQueueIfNecessaryAsync();
                await tcs.Task;
            }
        }
예제 #2
0
        public Task FireAsync <TArgument>(ParameterizedTrigger <TTrigger, TArgument> parameterizedTrigger,
                                          TArgument argument)
        {
            if (IsEnabled)
            {
                var    tcs    = new TaskCompletionSource <bool>();
                Action action = () => dispatcher.Execute(async() =>
                {
                    await machine.FireAsync(parameterizedTrigger, argument);
                    tcs.SetResult(true);

                    if (!isInQueue)
                    {
                        RunFromQueueIfNotEmpty();
                    }
                });

                if (isRunning || isPaused)
                {
                    lock (actionsQueue)
                    {
                        actionsQueue = actionsQueue.Enqueue(action);
                        queueCount++;
                    }
                    return(tcs.Task);
                }

                isRunning = true;
                action();
                return(tcs.Task);
            }
            return(TaskCache.FalseTask);
        }
예제 #3
0
        public void TestEnqueue()
        {
            int value = Generator.GetInt32();

            var queue = ImmutableTreeQueue.Create <int>();

            Assert.Empty(queue);

            // Enqueue doesn't change the original queue
            queue.Enqueue(value);
            Assert.Empty(queue);

            queue = queue.Enqueue(value);
            Assert.Single(queue);
            Assert.Equal(value, queue.Peek());
            int[] expected = { value };
            int[] actual   = queue.ToArray();
            Assert.Equal(expected, actual);

            // Test through the IImmutableQueue<T> interface
            IImmutableQueue <int> immutableQueue = queue;

            immutableQueue.Enqueue(Generator.GetInt32());
            Assert.Equal(expected, immutableQueue);

            int nextValue = Generator.GetInt32();

            immutableQueue = immutableQueue.Enqueue(nextValue);
            Assert.Equal(new[] { value, nextValue }, immutableQueue);
        }
        /// <summary>
        /// Add to path.
        /// </summary>
        /// <returns></returns>
        private HierarchyBuilder ByAppName()
        {
            var next = new HierarchyBuilder(this);

            next._path = _path.Enqueue(APP_KEY);
            return(next);
        }
예제 #5
0
        public override async Task MoveToStateAsync(TState state,
                                                    StateTransitionOption option = StateTransitionOption.Default)
        {
            var flag = true;

            m_queueMonitor.Enter();
            if (m_monitor.TryEnter())
            {
                if (m_queueCount == 0)
                {
                    m_queueMonitor.Exit();
                    flag = false;

                    try { await base.MoveToStateAsync(state, option); }
                    finally
                    {
                        m_queueMonitor.Enter();
                        if (m_queueCount > 0)
                        {
                            m_queueMonitor.Exit();
                            var _ = StartQueueIfNecessaryAsync(true);
                            // Should not exit monitor here. Its handled by the process queue.
                        }
                        else
                        {
                            m_queueMonitor.Exit();
                            m_monitor.Exit();
                        }
                    }
                }
            }

            if (flag)
            {
                var tcs = new TaskCompletionSource <bool>();

                m_actionsQueue = m_actionsQueue.Enqueue(async() =>
                {
                    try
                    {
                        await base.MoveToStateAsync(state, option);
                        tcs.TrySetResult(true);
                    }
                    catch (Exception ex) {
                        tcs.SetException(ex);
                    }
                });

                m_queueCount++;
                m_queueMonitor.Exit();
                var _ = StartQueueIfNecessaryAsync();
                await tcs.Task;
            }
        }
예제 #6
0
        public IImmutableQueue<TimeSpan> EnqueueElapsed(IImmutableQueue<TimeSpan> currentTimes)
        {
            if (!this.hasTimingInformation)
                return currentTimes;

            return currentTimes.Enqueue(this.Elapsed);
        }
예제 #7
0
        public IImmutableQueue <TimeSpan> EnqueueElapsed(IImmutableQueue <TimeSpan> currentTimes)
        {
            if (!this.hasTimingInformation)
            {
                return(currentTimes);
            }

            return(currentTimes.Enqueue(this.Elapsed));
        }
예제 #8
0
        public void EnqueueDequeueTest()
        {
            this.EnqueueDequeueTestHelper(new GenericParameterHelper(1), new GenericParameterHelper(2), new GenericParameterHelper(3));
            this.EnqueueDequeueTestHelper <GenericParameterHelper>();

            // interface test
            IImmutableQueue <GenericParameterHelper> queueInterface          = ImmutableQueue.Create <GenericParameterHelper>();
            IImmutableQueue <GenericParameterHelper> populatedQueueInterface = queueInterface.Enqueue(new GenericParameterHelper(5));

            Assert.Equal(new GenericParameterHelper(5), populatedQueueInterface.Peek());
        }
예제 #9
0
        public void Notify(T data)
        {
            lock (dequeueLocker)
            {
                buffer = buffer.Enqueue(data);
                var oldWaitable = waitable;
                waitable = new TaskCompletionSource <T>();
                cancelToken.Register(() => waitable.TrySetCanceled());

                oldWaitable.TrySetResult(data);
            }
        }
예제 #10
0
 public static IImmutableQueue <T> enq <T>(IImmutableQueue <T> queue, T value) =>
 queue.Enqueue(value);