Example #1
0
        private bool ProcessActiveBlock(ManagedBufferBlock block, WaitHandle[] processEvents)
        {
            while (block.Active)
            {
                switch (WaitHandle.WaitAny(processEvents))
                {
                case 0:
                    _communicationManager.HandleCommunicationBlock(block.CommunicationBlock, b => { });
                    break;

                case 1:
                    var data = _communicationManager.HandleMemoryBlock(block.MemoryBlock);
                    // don't let the queue get too big as using too much memory causes
                    // problems i.e. the target process closes down but the host takes
                    // ages to shutdown; this is a compromise.
                    _messageQueue.Enqueue(data);
                    if (_messageQueue.Count > 400)
                    {
                        do
                        {
                            ThreadHelper.YieldOrSleep(100);
                        } while (_messageQueue.Count > 200);
                    }
                    break;

                default:     // 2
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
                                          EventWaitHandle threadActivatedEvent, ThreadTermination threadTermination)
        {
            return(state =>
            {
                try
                {
                    var processEvents = new WaitHandle[]
                    {
                        block.CommunicationBlock.ProfilerRequestsInformation,
                        block.MemoryBlock.ProfilerHasResults,
                        threadTermination.CancelThreadEvent
                    };
                    threadActivatedEvent.Set();

                    try
                    {
                        if (ProcessActiveBlock(block, processEvents))
                        {
                            return;
                        }
                        _memoryManager.RemoveDeactivatedBlock(block);
                    }
                    finally
                    {
                        threadTermination.ThreadFinishedEvent.Set();
                    }
                }
                catch (ObjectDisposedException)
                {
                    /* an attempt to close thread has probably happened and the events disposed */
                }
            });
        }
Example #3
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
                                          EventWaitHandle threadActivatedEvent, ThreadTermination threadTermination)
        {
            return(state =>
            {
                try
                {
                    var processEvents = new WaitHandle[]
                    {
                        block.CommunicationBlock.ProfilerRequestsInformation,
                        block.MemoryBlock.ProfilerHasResults,
                        threadTermination.CancelThreadEvent
                    };
                    threadActivatedEvent.Set();

                    try
                    {
                        while (block.Active)
                        {
                            switch (WaitHandle.WaitAny(processEvents))
                            {
                            case 0:
                                _communicationManager.HandleCommunicationBlock(block.CommunicationBlock, b => { });
                                break;

                            case 1:
                                var data = _communicationManager.HandleMemoryBlock(block.MemoryBlock);
                                // don't let the queue get too big as using too much memory causes
                                // problems i.e. the target process closes down but the host takes
                                // ages to shutdown; this is a compromise.
                                _messageQueue.Enqueue(data);
                                if (_messageQueue.Count > 400)
                                {
                                    do
                                    {
                                        ThreadHelper.YieldOrSleep(100);
                                    } while (_messageQueue.Count > 200);
                                }
                                break;

                            default:     // 2
                                return;
                            }
                        }
                        _memoryManager.RemoveDeactivatedBlock(block);
                    }
                    finally
                    {
                        threadTermination.ThreadFinishedEvent.Set();
                    }
                }
                catch (ObjectDisposedException)
                {
                    /* an attempt to close thread has probably happened and the events disposed */
                }
            });
        }
Example #4
0
 /// <summary>
 /// remove deactivated blocks
 /// </summary>
 public void RemoveDeactivatedBlock(ManagedBufferBlock block)
 {
     lock (_lockObject)
     {
         if (block.Active)
         {
             return;
         }
         block.CommunicationBlock.Do(x => x.Dispose());
         block.MemoryBlock.Do(x => x.Dispose());
         _blocks.RemoveAt(_blocks.IndexOf(block));
     }
 }
Example #5
0
        private Tuple <EventWaitHandle, EventWaitHandle> StartProcessingThread(ManagedBufferBlock block)
        {
            var terminateThread  = new ManualResetEvent(false);
            var threadTerminated = new ManualResetEvent(false);

            using (var threadActivated = new AutoResetEvent(false))
            {
                ThreadPool.QueueUserWorkItem(ProcessBlock(block, terminateThread,
                                                          threadActivated, threadTerminated));
                threadActivated.WaitOne();
            }
            return(new Tuple <EventWaitHandle, EventWaitHandle>(terminateThread, threadTerminated));
        }
Example #6
0
        private ThreadTermination StartProcessingThread(ManagedBufferBlock block)
        {
            DebugLogger.InfoFormat("Starting Process Block => {0}", block.BufferId);

            var threadTermination = new ThreadTermination();

            using (var threadActivatedEvent = new AutoResetEvent(false))
            {
                ThreadPool.QueueUserWorkItem(ProcessBlock(block, threadActivatedEvent, threadTermination));
                threadActivatedEvent.WaitOne();
            }

            DebugLogger.InfoFormat("Started Process Block => {0}", block.BufferId);
            return(threadTermination);
        }
Example #7
0
        private Tuple <EventWaitHandle, EventWaitHandle> StartProcessingThread(ManagedBufferBlock block)
        {
            DebugLogger.InfoFormat("Starting Process Block => {0}", block.BufferId);
            var terminateThread  = new ManualResetEvent(false);
            var threadTerminated = new ManualResetEvent(false);

            using (var threadActivated = new AutoResetEvent(false))
            {
                ThreadPool.QueueUserWorkItem(ProcessBlock(block, terminateThread,
                                                          threadActivated, threadTerminated));
                threadActivated.WaitOne();
            }
            DebugLogger.InfoFormat("Started Process Block => {0}", block.BufferId);
            return(new Tuple <EventWaitHandle, EventWaitHandle>(terminateThread, threadTerminated));
        }
Example #8
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
                                          WaitHandle terminateThread, EventWaitHandle threadActivated, EventWaitHandle threadTerminated)
        {
            return(state =>
            {
                var processEvents = new []
                {
                    block.CommunicationBlock.ProfilerRequestsInformation,
                    block.MemoryBlock.ProfilerHasResults,
                    terminateThread
                };
                threadActivated.Set();

                while (block.Active)
                {
                    switch (WaitHandle.WaitAny(processEvents))
                    {
                    case 0:
                        _communicationManager.HandleCommunicationBlock(block.CommunicationBlock, b => { });
                        break;

                    case 1:
                        var data = _communicationManager.HandleMemoryBlock(block.MemoryBlock);
                        // don't let the queue get too big as using too much memory causes
                        // problems i.e. the target process closes down but the host takes
                        // ages to shutdown; this is a compromise.
                        _messageQueue.Enqueue(data);
                        if (_messageQueue.Count > 400)
                        {
                            do
                            {
                                Thread.Yield();
                            } while (_messageQueue.Count > 200);
                        }
                        break;

                    case 2:
                        threadTerminated.Set();
                        return;
                    }
                }
                threadTerminated.Set();
                _memoryManager.RemoveDeactivatedBlocks();
            });
        }
