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

◆ Wait() [4/6]

bool System.Threading.SemaphoreSlim.Wait ( int millisecondsTimeout,
CancellationToken cancellationToken )
inline

Definition at line 155 of file SemaphoreSlim.cs.

156 {
157 CheckDispose();
158 if (millisecondsTimeout < -1)
159 {
160 throw new ArgumentOutOfRangeException("millisecondsTimeout", millisecondsTimeout, SR.SemaphoreSlim_Wait_TimeoutWrong);
161 }
162 cancellationToken.ThrowIfCancellationRequested();
163 if (millisecondsTimeout == 0 && m_currentCount == 0)
164 {
165 return false;
166 }
167 uint startTime = 0u;
169 {
170 startTime = TimeoutHelper.GetTime();
171 }
172 bool flag = false;
173 Task<bool> task = null;
174 bool lockTaken = false;
175 CancellationTokenRegistration cancellationTokenRegistration = cancellationToken.UnsafeRegister(s_cancellationTokenCanceledEventHandler, this);
176 try
177 {
178 if (m_currentCount == 0)
179 {
180 int num = SpinWait.SpinCountforSpinBeforeWait * 4;
181 SpinWait spinWait = default(SpinWait);
182 while (spinWait.Count < num)
183 {
184 spinWait.SpinOnce(-1);
185 if (m_currentCount != 0)
186 {
187 break;
188 }
189 }
190 }
191 Monitor.Enter(m_lockObjAndDisposed, ref lockTaken);
192 m_waitCount++;
193 if (m_asyncHead != null)
194 {
196 }
197 else
198 {
199 OperationCanceledException ex = null;
200 if (m_currentCount == 0)
201 {
202 if (millisecondsTimeout == 0)
203 {
204 return false;
205 }
206 try
207 {
209 }
210 catch (OperationCanceledException ex2)
211 {
212 ex = ex2;
213 }
214 }
215 if (m_currentCount > 0)
216 {
217 flag = true;
219 }
220 else if (ex != null)
221 {
222 throw ex;
223 }
224 if (m_waitHandle != null && m_currentCount == 0)
225 {
227 }
228 }
229 }
230 finally
231 {
232 if (lockTaken)
233 {
234 m_waitCount--;
235 Monitor.Exit(m_lockObjAndDisposed);
236 }
237 cancellationTokenRegistration.Dispose();
238 }
239 return task?.GetAwaiter().GetResult() ?? flag;
240 }
bool WaitUntilCountOrTimeout(int millisecondsTimeout, uint startTime, CancellationToken cancellationToken)
static readonly Action< object > s_cancellationTokenCanceledEventHandler
readonly StrongBox< bool > m_lockObjAndDisposed
volatile ManualResetEvent m_waitHandle

References System.cancellationToken, System.Threading.SemaphoreSlim.CheckDispose(), System.Threading.SpinWait.Count, System.Threading.CancellationTokenRegistration.Dispose(), System.Threading.Monitor.Enter(), System.Threading.Monitor.Exit(), System.Threading.TimeoutHelper.GetTime(), System.Threading.SemaphoreSlim.m_asyncHead, System.Threading.SemaphoreSlim.m_currentCount, System.Threading.SemaphoreSlim.m_lockObjAndDisposed, System.Threading.SemaphoreSlim.m_waitCount, System.Threading.SemaphoreSlim.m_waitHandle, System.millisecondsTimeout, System.Threading.EventWaitHandle.Reset(), System.Threading.SemaphoreSlim.s_cancellationTokenCanceledEventHandler, System.SR.SemaphoreSlim_Wait_TimeoutWrong, System.Threading.SpinWait.SpinOnce(), System.task, System.Threading.SemaphoreSlim.WaitAsync(), and System.Threading.SemaphoreSlim.WaitUntilCountOrTimeout().