Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ Wait()

bool System.Threading.LowLevelLifoSemaphore.Wait ( int timeoutMs,
bool spinWait )
inline

Definition at line 160 of file LowLevelLifoSemaphore.cs.

161 {
162 int num = (spinWait ? _spinCount : 0);
163 Counts counts = _separated._counts;
164 Counts newCounts;
165 while (true)
166 {
167 newCounts = counts;
168 if (counts.SignalCount != 0)
169 {
170 newCounts.DecrementSignalCount();
171 }
172 else if (timeoutMs != 0)
173 {
174 if (num > 0 && newCounts.SpinnerCount < byte.MaxValue)
175 {
176 newCounts.IncrementSpinnerCount();
177 }
178 else
179 {
180 newCounts.IncrementWaiterCount();
181 }
182 }
183 Counts counts2 = _separated._counts.InterlockedCompareExchange(newCounts, counts);
184 if (counts2 == counts)
185 {
186 break;
187 }
188 counts = counts2;
189 }
190 if (counts.SignalCount != 0)
191 {
192 return true;
193 }
194 if (newCounts.WaiterCount != counts.WaiterCount)
195 {
196 return WaitForSignal(timeoutMs);
197 }
198 if (timeoutMs == 0)
199 {
200 return false;
201 }
202 int processorCount = Environment.ProcessorCount;
203 int num2 = ((processorCount <= 1) ? 10 : 0);
204 while (num2 < num)
205 {
206 LowLevelSpinWaiter.Wait(num2, 10, processorCount);
207 num2++;
208 counts = _separated._counts;
209 while (counts.SignalCount != 0)
210 {
211 Counts newCounts2 = counts;
212 newCounts2.DecrementSignalCount();
213 newCounts2.DecrementSpinnerCount();
214 Counts counts3 = _separated._counts.InterlockedCompareExchange(newCounts2, counts);
215 if (counts3 == counts)
216 {
217 return true;
218 }
219 counts = counts3;
220 }
221 }
222 counts = _separated._counts;
223 while (true)
224 {
225 Counts newCounts3 = counts;
226 newCounts3.DecrementSpinnerCount();
227 if (counts.SignalCount != 0)
228 {
229 newCounts3.DecrementSignalCount();
230 }
231 else
232 {
233 newCounts3.IncrementWaiterCount();
234 }
235 Counts counts4 = _separated._counts.InterlockedCompareExchange(newCounts3, counts);
236 if (counts4 == counts)
237 {
238 break;
239 }
240 counts = counts4;
241 }
242 if (counts.SignalCount == 0)
243 {
244 return WaitForSignal(timeoutMs);
245 }
246 return true;
247 }
Counts InterlockedCompareExchange(Counts newCounts, Counts oldCounts)

References System.Threading.LowLevelLifoSemaphore.CacheLineSeparatedCounts._counts, System.Threading.LowLevelLifoSemaphore._separated, System.Threading.LowLevelLifoSemaphore._spinCount, System.Threading.LowLevelLifoSemaphore.Counts.DecrementSignalCount(), System.Threading.LowLevelLifoSemaphore.Counts.DecrementSpinnerCount(), System.Threading.LowLevelLifoSemaphore.Counts.IncrementSpinnerCount(), System.Threading.LowLevelLifoSemaphore.Counts.IncrementWaiterCount(), System.Threading.LowLevelLifoSemaphore.Counts.InterlockedCompareExchange(), System.Environment.ProcessorCount, System.Threading.LowLevelLifoSemaphore.Counts.SignalCount, System.Threading.LowLevelLifoSemaphore.Counts.SpinnerCount, System.Threading.LowLevelSpinWaiter.Wait(), System.Threading.LowLevelLifoSemaphore.Counts.WaiterCount, and System.Threading.LowLevelLifoSemaphore.WaitForSignal().

Referenced by System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart().