///<summary>Derived class overrides <c>OnEnter</c> to provide specific reader locking semantics.</summary> protected override void OnEnter(Boolean exclusive) { if (exclusive) { Interlocked.Increment(ref m_WriteRequests); Interlocked.Increment(ref m_WritersWaiting); Int64 startTime = Environment.TickCount; InnerLock.Enter(exclusive); // Only 1 thread is writing, so no thread safety is required here m_WriterMaxWaitTime = Math.Max(m_WriterMaxWaitTime, checked ((Int64)(Environment.TickCount - startTime))); Interlocked.Decrement(ref m_WritersWaiting); Interlocked.Increment(ref m_WritersWriting); m_WriterStartHoldTime = Environment.TickCount; } else { Interlocked.Increment(ref m_ReadRequests); Interlocked.Increment(ref m_ReadersWaiting); Int64 startTime = Environment.TickCount; InnerLock.Enter(exclusive); InterlockedEx.Max(ref m_ReaderMaxWaitTime, checked ((Int64)(Environment.TickCount - startTime))); Interlocked.Decrement(ref m_ReadersWaiting); Interlocked.Increment(ref m_ReadersReading); Monitor.Enter(m_ReaderStartHoldTime); m_ReaderStartHoldTime.Add(Thread.CurrentThread.ManagedThreadId, Environment.TickCount); Monitor.Exit(m_ReaderStartHoldTime); } }
///<summary>Derived class overrides <c>OnDoneReading</c> to provide specific reader unlocking semantics.</summary> ///<remarks>You do not need to override this method if the specific lock provides mutual-exclusive locking semantics.</remarks> protected override void OnLeave(Boolean write) { if (write) { // Only 1 thread is writing, so no thread safety is required here Int64 HoldTime = checked ((Int64)(Environment.TickCount - m_WriterStartHoldTime)); m_WriterMinHoldTime = Math.Min(m_WriterMinHoldTime, HoldTime); m_WriterMaxHoldTime = Math.Max(m_WriterMaxHoldTime, HoldTime); m_WritersWriting--; m_WritersDone++; InnerLock.Leave(); } else { Int32 threadId = Thread.CurrentThread.ManagedThreadId; Int64 HoldTime = checked ((Int64)(Environment.TickCount - m_ReaderStartHoldTime[threadId])); Monitor.Enter(m_ReaderStartHoldTime); m_ReaderStartHoldTime.Remove(threadId); Monitor.Exit(m_ReaderStartHoldTime); InterlockedEx.Min(ref m_ReaderMinHoldTime, HoldTime); InterlockedEx.Max(ref m_ReaderMaxHoldTime, HoldTime); Interlocked.Decrement(ref m_ReadersReading); Interlocked.Increment(ref m_ReadersDone); InnerLock.Leave(); } }
// Make sure InnerLock.LockAsync() does not use await, because an async function triggers a snapshot of // the AsyncLocal value. public Task <IDisposable> LockAsync(CancellationToken ct = default) { var locker = new InnerLock(this, _asyncId.Value, ThreadId); _asyncId.Value = Interlocked.Increment(ref ExclusiveLock.AsyncStackCounter); return(locker.ObtainLockAsync(ct)); }
public async Task <IDisposable> LockAsync(CancellationToken ct) { var @lock = new InnerLock(this); await @lock.ObtainLockAsync(ct); return(@lock); }
public async Task <IDisposable> LockAsync() { var @lock = new InnerLock(this); await @lock.ObtainLockAsync(); return(@lock); }
public IDisposable Lock() { var @lock = new InnerLock(this); @lock.ObtainLock(); return(@lock); }
/// <summary>Implements the ResourceLock's WaitToWrite behavior.</summary> protected override void OnEnter(Boolean exclusive) { Int32 CallingThreadId = Thread.CurrentThread.ManagedThreadId; if (exclusive) { // If the calling thread already owns the lock, add 1 to the recursion count and return if (CallingThreadId == m_WriterThreadIdAndRecurseCount.m_Id) { m_WriterThreadIdAndRecurseCount.m_Count++; return; } InnerLock.Enter(exclusive); Interlocked.Exchange(ref m_WriterThreadIdAndRecurseCount.m_Id, CallingThreadId); m_WriterThreadIdAndRecurseCount.m_Count = 0; } else { Int32 index; if (TryFindThreadIdIndex(CallingThreadId, out index)) { // This thread has the reader lock, increment the count and return m_ReaderThreadIdsAndRecurseCounts[index].m_Count++; return; } // This thread doesn't have the lock, wait for it InnerLock.Enter(exclusive); // Record that this thread has the reader lock once AddThreadIdWithRecurseCountOf1(CallingThreadId); } }
public IDisposable Lock() { var locker = new InnerLock(this, _asyncId.Value, ThreadId); // Increment the async stack counter to prevent a child task from getting // the lock at the same time as a child thread. _asyncId.Value = Interlocked.Increment(ref ExclusiveLock.AsyncStackCounter); return(locker.ObtainLock()); }
// Make sure InnerLock.LockAsync() does not use await, because an async function triggers a snapshot of // the AsyncLocal value. public Task <IDisposable> LockAsync(CancellationToken ct) { if (AsyncId == 0) { _asyncId.Value = Interlocked.Increment(ref AsyncLock.AsyncStackCounter); } var @lock = new InnerLock(this); return(@lock.ObtainLockAsync(ct)); }
public IDisposable Lock() { if (AsyncId == 0) { _asyncId.Value = Interlocked.Increment(ref AsyncLock.AsyncStackCounter); } var @lock = new InnerLock(this); return(@lock.ObtainLock()); }
/// <summary>Implements the ResourceLock's Enter behavior.</summary> protected override void OnEnter(Boolean exclusive) { StackTrace st = new System.Diagnostics.StackTrace(0, true); using (new Timer(OnTimeout, st, m_timeout, -1)) { if (exclusive) { InnerLock.Enter(exclusive); } } }
private static InnerLock AcquireLock([NotNull] object key) { lock (Locks) { if (!Locks.ContainsKey(key)) { Locks[key] = new InnerLock(); } return(Locks[key]); } }
/// <summary> /// Führt anwendungsspezifische Aufgaben durch, die mit der Freigabe, der Zurückgabe oder dem Zurücksetzen von nicht verwalteten Ressourcen zusammenhängen. /// </summary> /// <filterpriority>2</filterpriority> public void Dispose() { if (InnerLock != null) { InnerLock.Dispose(); InnerLock = null; } if (buffer != null) { buffer.DecreaseActiveConnectionCount(target); buffer = null; } }
protected override void OnLeave(Boolean write) { if (write) { VerifyOneWriter("Done writing while not writing!"); VerifyNoReaders("Done writing while already reading!"); InterlockedEx.BitTestAndReset(ref m_LockState, 31); // Remove the writer } else { VerifySomeReaders("Done reading while not reading!"); VerifyNoWriters("Done reading while already writing!"); Interlocked.Decrement(ref m_LockState); // Subtract a reader } InnerLock.Leave(); }
protected override void OnEnter(Boolean exclusive) { if (exclusive) { InnerLock.Enter(exclusive); VerifyNoWriters("Writing while already writing!"); VerifyNoReaders("Writing while already reading!"); InterlockedEx.BitTestAndSet(ref m_LockState, 31); // Add the writer } else { InnerLock.Enter(exclusive); VerifyNoWriters("Reading while already writing!"); // Sanity check for no writers Interlocked.Increment(ref m_LockState); // Add a reader } }
private void LoginForm_Shown(object sender, EventArgs e) { InnerLock.Check(); AuthResponse response = new AuthResponse(); if (response.AuthenticateProgram("d2BwyxWCiMv7P$#sG99KfjaFH")) //Chnage to match the Auth token you generate to use without error. { if (response.FreeModeActive) //This is only if you wish to have free mode implemented in your application { response.ExecuteFreeMode(new Main(), this); } } else { MessageBox.Show("Mismatch in program authentication tokens has been noticed. Program will now close for security purposes."); Application.Exit(); } }
/// <summary>Implements the ResourceLock's Leave behavior.</summary> protected override void OnLeave(Boolean exclusive) { Int32 CallingThreadId = Thread.CurrentThread.ManagedThreadId; if (exclusive) { if (m_WriterThreadIdAndRecurseCount.m_Id != CallingThreadId) { throw new InvalidOperationException("Calling thread doesn't own this lock for writing!"); } if (--m_WriterThreadIdAndRecurseCount.m_Count > 0) { return; } Interlocked.Exchange(ref m_WriterThreadIdAndRecurseCount.m_Id, 0); InnerLock.Leave(); } else { Int32 index; if (!TryFindThreadIdIndex(CallingThreadId, out index)) { throw new InvalidOperationException("Calling thread doesn't own the lock for reading!"); } // Decrement this readers recursion count if (--m_ReaderThreadIdsAndRecurseCounts[index].m_Count == 0) { // If this reader is done (recursion count == 0), remove this reader off the list Interlocked.Exchange(ref m_ReaderThreadIdsAndRecurseCounts[index].m_Id, 0); // This reader gives up the lock too InnerLock.Leave(); } } }
/// <summary>Implements the ResourceLock's Leave behavior.</summary> protected override void OnLeave(Boolean exclusive) { InnerLock.Leave(); }