-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathatomic.h
1362 lines (1149 loc) · 31.9 KB
/
atomic.h
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
/*
* atomic.h: Atomic operations
*
* Author:
* Dick Porter ([email protected])
*
* (C) 2002 Ximian, Inc.
*/
#include "fake-glib.h"
#ifndef _WAPI_ATOMIC_H_
#define _WAPI_ATOMIC_H_
#ifdef USE_GCC_ATOMIC_OPS
static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
gint32 exch, gint32 comp)
{
return __sync_val_compare_and_swap (dest, comp, exch);
}
static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
{
return __sync_val_compare_and_swap (dest, comp, exch);
}
static inline gint32 InterlockedIncrement(volatile gint32 *val)
{
return __sync_add_and_fetch (val, 1);
}
static inline gint32 InterlockedDecrement(volatile gint32 *val)
{
return __sync_add_and_fetch (val, -1);
}
static inline gint32 InterlockedExchange(volatile gint32 *val, gint32 new_val)
{
gint32 old_val;
do {
old_val = *val;
} while (__sync_val_compare_and_swap (val, old_val, new_val) != old_val);
return old_val;
}
static inline gpointer InterlockedExchangePointer(volatile gpointer *val,
gpointer new_val)
{
gpointer old_val;
do {
old_val = *val;
} while (__sync_val_compare_and_swap (val, old_val, new_val) != old_val);
return old_val;
}
static inline gint32 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
{
return __sync_fetch_and_add (val, add);
}
#else
#if defined(__NetBSD__)
#include <sys/param.h>
#if __NetBSD_Version__ > 499004000
#include <sys/atomic.h>
#define HAVE_ATOMIC_OPS
#endif
#endif
#include "fake-glib.h"
#if defined(__NetBSD__) && defined(HAVE_ATOMIC_OPS)
#define WAPI_ATOMIC_ASM
static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
gint32 exch, gint32 comp)
{
return atomic_cas_32((uint32_t*)dest, comp, exch);
}
static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
{
return atomic_cas_ptr(dest, comp, exch);
}
static inline gint32 InterlockedIncrement(volatile gint32 *val)
{
return atomic_inc_32_nv((uint32_t*)val);
}
static inline gint32 InterlockedDecrement(volatile gint32 *val)
{
return atomic_dec_32_nv((uint32_t*)val);
}
static inline gint32 InterlockedExchange(volatile gint32 *val, gint32 new_val)
{
return atomic_swap_32((uint32_t*)val, new_val);
}
static inline gpointer InterlockedExchangePointer(volatile gpointer *val,
gpointer new_val)
{
return atomic_swap_ptr(val, new_val);
}
static inline gint32 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
{
return atomic_add_32_nv((uint32_t*)val, add) - add;
}
#elif defined(__i386__) || defined(__x86_64__)
#define WAPI_ATOMIC_ASM
/*
* NB: The *Pointer() functions here assume that
* sizeof(pointer)==sizeof(gint32)
*
* NB2: These asm functions assume 486+ (some of the opcodes dont
* exist on 386). If this becomes an issue, we can get configure to
* fall back to the non-atomic C versions of these calls.
*/
static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
gint32 exch, gint32 comp)
{
gint32 old;
__asm__ __volatile__ ("lock; cmpxchgl %2, %0"
: "=m" (*dest), "=a" (old)
: "r" (exch), "m" (*dest), "a" (comp));
return(old);
}
static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
{
gpointer old;
__asm__ __volatile__ ("lock; "
#if defined(__x86_64__) && !defined(__native_client__)
"cmpxchgq"
#else
"cmpxchgl"
#endif
" %2, %0"
: "=m" (*dest), "=a" (old)
: "r" (exch), "m" (*dest), "a" (comp));
return(old);
}
#ifdef __x86_64__
static inline gint64 atomic64_cmpxchg(volatile gint64 *v, gint64 old, gint64 new)
{
return (gint64) InterlockedCompareExchangePointer ((volatile gpointer*)v, (gpointer)new, (gpointer)old);
}
static inline gint64 atomic64_read (const volatile gint64 *v)
{
return *v;
}
#endif
static inline gint32 InterlockedIncrement(volatile gint32 *val)
{
gint32 tmp;
__asm__ __volatile__ ("lock; xaddl %0, %1"
: "=r" (tmp), "=m" (*val)
: "0" (1), "m" (*val));
return(tmp+1);
}
static inline gint32 InterlockedDecrement(volatile gint32 *val)
{
gint32 tmp;
__asm__ __volatile__ ("lock; xaddl %0, %1"
: "=r" (tmp), "=m" (*val)
: "0" (-1), "m" (*val));
return(tmp-1);
}
/*
* See
* http://msdn.microsoft.com/library/en-us/dnmag00/html/win320700.asp?frame=true
* for the reasons for using cmpxchg and a loop here.
*
* That url is no longer valid, but it's still in the google cache at the
* moment: http://www.google.com/search?q=cache:http://msdn.microsoft.com/library/en-us/dnmag00/html/win320700.asp?frame=true
*
* For the time being, http://msdn.microsoft.com/msdnmag/issues/0700/Win32/
* might work. Bet it will change soon enough though.
*/
static inline gint32 InterlockedExchange(volatile gint32 *val, gint32 new_val)
{
gint32 ret;
__asm__ __volatile__ ("1:; lock; cmpxchgl %2, %0; jne 1b"
: "=m" (*val), "=a" (ret)
: "r" (new_val), "m" (*val), "a" (*val));
return(ret);
}
static inline gpointer InterlockedExchangePointer(volatile gpointer *val,
gpointer new_val)
{
gpointer ret;
__asm__ __volatile__ ("1:; lock; "
#if defined(__x86_64__) && !defined(__native_client__)
"cmpxchgq"
#else
"cmpxchgl"
#endif
" %2, %0; jne 1b"
: "=m" (*val), "=a" (ret)
: "r" (new_val), "m" (*val), "a" (*val));
return(ret);
}
static inline gint32 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
{
gint32 ret;
__asm__ __volatile__ ("lock; xaddl %0, %1"
: "=r" (ret), "=m" (*val)
: "0" (add), "m" (*val));
return(ret);
}
#elif (defined(sparc) || defined (__sparc__)) && defined(__GNUC__)
#define WAPI_ATOMIC_ASM
G_GNUC_UNUSED
static inline gint32 InterlockedCompareExchange(volatile gint32 *_dest, gint32 _exch, gint32 _comp)
{
register volatile gint32 *dest asm("g1") = _dest;
register gint32 comp asm("o4") = _comp;
register gint32 exch asm("o5") = _exch;
__asm__ __volatile__(
/* cas [%%g1], %%o4, %%o5 */
".word 0xdbe0500c"
: "=r" (exch)
: "0" (exch), "r" (dest), "r" (comp)
: "memory");
return exch;
}
G_GNUC_UNUSED
static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *_dest, gpointer _exch, gpointer _comp)
{
register volatile gpointer *dest asm("g1") = _dest;
register gpointer comp asm("o4") = _comp;
register gpointer exch asm("o5") = _exch;
__asm__ __volatile__(
#ifdef SPARCV9
/* casx [%%g1], %%o4, %%o5 */
".word 0xdbf0500c"
#else
/* cas [%%g1], %%o4, %%o5 */
".word 0xdbe0500c"
#endif
: "=r" (exch)
: "0" (exch), "r" (dest), "r" (comp)
: "memory");
return exch;
}
G_GNUC_UNUSED
static inline gint32 InterlockedIncrement(volatile gint32 *_dest)
{
register volatile gint32 *dest asm("g1") = _dest;
register gint32 tmp asm("o4");
register gint32 ret asm("o5");
__asm__ __volatile__(
"1: ld [%%g1], %%o4\n\t"
" add %%o4, 1, %%o5\n\t"
/* cas [%%g1], %%o4, %%o5 */
" .word 0xdbe0500c\n\t"
" cmp %%o4, %%o5\n\t"
" bne 1b\n\t"
" add %%o5, 1, %%o5"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest)
: "memory", "cc");
return ret;
}
G_GNUC_UNUSED
static inline gint32 InterlockedDecrement(volatile gint32 *_dest)
{
register volatile gint32 *dest asm("g1") = _dest;
register gint32 tmp asm("o4");
register gint32 ret asm("o5");
__asm__ __volatile__(
"1: ld [%%g1], %%o4\n\t"
" sub %%o4, 1, %%o5\n\t"
/* cas [%%g1], %%o4, %%o5 */
" .word 0xdbe0500c\n\t"
" cmp %%o4, %%o5\n\t"
" bne 1b\n\t"
" sub %%o5, 1, %%o5"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest)
: "memory", "cc");
return ret;
}
G_GNUC_UNUSED
static inline gint32 InterlockedExchange(volatile gint32 *_dest, gint32 exch)
{
register volatile gint32 *dest asm("g1") = _dest;
register gint32 tmp asm("o4");
register gint32 ret asm("o5");
__asm__ __volatile__(
"1: ld [%%g1], %%o4\n\t"
" mov %3, %%o5\n\t"
/* cas [%%g1], %%o4, %%o5 */
" .word 0xdbe0500c\n\t"
" cmp %%o4, %%o5\n\t"
" bne 1b\n\t"
" nop"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest), "r" (exch)
: "memory", "cc");
return ret;
}
G_GNUC_UNUSED
static inline gpointer InterlockedExchangePointer(volatile gpointer *_dest, gpointer exch)
{
register volatile gpointer *dest asm("g1") = _dest;
register gpointer tmp asm("o4");
register gpointer ret asm("o5");
__asm__ __volatile__(
#ifdef SPARCV9
"1: ldx [%%g1], %%o4\n\t"
#else
"1: ld [%%g1], %%o4\n\t"
#endif
" mov %3, %%o5\n\t"
#ifdef SPARCV9
/* casx [%%g1], %%o4, %%o5 */
" .word 0xdbf0500c\n\t"
#else
/* cas [%%g1], %%o4, %%o5 */
" .word 0xdbe0500c\n\t"
#endif
" cmp %%o4, %%o5\n\t"
" bne 1b\n\t"
" nop"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest), "r" (exch)
: "memory", "cc");
return ret;
}
G_GNUC_UNUSED
static inline gint32 InterlockedExchangeAdd(volatile gint32 *_dest, gint32 add)
{
register volatile gint32 *dest asm("g1") = _dest;
register gint32 tmp asm("o4");
register gint32 ret asm("o5");
__asm__ __volatile__(
"1: ld [%%g1], %%o4\n\t"
" add %%o4, %3, %%o5\n\t"
/* cas [%%g1], %%o4, %%o5 */
" .word 0xdbe0500c\n\t"
" cmp %%o4, %%o5\n\t"
" bne 1b\n\t"
" add %%o5, %3, %%o5"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest), "r" (add)
: "memory", "cc");
return ret;
}
#elif __s390__
#define WAPI_ATOMIC_ASM
static inline gint32
InterlockedCompareExchange(volatile gint32 *dest,
gint32 exch, gint32 comp)
{
gint32 old;
__asm__ __volatile__ ("\tLA\t1,%0\n"
"\tLR\t%1,%3\n"
"\tCS\t%1,%2,0(1)\n"
: "+m" (*dest), "=&r" (old)
: "r" (exch), "r" (comp)
: "1", "cc");
return(old);
}
#ifndef __s390x__
static inline gpointer
InterlockedCompareExchangePointer(volatile gpointer *dest,
gpointer exch, gpointer comp)
{
gpointer old;
__asm__ __volatile__ ("\tLA\t1,%0\n"
"\tLR\t%1,%3\n"
"\tCS\t%1,%2,0(1)\n"
: "+m" (*dest), "=&r" (old)
: "r" (exch), "r" (comp)
: "1", "cc");
return(old);
}
# else
static inline gpointer
InterlockedCompareExchangePointer(volatile gpointer *dest,
gpointer exch,
gpointer comp)
{
gpointer old;
__asm__ __volatile__ ("\tLA\t1,%0\n"
"\tLGR\t%1,%3\n"
"\tCSG\t%1,%2,0(1)\n"
: "+m" (*dest), "=&r" (old)
: "r" (exch), "r" (comp)
: "1", "cc");
return(old);
}
# endif
# ifndef __s390x__
static inline gint32
InterlockedIncrement(volatile gint32 *val)
{
gint32 tmp;
__asm__ __volatile__ ("\tLA\t2,%1\n"
"0:\tL\t%0,%1\n"
"\tLR\t1,%0\n"
"\tAHI\t1,1\n"
"\tCS\t%0,1,0(2)\n"
"\tJNZ\t0b\n"
"\tLR\t%0,1"
: "=r" (tmp), "+m" (*val)
: : "1", "2", "cc");
return(tmp);
}
# else
static inline gint32
InterlockedIncrement(volatile gint32 *val)
{
gint32 tmp;
__asm__ __volatile__ ("\tLA\t2,%1\n"
"0:\tLGF\t%0,%1\n"
"\tLGFR\t1,%0\n"
"\tAGHI\t1,1\n"
"\tCS\t%0,1,0(2)\n"
"\tJNZ\t0b\n"
"\tLGFR\t%0,1"
: "=r" (tmp), "+m" (*val)
: : "1", "2", "cc");
return(tmp);
}
# endif
# ifndef __s390x__
static inline gint32
InterlockedDecrement(volatile gint32 *val)
{
gint32 tmp;
__asm__ __volatile__ ("\tLA\t2,%1\n"
"0:\tL\t%0,%1\n"
"\tLR\t1,%0\n"
"\tAHI\t1,-1\n"
"\tCS\t%0,1,0(2)\n"
"\tJNZ\t0b\n"
"\tLR\t%0,1"
: "=r" (tmp), "+m" (*val)
: : "1", "2", "cc");
return(tmp);
}
# else
static inline gint32
InterlockedDecrement(volatile gint32 *val)
{
gint32 tmp;
__asm__ __volatile__ ("\tLA\t2,%1\n"
"0:\tLGF\t%0,%1\n"
"\tLGFR\t1,%0\n"
"\tAGHI\t1,-1\n"
"\tCS\t%0,1,0(2)\n"
"\tJNZ\t0b\n"
"\tLGFR\t%0,1"
: "=r" (tmp), "+m" (*val)
: : "1", "2", "cc");
return(tmp);
}
# endif
static inline gint32
InterlockedExchange(volatile gint32 *val, gint32 new_val)
{
gint32 ret;
__asm__ __volatile__ ("\tLA\t1,%0\n"
"0:\tL\t%1,%0\n"
"\tCS\t%1,%2,0(1)\n"
"\tJNZ\t0b"
: "+m" (*val), "=&r" (ret)
: "r" (new_val)
: "1", "cc");
return(ret);
}
# ifndef __s390x__
static inline gpointer
InterlockedExchangePointer(volatile gpointer *val, gpointer new_val)
{
gpointer ret;
__asm__ __volatile__ ("\tLA\t1,%0\n"
"0:\tL\t%1,%0\n"
"\tCS\t%1,%2,0(1)\n"
"\tJNZ\t0b"
: "+m" (*val), "=&r" (ret)
: "r" (new_val)
: "1", "cc");
return(ret);
}
# else
static inline gpointer
InterlockedExchangePointer(volatile gpointer *val, gpointer new_val)
{
gpointer ret;
__asm__ __volatile__ ("\tLA\t1,%0\n"
"0:\tLG\t%1,%0\n"
"\tCSG\t%1,%2,0(1)\n"
"\tJNZ\t0b"
: "+m" (*val), "=&r" (ret)
: "r" (new_val)
: "1", "cc");
return(ret);
}
# endif
# ifndef __s390x__
static inline gint32
InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
{
gint32 ret;
__asm__ __volatile__ ("\tLA\t2,%1\n"
"0:\tL\t%0,%1\n"
"\tLR\t1,%0\n"
"\tAR\t1,%2\n"
"\tCS\t%0,1,0(2)\n"
"\tJNZ\t0b"
: "=&r" (ret), "+m" (*val)
: "r" (add)
: "1", "2", "cc");
return(ret);
}
# else
static inline gint32
InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
{
gint32 ret;
__asm__ __volatile__ ("\tLA\t2,%1\n"
"0:\tLGF\t%0,%1\n"
"\tLGFR\t1,%0\n"
"\tAGR\t1,%2\n"
"\tCS\t%0,1,0(2)\n"
"\tJNZ\t0b"
: "=&r" (ret), "+m" (*val)
: "r" (add)
: "1", "2", "cc");
return(ret);
}
# endif
#elif defined(__mono_ppc__)
#define WAPI_ATOMIC_ASM
#ifdef G_COMPILER_CODEWARRIOR
static inline gint32 InterlockedIncrement(volatile register gint32 *val)
{
gint32 result = 0, tmp;
register gint32 result = 0;
register gint32 tmp;
asm
{
@1:
lwarx tmp, 0, val
addi result, tmp, 1
stwcx. result, 0, val
bne- @1
}
return result;
}
static inline gint32 InterlockedDecrement(register volatile gint32 *val)
{
register gint32 result = 0;
register gint32 tmp;
asm
{
@1:
lwarx tmp, 0, val
addi result, tmp, -1
stwcx. result, 0, val
bne- @1
}
return result;
}
#define InterlockedCompareExchangePointer(dest,exch,comp) (void*)InterlockedCompareExchange((volatile gint32 *)(dest), (gint32)(exch), (gint32)(comp))
static inline gint32 InterlockedCompareExchange(volatile register gint32 *dest, register gint32 exch, register gint32 comp)
{
register gint32 tmp = 0;
asm
{
@1:
lwarx tmp, 0, dest
cmpw tmp, comp
bne- @2
stwcx. exch, 0, dest
bne- @1
@2:
}
return tmp;
}
static inline gint32 InterlockedExchange(register volatile gint32 *dest, register gint32 exch)
{
register gint32 tmp = 0;
asm
{
@1:
lwarx tmp, 0, dest
stwcx. exch, 0, dest
bne- @1
}
return tmp;
}
#define InterlockedExchangePointer(dest,exch) (void*)InterlockedExchange((volatile gint32 *)(dest), (gint32)(exch))
#else
#if defined(__mono_ppc64__) && !defined(__mono_ilp32__)
#define LDREGX "ldarx"
#define STREGCXD "stdcx."
#define CMPREG "cmpd"
#else
#define LDREGX "lwarx"
#define STREGCXD "stwcx."
#define CMPREG "cmpw"
#endif
static inline gint32 InterlockedIncrement(volatile gint32 *val)
{
gint32 result = 0, tmp;
__asm__ __volatile__ ("\n1:\n\t"
"lwarx %0, 0, %2\n\t"
"addi %1, %0, 1\n\t"
"stwcx. %1, 0, %2\n\t"
"bne- 1b"
: "=&b" (result), "=&b" (tmp): "r" (val): "cc", "memory");
return result + 1;
}
static inline gint32 InterlockedDecrement(volatile gint32 *val)
{
gint32 result = 0, tmp;
__asm__ __volatile__ ("\n1:\n\t"
"lwarx %0, 0, %2\n\t"
"addi %1, %0, -1\n\t"
"stwcx. %1, 0, %2\n\t"
"bne- 1b"
: "=&b" (result), "=&b" (tmp): "r" (val): "cc", "memory");
return result - 1;
}
static inline gpointer InterlockedCompareExchangePointer (volatile gpointer *dest,
gpointer exch, gpointer comp)
{
gpointer tmp = NULL;
__asm__ __volatile__ ("\n1:\n\t"
LDREGX " %0, 0, %1\n\t"
CMPREG " %0, %2\n\t"
"bne- 2f\n\t"
STREGCXD " %3, 0, %1\n\t"
"bne- 1b\n"
"2:"
: "=&r" (tmp)
: "b" (dest), "r" (comp), "r" (exch): "cc", "memory");
return(tmp);
}
static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
gint32 exch, gint32 comp) {
gint32 tmp = 0;
__asm__ __volatile__ ("\n1:\n\t"
"lwarx %0, 0, %1\n\t"
"cmpw %0, %2\n\t"
"bne- 2f\n\t"
"stwcx. %3, 0, %1\n\t"
"bne- 1b\n"
"2:"
: "=&r" (tmp)
: "b" (dest), "r" (comp), "r" (exch): "cc", "memory");
return(tmp);
}
static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
{
gint32 tmp = 0;
__asm__ __volatile__ ("\n1:\n\t"
"lwarx %0, 0, %2\n\t"
"stwcx. %3, 0, %2\n\t"
"bne 1b"
: "=r" (tmp) : "0" (tmp), "b" (dest), "r" (exch): "cc", "memory");
return(tmp);
}
static inline gpointer InterlockedExchangePointer (volatile gpointer *dest, gpointer exch)
{
gpointer tmp = NULL;
__asm__ __volatile__ ("\n1:\n\t"
LDREGX " %0, 0, %2\n\t"
STREGCXD " %3, 0, %2\n\t"
"bne 1b"
: "=r" (tmp) : "0" (tmp), "b" (dest), "r" (exch): "cc", "memory");
return(tmp);
}
static inline gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
{
gint32 result, tmp;
__asm__ __volatile__ ("\n1:\n\t"
"lwarx %0, 0, %2\n\t"
"add %1, %0, %3\n\t"
"stwcx. %1, 0, %2\n\t"
"bne 1b"
: "=&r" (result), "=&r" (tmp)
: "r" (dest), "r" (add) : "cc", "memory");
return(result);
}
#undef LDREGX
#undef STREGCXD
#undef CMPREG
#endif /* !G_COMPILER_CODEWARRIOR */
#elif defined(__arm__)
#define WAPI_ATOMIC_ASM
static inline gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch, gint32 comp)
{
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7__)
gint32 ret, tmp;
__asm__ __volatile__ ( "1:\n"
"mov %0, #0\n"
"ldrex %1, [%2]\n"
"teq %1, %3\n"
"it eq\n"
"strexeq %0, %4, [%2]\n"
"teq %0, #0\n"
"bne 1b\n"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest), "r" (comp), "r" (exch)
: "memory", "cc");
return ret;
#else
gint32 a, b;
__asm__ __volatile__ ( "0:\n\t"
"ldr %1, [%2]\n\t"
"cmp %1, %4\n\t"
"mov %0, %1\n\t"
"bne 1f\n\t"
"swp %0, %3, [%2]\n\t"
"cmp %0, %1\n\t"
"swpne %3, %0, [%2]\n\t"
"bne 0b\n\t"
"1:"
: "=&r" (a), "=&r" (b)
: "r" (dest), "r" (exch), "r" (comp)
: "cc", "memory");
return a;
#endif
}
static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
{
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7__)
gpointer ret, tmp;
__asm__ __volatile__ ( "1:\n"
"mov %0, #0\n"
"ldrex %1, [%2]\n"
"teq %1, %3\n"
"it eq\n"
"strexeq %0, %4, [%2]\n"
"teq %0, #0\n"
"bne 1b\n"
: "=&r" (tmp), "=&r" (ret)
: "r" (dest), "r" (comp), "r" (exch)
: "memory", "cc");
return ret;
#else
gpointer a, b;
__asm__ __volatile__ ( "0:\n\t"
"ldr %1, [%2]\n\t"
"cmp %1, %4\n\t"
"mov %0, %1\n\t"
"bne 1f\n\t"
"swpeq %0, %3, [%2]\n\t"
"cmp %0, %1\n\t"
"swpne %3, %0, [%2]\n\t"
"bne 0b\n\t"
"1:"
: "=&r" (a), "=&r" (b)
: "r" (dest), "r" (exch), "r" (comp)
: "cc", "memory");
return a;
#endif
}
static inline gint32 InterlockedIncrement(volatile gint32 *dest)
{
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7__)
gint32 ret, flag;
__asm__ __volatile__ ( "1:\n"
"ldrex %0, [%2]\n"
"add %0, %0, %3\n"
"strex %1, %0, [%2]\n"
"teq %1, #0\n"
"bne 1b\n"
: "=&r" (ret), "=&r" (flag)
: "r" (dest), "r" (1)
: "memory", "cc");
return ret;
#else
gint32 a, b, c;
__asm__ __volatile__ ( "0:\n\t"
"ldr %0, [%3]\n\t"
"add %1, %0, %4\n\t"
"swp %2, %1, [%3]\n\t"
"cmp %0, %2\n\t"
"swpne %1, %2, [%3]\n\t"
"bne 0b"
: "=&r" (a), "=&r" (b), "=&r" (c)
: "r" (dest), "r" (1)
: "cc", "memory");
return b;
#endif
}
static inline gint32 InterlockedDecrement(volatile gint32 *dest)
{
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7__)
gint32 ret, flag;
__asm__ __volatile__ ( "1:\n"
"ldrex %0, [%2]\n"
"sub %0, %0, %3\n"
"strex %1, %0, [%2]\n"
"teq %1, #0\n"
"bne 1b\n"
: "=&r" (ret), "=&r" (flag)
: "r" (dest), "r" (1)
: "memory", "cc");
return ret;
#else
gint32 a, b, c;
__asm__ __volatile__ ( "0:\n\t"
"ldr %0, [%3]\n\t"
"add %1, %0, %4\n\t"
"swp %2, %1, [%3]\n\t"
"cmp %0, %2\n\t"
"swpne %1, %2, [%3]\n\t"
"bne 0b"
: "=&r" (a), "=&r" (b), "=&r" (c)
: "r" (dest), "r" (-1)
: "cc", "memory");
return b;
#endif
}
static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
{
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7__)
gint32 ret, flag;
__asm__ __volatile__ (
"1:\n"
"ldrex %0, [%3]\n"
"strex %1, %2, [%3]\n"
"teq %1, #0\n"
"bne 1b\n"
: "=&r" (ret), "=&r" (flag)
: "r" (exch), "r" (dest)
: "memory", "cc");
return ret;
#else
gint32 a;
__asm__ __volatile__ ( "swp %0, %2, [%1]"
: "=&r" (a)
: "r" (dest), "r" (exch));
return a;
#endif
}
static inline gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch)
{
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7__)
gpointer ret, flag;
__asm__ __volatile__ (
"1:\n"
"ldrex %0, [%3]\n"
"strex %1, %2, [%3]\n"
"teq %1, #0\n"
"bne 1b\n"
: "=&r" (ret), "=&r" (flag)
: "r" (exch), "r" (dest)
: "memory", "cc");
return ret;
#else
gpointer a;
__asm__ __volatile__ ( "swp %0, %2, [%1]"
: "=&r" (a)
: "r" (dest), "r" (exch));
return a;
#endif
}