Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RangeWorker.cs
Go to the documentation of this file.
3
5
6[StructLayout(LayoutKind.Auto)]
7internal struct RangeWorker
8{
9 internal readonly IndexRange[] _indexRanges;
10
11 internal int _nCurrentIndexRange;
12
13 internal long _nStep;
14
15 internal long _nIncrementValue;
16
17 internal readonly long _nMaxIncrementValue;
18
19 internal readonly bool _use32BitCurrentIndex;
20
21 internal bool IsInitialized => _indexRanges != null;
22
23 internal RangeWorker(IndexRange[] ranges, int nInitialRange, long nStep, bool use32BitCurrentIndex)
24 {
25 _indexRanges = ranges;
26 _use32BitCurrentIndex = use32BitCurrentIndex;
27 _nCurrentIndexRange = nInitialRange;
28 _nStep = nStep;
29 _nIncrementValue = nStep;
30 _nMaxIncrementValue = 16 * nStep;
31 }
32
33 internal unsafe bool FindNewWork(out long nFromInclusiveLocal, out long nToExclusiveLocal)
34 {
35 int num = _indexRanges.Length;
36 do
37 {
39 if (indexRange._bRangeFinished == 0)
40 {
42 if (nSharedCurrentIndexOffset == null)
43 {
44 Interlocked.CompareExchange(ref _indexRanges[_nCurrentIndexRange]._nSharedCurrentIndexOffset, new StrongBox<long>(0L), null);
46 }
47 long num2;
49 {
50 fixed (long* ptr = &nSharedCurrentIndexOffset.Value)
51 {
52 num2 = Interlocked.Add(ref *(int*)ptr, (int)_nIncrementValue) - _nIncrementValue;
53 }
54 }
55 else
56 {
57 num2 = Interlocked.Add(ref nSharedCurrentIndexOffset.Value, _nIncrementValue) - _nIncrementValue;
58 }
59 if (indexRange._nToExclusive - indexRange._nFromInclusive > num2)
60 {
61 nFromInclusiveLocal = indexRange._nFromInclusive + num2;
62 nToExclusiveLocal = nFromInclusiveLocal + _nIncrementValue;
63 if (nToExclusiveLocal > indexRange._nToExclusive || nToExclusiveLocal < indexRange._nFromInclusive)
64 {
65 nToExclusiveLocal = indexRange._nToExclusive;
66 }
68 {
71 {
73 }
74 }
75 return true;
76 }
78 }
80 num--;
81 }
82 while (num > 0);
83 nFromInclusiveLocal = 0L;
84 nToExclusiveLocal = 0L;
85 return false;
86 }
87
88 internal bool FindNewWork32(out int nFromInclusiveLocal32, out int nToExclusiveLocal32)
89 {
90 long nFromInclusiveLocal33;
91 long nToExclusiveLocal33;
92 bool result = FindNewWork(out nFromInclusiveLocal33, out nToExclusiveLocal33);
93 nFromInclusiveLocal32 = (int)nFromInclusiveLocal33;
94 nToExclusiveLocal32 = (int)nToExclusiveLocal33;
95 return result;
96 }
97}
static int CompareExchange(ref int location1, int value, int comparand)
static int Exchange(ref int location1, int value)
static int Add(ref int location1, int value)
static int Size
Definition IntPtr.cs:21
volatile StrongBox< long > _nSharedCurrentIndexOffset
Definition IndexRange.cs:13
bool FindNewWork32(out int nFromInclusiveLocal32, out int nToExclusiveLocal32)
unsafe bool FindNewWork(out long nFromInclusiveLocal, out long nToExclusiveLocal)
readonly IndexRange[] _indexRanges
Definition RangeWorker.cs:9
RangeWorker(IndexRange[] ranges, int nInitialRange, long nStep, bool use32BitCurrentIndex)