My favorites | Sign in
Project Logo
                
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/**
* 该模块定义了一些原子操作。
* License: BSD
* Authors: Lucifer (786325481@QQ.com)
* Copyright: Copyright (C) 2008 Lucifer. All rights reserved.
*/

/**
* The atomic module is intended to provide some basic support for lock-free
* concurrent programming. Some common operations are defined, each of which
* may be performed using the specified memory barrier or a less granular
* barrier if the hardware does not support the version requested. This
* model is based on a design by Alexander Terekhov as outlined in
* <a href=http://groups.google.com/groups?threadm=3E4820EE.6F408B25%40web.de>
* this thread</a>. Another useful reference for memory ordering on modern
* architectures is <a href=http://www.linuxjournal.com/article/8211>this
* article by Paul McKenney</a>.
*
* Copyright: Copyright (C) 2005-2006 Sean Kelly. All rights reserved.
* License: BSD style: $(LICENSE)
* Authors: Sean Kelly
*/
module system.threading.Atomic;

import system.Traits;

/*
version(Windows)
{
private extern(Windows) int InterlockedIncrement(ref int);
private extern(Windows) int InterlockedDecrement(ref int);
private extern(Windows) int InterlockedExchange(ref int, int);
private extern(Windows) int InterlockedExchangeAdd(ref int, int);
private extern(C) void* InterlockedExchangePointer(ref void*, void*);
private extern(Windows) int InterlockedCompareExchange(ref int, int, int);
private extern(C) void* InterlockedCompareExchangePointer(ref void*, void*, void*);
}
*/

/**
* Memory synchronization flag. If the supplied option is not available on the
* current platform then a stronger method will be used instead.
*/
public enum MemorySemantics
{
Raw, /// not sequenced
HoistLoadBarrier, /// hoist-load barrier
HoistStoreBarrier, /// hoist-store barrier
SinkLoadBarrier, /// sink-load barrier
SinkStoreBarrier, /// sink-store barrier
Acquire, /// hoist-load + hoist-store barrier
Release, /// sink-load + sink-store barrier
FullFence /// fully sequenced (acquire + release)
}

//Internal Type Checking

private template IsValidAtomicType(T)
{
const bool IsValidAtomicType = T.sizeof == byte.sizeof ||
T.sizeof == short.sizeof ||
T.sizeof == int.sizeof ||
T.sizeof == long.sizeof;
}

private template IsValidNumericType(T)
{
const bool IsValidNumericType = IsIntegerType!(T) ||
IsPointerType!(T) ;
}

private template IsHoistOperation(MemorySemantics ms)
{
const bool IsHoistOperation = ms == MemorySemantics.HoistLoadBarrier ||
ms == MemorySemantics.HoistStoreBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.FullFence;
}

private template IsSinkOperation(MemorySemantics ms)
{
const bool IsSinkOperation = ms == MemorySemantics.SinkLoadBarrier ||
ms == MemorySemantics.SinkStoreBarrier ||
ms == MemorySemantics.Release ||
ms == MemorySemantics.FullFence;
}

// x86 Value Requirements

// NOTE: Strictly speaking, the x86 supports atomic operations on
// unaligned values. However, this is far slower than the
// common case, so such behavior should be prohibited.
private template AtomicValueIsProperlyAligned(T)
{
bool AtomicValueIsProperlyAligned(size_t address)
{
return address % T.sizeof == 0;
}
}

//x86 Synchronization Requirements

// NOTE: While x86 loads have acquire semantics for stores, it appears
// that independent loads may be reordered by some processors
// (notably the AMD64). This implies that the hoist-load barrier
// op requires an ordering instruction, which also extends this
// requirement to acquire ops (though hoist-store should not need
// one if support is added for this later). However, since no
// modern architectures will reorder dependent loads to occur
// before the load they depend on (except the Alpha), raw loads
// are actually a possible means of ordering specific sequences
// of loads in some instances. The original atomic<>
// implementation provides a 'ddhlb' ordering specifier for
// data-dependent loads to handle this situation, but as there
// are no plans to support the Alpha there is no reason to add
// that option here.
//
// For reference, the old behavior (acquire semantics for loads)
// required a memory barrier if: ms == MemorySemantics.FullFence || IsSinkOperation!(ms)
private template NeedLoadBarrier(MemorySemantics ms)
{
const bool NeedLoadBarrier = ms != MemorySemantics.Raw;
}

