Exemplo n.º 1
0
        /// <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);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers the calling thread with this instance of ManagedIOCP
        /// </summary>
        /// <returns>
        /// IOCPHandle class instance that can be used by this thread
        /// to retrieve the next available object from this ManagedIOCP
        /// instance queue.
        /// </returns>
        public IOCPHandle <T> Register()
        {
            IOCPHandle <T> hIOCP = null;
            Thread         th    = Thread.CurrentThread;

            try
            {
                Monitor.Enter(this);
                if (_regThreads.ContainsKey(th))
                {
                    hIOCP = _regThreads[th] as IOCPHandle <T>;
                }
                else
                {
                    hIOCP = new IOCPHandle <T>(this, th);
                    _regThreads.Add(th, hIOCP);
                    _regIOCPHandles.Add(hIOCP);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                Monitor.Exit(this);
            }
            return(hIOCP);
        }
Exemplo n.º 3
0
        private void ThreadFunc()
        {
            IOCPHandle <ITask> hIOCP = _mIOCP.Register();

            _ev.Set();
            try
            {
                while (true)
                {
                    ITask task = hIOCP.Wait();
                    if (task.Active == true)
                    {
                        task.Execute(this);
                        task.Done();
                    }
                }
            }
            catch (Exception ex)
            {
                // Raise an event to the registered even handlers
                // for ThreadExceptions

                Console.WriteLine(string.Format("Thread ID: {0} -- {1}, {2}",
                                                AppDomain.GetCurrentThreadId(), ex.Message, ex.StackTrace));
                if (_tpThreadExceptionHandler != null)
                {
                    _tpThreadExceptionHandler(ex);
                }
            }
            _mIOCP.UnRegister();
        }
Exemplo n.º 4
0
        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;
                }
            }
        }
Exemplo n.º 5
0
 internal void SuspendIOCPHandle(IOCPHandle <T> hIOCP)
 {
     _qSuspendedIOCPHandle.Enqueue(hIOCP);
 }
Exemplo n.º 6
0
 internal void QueueIOCPHandle(IOCPHandle <T> hIOCP)
 {
     _qIOCPHandle.Enqueue(hIOCP);
 }