/// <summary> /// Releases all locks taken as part of this transaction. /// </summary> public void Terminate() { AppTrace.TraceSource.WriteNoise("Transaction.Terminate", "{0}", this.id); this.stateLock.EnterWriteLock(); this.status = false; Dictionary <string, List <ILock> > .Enumerator enumerate = this.lockRequests.GetEnumerator(); while (enumerate.MoveNext()) { foreach (ILock x in enumerate.Current.Value) { while (0 != x.Count) { UnlockStatus unlockStatus = this.lockManager.ReleaseLock(x); Debug.Assert(UnlockStatus.Success == unlockStatus); AppTrace.TraceSource.WriteNoise( "Transaction.Terminate", "({0}, {1}, {2}, {3}, {4})", this.id, x.ResourceName, x.Mode, x.Status, x.Count); } } } this.lockRequests.Clear(); this.stateLock.ExitWriteLock(); }
/// <summary> /// Create a new OCPP unlock connector response. /// </summary> /// <param name="Status">The success or failure of the unlock connector command.</param> public UnlockConnectorResponse(UnlockStatus Status) : base(Result.OK()) { this.Status = Status; }
/// <summary> /// Create a new unlock connector response. /// </summary> /// <param name="Request">The start transaction request leading to this response.</param> /// <param name="Status">The success or failure of the unlock connector command.</param> public UnlockConnectorResponse(CS.UnlockConnectorRequest Request, UnlockStatus Status) : base(Request, Result.OK()) { this.Status = Status; }
public static String AsText(this UnlockStatus UnlockStatus) { switch (UnlockStatus) { case UnlockStatus.Unlocked: return("Unlocked"); case UnlockStatus.UnlockFailed: return("UnlockFailed"); case UnlockStatus.NotSupported: return("NotSupported"); default: return("unknown"); } }
public override void ReadFromStream(Stream stream) { BinaryReader reader = new BinaryReader(stream); UnlockStatus = (UnlockStatus)reader.ReadByte(); }
/// <summary> /// Releases a previously acquired lock as part of this transaction. /// </summary> /// <param name="acquiredLock">Previously acquired lock.</param> /// <returns></returns> public bool Unlock(ILock acquiredLock) { // // Check arguments. // if (null == acquiredLock || null == acquiredLock.ResourceName) { throw new ArgumentNullException("acquiredLock"); } if (this.isReadOnly) { if (!this.lockManager.IsShared(acquiredLock.Mode)) { throw new ArgumentException(StringResources.Error_InvalidLock, "acquiredLock"); } } this.stateLock.EnterWriteLock(); try { if (!this.status) { // // All locks have already been released. // return(false); } if (!this.lockRequests.ContainsKey(acquiredLock.ResourceName) || !this.lockRequests[acquiredLock.ResourceName].Contains(acquiredLock)) { // // All locks for this lock resource have been released or // this lock resource was released before. // return(false); } // // Release lock. // UnlockStatus unlockStatus = this.lockManager.ReleaseLock(acquiredLock); Debug.Assert(UnlockStatus.Success == unlockStatus); // // Remove the lock only if this is the last reference to the lock. // if (0 == acquiredLock.Count) { this.lockRequests[acquiredLock.ResourceName].Remove(acquiredLock); } AppTrace.TraceSource.WriteNoise( "Transaction.Unlock", "({0}, {1}, {2}, {3}, {4})", this.id, acquiredLock.ResourceName, acquiredLock.Mode, acquiredLock.Status, acquiredLock.Count); return(true); } finally { this.stateLock.ExitWriteLock(); } }
/// <summary> /// Attempts to acquire the specified lock on behalf of this transaction within the given timeout. /// </summary> /// <param name="resourceName">Resource name to lock.</param> /// <param name="mode">Lock mode for this request.</param> /// <param name="timeout">Lock timeout for this request.</param> /// <exception cref="System.OutOfMemoryException">Operation could not be completed due to insufficient resources.</exception> /// <exception cref="System.TimeoutException">A specified timeout has expired.</exception> /// <exception cref="System.Fabric.FabricObjectClosedException">The object has been closed.</exception> /// <exception cref="System.ArgumentNullException">Rresource name is null or empty.</exception> /// <returns></returns> public async Task <ILock> LockAsync(string resourceName, LockMode mode, int timeout) { // // Check arguments. // if (string.IsNullOrEmpty(resourceName) || string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentNullException("resourceName"); } if (0 > timeout && Timeout.Infinite != timeout) { throw new ArgumentException(StringResources.Error_InvalidTimeOut, "timeout"); } if (LockMode.Free == mode) { throw new ArgumentException(StringResources.Error_InvalidLockMode, "mode"); } if (this.isReadOnly) { if (!this.lockManager.IsShared(mode)) { throw new ArgumentException(StringResources.Error_InvalidLockMode, "mode"); } } // // Check status of this transaction. // this.stateLock.EnterReadLock(); try { if (!this.status) { AppTrace.TraceSource.WriteError( "Transaction.Lock", "({0}, {1}, {2}, {3})", this.id, resourceName, mode, timeout); throw new FabricObjectClosedException(); } } finally { this.stateLock.ExitReadLock(); } ILock acquiredLock = null; try { // // Attempt to acquire lock. // acquiredLock = await this.lockManager.AcquireLockAsync(this.id, resourceName, mode, timeout); } catch (Exception e) { e = (e is AggregateException) ? e.InnerException : e; Debug.Assert(e is OutOfMemoryException || e is ArgumentException); throw; } if (LockStatus.Timeout == acquiredLock.Status) { throw new TimeoutException(); } Debug.Assert(LockStatus.Granted == acquiredLock.Status); Debug.Assert(mode == acquiredLock.Mode); Debug.Assert(this.id == acquiredLock.Owner); // // Store the acquired lock. // this.stateLock.EnterWriteLock(); try { // // Check status of this transaction. // if (!this.status) { AppTrace.TraceSource.WriteError( "Transaction.Lock", "({0}, {1}, {2}, {3})", this.id, resourceName, mode, timeout); throw new FabricObjectClosedException(); } if (!this.lockRequests.ContainsKey(resourceName)) { // // New lock resource requested. // this.lockRequests.Add(resourceName, new List <ILock>()); } // // Add only the first instance of the acquired lock. // if (1 == acquiredLock.Count) { this.lockRequests[resourceName].Add(acquiredLock); } AppTrace.TraceSource.WriteNoise( "Transaction.Lock", "({0}, {1}, {2}, {3}, {4}, {5})", this.id, acquiredLock.ResourceName, acquiredLock.Mode, acquiredLock.Status, acquiredLock.Count, acquiredLock.GrantTime); return(acquiredLock); } catch (Exception e) { Debug.Assert(e is OutOfMemoryException || e is FabricObjectClosedException); // // Unlock acquired lock. // UnlockStatus unlockStatus = this.lockManager.ReleaseLock(acquiredLock); Debug.Assert(UnlockStatus.Success == unlockStatus); throw; } finally { this.stateLock.ExitWriteLock(); } }