private int ProcessSignaledWaitState(WaitHandle[] waitHandlesForAbandon, out Exception exception)
            {
                s_lock.VerifyIsNotLocked();
                _waitMonitor.VerifyIsLocked();
                Debug.Assert(_thread == RuntimeThread.CurrentThread);

                switch (_waitSignalState)
                {
                case WaitSignalState.Waiting:
                    exception = null;
                    return(WaitHandle.WaitTimeout);

                case WaitSignalState.Waiting_SignaledToSatisfyWait:
                {
                    Debug.Assert(_waitedObjectIndexThatSatisfiedWait >= 0);
                    int waitedObjectIndexThatSatisfiedWait = _waitedObjectIndexThatSatisfiedWait;
                    _waitedObjectIndexThatSatisfiedWait = -1;
                    exception = null;
                    return(waitedObjectIndexThatSatisfiedWait);
                }

                case WaitSignalState.Waiting_SignaledToSatisfyWaitWithAbandonedMutex:
                    Debug.Assert(_waitedObjectIndexThatSatisfiedWait >= 0);
                    if (waitHandlesForAbandon == null)
                    {
                        _waitedObjectIndexThatSatisfiedWait = -1;
                        exception = new AbandonedMutexException();
                    }
                    else
                    {
                        int waitedObjectIndexThatSatisfiedWait = _waitedObjectIndexThatSatisfiedWait;
                        _waitedObjectIndexThatSatisfiedWait = -1;
                        exception =
                            new AbandonedMutexException(
                                waitedObjectIndexThatSatisfiedWait,
                                waitHandlesForAbandon[waitedObjectIndexThatSatisfiedWait]);
                    }
                    return(0);

                case WaitSignalState.Waiting_SignaledToAbortWaitDueToMaximumMutexReacquireCount:
                    Debug.Assert(_waitedObjectIndexThatSatisfiedWait < 0);
                    exception = new OverflowException(SR.Overflow_MutexReacquireCount);
                    return(0);

                default:
                    Debug.Assert(_waitSignalState == WaitSignalState.Waiting_SignaledToInterruptWait);
                    Debug.Assert(_waitedObjectIndexThatSatisfiedWait < 0);
                    exception = new ThreadInterruptedException();
                    return(0);
                }
            }
        public void SaveSettings_Throws_Exception_When_Serialization_Fails()
        {
            // Arrange
            var testException = new ThreadInterruptedException();
            var testBundle = new SettingsManagerTestBundle();
            Exception thrownException = null;

            testBundle.MockSettingsRepository.Setup(x => x.Save(It.IsAny<SettingsRoot>())).Throws(testException);

            // Act
            try
            {
                testBundle.SettingsManager.SaveSettings();
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            // Assert
            testBundle.MockSemaphore.Verify(x => x.WaitOne(), Times.Once);
            testBundle.MockSemaphore.Verify(x => x.Release(), Times.Once);
            Assert.IsNotNull(thrownException);
        }