2using System.Collections.Generic;
10 internal class NestedLocks
12 private readonly ObjectBTreeFile file;
14 private Dictionary<ObjectBTreeFile, int> additionalLocks =
null;
15 private Dictionary<ObjectBTreeFile, bool> touched =
null;
22 public NestedLocks(ObjectBTreeFile File,
bool WriteLock)
25 this.count = WriteLock ? 1 : -1;
36 public static NestedLocks CreateIfNested(ObjectBTreeFile File,
bool WriteLock,
IObjectSerializer Serializer)
52 public static NestedLocks CreateIfNested(ObjectBTreeFile File,
bool WriteLock,
ObjectSerializer Serializer)
54 return Serializer.HasByReference ?
new NestedLocks(File, WriteLock) : null;
63 public bool HasLock(ObjectBTreeFile File, out
bool WriteLock)
65 if (this.touched is
null)
66 this.touched =
new Dictionary<ObjectBTreeFile, bool>();
68 this.touched[File] =
true;
70 if (File == this.file)
72 WriteLock = this.count > 0;
76 if (this.additionalLocks is
null)
82 if (this.additionalLocks.TryGetValue(File, out
int i))
99 public void AddLock(ObjectBTreeFile File,
bool WriteLock)
101 if (this.touched is
null)
102 this.touched =
new Dictionary<ObjectBTreeFile, bool>();
104 this.touched[File] =
true;
106 if (File == this.file)
108 if (WriteLock ^ (this.count > 0))
109 throw new InvalidOperationException(
"Mismatching lock state.");
118 if (this.additionalLocks is
null)
120 if (this.additionalLocks is
null)
121 this.additionalLocks =
new Dictionary<ObjectBTreeFile, int>();
124 if (this.additionalLocks.TryGetValue(File, out
int i))
126 if (WriteLock ^ (i > 0))
127 throw new InvalidOperationException(
"Mismatching lock state.");
135 i = WriteLock ? 1 : -1;
137 this.additionalLocks[File] = i;
146 public bool RemoveLock(ObjectBTreeFile File)
148 if (File == this.file)
159 if (this.additionalLocks is
null)
162 if (!this.additionalLocks.TryGetValue(File, out
int i))
171 this.additionalLocks.Remove(File);
173 this.additionalLocks[File] = i;
184 public bool HasBeenTouched(ObjectBTreeFile File)
186 return this.touched?.ContainsKey(File) ??
false;
Serializes a class, taking into account attributes defined in Attributes.
Interface for object serializers.