コード例 #1
0
        /// <summary>
        /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource.
        /// </summary>
        /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
        /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
        /// <param name="asyncState">Additional state to pass to the callback method.</param>
        public void BeginRegion(SyncGateMode mode, ChoAsyncCallback asyncCallback, object asyncState)
        {
            ChoGuard.ArgumentNotNull(asyncCallback, "asyncCallback");

            SyncGateAsyncResult ar = new SyncGateAsyncResult(mode, asyncCallback, asyncState);

            lock (_syncLock)
            {
                switch (mode)
                {
                case SyncGateMode.Exclusive:
                    switch (_state)
                    {
                    case SyncGateStates.OwnedByReaders:
                    case SyncGateStates.OwnedByReadersAndWriterPending:
                        _state = SyncGateStates.OwnedByReadersAndWriterPending;
                        _qWriteRequests.Enqueue(ar);
                        break;

                    case SyncGateStates.OwnedByWriter:
                        _qWriteRequests.Enqueue(ar);
                        break;
                    }
                    break;

                case SyncGateMode.Shared:
                    switch (_state)
                    {
                    case SyncGateStates.Free:
                    case SyncGateStates.OwnedByReaders:
                        _state = SyncGateStates.OwnedByReaders;
                        _numReaders++;
                        QueueCallback(ar);
                        break;

                    case SyncGateStates.OwnedByReadersAndWriterPending:
                    case SyncGateStates.OwnedByWriter:
                    case SyncGateStates.ReservedForWriter:
                        _qReadRequests.Enqueue(ar);
                        break;
                    }
                    break;
                }
            }
        }
コード例 #2
0
ファイル: SyncGate.cs プロジェクト: blinds52/PowerThreading
      /// <summary>
      /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource. 
      /// </summary>
      /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
      /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
      /// <param name="asyncState">Additional state to pass to the callback method.</param>
      public void BeginRegion(SyncGateMode mode, AsyncCallback asyncCallback, Object asyncState) {
         Contract.Assume(m_qReadRequests != null);
         Contract.Assume(m_qWriteRequests != null);
         // This method supports the method callback version of the IAsyncResult APM only; therefore,
         // a callback method must always be specified and this is also why the method returns void 
         // instead of returning an IAsyncResult
         if (asyncCallback == null) throw new ArgumentNullException("asyncCallback");
         SyncGateAsyncResult ar = new SyncGateAsyncResult(mode, asyncCallback, asyncState);
         Boolean goodToGo = false;
         Monitor.Enter(m_syncLock);
         switch (mode) {
            case SyncGateMode.Exclusive:
               switch (m_state) {
                  case SyncGateStates.Free:             // If Free  | RFW -> OBW, invoke, return
                  case SyncGateStates.ReservedForWriter:
                     m_state = SyncGateStates.OwnedByWriter;
                     goodToGo = true;  // QueueCallback(ar);
                     break;

                  case SyncGateStates.OwnedByReaders:   // If OBR | OBRAWP -> OBRAWP, queue, return
                  case SyncGateStates.OwnedByReadersAndWriterPending:
                     m_state = SyncGateStates.OwnedByReadersAndWriterPending;
                     m_qWriteRequests.Enqueue(ar);
                     break;

                  case SyncGateStates.OwnedByWriter:   // If OBW, queue, return
                     m_qWriteRequests.Enqueue(ar);
                     break;
               }
               break;

            case SyncGateMode.Shared:
               switch (m_state) {
                  case SyncGateStates.Free:   // If Free | OBR -> OBR, NR++, invoke, return
                  case SyncGateStates.OwnedByReaders:
                     m_state = SyncGateStates.OwnedByReaders;
                     m_numReaders++;
                     goodToGo = true; // QueueCallback(ar);
                     break;

                  case SyncGateStates.OwnedByWriter:   // If OBW | OBRAWP | RFW, queue, return
                  case SyncGateStates.OwnedByReadersAndWriterPending:
                  case SyncGateStates.ReservedForWriter:
                     m_qReadRequests.Enqueue(ar);
                     break;
               }
               break;
         }
         Monitor.Exit(m_syncLock);
         if (goodToGo) ar.SetAsCompleted(null, true);
      }
