Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NestedLocks.cs
1using System;
2using System.Collections.Generic;
4
6{
10 internal class NestedLocks
11 {
12 private readonly ObjectBTreeFile file;
13 private int count;
14 private Dictionary<ObjectBTreeFile, int> additionalLocks = null;
15 private Dictionary<ObjectBTreeFile, bool> touched = null;
16
22 public NestedLocks(ObjectBTreeFile File, bool WriteLock)
23 {
24 this.file = File;
25 this.count = WriteLock ? 1 : -1;
26 }
27
36 public static NestedLocks CreateIfNested(ObjectBTreeFile File, bool WriteLock, IObjectSerializer Serializer)
37 {
38 if (Serializer is ObjectSerializer ObjectSerializer)
39 return CreateIfNested(File, WriteLock, ObjectSerializer);
40 else
41 return null;
42 }
43
52 public static NestedLocks CreateIfNested(ObjectBTreeFile File, bool WriteLock, ObjectSerializer Serializer)
53 {
54 return Serializer.HasByReference ? new NestedLocks(File, WriteLock) : null;
55 }
56
63 public bool HasLock(ObjectBTreeFile File, out bool WriteLock)
64 {
65 if (this.touched is null)
66 this.touched = new Dictionary<ObjectBTreeFile, bool>();
67
68 this.touched[File] = true;
69
70 if (File == this.file)
71 {
72 WriteLock = this.count > 0;
73 return true;
74 }
75
76 if (this.additionalLocks is null)
77 {
78 WriteLock = false;
79 return false;
80 }
81
82 if (this.additionalLocks.TryGetValue(File, out int i))
83 {
84 WriteLock = i > 0;
85 return true;
86 }
87 else
88 {
89 WriteLock = false;
90 return false;
91 }
92 }
93
99 public void AddLock(ObjectBTreeFile File, bool WriteLock)
100 {
101 if (this.touched is null)
102 this.touched = new Dictionary<ObjectBTreeFile, bool>();
103
104 this.touched[File] = true;
105
106 if (File == this.file)
107 {
108 if (WriteLock ^ (this.count > 0))
109 throw new InvalidOperationException("Mismatching lock state.");
110
111 if (WriteLock)
112 this.count++;
113 else
114 this.count--;
115 }
116 else
117 {
118 if (this.additionalLocks is null)
119 {
120 if (this.additionalLocks is null)
121 this.additionalLocks = new Dictionary<ObjectBTreeFile, int>();
122 }
123
124 if (this.additionalLocks.TryGetValue(File, out int i))
125 {
126 if (WriteLock ^ (i > 0))
127 throw new InvalidOperationException("Mismatching lock state.");
128
129 if (WriteLock)
130 i++;
131 else
132 i--;
133 }
134 else
135 i = WriteLock ? 1 : -1;
136
137 this.additionalLocks[File] = i;
138 }
139 }
140
146 public bool RemoveLock(ObjectBTreeFile File)
147 {
148 if (File == this.file)
149 {
150 if (this.count > 0)
151 this.count--;
152 else
153 this.count++;
154
155 return true;
156 }
157 else
158 {
159 if (this.additionalLocks is null)
160 return false;
161
162 if (!this.additionalLocks.TryGetValue(File, out int i))
163 return false;
164
165 if (i > 0)
166 i--;
167 else
168 i++;
169
170 if (i == 0)
171 this.additionalLocks.Remove(File);
172 else
173 this.additionalLocks[File] = i;
174
175 return true;
176 }
177 }
178
184 public bool HasBeenTouched(ObjectBTreeFile File)
185 {
186 return this.touched?.ContainsKey(File) ?? false;
187 }
188
189 }
190}
Serializes a class, taking into account attributes defined in Attributes.