No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

070-TestAtomics.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Threading;
  3. namespace Burst.Compiler.IL.Tests
  4. {
  5. /// <summary>
  6. /// Tests of the <see cref="Interlocked"/> functions.
  7. /// </summary>
  8. internal class TestAtomics
  9. {
  10. [TestCompiler(1)]
  11. [TestCompiler(-1)]
  12. public static int test_atomic_increment_int(ref int value)
  13. {
  14. return Interlocked.Increment(ref value);
  15. }
  16. [TestCompiler(1L)]
  17. [TestCompiler(-1L)]
  18. public static long test_atomic_increment_long(ref long value)
  19. {
  20. return Interlocked.Increment(ref value);
  21. }
  22. [TestCompiler(1)]
  23. [TestCompiler(-1)]
  24. public static int test_atomic_add_int(ref int value)
  25. {
  26. return Interlocked.Add(ref value, 2);
  27. }
  28. [TestCompiler(1L)]
  29. [TestCompiler(-1L)]
  30. public static long test_atomic_add_long(ref long value)
  31. {
  32. return Interlocked.Add(ref value, 2);
  33. }
  34. [TestCompiler(1, 2, 1)]
  35. [TestCompiler(1, 10, 1)]
  36. [TestCompiler(1, 2, 2)]
  37. [TestCompiler(7, 2, 1)]
  38. [TestCompiler(7, 10, 1)]
  39. [TestCompiler(7, 2, 2)]
  40. public static int test_atomic_compare_and_exchange_int(ref int location, int value, int compareAnd)
  41. {
  42. return Interlocked.CompareExchange(ref location, value, compareAnd);
  43. }
  44. [TestCompiler(1L, 2L, 1L)]
  45. [TestCompiler(1L, 10L, 1L)]
  46. [TestCompiler(1L, 2L, 2L)]
  47. [TestCompiler(7L, 2L, 1L)]
  48. [TestCompiler(7L, 10L, 1L)]
  49. [TestCompiler(7L, 2L, 2L)]
  50. public static long test_atomic_compare_and_exchange_long(ref long location, long value, long compareAnd)
  51. {
  52. return Interlocked.CompareExchange(ref location, value, compareAnd);
  53. }
  54. [TestCompiler(1)]
  55. [TestCompiler(-1)]
  56. public static int test_atomic_decrement_int(ref int value)
  57. {
  58. return Interlocked.Decrement(ref value);
  59. }
  60. [TestCompiler(1L)]
  61. [TestCompiler(-1L)]
  62. public static long test_atomic_decrement_long(ref long value)
  63. {
  64. return Interlocked.Decrement(ref value);
  65. }
  66. [TestCompiler(1)]
  67. public static int test_atomic_exchange_int(ref int value)
  68. {
  69. return Interlocked.Exchange(ref value, 5);
  70. }
  71. [TestCompiler(1L)]
  72. public static long test_atomic_exchange_long(ref long value)
  73. {
  74. return Interlocked.Exchange(ref value, 5);
  75. }
  76. [TestCompiler(1)]
  77. public static IntPtr ExchangeIntPtr(IntPtr value)
  78. {
  79. return Interlocked.Exchange(ref value, new IntPtr(5));
  80. }
  81. [TestCompiler(1, 2, 1)]
  82. [TestCompiler(1, 10, 1)]
  83. [TestCompiler(1, 2, 2)]
  84. public static IntPtr CompareExchangeIntPtr(IntPtr location, IntPtr value, IntPtr compareAnd)
  85. {
  86. return Interlocked.CompareExchange(ref location, value, compareAnd);
  87. }
  88. [TestCompiler]
  89. public static void test_atomic_memorybarrier()
  90. {
  91. Interlocked.MemoryBarrier();
  92. }
  93. [TestCompiler(0)]
  94. public static int Case1111040(int val)
  95. {
  96. int test = val;
  97. Interlocked.Increment(ref test);
  98. Interlocked.Decrement(ref test);
  99. return test;
  100. }
  101. [TestCompiler(42.0f)]
  102. public static float ExchangeFloat(ref float f)
  103. {
  104. if (Interlocked.Exchange(ref f, 13) == 42)
  105. {
  106. return f;
  107. }
  108. return 0;
  109. }
  110. [TestCompiler(42.0)]
  111. public static double ExchangeDouble(ref double d)
  112. {
  113. if (Interlocked.Exchange(ref d, 13) == 42)
  114. {
  115. return d;
  116. }
  117. return 0;
  118. }
  119. #if !BURST_APPLE_SILICON_TESTING // https://jira.unity3d.com/browse/UUM-9159
  120. [TestCompiler(42.0f)]
  121. public static float CompareExchangeFloat(ref float f)
  122. {
  123. if (Interlocked.CompareExchange(ref f, 13, 42) == 42)
  124. {
  125. return f;
  126. }
  127. return 0;
  128. }
  129. #endif
  130. [TestCompiler(42.0)]
  131. public static double CompareExchangeDouble(ref double d)
  132. {
  133. if (Interlocked.CompareExchange(ref d, 13, 42) == 42)
  134. {
  135. return d;
  136. }
  137. return 0;
  138. }
  139. [TestCompiler(42L)]
  140. public static double Read(ref long l)
  141. {
  142. if (Interlocked.Read(ref l) == 42)
  143. {
  144. return l;
  145. }
  146. return 0;
  147. }
  148. }
  149. }