Ejemplo n.º 1
0
        public bool TryEnterReadLock(int timeOut)
        {
            bool result = false;
            SPW  sw     = new SPW();
            int  tid    = ReadLockIndex;
            // Wait until there are no writers.
            DateTime endTime = DateTime.Now.AddMilliseconds(timeOut);

            while (DateTime.Now < endTime)
            {
                while (m_writer == 1)
                {
                    sw.SpinOnce();
                }
                // Try to take the read lock.
                Interlocked.Increment(ref m_readers[tid].m_taken);
                if (m_writer == 0)
                {
                    // Success, no writer, proceed.
                    result = true;
                    break;
                }
                // Back off, to let the writer go through.
                Interlocked.Decrement(ref m_readers[tid].m_taken);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public bool TryEnterWriteLock(int timeOut)
        {
            bool     result  = false;
            SPW      sw      = new SPW();
            DateTime endTime = DateTime.Now.AddMilliseconds(timeOut);

            while (DateTime.Now < endTime)
            {
                if (m_writer == 0 && Interlocked.Exchange(ref m_writer, 1) == 0)
                {
                    // We now hold the write lock, and prevent new readers.
                    // But we must ensure no readers exist before proceeding.
                    for (int i = 0; i < m_readers.Length; i++)
                    {
                        while (m_readers[i].m_taken != 0)
                        {
                            sw.SpinOnce();
                        }
                    }
                    result = true;
                    break;
                }
                // We failed to take the write lock; wait a bit and retry.
                sw.SpinOnce();
            }
            return(result);
        }
		public void EnterReadLock()
		{
			SPW sw = new SPW();
			int tid = ReadLockIndex;
			// Wait until there are no writers.
			while (true)
			{
				while (m_writer == 1) sw.SpinOnce();
				// Try to take the read lock.
				Interlocked.Increment(ref m_readers[tid].m_taken);
				if (m_writer == 0)
				{
					// Success, no writer, proceed.
					break;
				}
				// Back off, to let the writer go through.
				Interlocked.Decrement(ref m_readers[tid].m_taken);
			}
		}
		public bool TryEnterReadLock(int timeOut)
		{
			bool result = false;
			SPW sw = new SPW();
			int tid = ReadLockIndex;
			// Wait until there are no writers.
			DateTime endTime = DateTime.Now.AddMilliseconds(timeOut);
			while (DateTime.Now < endTime)
			{
				while (m_writer == 1) sw.SpinOnce();
				// Try to take the read lock.
				Interlocked.Increment(ref m_readers[tid].m_taken);
				if (m_writer == 0)
				{
					// Success, no writer, proceed.
					result = true;
					break;
				}
				// Back off, to let the writer go through.
				Interlocked.Decrement(ref m_readers[tid].m_taken);
			}
			return result;
		}
Ejemplo n.º 5
0
        public void EnterReadLock()
        {
            SPW sw  = new SPW();
            int tid = ReadLockIndex;

            // Wait until there are no writers.
            while (true)
            {
                while (m_writer == 1)
                {
                    sw.SpinOnce();
                }
                // Try to take the read lock.
                Interlocked.Increment(ref m_readers[tid].m_taken);
                if (m_writer == 0)
                {
                    // Success, no writer, proceed.
                    break;
                }
                // Back off, to let the writer go through.
                Interlocked.Decrement(ref m_readers[tid].m_taken);
            }
        }
Ejemplo n.º 6
0
        public void EnterWriteLock()
        {
            SPW sw = new SPW();

            while (true)
            {
                if (m_writer == 0 && Interlocked.Exchange(ref m_writer, 1) == 0)
                {
                    // We now hold the write lock, and prevent new readers.
                    // But we must ensure no readers exist before proceeding.
                    for (int i = 0; i < m_readers.Length; i++)
                    {
                        while (m_readers[i].m_taken != 0)
                        {
                            sw.SpinOnce();
                        }
                    }
                    break;
                }
                // We failed to take the write lock; wait a bit and retry.
                sw.SpinOnce();
            }
        }
		public void EnterWriteLock()
		{
			SPW sw = new SPW();
			while (true)
			{
				if (m_writer == 0 && Interlocked.Exchange(ref m_writer, 1) == 0)
				{
					// We now hold the write lock, and prevent new readers.
					// But we must ensure no readers exist before proceeding.
					for (int i = 0; i < m_readers.Length; i++)
						while (m_readers[i].m_taken != 0) sw.SpinOnce();
					break;
				}
				// We failed to take the write lock; wait a bit and retry.
				sw.SpinOnce();
			}
		}
		public bool TryEnterWriteLock(int timeOut)
		{
			bool result = false;
			SPW sw = new SPW();
			DateTime endTime = DateTime.Now.AddMilliseconds(timeOut);
			while (DateTime.Now < endTime)
			{
				if (m_writer == 0 && Interlocked.Exchange(ref m_writer, 1) == 0)
				{
					// We now hold the write lock, and prevent new readers.
					// But we must ensure no readers exist before proceeding.
					for (int i = 0; i < m_readers.Length; i++)
						while (m_readers[i].m_taken != 0) sw.SpinOnce();
					result = true;
					break;
				}
				// We failed to take the write lock; wait a bit and retry.
				sw.SpinOnce();
			}
			return result;
		}