// NOTE: x86 stores implicitly have release semantics so a membar is only
// necessary on acquires.
private template NeedStoreBarrier(MemorySemantics ms)
{
const bool NeedStoreBarrier = ms == MemorySemantics.FullFence || IsHoistOperation!(ms) ;
}

version(D_InlineAsm_X86)
{
version(X86)
{
pragma( msg, "system.threading.Atomic: using IA-32 inline asm" );

version = Has32BitOps;
version = Has64BitCAS;
}
version(X86_64)
{
pragma( msg, "system.threading.Atomic: using AMD64 inline asm" );

version = Has64BitOps;
}

/**
* Supported MemorySemantics values:
* MemorySemantics.Raw
* MemorySemantics.HoistLoadBarrier
* MemorySemantics.Acquire
* MemorySemantics.FullFence
*/
template atomicLoad( T, MemorySemantics ms = MemorySemantics.FullFence )
{
static assert( ms == MemorySemantics.Raw ||
ms == MemorySemantics.HoistLoadBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.FullFence,
"ms must be one of: MemorySemantics.Raw, MemorySemantics.HoistLoadBarrier, MemorySemantics.Acquire, MemorySemantics.FullFence.");

/**
* Refreshes the contents of 'value' from main memory. This operation is
* both lock-free and atomic.
*
* Params:
* value = The value to load. This value must be properly aligned.
*
* Returns:
* The loaded value.
*/

T atomicLoad(ref T value)
in
{
assert( AtomicValueIsProperlyAligned!(T)(cast(size_t)(&value)) );
}
body
{
static if( T.sizeof == byte.sizeof )
{
//1 Byte Load
static if( NeedLoadBarrier!(ms) )
{
asm
{
mov DL, 42;
mov AL, 42;
mov ECX, value;
lock;
cmpxchg [ECX], DL;
}
}
else
{
synchronized
{
return value;
}
}
}//static if( T.sizeof == byte.sizeof )
else static if( T.sizeof == short.sizeof )
{
//2 Byte Load
static if( NeedLoadBarrier!(ms) )
{
asm
{
mov DX, 42;
mov AX, 42;
mov ECX, value;
lock;
cmpxchg [ECX], DX;
}
}
else
{
synchronized
{
return value;
}
}
}//else static if( T.sizeof == short.sizeof )
else static if( T.sizeof == int.sizeof )
{
//4 Byte Load
static if( NeedLoadBarrier!(ms) )
{
asm
{
mov EDX, 42;
mov EAX, 42;
mov ECX, value;
lock;
cmpxchg [ECX], EDX;
}
}
else
{
synchronized
{
return value;
}
}
}//else static if( T.sizeof == int.sizeof )
else static if( T.sizeof == long.sizeof )
{
//8 Byte Load
version(Has64BitOps)
{
//8 Byte Load on 64-Bit Processor
static if( NeedLoadBarrier!(ms) )
{
asm
{
mov RAX, value;
lock;
mov RAX, [RAX];
}
}
else
{
synchronized
{
return value;
}
}
}//version(Has64BitOps)
else
{
//8 Byte Load on 32-Bit Processor
static assert(false, "This operation is only available on 64-bit platforms.");
}
}//else static if( T.sizeof == long.sizeof )
else
{
//Not a 1, 2, 4, or 8 Byte Type
static assert(false, "Invalid template type specified.");
}
}
}

/**
* Supported MemorySemantics values:
* MemorySemantics.Raw
* MemorySemantics.SinkStoreBarrier
* MemorySemantics.Acquire
* MemorySemantics.Release
* MemorySemantics.FullFence
*/
template atomicStore(T, MemorySemantics ms = MemorySemantics.FullFence )
{
static assert( ms == MemorySemantics.Raw ||
ms == MemorySemantics.SinkStoreBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.Release ||
ms == MemorySemantics.FullFence,
"ms must be one of: MemorySemantics.Raw, MemorySemantics.SinkStoreBarrier, MemorySemantics.Acquire, MemorySemantics.Acquire, MemorySemantics.Release, MemorySemantics.FullFence." );
/**
* Stores 'newval' to the memory referenced by 'value'. This operation
* is both lock-free and atomic.
*
* Params:
* value = The destination variable.
* newValue = The value to store.
*/
void atomicStore(ref T value, T newValue)
in
{
assert( AtomicValueIsProperlyAligned!(T)(cast(size_t)(&value)) );
}
body
{
static if( T.sizeof == byte.sizeof )
{
//1 Byte Store
static if( NeedStoreBarrier!(ms) )
{
asm
{
mov EAX, value;
mov DL, newValue;
lock;
xchg [EAX], DL;
}
}
else
{
asm
{
mov EAX, val;
mov DL, newval;
mov [EAX], DL;
}
}
}
else static if( T.sizeof == short.sizeof )
{
//2 Byte Store
static if( NeedStoreBarrier!(ms) )
{
asm
{
mov EAX, value;
mov DX, newValue;
lock;
xchg [EAX], DX;
}
}
else
{
mov EAX, value;
mov DX, newValue;
xchg [EAX], DX;
}
}
else static if( T.sizeof == int.sizeof )
{
//4 Byte Store
static if( NeedStoreBarrier!(ms) )
{
asm
{
mov EAX, value;
mov EDX, newValue;
lock;
xchg [EAX], EDX;
}
}
else
{
asm
{
mov EAX, value;
mov EDX, newValue;
xchg [EAX], EDX;
}
}
}
else static if( T.sizeof == long.sizeof )
{
//8 Byte Store
version( Has64BitOps )
{
//8 Byte Store on 64-Bit Processor
static if( NeedStoreBarrier!(ms) )
{
asm
{
mov RAX, value;
mov RBX, newValue;
lock;
xchg [RAX], RBX;
}
}
else
{
asm
{
mov RAX, value;
mov RDX, newValue;
xchg [RAX], RDX;
}
}
}
else
{
//8 Byte Store on 32-Bit Processor
static assert(false, "This operation is only available on 64-bit platforms.");
}
}
else
{
//Not a 1, 2, 4, or 8 Byte Type
static assert(false, "Invalid template type specified.");
}
}
}

/**
* Supported MemorySemantics values:
* MemorySemantics.Raw
* MemorySemantics.SinkStoreBarrier
* MemorySemantics.Acquire
* MemorySemantics.Release
* MemorySemantics.FullFence
*/
template atomicCAS(T, MemorySemantics ms = MemorySemantics.FullFence)
{
static assert( ms == MemorySemantics.Raw ||
ms == MemorySemantics.SinkStoreBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.Release ||
ms == MemorySemantics.FullFence,
"ms must be one of: MemorySemantics.Raw, MemorySemantics.SinkStoreBarrier, MemorySemantics.Acquire, MemorySemantics.Acquire, MemorySemantics.Release, MemorySemantics.FullFence." );
/**
* Stores 'newValue' to the memory referenced by 'value' if val is equal to
* 'equalTo'. This operation is both lock-free and atomic.
*
* Params:
* value = The destination variable.
* newValue = The value to store.
* equalTo = The comparison value.
*
* Returns:
* true if the store occurred, false if not.
*/
bool atomicCAS(ref T value, T newValue, T equalTo)
in
{
// NOTE: 32 bit x86 systems support 8 byte CAS, which only requires
// 4 byte alignment, so use size_t as the align type here.
static if( T.sizeof > size_t.sizeof )
assert( AtomicValueIsProperlyAligned!(size_t)( cast(size_t) (&value) ) );
else
assert( AtomicValueIsProperlyAligned!(T)( cast(size_t) (&value) ) );
}
body
{
static if( T.sizeof == byte.sizeof )
{
//1 Byte CAS
asm
{
mov DL, newValue;
mov AL, equalTo;
mov ECX, value;
lock;
cmpxchg [ECX], DL;
setz AL;
}
}
else static if( T.sizeof == short.sizeof )
{
//2 Byte CAS
asm
{
mov DX, newValue;
mov AX, equalTo;
mov ECX, value;
lock;
cmpxchg [ECX], DX;
setz AL;
}
}
else static if( T.sizeof == int.sizeof )
{
//4 Byte CAS
asm
{
mov EDX, newValue;
mov EAX, equalTo;
mov ECX, value;
lock;
cmpxchg [ECX], EDX;
setz AL;
}
}
else static if( T.sizeof == long.sizeof )
{
//8 Byte CAS
version(Has64BitOps)
{
//8 Byte CAS on 64-bit Processor
asm
{
mov RDX, newValue;
mov RAX, equalTo;
mov RCX, value;
lock;
cmpxchg [RCX], RDX;
setz AL;
}
}
else version(Has64BitCAS)
{
//8 Byte CAS on 32-Bit Processor
asm
{
push EDI;
push EBX;
lea EDI, newValue;
mov EBX, [EDI];
mov ECX, 4[EDI];
lea EDI, equalTo;
mov EAX, [EDI];
mov EDX, 4[EDI];
mov EDI, value;
lock;
cmpxch8b [EDI];
setz AL;
pop EBX;
pop EDI;
}
}
}
else
{
//Not a 1, 2, 4, or 8 Byte Type
static assert(false, "Invalid template type specified.");
}
}
}

/**
* Supported MemorySemantics values:
* MemorySemantics.Raw
* MemorySemantics.SinkStoreBarrier
* MemorySemantics.Acquire
* MemorySemantics.Release
* MemorySemantics.FullFence
*/
template atomicIncrement(T, MemorySemantics ms = MemorySemantics.FullFence)
{
// NOTE: This operation is only valid for integer or pointer types
static assert( IsValidNumericType!(T) );

static assert( ms == MemorySemantics.Raw ||
ms == MemorySemantics.SinkStoreBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.Release ||
ms == MemorySemantics.FullFence,
"ms must be one of: MemorySemantics.Raw, MemorySemantics.SinkStoreBarrier, MemorySemantics.Acquire, MemorySemantics.Acquire, MemorySemantics.Release, MemorySemantics.FullFence." );

/**
* This operation is only legal for built-in value and pointer types,
* and is equivalent to an atomic "value = value + 1" operation. This
* function exists to facilitate use of the optimized increment
* instructions provided by some architecures. If no such instruction
* exists on the target platform then the behavior will perform the
* operation using more traditional means. This operation is both
* lock-free and atomic.
*
* Params:
* value = The value to increment.
*
* Returns:
* The result of an atomicLoad of val immediately following the
* increment operation. This value is not required to be equal to the
* newly stored value. Thus, competing writes are allowed to occur
* between the increment and successive load operation.
*/
T atomicIncrement(ref T value)
in
{
assert( AtomicTypeIsProperlyAligned!(T)(cast(size_t)(&value)) );
}
body
{
static if( T.sizeof == byte.sizeof )
{
//1 Byte Increment
asm
{
mov EAX, value;
lock;
inc [EAX];
mov AL, [EAX];
}
}
else static if( T.sizeof == short.sizeof )
{
//2 Byte Increment
asm
{
mov EAX, value;
lock;
inc short ptr [EAX];
mov AX, [EAX];
}
}
else static if( T.sizeof == int.sizeof )
{
//4 Byte Increment
asm
{
mov EAX, value;
lock;
inc int ptr [EAX];
mov EAX, [EAX];
}
}
else static if( T.sizeof == long.sizeof )
{
//8 Byte Increment
version(Has64BitOps)
{
//8 Byte Increment on 64-Bit Processor
asm
{
mov RAX, value;
lock;
inc qword ptr [RAX];
mov RAX, [RAX];
}
}
else
{
//8 Byte Increment on 32-Bit Processor
static assert(false, "This operation is only available on 64-bit platforms.");
}
}
else
{
//Not a 1, 2, 4, or 8 Byte Type
static assert(false, "Invalid template type specified.");
}
}
}

/**
* Supported MemorySemantics values:
* MemorySemantics.Raw
* MemorySemantics.SinkStoreBarrier
* MemorySemantics.Acquire
* MemorySemantics.Release
* MemorySemantics.FullFence
*/
template atomicDecrement(T, MemorySemantics ms = MemorySemantics.FullFence)
{
//NOTE: This operation is only valid for integer or pointer types
static assert( IsValidNumericType!(T) );

static assert( ms == MemorySemantics.Raw ||
ms == MemorySemantics.SinkStoreBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.Release ||
ms == MemorySemantics.FullFence,
"ms must be one of: MemorySemantics.Raw, MemorySemantics.SinkStoreBarrier, MemorySemantics.Acquire, MemorySemantics.Acquire, MemorySemantics.Release, MemorySemantics.FullFence." );

/**
* This operation is only legal for built-in value and pointer types,
* and is equivalent to an atomic "value = value - 1" operation. This
* function exists to facilitate use of the optimized decrement
* instructions provided by some architecures. If no such instruction
* exists on the target platform then the behavior will perform the
* operation using more traditional means. This operation is both
* lock-free and atomic.
*
* Params:
* value = The value to decrement.
*
* Returns:
* The result of an atomicLoad of val immediately following the
* increment operation. This value is not required to be equal to the
* newly stored value. Thus, competing writes are allowed to occur
* between the increment and successive load operation.
*/
T atomicDecrement(ref T value)
in
{
assert( AtomicValueIsProperlyAligned!(T)(cast(size_t)(&value)) );
}
body
{
static if( T.sizeof == byte.sizeof )
{
//1 Byte Decrement
asm
{
mov EAX, value;
lock;
dec [EAX];
mov AL, [EAX];
}
}
else static if( T.sizeof == short.sizeof )
{
//2 Byte Decrement
asm
{
mov EAX, value;
lock;
dec short ptr [EAX];
mov AX, [EAX];
}
}
else static if( T.sizeof == int.sizeof )
{
//4 Byte Decrement
asm
{
mov EAX, value;
lock;
dec int ptr [EAX];
mov EAX, [EAX];
}
}
else static if( T.sizeof == long.sizeof )
{
//8 Byte Decrement
version(Has64BitOps)
{
// 8 Byte Decrement on 64-Bit Processor
asm
{
mov RAX, value;
lock;
dec qword ptr [RAX];
mov RAX, [RAX];
}
}
else
{
//8 Byte Decrement on 32-Bit Processor
static assert(false, "This operation is only available on 64-bit platforms.");
}
}
else
{
static assert(false, "Invalid template type specified.");
}
}
}

/**
* Supported MemorySemantics values:
* MemorySemantics.Raw
* MemorySemantics.SinkStoreBarrier
* MemorySemantics.Acquire
* MemorySemantics.Release
* MemorySemantics.FullFence
*/
template atomicAdd(T, MemorySemantics ms = MemorySemantics.FullFence)
{
static assert( IsValidNumericType!(T) );

static assert( ms == MemorySemantics.Raw ||
ms == MemorySemantics.SinkStoreBarrier ||
ms == MemorySemantics.Acquire ||
ms == MemorySemantics.Release ||
ms == MemorySemantics.FullFence,
"ms must be one of: MemorySemantics.Raw, MemorySemantics.SinkStoreBarrier, MemorySemantics.Acquire, MemorySemantics.Acquire, MemorySemantics.Release, MemorySemantics.FullFence." );

/**
* Adds two integers and replaces the first integer with the sum, as an atomic operation.
*/
void atomicAdd(ref T value, T newValue)
in
{
assert( AtomicValueIsProperlyAligned!(T)(cast(size_t)(&value)) );
}
body
{
static if( T.sizeof == byte.sizeof )
{
//1 Byte Add
asm
{
mov EAX, value;
mov DL, newValue;
lock;
xadd [EAX], DL;
}
}
else static if( T.sizeof == short.sizeof )
{
//2 Byte Add
asm
{
mov EAX, value;
mov DX, newValue;
lock;
xadd [EAX], DX;
}
}
else static if( T.sizeof == int.sizeof )
{
//4 Byte Add
asm
{
mov EAX, value;
mov EDX, newValue;
lock;
xadd [EAX], EDX;
}
}
else static if( T.sizeof == long.sizeof )
{
//8 Byte Add
version(Has64BitOps)
{
asm
{
mov RAX, value;
mov RDX, newValue;
lock;
xadd [RAX], RDX;
}
}
else
{
//8 Byte Store on 32-Bit Processor
static assert(false, "This operation is only available on 64-bit platforms.");
}
}
else
{
//Not a 1, 2, 4, or 8 Byte Type
static assert(false, "Invalid template type specified.");
}
}
}
}
else version(LDC)
{
/*******************************
* LDC Atomics Implementation
*******************************/
import ldc.intrinsics;

/*******************************
* Atomic Load
*******************************/

public enum MemorySemantics
{
Raw, /// not sequenced
HoistLoadBarrier, /// hoist-load barrier
HoistStoreBarrier, /// hoist-store barrier
SinkLoadBarrier, /// sink-load barrier
SinkStoreBarrier, /// sink-store barrier
Acquire, /// hoist-load + hoist-store barrier
Release, /// sink-load + sink-store barrier
FullFence /// fully sequenced (acquire + release)
}

template atomicLoad(T, MemorySemantics ms = MemorySemantics.FullFence)
{
T atomicLoad(ref T val)
{
llvm_memory_barrier(
ms == MemorySemantics.HoistLoadBarrier || ms == MemorySemantics.Acquire || ms == MemorySemantics.FullFence,
ms == MemorySemantics.HoistStoreBarrier || ms == MemorySemantics.Acquir || ms == MemorySemantics.FullFence,
ms == MemorySemantics.SinkLoadBarrier || ms == MemorySemantics.Release || ms == MemorySemantics.FullFence,
ms == MemorySemantics.SinkStoreBarrier || ms == MemorySemantics.Release || ms == MemorySemantics.FullFence,
false);
static if (IsPointerType!(T))
{
return cast(T)llvm_atomic_load_add!(size_t)(cast(size_t*)&val, 0);
}
else static if (is(T == bool))
{
return llvm_atomic_load_add!(ubyte)(cast(ubyte*)&val, cast(ubyte)0) ? 1 : 0;
}
else
{
return llvm_atomic_load_add!(T)(&val, cast(T)0);
}
}
}


/***************
* Atomic Store
***************/


template atomicStore(T, MemorySemantics ms = MemorySemantics.FullFence)
{
void atomicStore( ref T val, T newval )
{
llvm_memory_barrier(
ms == MemorySemantics.HoistLoadBarrier || ms == MemorySemantics.Acquire || ms == MemorySemantics.FullFence,
ms == MemorySemantics.HoistStoreBarrier || ms == MemorySemantics.Acquir || ms == MemorySemantics.FullFence,
ms == MemorySemantics.SinkLoadBarrier || ms == MemorySemantics.Release || ms == MemorySemantics.FullFence,
ms == MemorySemantics.SinkStoreBarrier || ms == MemorySemantics.Release || ms == MemorySemantics.FullFence,
false);
static if (IsPointerType!(T))
{
llvm_atomic_swap!(size_t)(cast(size_t*)&val, cast(size_t)newval);
}
else static if (is(T == bool))
{
llvm_atomic_swap!(ubyte)(cast(ubyte*)&val, newval?1:0);
}
else
{
llvm_atomic_swap!(T)(&val, newval);
}
}
}


/********************
* Atomic CAS
********************/


template atomicCAS(T, MemorySemantics ms = MemorySemantics.FullFence)
{
bool atomicStoreIf( ref T val, T newval, T equalTo )
{
llvm_memory_barrier(
ms == MemorySemantics.HoistLoadBarrier || ms == MemorySemantics.Acquire || ms == MemorySemantics.FullFence,
ms == MemorySemantics.HoistStoreBarrier || ms == MemorySemantics.Acquir || ms == MemorySemantics.FullFence,
ms == MemorySemantics.SinkLoadBarrier || ms == MemorySemantics.Release || ms == MemorySemantics.FullFence,
ms == MemorySemantics.SinkStoreBarrier || ms == MemorySemantics.Release || ms == MemorySemantics.FullFence,
false);
T oldval = void;
static if (IsPointerType!(T))
{
oldval = cast(T)llvm_atomic_cmp_swap!(size_t)(cast(size_t*)&val, cast(size_t)equalTo, cast(size_t)newval);
}
else static if (is(T == bool))
{
oldval = llvm_atomic_cmp_swap!(ubyte)(cast(ubyte*)&val, equalTo?1:0, newval?1:0)?0:1;
}
else
{
oldval = llvm_atomic_cmp_swap!(T)(&val, equalTo, newval);
}
return oldval == equalTo;
}
}


/*****************************
* Atomic Increment
*****************************/


template atomicIncrement(T, MemorySemantics ms = MemorySemantics.FullFence)
{
//
// NOTE: This operation is only valid for integer or pointer types
//
static assert( IsValidNumericType!(T) );


T atomicIncrement( ref T val )
{
static if (IsPointerType!(T))
{
llvm_atomic_load_add!(size_t)(cast(size_t*)&val, 1);
}
else
{
llvm_atomic_load_add!(T)(&val, cast(T)1);
}
return val;
}
}


/***************************
* Atomic Decrement
***************************/


template atomicDecrement( msync ms = msync.seq, T )
{
//
// NOTE: This operation is only valid for integer or pointer types
//
static assert( IsValidNumericType!(T) );


T atomicDecrement( ref T val )
{
static if (IsPointerType!(T))
{
llvm_atomic_load_sub!(size_t)(cast(size_t*)&val, 1);
}
else
{
llvm_atomic_load_sub!(T)(&val, cast(T)1);
}
return val;
}
}
}
Show details Hide details

Change log

r122 by 786325...@qq.com on Jan 23, 2009   Diff
1. 完善迁移到 druntime 上的问题。
2. 增加国际化支持。
3. 修正旧有的问题。
Go to: 
Project members, sign in to write a code review

Older revisions

r100 by lucifer1...@yeah.net on Nov 19, 2008   Diff
[No log message]
r71 by lucifer1...@yeah.net on Sep 14, 2008   Diff
[No log message]
r70 by lucifer1...@yeah.net on Sep 13, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 27817 bytes, 1013 lines
Hosted by Google Code