/// <summary> /// UnRegisters the calling thread with this instance of ManagedIOCP. After this call /// the thread will not be able to wait on its IOCPHandle for receiving any /// objects queued to this instance of ManagedIOCP. The calling thread can re-register /// with this instance of ManagedIOCP and receive objects queued to this instance /// of ManagedIOCP /// </summary> public void UnRegister() { IOCPHandle <T> hIOCP = null; Thread th = Thread.CurrentThread; try { Monitor.Enter(this); if (_regThreads.ContainsKey(th)) { hIOCP = _regThreads[th] as IOCPHandle <T>; _regThreads.Remove(th); _regIOCPHandles.Remove(hIOCP); hIOCP.InvalidateOwningManagedIOCP(); hIOCP.SetEvent(); if (hIOCP.Active == 1) { DecrementActiveThreads(); } } } catch (Exception) { throw; } finally { Monitor.Exit(this); } }
private void WakeupNextThread() { bool empty = false; #if (DYNAMIC_IOCP) // First check if we should service this request from suspended // IOCPHandle queue // if ((_activeThreads < _concurrentThreads) && (_qIOCPHandle.Count >= _concurrentThreads)) { IOCPHandle <T> hSuspendedIOCP = _qSuspendedIOCPHandle.Dequeue(ref empty); if ((empty == false) && (hSuspendedIOCP != null)) { hSuspendedIOCP.SetEvent(); return; } } empty = false; #endif while (true) { IOCPHandle <T> hIOCP = _qIOCPHandle.Dequeue(ref empty); if ((empty == false) && (hIOCP != null)) { if (hIOCP.WaitingOnIOCP == true) { hIOCP.SetEvent(); break; } else { if (hIOCP.OwningThread.ThreadState != ThreadState.Running) { // Set the active flag to 2 and decrement the active threads // so that other waiting threads can process requests // int activeTemp = hIOCP._active; int newActiveState = 2; if (Interlocked.CompareExchange(ref hIOCP._active, newActiveState, activeTemp) == activeTemp) { DecrementActiveThreads(); } } // This is required because, Thread associated with hIOCP // may have got null out of ManagedIOCP queue, but still // not yet reached the QueuIOCPHandle and Wait state. // Now we had a dispatch and we enqueued the object and // trying to wake up any waiting threads. If we ignore this // running thread, this may be the only thread for us and we // will never be able to service this dispatch untill another // dispatch comes in. // hIOCP.SetEvent(); break; } } else { // Do we need to throw this exception ??? // //throw new Exception("No threads avialable to handle the dispatch"); break; } } }