DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
LockHandle.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
19 using Deveel.Data.Sql;
20 
21 namespace Deveel.Data.Transactions {
22  public sealed class LockHandle : IDisposable {
23  private Lock[] locks;
24  private int lockIndex;
25 
26  internal LockHandle(int lockCount) {
27  locks = new Lock[lockCount];
28  lockIndex = 0;
29  IsUnlocked = false;
30  }
31 
32  private bool IsUnlocked { get; set; }
33 
34  internal void AddLock(Lock @lock) {
35  locks[lockIndex++] = @lock;
36  }
37 
38  internal void Release() {
39  if (!IsUnlocked) {
40  for (int i = locks.Length - 1; i >= 0; --i) {
41  locks[i].Release();
42  }
43 
44  IsUnlocked = true;
45  }
46  }
47 
48  public void CheckAccess(ILockable lockable, AccessType accessType) {
49  for (int i = locks.Length - 1; i >= 0; --i) {
50  var tableLock = locks[i];
51  if (tableLock.Lockable == lockable) {
52  tableLock.CheckAccess(accessType);
53  return;
54  }
55  }
56 
57  throw new Exception("The given object was not found in the lock list for this handle");
58  }
59 
60  public bool IsHandled(ILockable lockable) {
61  for (int i = locks.Length - 1; i >= 0; i--) {
62  if (locks[i].Lockable == lockable)
63  return true;
64  }
65 
66  return false;
67  }
68 
69  public void Dispose() {
70  if (!IsUnlocked) {
71  Release();
72 
73  // TODO: report the situation: there should not be
74  // a call to Release() at this point...
75 
76  locks = null;
77  }
78  }
79  }
80 }
bool IsHandled(ILockable lockable)
Definition: LockHandle.cs:60
void CheckAccess(ILockable lockable, AccessType accessType)
Definition: LockHandle.cs:48
void CheckAccess(AccessType accessType)
Definition: Lock.cs:101