Example #9
0
        /// <summary>
        /// Allocate a memory buffer
        /// </summary>
        /// <param name="bufferSize"></param>
        /// <param name="bufferId"></param>
        /// <returns></returns>
        public ManagedBufferBlock AllocateMemoryBuffer(int bufferSize, uint bufferId)
        {
            if (!_isIntialised) return null;

            lock (_lockObject)
            {
                var tuple = new ManagedBufferBlock
                {
                    CommunicationBlock =
                        new ManagedCommunicationBlock(_namespace, _key, bufferSize, (int) bufferId, _servicePrincipal),
                    MemoryBlock =
                        new ManagedMemoryBlock(_namespace, _key, bufferSize, (int) bufferId, _servicePrincipal),
                    BufferId = bufferId
                };
                _blocks.Add(tuple);
                return tuple;
            }
        }
Example #10
0
        /// <summary>
        /// Allocate a memory buffer
        /// </summary>
        /// <param name="bufferSize"></param>
        /// <param name="bufferId"></param>
        /// <returns></returns>
        public ManagedBufferBlock AllocateMemoryBuffer(int bufferSize, uint bufferId)
        {
            if (!_isIntialised)
            {
                return(null);
            }

            lock (_lockObject)
            {
                var tuple = new ManagedBufferBlock
                {
                    CommunicationBlock =
                        new ManagedCommunicationBlock(_namespace, _key, bufferSize, (int)bufferId, _servicePrincipal),
                    MemoryBlock =
                        new ManagedMemoryBlock(_namespace, _key, bufferSize, (int)bufferId, _servicePrincipal),
                    BufferId = bufferId
                };
                _blocks.Add(tuple);
                return(tuple);
            }
        }
Example #11
0
        private Tuple<EventWaitHandle, EventWaitHandle> StartProcessingThread(ManagedBufferBlock block)
        {
            DebugLogger.InfoFormat("Starting Process Block => {0}", block.BufferId);
            var terminateThread = new ManualResetEvent(false);
            var threadTerminated = new ManualResetEvent(false);

            using (var threadActivated = new AutoResetEvent(false))
            {
                ThreadPool.QueueUserWorkItem(ProcessBlock(block, terminateThread,
                    threadActivated, threadTerminated));
                threadActivated.WaitOne();
            }
            DebugLogger.InfoFormat("Started Process Block => {0}", block.BufferId);
            return new Tuple<EventWaitHandle, EventWaitHandle>(terminateThread, threadTerminated);
        }
