-
Notifications
You must be signed in to change notification settings - Fork 2
/
PasMP.pas
10912 lines (10090 loc) · 336 KB
/
PasMP.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(******************************************************************************
* PasMP *
******************************************************************************
* Version 2016-06-05-18-00-0000 *
******************************************************************************
* zlib license *
*============================================================================*
* *
* Copyright (C) 2016, Benjamin Rosseaux (benjamin@rosseaux.de) *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement in the product documentation would be *
* appreciated but is not required. *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
* *
******************************************************************************
* General guidelines for code contributors *
*============================================================================*
* *
* 1. Make sure you are legally allowed to make a contribution under the zlib *
* license. *
* 2. The zlib license header goes at the top of each source file, with *
* appropriate copyright notice. *
* 3. After a pull request, check the status of your pull request on *
http://github.com/BeRo1985/pasmp *
* 4. Write code, which is compatible with Delphi 7-XE7 and FreePascal >= 2.6 *
* so don't use generics/templates, operator overloading and another newer *
* syntax features than Delphi 7 has support for that, but if needed, make *
* it out-ifdef-able. *
* 5. Don't use Delphi-only, FreePascal-only or Lazarus-only libraries/units, *
* but if needed, make it out-ifdef-able. *
* 6. No use of third-party libraries/units as possible, but if needed, make *
* it out-ifdef-able. *
* 7. Try to use const when possible. *
* 8. Make sure to comment out writeln, used while debugging. *
* 9. Make sure the code compiles on 32-bit and 64-bit platforms (x86-32, *
* x86-64, ARM, ARM64, etc.). *
* 10. Make sure the code runs on platforms with weak and strong memory *
* models without any issues. *
* *
******************************************************************************)
unit PasMP;
{$ifdef fpc}
{$mode delphi}
{$ifdef CPUi386}
{$define CPU386}
{$endif}
{$ifdef CPUamd64}
{$define CPUx86_64}
{$endif}
{$ifdef CPU386}
{$define CPUx86}
{$define CPU32}
{$asmmode intel}
{$endif}
{$ifdef CPUx86_64}
{$define CPUx64}
{$define CPU64}
{$asmmode intel}
{$endif}
{$ifdef FPC_LITTLE_ENDIAN}
{$define LITTLE_ENDIAN}
{$else}
{$ifdef FPC_BIG_ENDIAN}
{$define BIG_ENDIAN}
{$endif}
{$endif}
{-$pic off}
{$define CAN_INLINE}
{$ifdef FPC_HAS_TYPE_EXTENDED}
{$define HAS_TYPE_EXTENDED}
{$else}
{$undef HAS_TYPE_EXTENDED}
{$endif}
{$ifdef FPC_HAS_TYPE_DOUBLE}
{$define HAS_TYPE_DOUBLE}
{$else}
{$undef HAS_TYPE_DOUBLE}
{$endif}
{$ifdef FPC_HAS_TYPE_SINGLE}
{$define HAS_TYPE_SINGLE}
{$else}
{$undef HAS_TYPE_SINGLE}
{$endif}
{$if declared(RawByteString)}
{$define HAS_TYPE_RAWBYTESTRING}
{$else}
{$undef HAS_TYPE_RAWBYTESTRING}
{$ifend}
{$if declared(UTF8String)}
{$define HAS_TYPE_UTF8STRING}
{$else}
{$undef HAS_TYPE_UTF8STRING}
{$ifend}
{$define HAS_GENERICS}
{$define HAS_STATIC}
{$else}
{$realcompatibility off}
{$localsymbols on}
{$define LITTLE_ENDIAN}
{$ifndef CPU64}
{$define CPU32}
{$endif}
{$ifdef CPUx64}
{$define CPUx86_64}
{$define CPU64}
{$else}
{$ifdef CPU386}
{$define CPUx86}
{$define CPU32}
{$endif}
{$endif}
{$define HAS_TYPE_EXTENDED}
{$define HAS_TYPE_DOUBLE}
{$ifdef conditionalexpressions}
{$if declared(RawByteString)}
{$define HAS_TYPE_RAWBYTESTRING}
{$else}
{$undef HAS_TYPE_RAWBYTESTRING}
{$ifend}
{$if declared(UTF8String)}
{$define HAS_TYPE_UTF8STRING}
{$else}
{$undef HAS_TYPE_UTF8STRING}
{$ifend}
{$else}
{$undef HAS_TYPE_RAWBYTESTRING}
{$undef HAS_TYPE_UTF8STRING}
{$endif}
{$if CompilerVersion>=24}
{$legacyifend on}
{$define HAS_ATOMICS}
{$ifend}
{$if CompilerVersion>=20}
{$define CAN_INLINE}
{$define HAS_ANONYMOUS_METHODS}
{$define HAS_GENERICS}
{$define HAS_STATIC}
{$ifend}
{$if CompilerVersion>=25}
{$define HAS_WEAK}
{$define HAS_VOLATILE}
{$define HAS_REF}
{$ifend}
{$endif}
{$ifdef CPU386}
{$define HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE}
{$endif}
{$ifdef CPUx86_64}
{$define HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE}
{$endif}
{$ifdef CPUARM}
{$define HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE}
{$endif}
{$ifdef Win32}
{$define Windows}
{$endif}
{$ifdef Win64}
{$define Windows}
{$endif}
{$ifdef WinCE}
{$define Windows}
{$endif}
{$rangechecks off}
{$extendedsyntax on}
{$writeableconst on}
{$hints off}
{$booleval off}
{$typedaddress off}
{$stackframes off}
{$varstringchecks on}
{$typeinfo on}
{$overflowchecks off}
{$longstrings on}
{$openstrings on}
{$undef UseXorShift128}
{$undef UseThreadLocalStorage}
{$undef UseThreadLocalStorageX8632}
{$ifdef PasMPUseAsStrictSingletonInstance}
{$if defined(Windows) and (defined(CPU386) or defined(CPUx86_64)) and not (defined(FPC) or defined(UseMultiplePasMPInstanceInstances))}
// Delphi (under x86 Windows) has fast thread local storage handling (per nearly direct TEB access by reading fs:[0x18])
{$define UseThreadLocalStorage}
{$ifdef cpu386}
{$define UseThreadLocalStorageX8632}
{$endif}
{$ifdef cpux86_64}
{$define UseThreadLocalStorageX8664}
{$endif}
{$else}
// FreePascal has portable but unfortunately slow thread local storage handling (for example under Windows, over TLSGetIndex
// calls etc. in FPC_THREADVAR_RELOCATE), so use here the bit faster thread ID hash table approach with less total CPU-cycle
// count and less OS-API calls than with the FPC_THREADVAR_RELOCATE variant
{$ifend}
{$endif}
{$define PasMPUseWakeUpConditionVariable}
interface
uses {$ifdef Windows}
Windows,MMSystem,
{$else}
{$ifdef fpc}
{$ifdef Unix}
{$ifdef usecthreads}
cthreads,
{$endif}
BaseUnix,Unix,UnixType,PThreads,
{$ifdef Linux}
Linux,
{$else}
ctypes,sysctl,
{$endif}
{$endif}
{$endif}
{$endif}
SysUtils,Classes,
{$ifdef HAS_GENERICS}
{$ifdef fpc}
{$ifdef FreePascalGenericsCollectionsLibrary}
Generics.Defaults,
{$define HasGenericsCollections}
{$endif}
{$else}
System.Generics.Defaults,
{$define HasGenericsCollections}
{$endif}
{$endif}
Math,SyncObjs;
type PPasMPInt8=^TPasMPInt8;
TPasMPInt8=shortint;
PPasMPUInt8=^TPasMPUInt8;
TPasMPUInt8=byte;
PPasMPInt16=^TPasMPInt16;
TPasMPInt16=smallint;
PPasMPUInt16=^TPasMPUInt16;
TPasMPUInt16=word;
PPasMPInt32=^TPasMPInt32;
TPasMPInt32=longint;
PPasMPUInt32=^TPasMPUInt32;
TPasMPUInt32=longword;
PPasMPInt64=^TPasMPInt64;
TPasMPInt64=int64;
PPasMPUInt64=^TPasMPUInt64;
PPasMPPtrUInt=^TPasMPPtrUInt;
PPasMPPtrInt=^TPasMPPtrInt;
{$ifdef fpc}
{$undef OldDelphi}
TPasMPUInt64=uint64;
TPasMPPtrUInt=PtrUInt;
TPasMPPtrInt=PtrInt;
{$else}
{$ifdef conditionalexpressions}
{$if CompilerVersion>=23.0}
{$undef OldDelphi}
TPasMPUInt64=uint64;
TPasMPPtrUInt=NativeUInt;
TPasMPPtrInt=NativeInt;
{$else}
{$define OldDelphi}
{$ifend}
{$else}
{$define OldDelphi}
{$endif}
{$endif}
{$ifdef OldDelphi}
{$if CompilerVersion>=15.0}
TPasMPUInt64=uint64;
{$else}
TPasMPUInt64=TPasMPInt64;
{$ifend}
{$ifdef CPU64}
TPasMPPtrUInt=qword;
TPasMPPtrInt=TPasMPInt64;
{$else}
TPasMPPtrUInt=TPasMPUInt32;
TPasMPPtrInt=TPasMPInt32;
{$endif}
{$endif}
const PasMPAllocatorPoolBucketBits=12;
PasMPAllocatorPoolBucketSize=1 shl PasMPAllocatorPoolBucketBits;
PasMPAllocatorPoolBucketMask=PasMPAllocatorPoolBucketSize-1;
PasMPJobQueueStartSize=4096; // must be power of two
PasMPJobWorkerThreadHashTableSize=4096;
PasMPJobWorkerThreadHashTableMask=PasMPJobWorkerThreadHashTableSize-1;
PasMPDefaultDepth=16;
PasMPJobThreadIndexBits=16;
PasMPJobThreadIndexSize=TPasMPUInt32(TPasMPUInt32(1) shl PasMPJobThreadIndexBits);
PasMPJobThreadIndexMask=PasMPJobThreadIndexSize-1;
PasMPJobFlagHasOwnerWorkerThread=TPasMPUInt32(TPasMPUInt32(1) shl (PasMPJobThreadIndexBits+1));
PasMPJobFlagReleaseOnFinish=TPasMPUInt32(TPasMPUInt32(1) shl (PasMPJobThreadIndexBits+2));
PasMPCPUCacheLineSize=64;
PasMPOnceInit={$ifdef Linux}PTHREAD_ONCE_INIT{$else}0{$endif};
PasMPVersionMajor=1000000;
PasMPVersionMinor=1000;
PasMPVersionRelease=1;
{$ifndef FPC}
// Delphi evaluates every $IF-directive even if it is disabled by a surrounding, so it's then a error in Delphi, and for to avoid it, we define dummys here.
FPC_VERSION=0;
FPC_RELEASE=0;
FPC_PATCH=0;
FPC_FULLVERSION=(FPC_VERSION*10000)+(FPC_RELEASE*100)+(FPC_PATCH*1);
{$endif}
// FPC_VERSION_PASMP=(FPC_VERSION*PasMPVersionMajor)+(FPC_RELEASE*PasMPVersionMinor)+(FPC_PATCH*PasMPVersionRelease);
{$ifndef Windows}
{$ifndef fpc}
INFINITE=TPasMPUInt32(-1);
{$endif}
{$endif}
PasMPThreadSafeDynamicArrayFirstBucketBits=3;
PasMPThreadSafeDynamicArrayFirstBucketSize=1 shl PasMPThreadSafeDynamicArrayFirstBucketBits;
PasMPThreadSafeDynamicArrayNumberOfBuckets=30;
PasMPThreadSafeDynamicArrayMarkFirstBit=TPasMPUInt32($80000000);
PasMPCLZDebruijn32Multiplicator=TPasMPUInt32($07c4acdd);
PasMPCLZDebruijn32Shift=27;
PasMPCLZDebruijn32Mask=31;
PasMPCLZDebruijn32Table:array[0..31] of TPasMPInt32=(31,22,30,21,18,10,29,2,20,17,15,13,9,6,28,1,23,19,11,3,16,14,7,24,12,4,8,25,5,26,27,0);
PasMPCLZDebruijn64Multiplicator:TPasMPUInt64=TPasMPUInt64($03f79d71b4cb0a89);
PasMPCLZDebruijn64Shift=58;
PasMPCLZDebruijn64Mask=63;
PasMPCLZDebruijn64Table:array[0..63] of TPasMPInt32=(63,16,62,7,15,36,61,3,6,14,22,26,35,47,60,2,9,5,28,11,13,21,42,19,25,31,34,40,46,52,59,1,
17,8,37,4,23,27,48,10,29,12,43,20,32,41,53,18,38,24,49,30,44,33,54,39,50,45,55,51,56,57,58,0);
PasMPCTZDebruijn32Multiplicator=TPasMPUInt32($077cb531);
PasMPCTZDebruijn32Shift=27;
PasMPCTZDebruijn32Mask=31;
PasMPCTZDebruijn32Table:array[0..31] of TPasMPInt32=(0,1,28,2,29,14,24,3,30,22,20,15,25,17,4,8,31,27,13,23,21,19,16,7,26,12,18,6,11,5,10,9);
PasMPCTZDebruijn64Multiplicator:TPasMPUInt64=TPasMPUInt64($07edd5e59a4e28c2);
PasMPCTZDebruijn64Shift=58;
PasMPCTZDebruijn64Mask=63;
PasMPCTZDebruijn64Table:array[0..63] of TPasMPInt32=(63,0,58,1,59,47,53,2,60,39,48,27,54,33,42,3,61,51,37,40,49,18,28,20,55,30,34,11,43,14,22,4,
62,57,46,52,38,26,32,41,50,36,17,19,29,10,13,21,56,45,25,31,35,16,9,12,44,24,15,8,23,7,6,5);
PasMPBSFDebruijn32Multiplicator=TPasMPUInt32($077cb531);
PasMPBSFDebruijn32Shift=27;
PasMPBSFDebruijn32Mask=31;
PasMPBSFDebruijn32Table:array[0..31] of TPasMPInt32=(0,1,28,2,29,14,24,3,30,22,20,15,25,17,4,8,31,27,13,23,21,19,16,7,26,12,18,6,11,5,10,9);
PasMPBSFDebruijn64Multiplicator:TPasMPUInt64=TPasMPUInt64($03f79d71b4cb0a89);
PasMPBSFDebruijn64Shift=58;
PasMPBSFDebruijn64Mask=63;
PasMPBSFDebruijn64Table:array[0..63] of TPasMPInt32=(0,1,48,2,57,49,28,3,61,58,50,42,38,29,17,4,62,55,59,36,53,51,43,22,45,39,33,30,24,18,12,5,
63,47,56,27,60,41,37,16,54,35,52,21,44,32,23,11,46,26,40,15,34,20,31,10,25,14,19,9,13,8,7,6);
PasMPBSRDebruijn32Multiplicator=TPasMPUInt32($07c4acdd);
PasMPBSRDebruijn32Shift=27;
PasMPBSRDebruijn32Mask=31;
PasMPBSRDebruijn32Table:array[0..31] of TPasMPInt32=(0,9,1,10,13,21,2,29,11,14,16,18,22,25,3,30,8,12,20,28,15,17,24,7,19,27,23,6,26,5,4,31);
PasMPBSRDebruijn64Multiplicator:TPasMPUInt64=TPasMPUInt64($03f79d71b4cb0a89);
PasMPBSRDebruijn64Shift=58;
PasMPBSRDebruijn64Mask=63;
PasMPBSRDebruijn64Table:array[0..63] of TPasMPInt32=(0,47,1,56,48,27,2,60,57,49,41,37,28,16,3,61,54,58,35,52,50,42,21,44,38,32,29,23,17,11,4,62,
46,55,26,59,40,36,15,53,34,51,20,43,31,22,10,45,25,39,14,33,19,30,9,24,13,18,8,12,7,6,5,63);
type TPasMPAvailableCPUCores=array of TPasMPInt32;
PPasMPInt128Record=^TPasMPInt128Record;
TPasMPInt128Record=record
{$ifdef BIG_ENDIAN}
Hi,Lo:TPasMPUInt64;
{$else}
Lo,Hi:TPasMPUInt64;
{$endif}
end;
PPasMPInt64Record=^TPasMPInt64Record;
TPasMPInt64Record=record
case boolean of
false:(
{$ifdef BIG_ENDIAN}
Hi,Lo:TPasMPUInt32;
{$else}
Lo,Hi:TPasMPUInt32;
{$endif}
);
true:(
Value:TPasMPInt64;
);
end;
PPasMPTaggedPointer=^TPasMPTaggedPointer;
TPasMPTaggedPointer=record
case TPasMPInt32 of
0:(
PointerValue:pointer;
TagValue:TPasMPPtrUInt;
);
1:(
Value:{$ifdef CPU64}TPasMPInt128Record{$else}TPasMPInt64Record{$endif};
);
end;
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPMath=class
public
class function PopulationCount32(Value:TPasMPUInt32):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function PopulationCount64(Value:TPasMPUInt64):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function PopulationCount(Value:TPasMPPtrUInt):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function BitScanForward32(Value:TPasMPUInt32):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function BitScanForward64(Value:TPasMPUInt64):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function BitScanForward(Value:TPasMPPtrUInt):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function BitScanReverse32(Value:TPasMPUInt32):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function BitScanReverse64(Value:TPasMPUInt64):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function BitScanReverse(Value:TPasMPPtrUInt):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function CountLeadingZeros32(Value:TPasMPUInt32):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function CountLeadingZeros64(Value:TPasMPUInt64):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function CountLeadingZeros(Value:TPasMPPtrUInt):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function CountTrailingZeros32(Value:TPasMPUInt32):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function CountTrailingZeros64(Value:TPasMPUInt64):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function CountTrailingZeros(Value:TPasMPPtrUInt):TPasMPInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function RoundUpToPowerOfTwo32(Value:TPasMPUInt32):TPasMPUInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function RoundUpToPowerOfTwo64(Value:TPasMPUInt64):TPasMPUInt64; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function RoundUpToPowerOfTwo(Value:TPasMPPtrUInt):TPasMPPtrUInt; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function RoundUpToMask32(Value,Mask:TPasMPUInt32):TPasMPUInt32; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function RoundUpToMask64(Value,Mask:TPasMPUInt64):TPasMPUInt64; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class function RoundUpToMask(Value,Mask:TPasMPPtrUInt):TPasMPPtrUInt; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPInterlocked=class
public
class function Increment(var Destination:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifdef CPU64}
class function Increment(var Destination:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$endif}
class function Decrement(var Destination:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifdef CPU64}
class function Decrement(var Destination:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$endif}
class function Add(var Destination:TPasMPInt32;const Value:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifdef CPU64}
class function Add(var Destination:TPasMPInt64;const Value:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$endif}
class function Sub(var Destination:TPasMPInt32;const Value:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifdef CPU64}
class function Sub(var Destination:TPasMPInt64;const Value:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$endif}
class function Exchange(var Destination:TPasMPInt32;const Source:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Exchange(var Destination:TPasMPUInt32;const Source:TPasMPUInt32):TPasMPUInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifdef CPU64}
class function Exchange(var Destination:TPasMPInt64;const Source:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Exchange(var Destination:TPasMPUInt64;const Source:TPasMPUInt64):TPasMPUInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$endif}
class function Exchange(var Destination:pointer;const Source:pointer):pointer; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Exchange(var Destination:TObject;const Source:TObject):TObject; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function CompareExchange(var Destination:TPasMPInt32;const NewValue,Comperand:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function CompareExchange(var Destination:TPasMPUInt32;const NewValue,Comperand:TPasMPUInt32):TPasMPUInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$if defined(CPU64) or defined(CPU386) or defined(CPUARM)}
class function CompareExchange(var Destination:TPasMPInt64;const NewValue,Comperand:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function CompareExchange(var Destination:TPasMPInt64Record;const NewValue,Comperand:TPasMPInt64Record):TPasMPInt64Record; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function CompareExchange(var Destination:TPasMPUInt64;const NewValue,Comperand:TPasMPUInt64):TPasMPUInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifend}
{$if defined(CPU64) and defined(HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE)}
class function CompareExchange(var Destination:TPasMPInt128Record;const NewValue,Comperand:TPasMPInt128Record):TPasMPInt128Record; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(fpc)}inline;{$ifend}
{$ifend}
class function CompareExchange(var Destination:pointer;const NewValue,Comperand:pointer):pointer; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function CompareExchange(var Destination:TObject;const NewValue,Comperand:TObject):TObject; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Read(var Source:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$if defined(CPU64) or defined(CPU386) or defined(CPUARM)}
class function Read(var Source:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Read(var Source:TPasMPInt64Record):TPasMPInt64Record; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$if defined(CPU64) and defined(HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE)}
class function Read(var Source:TPasMPInt128Record):TPasMPInt128Record; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(fpc)}inline;{$ifend}
{$ifend}
class function Read(var Source:pointer):pointer; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Read(var Source:TObject):TObject; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$ifend}
class function Write(var Destination:TPasMPInt32;const Source:TPasMPInt32):TPasMPInt32; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$if defined(CPU64) or defined(CPU386) or defined(CPUARM)}
class function Write(var Destination:TPasMPInt64;const Source:TPasMPInt64):TPasMPInt64; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Write(var Destination:TPasMPInt64Record;const Source:TPasMPInt64Record):TPasMPInt64Record; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
{$if defined(CPU64) and defined(HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE)}
class function Write(var Destination:TPasMPInt128Record;const Source:TPasMPInt128Record):TPasMPInt128Record; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(fpc)}inline;{$ifend}
{$ifend}
{$ifend}
class function Write(var Destination:pointer;const Source:pointer):pointer; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function Write(var Destination:TObject;const Source:TObject):TObject; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPAtomic=class(TPasMPInterlocked);
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPMemoryBarrier=class
public
class procedure Read; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class procedure ReadDependency; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class procedure ReadWrite; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class procedure Write; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
class procedure Sync; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPMemory=class
public
class procedure AllocateAlignedMemory(var p;Size:TPasMPInt32;Align:TPasMPInt32=PasMPCPUCacheLineSize); {$ifdef HAS_STATIC}static;{$endif}
class procedure FreeAlignedMemory(const p); {$ifdef HAS_STATIC}static;{$endif}
class procedure Barrier; {$ifdef HAS_STATIC}static;{$endif}{$ifdef CAN_INLINE}inline;{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
TPasMP=class;
PPasMPOnce=^TPasMPOnce;
TPasMPOnce={$ifdef Linux}pthread_once_t{$else}TPasMPInt32{$endif};
TPasMPOnceInitRoutine={$ifdef fpc}TProcedure{$else}procedure{$endif};
TPasMPEvent=class(TEvent);
TPasMPSimpleEvent=class(TPasMPEvent)
public
constructor Create;
end;
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPCriticalSection=class(TCriticalSection)
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TRTLCriticalSection))-1] of TPasMPUInt8;
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPMutex=class(TSynchroObject)
{$ifdef Windows}
private
fMutex:THandle;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TRTLCriticalSection))-1] of TPasMPUInt8;
{$else}
{$ifdef Unix}
private
fMutex:pthread_mutex_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_mutex_t))-1] of TPasMPUInt8;
{$else}
private
fCriticalSection:TPasMPCriticalSection;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TPasMPCriticalSection))-1] of TPasMPUInt8;
{$endif}
{$endif}
public
constructor Create; overload;
{$ifdef Unix}
constructor Create(const lpMutexAttributes:pointer); overload;
{$endif}
{$ifdef Windows}
constructor Create(const lpMutexAttributes:pointer;const bInitialOwner:boolean;const lpName:string); overload;
constructor Create(const DesiredAccess:TPasMPUInt32;const bInitialOwner:boolean;const lpName:string); overload;
{$endif}
destructor Destroy; override;
procedure Acquire; override;
procedure Release; override;
{$ifdef Windows}
property Mutex:THandle read fMutex;
{$else}
{$ifdef Unix}
property Mutex:pthread_mutex_t read fMutex;
{$else}
property CriticalSection:TPasMPCriticalSection read fCriticalSection;
{$endif}
{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPConditionVariableLock=class(TSynchroObject)
{$ifdef Windows}
private
fCriticalSection:TRTLCriticalSection;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TRTLCriticalSection))-1] of TPasMPUInt8;
{$else}
{$ifdef Unix}
private
fMutex:pthread_mutex_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_mutex_t))-1] of TPasMPUInt8;
{$else}
private
fCriticalSection:TPasMPCriticalSection;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TPasMPCriticalSection))-1] of TPasMPUInt8;
{$endif}
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure Acquire; override;
procedure Release; override;
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$ifdef Windows}
PPasMPConditionVariableData=^TPasMPConditionVariableData;
TPasMPConditionVariableData=pointer;
{$endif}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPConditionVariable=class
{$ifdef Windows}
private
fConditionVariable:TPasMPConditionVariableData;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TPasMPConditionVariableData))-1] of TPasMPUInt8;
{$else}
{$ifdef unix}
private
fConditionVariable:pthread_cond_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_cond_t))-1] of TPasMPUInt8;
{$else}
private
{$ifdef HAS_VOLATILE}[volatile]{$endif}fWaitCounter:TPasMPInt32;
{$ifdef HAS_VOLATILE}[volatile]{$endif}fReleaseCounter:TPasMPInt32;
{$ifdef HAS_VOLATILE}[volatile]{$endif}fGenerationCounter:TPasMPInt32;
fCriticalSection:TPasMPCriticalSection;
fEvent:TPasMPEvent;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*3)+SizeOf(TPasMPCriticalSection)+SizeOf(TPasMPEvent)))-1] of TPasMPUInt8;
{$endif}
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure Signal; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure Broadcast; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
function Wait(const Lock:TPasMPConditionVariableLock;const dwMilliSeconds:TPasMPUInt32=INFINITE):TWaitResult; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPSemaphore=class(TSynchroObject)
private
fInitialCount:TPasMPInt32;
fMaximumCount:TPasMPInt32;
{$ifdef Windows}
fHandle:THandle;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*2)+SizeOf(THandle)))-1] of TPasMPUInt8;
{$else}
{$ifdef unix}
fHandle:TPasMPInt32;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-(SizeOf(TPasMPInt32)*3))-1] of TPasMPUInt8;
{$else}
{$define PasMPSemaphoreUseConditionVariable}
{$ifdef HAS_VOLATILE}[volatile]{$endif}fCurrentCount:TPasMPInt32;
{$ifdef PasMPSemaphoreUseConditionVariable}
fConditionVariableLock:TPasMPConditionVariableLock;
fConditionVariable:TPasMPConditionVariable;
{$else}
fCriticalSection:TPasMPCriticalSection;
fEvent:TPasMPEvent;
{$endif}
protected
{$ifdef PasMPSemaphoreUseConditionVariable}
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*3)+SizeOf(TPasMPConditionVariableLock)+SizeOf(TPasMPConditionVariable)))-1] of TPasMPUInt8;
{$else}
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*3)+SizeOf(TPasMPCriticalSection)+SizeOf(TPasMPEvent)))-1] of TPasMPUInt8;
{$endif}
{$endif}
{$endif}
public
constructor Create(const InitialCount,MaximumCount:TPasMPInt32);
destructor Destroy; override;
procedure Acquire; overload; override;
procedure Release; overload; override;
function Acquire(const AcquireCount:TPasMPInt32):TWaitResult; reintroduce; overload;
function Release(const ReleaseCount:TPasMPInt32):TPasMPInt32; reintroduce; overload;
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPInvertedSemaphore=class(TSynchroObject)
private
fInitialCount:TPasMPInt32;
fMaximumCount:TPasMPInt32;
{$ifdef HAS_VOLATILE}[volatile]{$endif}fCurrentCount:TPasMPInt32;
fConditionVariableLock:TPasMPConditionVariableLock;
fConditionVariable:TPasMPConditionVariable;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*3)+SizeOf(TPasMPConditionVariableLock)+SizeOf(TPasMPConditionVariable)))-1] of TPasMPUInt8;
public
constructor Create(const InitialCount,MaximumCount:TPasMPInt32);
destructor Destroy; override;
procedure Acquire; overload; override; // Acquire a number of resource elements. It never blocks.
procedure Release; overload; override; // Release a number of resource elements. It never blocks, but it may wake up waiting threads.
function Acquire(const AcquireCount:TPasMPInt32;out Count:TPasMPInt32):TPasMPInt32; reintroduce; overload;
function Release(const ReleaseCount:TPasMPInt32;out Count:TPasMPInt32):TPasMPInt32; reintroduce; overload;
function Wait(const dwMilliSeconds:TPasMPUInt32=INFINITE):TWaitResult; // Block until the inverted semaphore reaches zero
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$ifdef Windows}
PPasMPSRWLock=^TPasMPSRWLock;
TPasMPSRWLock=pointer;
{$endif}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPMultipleReaderSingleWriterLock=class(TInterfacedObject,IReadWriteSync)
{$ifdef Windows}
private
fSRWLock:TPasMPSRWLock;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TPasMPSRWLock))-1] of TPasMPUInt8;
{$else}
{$ifdef unix}
private
fReadWriteLock:pthread_rwlock_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_rwlock_t))-1] of TPasMPUInt8;
{$else}
private
{$ifdef HAS_VOLATILE}[volatile]{$endif}fReaders:TPasMPInt32;
{$ifdef HAS_VOLATILE}[volatile]{$endif}fWriters:TPasMPInt32;
fConditionVariableLock:TPasMPConditionVariableLock;
fConditionVariable:TPasMPConditionVariable;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*2)+SizeOf(TPasMPConditionVariableLock)+SizeOf(TPasMPConditionVariable)))-1] of TPasMPUInt8;
{$endif}
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure AcquireRead; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
function TryAcquireRead:boolean; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure ReleaseRead; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure AcquireWrite; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
function TryAcquireWrite:boolean; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure ReleaseWrite; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure ReadToWrite; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure WriteToRead; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure BeginRead;
procedure EndRead;
function BeginWrite:boolean;
procedure EndWrite;
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPMultipleReaderSingleWriterSpinLock=class(TInterfacedObject,IReadWriteSync)
private
{$ifdef HAS_VOLATILE}[volatile]{$endif}fState:TPasMPInt32;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*2)+SizeOf(TPasMPConditionVariableLock)+SizeOf(TPasMPConditionVariable)))-1] of TPasMPUInt8;
public
constructor Create;
destructor Destroy; override;
procedure AcquireRead; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
function TryAcquireRead:boolean; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure ReleaseRead; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure AcquireWrite; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
function TryAcquireWrite:boolean; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure ReleaseWrite; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure ReadToWrite; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure WriteToRead; overload; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
procedure BeginRead;
procedure EndRead;
function BeginWrite:boolean;
procedure EndWrite;
class procedure AcquireRead(var LockState:TPasMPInt32); overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function TryAcquireRead(var LockState:TPasMPInt32):boolean; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class procedure ReleaseRead(var LockState:TPasMPInt32); overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class procedure AcquireWrite(var LockState:TPasMPInt32); overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class function TryAcquireWrite(var LockState:TPasMPInt32):boolean; overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class procedure ReleaseWrite(var LockState:TPasMPInt32); overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class procedure ReadToWrite(var LockState:TPasMPInt32); overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
class procedure WriteToRead(var LockState:TPasMPInt32); overload; {$ifdef HAS_STATIC}static;{$endif}{$if defined(HAS_ATOMICS) or defined(fpc)}inline;{$ifend}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPSlimReaderWriterLock=class(TSynchroObject)
{$ifdef Windows}
private
fSRWLock:TPasMPSRWLock;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TPasMPSRWLock))-1] of TPasMPUInt8;
{$else}
{$ifdef unix}
private
fReadWriteLock:pthread_rwlock_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_rwlock_t))-1] of TPasMPUInt8;
{$else}
private
{$ifdef HAS_VOLATILE}[volatile]{$endif}fCount:TPasMPInt32;
fConditionVariableLock:TPasMPConditionVariableLock;
fConditionVariable:TPasMPConditionVariable;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-(SizeOf(TPasMPInt32)+SizeOf(TPasMPConditionVariableLock)+SizeOf(TPasMPConditionVariable)))-1] of TPasMPUInt8;
{$endif}
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure Acquire; override;
function TryAcquire:boolean;
procedure Release; override;
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPSpinLock=class(TSynchroObject)
{$ifdef unix}
private
fSpinLock:pthread_spinlock_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_spinlock_t))-1] of TPasMPUInt8;
{$else}
private
{$ifdef HAS_VOLATILE}[volatile]{$endif}fState:TPasMPInt32;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(TPasMPInt32))-1] of TPasMPUInt8;
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure Acquire; override;
function TryAcquire:longbool; {$if not defined(Unix)}{$if defined(cpu386) or defined(cpux86_64)}register;{$else}{$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}{$ifend}{$ifend}
procedure Release; override;
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
TPasMPBarrier=class
{$ifdef unix}
private
fBarrier:pthread_barrier_t;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-SizeOf(pthread_barrier_t))-1] of TPasMPUInt8;
{$else}
private
{$ifdef HAS_VOLATILE}[volatile]{$endif}fCount:TPasMPInt32;
{$ifdef HAS_VOLATILE}[volatile]{$endif}fTotal:TPasMPInt32;
fConditionVariableLock:TPasMPConditionVariableLock;
fConditionVariable:TPasMPConditionVariable;
protected
fCacheLineFillUp:array[0..(PasMPCPUCacheLineSize-((SizeOf(TPasMPInt32)*2)+SizeOf(TPasMPConditionVariableLock)+SizeOf(TPasMPConditionVariable)))-1] of TPasMPUInt8;
{$endif}
public
constructor Create(const Count:TPasMPInt32);
destructor Destroy; override;
function Wait:boolean; {$ifdef fpc}{$ifdef CAN_INLINE}inline;{$endif}{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
PPasMPThreadSafeStackEntry=^TPasMPThreadSafeStackEntry;
TPasMPThreadSafeStackEntry=pointer;
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
// The lock-free variant is based on the idea behind the concept of the internal workings of the "Interlocked Singly Linked Lists" Windows API, just stripped by the Depth stuff
// The lock-based variant is based of my head
TPasMPThreadSafeStack=class // only for PasMP internal usage
private
{$ifdef HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE}
fHead:PPasMPTaggedPointer;
{$else}
fCriticalSection:TPasMPCriticalSection;
fHead:pointer;
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure Clear; {$ifdef CAN_INLINE}inline;{$endif}
function IsEmpty:boolean; {$ifdef CAN_INLINE}inline;{$endif}
function Push(const Item:pointer):pointer; {$ifdef CAN_INLINE}inline;{$endif}
function Pop:pointer; {$ifdef CAN_INLINE}inline;{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
PPasMPThreadSafeQueueNode=^TPasMPThreadSafeQueueNode;
TPasMPThreadSafeQueueNode=record
{$ifdef HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE}
Previous:TPasMPTaggedPointer;
Next:TPasMPTaggedPointer;
{$else}
Next:pointer;
{$endif}
Data:record
// Empty
end;
end;
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
// The lock-free variant is based on http://people.csail.mit.edu/edya/publications/OptimisticFIFOQueue-journal.pdf
// The lock-based variant is based on the two-lock concurrent queue
TPasMPThreadSafeQueue=class // only for PasMP internal usage
private
{$ifdef HAS_DOUBLE_NATIVE_MACHINE_WORD_ATOMIC_COMPARE_EXCHANGE}
fHead:PPasMPTaggedPointer;
fTail:PPasMPTaggedPointer;
{$else}
fHeadCriticalSection:TPasMPCriticalSection;
fTailCriticalSection:TPasMPCriticalSection;
fHead:PPasMPThreadSafeQueueNode;
fTail:PPasMPThreadSafeQueueNode;
{$endif}
fItemSize:TPasMPInt32;
fInternalNodeSize:TPasMPInt32;
public
constructor Create(ItemSize:TPasMPInt32);
destructor Destroy; override;
procedure Clear; {$ifdef CAN_INLINE}inline;{$endif}
function IsEmpty:boolean; {$ifdef CAN_INLINE}inline;{$endif}
procedure Enqueue(const Item); {$ifdef CAN_INLINE}inline;{$endif}
function Dequeue(out Item):boolean; {$ifdef CAN_INLINE}inline;{$endif}
end;
{$if defined(fpc) and (fpc_version>=3)}{$pop}{$ifend}
TPasMPThreadSafeHashTableHash=TPasMPUInt32;
PPasMPThreadSafeHashTableItem=^TPasMPThreadSafeHashTableItem;
TPasMPThreadSafeHashTableItem=record
case TPasMPInt32 of
0:(
Lock:TPasMPInt32;
State:TPasMPInt32;
Hash:TPasMPThreadSafeHashTableHash;
Data:record
// Empty
end;
);
1:(
LockState:TPasMPInt64Record;
);
end;
PPasMPThreadSafeHashTableState=^TPasMPThreadSafeHashTableState;
TPasMPThreadSafeHashTableState=record
case TPasMPInt32 of
0:(
Previous:PPasMPThreadSafeHashTableState;
Next:PPasMPThreadSafeHashTableState;
ReferenceCounter:TPasMPInt32;
Version:TPasMPInt32;
Size:TPasMPInt32;
Mask:TPasMPInt32;
LogSize:TPasMPInt32;
Count:TPasMPInt32;
Items:pointer;
);
1:(
FillUp:array[0..(PasMPCPUCacheLineSize*(SizeOf(TPasMPPtrUInt) div SizeOf(TPasMPUInt32)))-1] of TPasMPUInt8;
);
end;
{$if defined(fpc) and (fpc_version>=3)}{$push}{$optimization noorderfields}{$ifend}
// A thread-safe hash table with open addressing and double-hashing-like probing (two values from one single hash value)
// The read operation is almost lock-free until the read-acquisition of the multiple-reader-single-writer-lock of a hash item,
// since a item value can larger than one and two native maschine words
// The write operations are almost multiple-reader-single-writer-lock-based
// Why not complete lock-free? => Because TPasMPThreadSafeHashTable should be universal usable independently by the key and
// value data types and also key-and-value-object-reference-counting-free as much as possble.