-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYWTypes.pas
515 lines (447 loc) · 11.2 KB
/
YWTypes.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
unit YWTypes;
interface
uses sysutils,Classes;
type
__T1 = array[0..0] of byte;
__T2 = array[0..1] of byte;
__T3 = array[0..2] of byte;
__T4 = array[0..3] of byte;
__T5 = array[0..4] of byte;
__T6 = array[0..5] of byte;
__T7 = array[0..6] of byte;
__T8 = array[0..7] of byte;
__T9 = array[0..8] of byte;
__T10 = array[0..9] of byte;
__T11 = array[0..10] of byte;
__T12 = array[0..11] of byte;
__T13 = array[0..12] of byte;
__T14 = array[0..13] of byte;
__T15 = array[0..14] of byte;
__T16 = array[0..15] of byte;
TBufferPool<T>=record
const BufferSize = 1 shl (11+sizeof(T));
class var
[volatile]buf : Pointer;
class function GetBuffer : Pointer; static; inline;
class procedure FreeBuffer(var B : Pointer); static; inline;
class constructor Create;
class destructor Destroy;
end;
BufferPool4k = TBufferPool<__T1>;
BufferPool16k = TBufferPool<__T3>;
BufferPool64k = TBufferPool<__T5>;
BufferPool128k = TBufferPool<__T6>;
BufferPool256k = TBufferPool<__T7>;
BufferPool512k = TBufferPool<__T8>;
BufferPool1M = TBufferPool<__T9>;
BufferPool2M = TBufferPool<__T10>;
BufferPool4M = TBufferPool<__T11>;
TRingQueue<T> = record
const
QueueSize = (1 shl sizeof(T)*3)*8-1;
var
[volatile]head : Integer;
[volatile]tail : Integer;
[volatile]Data : array[0..QueueSize] of Pointer;
procedure Init; inline;
function Put(const V : Pointer):boolean; inline;
function Get:Pointer; inline;
class operator Initialize (out Dest : TRingQueue<T>);
end;
TRingQueue64 = TRingQueue<__T1>;
TRingQueue512 = TRingQueue<__T2>;
TRingQueue4k = TRingQueue<__T3>;
TRingQueue256k = TRingQueue<__T5>;
__RWLock<T> =record
private
[volatile]R : Cardinal;
[volatile]WW : Cardinal;
[volatile]W : TThreadID;
public
const NeedSleep = sizeof(T)<=2;
class procedure idle; inline; static;
class operator Initialize (out Dest: __RWLock<T>);
class operator Finalize (var Dest: __RWLock<T>);
procedure BeginRead; inline;
procedure EndRead; inline;
procedure BeginWrite; inline;
procedure EndWrite; inline;
procedure UpgradeToWrite; inline;
procedure DownGradeToRead; inline;
end;
RWLock = __RWLock<__T3>;
RWLock4Calc = __RWLock<__T1>;
RWLock4IO = __RWLock<__T2>;
__RefPtr = ^__Ref;
__Ref = record
private
[volatile]RefCount : Cardinal;
[volatile]WeakRefCount : Cardinal;
public
RefObj : TObject;
procedure AddRef; inline;
procedure RemoveRef; inline;
procedure AddWeakRef; inline;
procedure RemoveWeakRef; inline;
class operator Initialize (out Dest: __Ref);
end;
R<T : class> =record
private
{$IFNDEF AUTOREFCOUNT}
GuardRef : __RefPtr;
{$ELSE}
obj : T;
{$ENDIF}
public
function O : T; inline;
class operator Implicit(a: T): R<T>; inline;
class operator Implicit(a: R<T>): T; inline;
class operator Equal(a, b: R<T>) : Boolean; inline;
class operator NotEqual(a, b: R<T>) : Boolean; inline;
class operator Equal(a : R<T>; b: Pointer) : Boolean; inline;
class operator NotEqual(a : R<T>; b: Pointer) : Boolean; inline;
class operator Equal(a : R<T>; b: T) : Boolean; inline;
class operator NotEqual(a : R<T>; b: T) : Boolean; inline;
class operator Positive(a: R<T>): T; inline;
{$IFNDEF AUTOREFCOUNT}
class operator Initialize (out Dest: R<T>);
class operator Finalize (var Dest: R<T>);
class operator Assign (var Dest: R<T>; const [ref] Src: R<T>);
inline;
{$ENDIF}
end;
WR<T : class> =record
private
{$IFNDEF AUTOREFCOUNT}
GuardRef : __RefPtr;
{$ELSE}
[WEAK]obj : T;
{$ENDIF}
public
function O : T; inline;
class operator Implicit(a: WR<T>): R<T>; inline;
class operator Implicit(a: R<T>): WR<T>; inline;
class operator Equal(a, b: WR<T>) : Boolean; inline;
class operator NotEqual(a, b: WR<T>) : Boolean; inline;
class operator Equal(a : WR<T>; b: Pointer) : Boolean; inline;
class operator NotEqual(a : WR<T>; b: Pointer) : Boolean; inline;
class operator Equal(a : WR<T>; b: T) : Boolean; inline;
class operator NotEqual(a : WR<T>; b: T) : Boolean; inline;
class operator Equal(a : WR<T>; b: R<T>) : Boolean; inline;
class operator NotEqual(a : WR<T>; b: R<T>) : Boolean; inline;
class operator Positive(a: WR<T>): T; inline;
{$IFNDEF AUTOREFCOUNT}
class operator Initialize (out Dest: WR<T>);
class operator Finalize (var Dest: WR<T>);
class operator Assign (var Dest: WR<T>; const [ref] Src: WR<T>);
inline;
{$ENDIF}
end;
implementation
{ TRingQueue<T> }
function TRingQueue<T>.Get: Pointer;
var t : integer;
successed : boolean;
begin
repeat
t := tail;
Result := Data[t];
if Result=nil then exit; //Queue is empty, Return a nil pointer
atomiccmpexchange(tail,(t+1)and QueueSize,t,successed);
until successed;
Data[t] := nil;
end;
procedure TRingQueue<T>.Init;
begin
fillchar(Self,Sizeof(Self),0);
end;
class operator TRingQueue<T>.Initialize(out Dest: TRingQueue<T>);
begin
Dest.Init;
end;
function TRingQueue<T>.Put(const V: Pointer): boolean;
begin
Result := (V<>nil);
if not Result then exit; //Nil pointer not allowed, Return false
var h : integer;
var successed : boolean:=false;
repeat
h := head;
Result := Data[h]=nil;
if not Result then
if h=tail then exit //Queue is full, Return false
else continue;
atomiccmpexchange(head,(h+1)and QueueSize,h,successed);
until successed;
Data[h] := V;
end;
{ _RWLock }
procedure __RWLock<T>.BeginRead;
begin
if W=TThread.Current.ThreadID then exit;
repeat
while WW>0 do idle;
atomicincrement(R);
if WW=0 then break;
atomicdecrement(R);
idle;
until false;
end;
procedure __RWLock<T>.BeginWrite;
begin
if W=TThread.Current.ThreadID then exit;
atomicincrement(WW);
repeat
while (R>0)or(W<>0) do idle;
var successed : boolean;
atomiccmpexchange(W,TThread.Current.ThreadID,0,successed);
if successed then break;
idle;
until false;
end;
procedure __RWLock<T>.DownGradeToRead;
begin
if W<>TThread.Current.ThreadID then BeginRead
else begin
atomicincrement(R);
W := 0;
atomicdecrement(WW);
end;
end;
procedure __RWLock<T>.EndRead;
begin
if W=0 then atomicdecrement(R);
end;
procedure __RWLock<T>.EndWrite;
begin
if W=TThread.Current.ThreadID then begin
W := 0;
atomicdecrement(WW);
end;
end;
class operator __RWLock<T>.Finalize(var Dest: __RWLock<T>);
begin
while Dest.R or Dest.WW<>0 do idle;
end;
class procedure __RWLock<T>.idle;
begin
if NeedSleep then sleep(sizeof(T)-1);
end;
class operator __RWLock<T>.Initialize(out Dest: __RWLock<T>);
begin
Dest.R := 0;
Dest.WW := 0;
Dest.W := 0;
end;
procedure __RWLock<T>.UpgradeToWrite;
begin
if W<>0 then BeginWrite
else begin
atomicincrement(WW);
while R>1 do idle;
W := TThread.Current.ThreadID;
atomicdecrement(R);
end;
end;
{ __Ref }
procedure __Ref.AddRef;
begin
atomicincrement(RefCount);
AddWeakRef;
end;
procedure __Ref.AddWeakRef;
begin
atomicincrement(WeakRefCount);
end;
class operator __Ref.Initialize(out Dest: __Ref);
begin
Dest.RefCount := 0;
Dest.WeakRefCount := 0;
Dest.RefObj := nil;
end;
procedure __Ref.RemoveRef;
begin
if atomicdecrement(RefCount)=0 then begin
RefObj.DisposeOf;
RefObj := nil;
end;
RemoveWeakRef;
end;
procedure __Ref.RemoveWeakRef;
begin
if atomicdecrement(WeakRefCount)=0 then begin
var p : __RefPtr := @Self;
dispose(p);
end;
end;
{ R<T> }
{$IFNDEF AUTOREFCOUNT}
class operator R<T>.Assign(var Dest: R<T>; const [Ref]Src: R<T>);
begin
if Dest.GuardRef<>nil then Dest.GuardRef.RemoveRef;
Dest.GuardRef := Src.GuardRef;
if Dest.GuardRef<>nil then Dest.GuardRef.AddRef;
end;
class operator R<T>.Initialize(out Dest: R<T>);
begin
Dest.GuardRef := nil;
end;
class operator R<T>.Finalize(var Dest: R<T>);
begin
if Dest.GuardRef<>nil then Dest.GuardRef.RemoveRef;
end;
{$ENDIF}
class operator R<T>.Equal(a: R<T>; b: T): Boolean;
begin
Result := b=a.O;
end;
class operator R<T>.Equal(a: R<T>; b: Pointer): Boolean;
begin
Result := b=Pointer(a.O);
end;
class operator R<T>.Equal(a, b: R<T>): Boolean;
begin
Result := a.O = b.O;
end;
class operator R<T>.Implicit(a: R<T>): T;
begin
Result := a.O;
end;
class operator R<T>.Implicit(a: T): R<T>;
begin
{$IFDEF AUTOREFCOUNT}
Result.obj := a;
{$ELSE}
if a<>nil then begin
new(Result.GuardRef);
Result.GuardRef.RefObj := a;
Result.GuardRef.AddRef;
end;
{$ENDIF}
end;
class operator R<T>.NotEqual(a: R<T>; b: Pointer): Boolean;
begin
Result := b<>Pointer(a.O);
end;
class operator R<T>.NotEqual(a: R<T>; b: T): Boolean;
begin
Result := b<>a.O;
end;
class operator R<T>.NotEqual(a, b: R<T>): Boolean;
begin
Result := a.O<>b.O;
end;
function R<T>.O: T;
begin
{$IFDEF AUTOREFCOUNT}
Result := obj;
{$ELSE}
Result := nil;
if GuardRef<>nil then Result := T(GuardRef.RefObj);
{$ENDIF}
end;
class operator R<T>.Positive(a: R<T>): T;
begin
Result := a.O;
end;
{ WR<T> }
class operator WR<T>.Equal(a: WR<T>; b: R<T>): Boolean;
begin
Result := a.O = b.O;
end;
class operator WR<T>.Equal(a: WR<T>; b: T): Boolean;
begin
Result := a.O = b;
end;
class operator WR<T>.Equal(a: WR<T>; b: Pointer): Boolean;
begin
Result := Pointer(a.O) = b;
end;
class operator WR<T>.Equal(a, b: WR<T>): Boolean;
begin
Result := a.O = b.O;
end;
{$IFNDEF AUTOREFCOUNT}
class operator WR<T>.Initialize(out Dest: WR<T>);
begin
Dest.GuardRef := nil;
end;
class operator WR<T>.Finalize(var Dest: WR<T>);
begin
if Dest.GuardRef<>nil then Dest.GuardRef.RemoveWeakRef;
end;
class operator WR<T>.Assign(var Dest: WR<T>; const [Ref]Src: WR<T>);
begin
if Dest.GuardRef<>nil then Dest.GuardRef.RemoveWeakRef;
Dest.GuardRef := Src.GuardRef;
if Dest.GuardRef<>nil then Dest.GuardRef.AddWeakRef;
end;
{$ENDIF}
class operator WR<T>.Implicit(a: R<T>): WR<T>;
begin
{$IFDEF AUTOREFCOUNT}
Result.obj := a.O;
{$ELSE}
Result.GuardRef := a.GuardRef;
if Result.GuardRef<>nil then Result.GuardRef.AddWeakRef;
{$ENDIF}
end;
class operator WR<T>.Implicit(a: WR<T>): R<T>;
begin
{$IFDEF AUTOREFCOUNT}
Result.obj := a.O;
{$ELSE}
Result.GuardRef := a.GuardRef;
if Result.GuardRef<>nil then Result.GuardRef.AddRef;
{$ENDIF}
end;
class operator WR<T>.NotEqual(a, b: WR<T>): Boolean;
begin
Result := a.O <> b.O;
end;
class operator WR<T>.NotEqual(a: WR<T>; b: Pointer): Boolean;
begin
Result := Pointer(a.O)<>b;
end;
class operator WR<T>.NotEqual(a: WR<T>; b: R<T>): Boolean;
begin
Result := a.O<>b.O;
end;
class operator WR<T>.NotEqual(a: WR<T>; b: T): Boolean;
begin
Result := a.O <> b;
end;
function WR<T>.O: T;
begin
{$IFDEF AUTOREFCOUNT}
Result := obj;
{$ELSE}
Result := nil;
if GuardRef<>nil then Result := T(GuardRef.RefObj);
{$ENDIF}
end;
class operator WR<T>.Positive(a: WR<T>): T;
begin
Result := a.O;
end;
{ TBufferPool<T> }
class constructor TBufferPool<T>.Create;
begin
buf := nil;
end;
class destructor TBufferPool<T>.Destroy;
begin
if Buf<>nil then Freemem(Buf);
end;
class procedure TBufferPool<T>.FreeBuffer(var B: Pointer);
begin
b := AtomicExchange(buf,b);
if b<>nil then Freemem(b);
end;
class function TBufferPool<T>.GetBuffer: Pointer;
begin
Result := nil;
Result := AtomicExchange(buf,Result);
if Result=nil then Getmem(Result,buffersize);
end;
end.