コード例 #3
0
ファイル: SyncGate.cs プロジェクト: blinds52/PowerThreading
 /// <summary>
 /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource. 
 /// </summary>
 /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
 /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
 public void BeginRegion(SyncGateMode mode, AsyncCallback asyncCallback) {
    BeginRegion(mode, asyncCallback, null);
 }
コード例 #4
0
ファイル: SyncGate.cs プロジェクト: blinds52/PowerThreading
 internal SyncGateAsyncResult(SyncGateMode mode, AsyncCallback asyncCallback, Object state)
    : base(asyncCallback, state) {
    m_mode = mode;
 }
コード例 #5
0
 /// <summary>
 /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource.
 /// </summary>
 /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
 /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
 public void BeginRegion(SyncGateMode mode, ChoAsyncCallback asyncCallback)
 {
     BeginRegion(mode, asyncCallback, null);
 }
コード例 #6
0
 internal SyncGateAsyncResult(SyncGateMode mode, ChoAsyncCallback asyncCallback, object state)
     : base(asyncCallback, state)
 {
     this._syncGateMode = mode;
 }
コード例 #7
0
 internal SyncGateAsyncResult(SyncGateMode mode, AsyncCallback asyncCallback, Object state)
     : base(asyncCallback, state)
 {
     m_mode = mode;
 }
コード例 #8
0
        /// <summary>
        /// Allows the caller to notify the SyncGate that it wants exclusive or shared access to a resource.
        /// </summary>
        /// <param name="mode">Indicates if exclusive or shared access is desired.</param>
        /// <param name="asyncCallback">The callback method to invoke once access can be granted.</param>
        /// <param name="asyncState">Additional state to pass to the callback method.</param>
        public void BeginRegion(SyncGateMode mode, AsyncCallback asyncCallback, Object asyncState)
        {
            Contract.Assume(m_qReadRequests != null);
            Contract.Assume(m_qWriteRequests != null);
            // This method supports the method callback version of the IAsyncResult APM only; therefore,
            // a callback method must always be specified and this is also why the method returns void
            // instead of returning an IAsyncResult
            if (asyncCallback == null)
            {
                throw new ArgumentNullException("asyncCallback");
            }
            SyncGateAsyncResult ar       = new SyncGateAsyncResult(mode, asyncCallback, asyncState);
            Boolean             goodToGo = false;

            Monitor.Enter(m_syncLock);
            switch (mode)
            {
            case SyncGateMode.Exclusive:
                switch (m_state)
                {
                case SyncGateStates.Free:               // If Free  | RFW -> OBW, invoke, return
                case SyncGateStates.ReservedForWriter:
                    m_state  = SyncGateStates.OwnedByWriter;
                    goodToGo = true;   // QueueCallback(ar);
                    break;

                case SyncGateStates.OwnedByReaders:     // If OBR | OBRAWP -> OBRAWP, queue, return
                case SyncGateStates.OwnedByReadersAndWriterPending:
                    m_state = SyncGateStates.OwnedByReadersAndWriterPending;
                    m_qWriteRequests.Enqueue(ar);
                    break;

                case SyncGateStates.OwnedByWriter:     // If OBW, queue, return
                    m_qWriteRequests.Enqueue(ar);
                    break;
                }
                break;

            case SyncGateMode.Shared:
                switch (m_state)
                {
                case SyncGateStates.Free:     // If Free | OBR -> OBR, NR++, invoke, return
                case SyncGateStates.OwnedByReaders:
                    m_state = SyncGateStates.OwnedByReaders;
                    m_numReaders++;
                    goodToGo = true;  // QueueCallback(ar);
                    break;

                case SyncGateStates.OwnedByWriter:     // If OBW | OBRAWP | RFW, queue, return
                case SyncGateStates.OwnedByReadersAndWriterPending:
                case SyncGateStates.ReservedForWriter:
                    m_qReadRequests.Enqueue(ar);
                    break;
                }
                break;
            }
            Monitor.Exit(m_syncLock);
            if (goodToGo)
            {
                ar.SetAsCompleted(null, true);
            }
        }