forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.sync.pas
309 lines (255 loc) · 6.38 KB
/
web3.sync.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2020 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.sync;
{$I web3.inc}
interface
uses
// Delphi
System.Generics.Collections,
System.RTLConsts,
System.SyncObjs,
System.Threading,
// Velthuis' BigNumbers
Velthuis.BigIntegers;
type
ICriticalThing = interface
procedure Enter;
procedure Leave;
end;
TCriticalThing = class abstract(TInterfacedObject, ICriticalThing)
strict private
Inner: TCriticalSection;
public
procedure Enter;
procedure Leave;
constructor Create; virtual;
destructor Destroy; override;
end;
type
ICriticalGeneric<T> = interface(ICriticalThing)
function Get: T;
procedure Put(const Value: T);
end;
TCriticalGeneric<T> = class(TCriticalThing, ICriticalGeneric<T>)
strict protected
Inner: T;
public
function Get: T;
procedure Put(const Value: T);
constructor Create(const Value: T); reintroduce;
end;
type
ICriticalInt64 = interface(ICriticalGeneric<Int64>)
function Inc: Int64;
end;
TCriticalInt64 = class(TCriticalGeneric<Int64>, ICriticalInt64)
public
function Inc: Int64;
end;
type
ICriticalBigInt = interface(ICriticalGeneric<BigInteger>)
function Inc: BigInteger;
end;
TCriticalBigInt = class(TCriticalGeneric<BigInteger>, ICriticalBigInt)
public
function Inc: BigInteger;
end;
type
ICriticalQueue<T> = interface(ICriticalThing)
function Length: Integer;
procedure Add(const Item: T);
function First: T;
function Get(Index: Integer): T;
procedure Delete(Index, Count: Integer);
end;
TCriticalQueue<T> = class(TCriticalThing, ICriticalQueue<T>)
strict private
Inner: TArray<T>;
public
function Length: Integer;
procedure Add(const Item: T);
function First: T;
function Get(Index: Integer): T;
procedure Delete(Index, Count: Integer);
end;
type
TCriticalList = class abstract(TCriticalThing)
strict private
Inner: TList<IInterface>;
strict protected
function Get(Index: Integer): IInterface;
procedure Put(Index: Integer; const Item: IInterface);
procedure Clear;
procedure Delete(Index: Integer);
function First: IInterface;
function Add(const Item: IInterface): Integer;
function Count: Integer;
public
constructor Create; override;
destructor Destroy; override;
end;
type
TCriticalDictionary<TKey,TValue> = class(TDictionary<TKey,TValue>)
strict private
Inner: TCriticalSection;
public
procedure Enter;
procedure Leave;
destructor Destroy; override;
end;
function ThreadPool: TThreadPool;
implementation
var
_ThreadPool: TThreadPool = nil;
function ThreadPool: TThreadPool;
begin
if not Assigned(_ThreadPool) then
_ThreadPool := TThreadPool.Create;
Result := _ThreadPool;
end;
{ TCriticalThing }
constructor TCriticalThing.Create;
begin
inherited Create;
Inner := TCriticalSection.Create;
end;
destructor TCriticalThing.Destroy;
begin
if Assigned(Inner) then Inner.Free;
inherited Destroy;
end;
procedure TCriticalThing.Enter;
begin
Inner.Enter;
end;
procedure TCriticalThing.Leave;
begin
Inner.Leave;
end;
{ TCriticalGeneric }
constructor TCriticalGeneric<T>.Create(const Value: T);
begin
inherited Create;
Inner := Value;
end;
function TCriticalGeneric<T>.Get: T;
begin
Result := Inner;
end;
procedure TCriticalGeneric<T>.Put(const Value: T);
begin
Inner := Value;
end;
{ TCriticalInt64 }
function TCriticalInt64.Inc: Int64;
begin
Inner := Inner + 1;
Result := Inner;
end;
{ TCriticalBigInt }
function TCriticalBigInt.Inc: BigInteger;
begin
Inner := Inner + 1;
Result := Inner;
end;
{ TCriticalQueue<T> }
function TCriticalQueue<T>.Length: Integer;
begin
Result := System.Length(Inner);
end;
procedure TCriticalQueue<T>.Add(const Item: T);
begin
Inner := Inner + [Item];
end;
function TCriticalQueue<T>.First: T;
begin
Result := Get(0);
end;
function TCriticalQueue<T>.Get(Index: Integer): T;
begin
Result := Inner[Index];
end;
procedure TCriticalQueue<T>.Delete(Index: Integer; Count: Integer);
begin
System.Delete(Inner, Index, Count);
end;
{ TCriticalList }
constructor TCriticalList.Create;
begin
inherited Create;
Inner := TList<IInterface>.Create;
end;
destructor TCriticalList.Destroy;
begin
Clear;
if Assigned(Inner) then Inner.Free;
inherited Destroy;
end;
procedure TCriticalList.Clear;
var
I: Integer;
begin
if Assigned(Inner) then
begin
for I := 0 to Pred(Inner.Count) do
Inner[I] := nil;
Inner.Clear;
end;
end;
procedure TCriticalList.Delete(Index: Integer);
begin
if (Index < 0) or (Index >= Count) then
Inner.Error(SListIndexError, Index);
Inner[Index] := nil;
Inner.Delete(Index);
end;
function TCriticalList.First: IInterface;
begin
Result := Get(0);
end;
function TCriticalList.Get(Index: Integer): IInterface;
begin
if (Index < 0) or (Index >= Count) then
Inner.Error(SListIndexError, Index);
Result := Inner[Index];
end;
function TCriticalList.Count: Integer;
begin
Result := Inner.Count;
end;
function TCriticalList.Add(const Item: IInterface): Integer;
begin
Result := Inner.Add(nil);
Inner[Result] := Item;
end;
procedure TCriticalList.Put(Index: Integer; const Item: IInterface);
begin
if (Index < 0) or (Index >= Count) then
Inner.Error(SListIndexError, Index);
Inner[Index] := Item;
end;
{ TCriticalDictionary }
destructor TCriticalDictionary<TKey,TValue>.Destroy;
begin
if Assigned(Inner) then Inner.Free;
inherited Destroy;
end;
procedure TCriticalDictionary<TKey,TValue>.Enter;
begin
if not Assigned(Inner) then
Inner := TCriticalSection.Create;
Inner.Enter;
end;
procedure TCriticalDictionary<TKey,TValue>.Leave;
begin
if Assigned(Inner) then Inner.Leave;
end;
end.