Example #12
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
            WaitHandle terminateThread, EventWaitHandle threadActivated, EventWaitHandle threadTerminated)
        {
            return state =>
            {
                var processEvents = new []
                {
                    block.CommunicationBlock.ProfilerRequestsInformation,
                    block.MemoryBlock.ProfilerHasResults,
                    terminateThread
                };
                threadActivated.Set();

                while(block.Active)
                {
                    switch (WaitHandle.WaitAny(processEvents))
                    {
                        case 0:
                            _communicationManager.HandleCommunicationBlock(block.CommunicationBlock, b => { });
                            break;
                        case 1:
                            var data = _communicationManager.HandleMemoryBlock(block.MemoryBlock);
                            // don't let the queue get too big as using too much memory causes
                            // problems i.e. the target process closes down but the host takes
                            // ages to shutdown; this is a compromise.
                            _messageQueue.Enqueue(data);
                            if (_messageQueue.Count > 400)
                            {
                                do
                                {
                                    Thread.Yield();
                                } while (_messageQueue.Count > 200);
                            }
                            break;
                        case 2:
                            threadTerminated.Set();
                            return;
                    }
                }
                threadTerminated.Set();
                _memoryManager.RemoveDeactivatedBlocks();
            };
        }
Example #13
0
        private WaitCallback ProcessBlock(ManagedBufferBlock block,
            EventWaitHandle threadActivatedEvent, ThreadTermination threadTermination)
        {
            return state =>
            {
                try
                {
                    var processEvents = new WaitHandle[]
                    {
                        block.CommunicationBlock.ProfilerRequestsInformation,
                        block.MemoryBlock.ProfilerHasResults,
                        threadTermination.CancelThreadEvent
                    };
                    threadActivatedEvent.Set();

                    try
                    {
                        while (block.Active)
                        {
                            switch (WaitHandle.WaitAny(processEvents))
                            {
                                case 0:
                                    _communicationManager.HandleCommunicationBlock(block.CommunicationBlock, b => { });
                                    break;
                                case 1:
                                    var data = _communicationManager.HandleMemoryBlock(block.MemoryBlock);
                                    // don't let the queue get too big as using too much memory causes 
                                    // problems i.e. the target process closes down but the host takes 
                                    // ages to shutdown; this is a compromise. 
                                    _messageQueue.Enqueue(data);
                                    if (_messageQueue.Count > 400)
                                    {
                                        do
                                        {
                                            ThreadHelper.YieldOrSleep(100);
                                        } while (_messageQueue.Count > 200);
                                    }
                                    break;
                                default: // 2
                                    return;
                            }
                        }
                        _memoryManager.RemoveDeactivatedBlock(block);
                    }
                    finally
                    {
                        threadTermination.ThreadFinishedEvent.Set();
                    }
                }
                catch (ObjectDisposedException)
                {
                    /* an attempt to close thread has probably happened and the events disposed */
                }
            };
        }
Example #14
0
        private ThreadTermination StartProcessingThread(ManagedBufferBlock block)
        {
            DebugLogger.InfoFormat("Starting Process Block => {0}", block.BufferId);

            var threadTermination = new ThreadTermination();

            using (var threadActivatedEvent = new AutoResetEvent(false))
            {
                ThreadPool.QueueUserWorkItem(ProcessBlock(block, threadActivatedEvent, threadTermination));
                threadActivatedEvent.WaitOne();
            }

            DebugLogger.InfoFormat("Started Process Block => {0}", block.BufferId);
            return threadTermination;
        }
Example #15
0
        private Tuple<EventWaitHandle, EventWaitHandle> StartProcessingThread(ManagedBufferBlock block)
        {
            var terminateThread = new ManualResetEvent(false);
            var threadTerminated = new ManualResetEvent(false);

            using (var threadActivated = new AutoResetEvent(false))
            {
                ThreadPool.QueueUserWorkItem(ProcessBlock(block, terminateThread,
                    threadActivated, threadTerminated));
                threadActivated.WaitOne();
            }
            return new Tuple<EventWaitHandle, EventWaitHandle>(terminateThread, threadTerminated);
        }
Example #16
0
 /// <summary>
 /// remove deactivated blocks
 /// </summary>
 public void RemoveDeactivatedBlock(ManagedBufferBlock block)
 {
     lock (_lockObject)
     {
         if (block.Active)
             return;
         block.CommunicationBlock.Do(x => x.Dispose());
         block.MemoryBlock.Do(x => x.Dispose());
         _blocks.RemoveAt(_blocks.IndexOf(block));
     }
 }