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); }
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 */ } }); }
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 */ } }); }
/// <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)); } }
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)); }
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); }
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)); }
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(); }); }
/// <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; } }
/// <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); } }
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); }
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(); }; }
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 */ } }; }
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; }
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); }
/// <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)); } }