Нема описа
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.

ConcurrentMask.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Threading;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using Unity.Mathematics;
  5. #pragma warning disable 0649
  6. namespace Unity.Collections
  7. {
  8. internal struct Long8
  9. {
  10. internal long f0,f1,f2,f3,f4,f5,f6,f7;
  11. }
  12. internal struct Long64
  13. {
  14. internal Long8 f0,f1,f2,f3,f4,f5,f6,f7;
  15. }
  16. internal struct Long512
  17. {
  18. internal Long64 f0,f1,f2,f3,f4,f5,f6,f7;
  19. }
  20. internal struct Long1024 : IIndexable<long>
  21. {
  22. internal Long512 f0,f1;
  23. public int Length { get { return 1024;} set {} }
  24. public ref long ElementAt(int index)
  25. {
  26. unsafe { fixed(Long512* p = &f0) {
  27. return ref UnsafeUtility.AsRef<long>((long*)p + index);
  28. } }
  29. }
  30. }
  31. internal class ConcurrentMask
  32. {
  33. internal static void longestConsecutiveOnes(long value, out int offset, out int count)
  34. {
  35. count = 0;
  36. var newvalue = value;
  37. while(newvalue != 0)
  38. {
  39. value = newvalue;
  40. newvalue = value & (long)((ulong)value >> 1);
  41. ++count;
  42. }
  43. offset = math.tzcnt(value);
  44. }
  45. internal static bool foundAtLeastThisManyConsecutiveOnes(long value, int minimum, out int offset, out int count)
  46. {
  47. if(minimum == 1)
  48. {
  49. offset = math.tzcnt(value); // find offset of first 1 bit
  50. count = 1;
  51. return offset != 64;
  52. }
  53. longestConsecutiveOnes(value, out offset, out count);
  54. return count >= minimum;
  55. }
  56. internal static bool foundAtLeastThisManyConsecutiveZeroes(long value, int minimum, out int offset, out int count)
  57. {
  58. return foundAtLeastThisManyConsecutiveOnes(~value, minimum, out offset, out count);
  59. }
  60. internal const int ErrorFailedToFree = -1;
  61. internal const int ErrorFailedToAllocate = -2;
  62. internal const int EmptyBeforeAllocation = 0;
  63. internal const int EmptyAfterFree = 0;
  64. internal static bool Succeeded(int error)
  65. {
  66. return error >= 0;
  67. }
  68. internal static long MakeMask(int offset, int bits)
  69. {
  70. return (long)(~0UL >> (64-bits)) << offset;
  71. }
  72. internal static int TryAllocate(ref long l, int offset, int bits)
  73. {
  74. var mask = MakeMask(offset, bits);
  75. var readValue = Interlocked.Read(ref l);
  76. long oldReadValue, writtenValue;
  77. do
  78. {
  79. if((readValue & mask) != 0)
  80. return ErrorFailedToAllocate;
  81. writtenValue = readValue | mask;
  82. oldReadValue = readValue;
  83. readValue = Interlocked.CompareExchange(ref l, writtenValue, oldReadValue);
  84. } while(readValue != oldReadValue);
  85. return math.countbits(readValue); // how many bits were set, before i allocated? sometimes if 0, do something special (allocate chunk?)
  86. }
  87. internal static int TryFree(ref long l, int offset, int bits)
  88. {
  89. var mask = MakeMask(offset, bits);
  90. var readValue = Interlocked.Read(ref l);
  91. long oldReadValue, writtenValue;
  92. do
  93. {
  94. if((readValue & mask) != mask)
  95. return ErrorFailedToFree;
  96. writtenValue = readValue & ~mask;
  97. oldReadValue = readValue;
  98. readValue = Interlocked.CompareExchange(ref l, writtenValue, oldReadValue);
  99. } while(readValue != oldReadValue);
  100. return math.countbits(writtenValue); // how many bits are set, after i freed? sometimes if 0, do something special (free chunk?)
  101. }
  102. internal static int TryAllocate(ref long l, out int offset, int bits)
  103. {
  104. var readValue = Interlocked.Read(ref l);
  105. long oldReadValue, writtenValue;
  106. do
  107. {
  108. if(!foundAtLeastThisManyConsecutiveZeroes(readValue, bits, out offset, out int _))
  109. return ErrorFailedToAllocate;
  110. var mask = MakeMask(offset, bits);
  111. writtenValue = readValue | mask;
  112. oldReadValue = readValue;
  113. readValue = Interlocked.CompareExchange(ref l, writtenValue, oldReadValue);
  114. } while(readValue != oldReadValue);
  115. return math.countbits(readValue); // how many bits were set, before i allocated? sometimes if 0, do something special (allocate chunk?)
  116. }
  117. internal static int TryAllocate<T>(ref T t, int offset, int bits) where T : IIndexable<long>
  118. {
  119. var wordOffset = offset >> 6;
  120. var bitOffset = offset & 63;
  121. return TryAllocate(ref t.ElementAt(wordOffset), bitOffset, bits);
  122. }
  123. internal static int TryFree<T>(ref T t, int offset, int bits) where T : IIndexable<long>
  124. {
  125. var wordOffset = offset >> 6;
  126. var bitOffset = offset & 63;
  127. return TryFree(ref t.ElementAt(wordOffset), bitOffset, bits);
  128. }
  129. internal static int TryAllocate<T>(ref T t, out int offset, int begin, int end, int bits) where T : IIndexable<long>
  130. {
  131. for(var wordOffset = begin; wordOffset < end; ++wordOffset)
  132. {
  133. int error, bitOffset;
  134. error = TryAllocate(ref t.ElementAt(wordOffset), out bitOffset, bits);
  135. if(Succeeded(error))
  136. {
  137. offset = wordOffset * 64 + bitOffset;
  138. return error;
  139. }
  140. }
  141. offset = -1;
  142. return ErrorFailedToAllocate;
  143. }
  144. internal static int TryAllocate<T>(ref T t, out int offset, int bits) where T : IIndexable<long>
  145. {
  146. return TryAllocate(ref t, out offset, 0, t.Length, bits);
  147. }
  148. }
  149. }