Here are the examples of the csharp api System.Threading.Interlocked.Decrement(ref int) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
982 Examples
19
View Source File : CelesteNetConnection.cs
License : MIT License
Project Creator : 0x0ade
License : MIT License
Project Creator : 0x0ade
protected virtual void ThreadLoop() {
try {
while (Con.IsAlive) {
DedupeTimestamp++;
int waited = 0;
if (Interlocked.CompareExchange(ref QueueCount, 0, 0) == 0)
waited = WaitHandle.WaitAny(EventHandles, 1000);
if ((waited == WaitHandle.WaitTimeout || DedupeTimestamp % 10 == 0) && LastSent.Count > 0) {
for (int i = Dedupes.Count - 1; i >= 0; --i) {
DataDedupe slot = Dedupes[i];
if (!slot.Update(DedupeTimestamp)) {
Dedupes.RemoveAt(i);
if (LastSent.TryGetValue(slot.Type, out Dictionary<uint, DataDedupe>? slotByID)) {
slotByID.Remove(slot.ID);
if (slotByID.Count == 0) {
LastSent.Remove(slot.Type);
}
}
}
}
}
if (!Con.IsAlive)
return;
DateTime now = DateTime.UtcNow;
while (Interlocked.CompareExchange(ref QueueCount, 0, 0) != 0) {
DataType? data;
lock (QueueLock) {
int next = QueueSendNext;
data = Queue[next];
Queue[next] = null;
QueueSendNext = (next + 1) % Queue.Length;
Interlocked.Decrement(ref QueueCount);
}
if (data == null)
continue;
if (data is DataInternalDisconnect) {
Con.Dispose();
return;
}
if ((data.DataFlags & DataFlags.OnlyLatest) == DataFlags.OnlyLatest) {
string type = data.GetTypeID(Con.Data);
uint id = data.GetDuplicateFilterID();
lock (QueueLock) {
int next = QueueSendNext;
int count = Interlocked.CompareExchange(ref QueueCount, 0, 0);
int length = Queue.Length;
for (int ii = 0; ii < count; ii++) {
int i = (next + ii) % length;
DataType? d = Queue[i];
if (d != null && d.GetTypeID(Con.Data) == type && d.GetDuplicateFilterID() == id) {
data = d;
Queue[i] = null;
}
}
}
}
if ((data.DataFlags & DataFlags.SkipDuplicate) == DataFlags.SkipDuplicate) {
string type = data.GetTypeID(Con.Data);
uint id = data.GetDuplicateFilterID();
if (!LastSent.TryGetValue(type, out Dictionary<uint, DataDedupe>? slotByID))
LastSent[type] = slotByID = new();
if (slotByID.TryGetValue(id, out DataDedupe? slot)) {
if (slot.Data.ConsideredDuplicate(data))
continue;
slot.Data = data;
slot.Timestamp = DedupeTimestamp;
slot.Iterations = 0;
} else {
Dedupes.Add(slotByID[id] = new(type, id, data, DedupeTimestamp));
}
}
Con.SendRaw(this, data);
if ((data.DataFlags & DataFlags.Update) == DataFlags.Update)
LastUpdate = now;
else
LastNonUpdate = now;
}
if (Con.SendStringMap) {
List<Tuple<string, int>> added = Strings.PromoteRead();
if (added.Count > 0) {
foreach (Tuple<string, int> mapping in added)
Con.SendRaw(this, new DataLowLevelStringMapping {
IsUpdate = SendStringMapUpdate,
StringMap = Strings.Name,
Value = mapping.Item1,
ID = mapping.Item2
});
if (SendStringMapUpdate)
LastUpdate = now;
else
LastNonUpdate = now;
}
}
if (Con.SendKeepAlive) {
if (SendKeepAliveUpdate && (now - LastUpdate).TotalSeconds >= 1D) {
Con.SendRaw(this, new DataLowLevelKeepAlive {
IsUpdate = true
});
LastUpdate = now;
}
if (SendKeepAliveNonUpdate && (now - LastNonUpdate).TotalSeconds >= 1D) {
Con.SendRaw(this, new DataLowLevelKeepAlive {
IsUpdate = false
});
LastNonUpdate = now;
}
}
Con.SendRawFlush();
lock (QueueLock)
if (Interlocked.CompareExchange(ref QueueCount, 0, 0) == 0)
Event.Reset();
}
} catch (ThreadInterruptedException) {
} catch (ThreadAbortException) {
} catch (Exception e) {
if (!(e is IOException) && !(e is ObjectDisposedException))
Logger.Log(LogLevel.CRI, "conqueue", $"Failed sending data:\n{e}");
Con.Dispose();
} finally {
Event.Dispose();
}
}
19
View Source File : AsyncLock.cs
License : MIT License
Project Creator : 1100100
License : MIT License
Project Creator : 1100100
public void Dispose()
{
#if DEBUG
Debug.replacedert(!_disposed);
_disposed = true;
#endif
lock (_parent._reentrancy)
{
Interlocked.Decrement(ref _parent._reentrances);
if (_parent._reentrances == 0)
{
//the owning thread is always the same so long as we are in a nested stack call
//we reset the owning id to null only when the lock is fully unlocked
_parent._owningId = UnlockedThreadId;
if (_parent._retry.CurrentCount == 0)
{
_parent._retry.Release();
}
}
}
}
19
View Source File : SentinelAdapter.cs
License : MIT License
Project Creator : 2881099
License : MIT License
Project Creator : 2881099
internal void ResetSentinel()
{
if (ResetSentinelFlag != 0) return;
if (Interlocked.Increment(ref ResetSentinelFlag) != 1)
{
Interlocked.Decrement(ref ResetSentinelFlag);
return;
}
string masterhostEnd = _masterHost;
var allkeys = _ib.GetKeys().ToList();
for (int i = 0; i < _sentinels.Count; i++)
{
if (i > 0)
{
var first = _sentinels.First;
_sentinels.RemoveFirst();
_sentinels.AddLast(first.Value);
}
try
{
using (var sentinelcli = new RedisSentinelClient(_sentinels.First.Value))
{
var masterhost = sentinelcli.GetMasterAddrByName(_connectionString.Host);
var masterConnectionString = localTestHost(masterhost, RoleType.Master);
if (masterConnectionString == null) continue;
masterhostEnd = masterhost;
if (_rw_splitting)
{
foreach (var slave in sentinelcli.Salves(_connectionString.Host))
{
ConnectionStringBuilder slaveConnectionString = localTestHost($"{slave.ip}:{slave.port}", RoleType.Slave);
if (slaveConnectionString == null) continue;
}
}
foreach (var sentinel in sentinelcli.Sentinels(_connectionString.Host))
{
var remoteSentinelHost = $"{sentinel.ip}:{sentinel.port}";
if (_sentinels.Contains(remoteSentinelHost)) continue;
_sentinels.AddLast(remoteSentinelHost);
}
}
break;
}
catch { }
}
foreach (var spkey in allkeys) _ib.TryRemove(spkey, true);
Interlocked.Exchange(ref _masterHost, masterhostEnd);
Interlocked.Decrement(ref ResetSentinelFlag);
ConnectionStringBuilder localTestHost(string host, RoleType role)
{
ConnectionStringBuilder connectionString = _connectionString.ToString();
connectionString.Host = host;
connectionString.MinPoolSize = 1;
connectionString.MaxPoolSize = 1;
using (var cli = new RedisClient(connectionString))
{
if (cli.Role().role != role)
return null;
if (role == RoleType.Master)
{
//test set/get
}
}
connectionString.MinPoolSize = _connectionString.MinPoolSize;
connectionString.MaxPoolSize = _connectionString.MaxPoolSize;
_ib.TryRegister(host, () => new RedisClientPool(connectionString, null, TopOwner));
allkeys.Remove(host);
return connectionString;
}
}
19
View Source File : ServerManager.cs
License : Apache License 2.0
Project Creator : AantCoder
License : Apache License 2.0
Project Creator : AantCoder
private void DoClient(ConnectClient client)
{
SessionServer session = null;
string addrIP = ((IPEndPoint)client.Client.Client.RemoteEndPoint).Address.ToString();
try
{
try
{
if (Repository.CheckIsBanIP(addrIP))
{
Loger.Log("Abort connect BanIP " + addrIP);
}
else
{
Loger.Log($"New connect {addrIP} (connects: {ActiveClientCount})");
session = new SessionServer();
lock (Sessions)
{
Sessions.Add(session);
}
session.Do(client, SessionsAction);
}
}
catch (ObjectDisposedException)
{
Loger.Log("Abort connect Relogin " + addrIP);
}
catch (Transfer.ConnectClient.ConnectSilenceTimeOutException)
{
Loger.Log("Abort connect TimeOut " + addrIP);
}
catch (Exception e)
{
if (!(e is SocketException) && !(e.InnerException is SocketException)
&& !(e is Transfer.ConnectClient.ConnectNotConnectedException) && !(e.InnerException is Transfer.ConnectClient.ConnectNotConnectedException))
{
ExceptionUtil.ExceptionLog(e, "Server Exception");
}
}
//if (LogMessage != null) LogMessage("End connect");
}
finally
{
Interlocked.Decrement(ref _ActiveClientCount);
Loger.Log($"Close connect {addrIP}{(session == null ? "" : " " + session?.GetNameWhoConnect())} (connects: {ActiveClientCount})");
try
{
if (session != null)
{
session.Dispose();
}
}
catch
{ }
}
}
19
View Source File : ProcessInvoker.cs
License : MIT License
Project Creator : actions
License : MIT License
Project Creator : actions
private void StartReadStream(StreamReader reader, ConcurrentQueue<string> dataBuffer)
{
Task.Run(() =>
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line != null)
{
dataBuffer.Enqueue(line);
_outputProcessEvent.Set();
}
}
Trace.Info("STDOUT/STDERR stream read finished.");
if (Interlocked.Decrement(ref _asyncStreamReaderCount) == 0 && _waitingOnStreams)
{
_processExitedCompletionSource.TrySetResult(true);
_processStandardInWriteCancellationTokenSource.Cancel();
}
});
}
19
View Source File : NamedLock.cs
License : MIT License
Project Creator : Adoxio
License : MIT License
Project Creator : Adoxio
public int Release()
{
return Interlocked.Decrement(ref _count);
}
19
View Source File : ThreadPool.cs
License : MIT License
Project Creator : adrenak
License : MIT License
Project Creator : adrenak
private void ThreadFunc() {
for (;;) {
m_semapreplaced.WaitOne();
int nCurrentPointer, nNextPointer;
do {
nCurrentPointer = m_nGetPointer;
nNextPointer = nCurrentPointer + 1;
if (nNextPointer == m_taskQueue.Length) {
nNextPointer = 0;
}
} while (Interlocked.CompareExchange(ref m_nGetPointer, nNextPointer, nCurrentPointer) != nCurrentPointer);
TaskInfo task = m_taskQueue[nCurrentPointer];
if (Interlocked.Decrement(ref m_numTasks) == m_taskQueue.Length - 1) {
m_getNotification.Set();
}
task.callback(task.args);
}
}
19
View Source File : ThreadPool.cs
License : MIT License
Project Creator : adrenak
License : MIT License
Project Creator : adrenak
private void SingleThreadFunc() {
for (;;) {
while (m_numTasks == 0) {
m_putNotification.WaitOne();
}
TaskInfo task = m_taskQueue[m_nGetPointer++];
if (m_nGetPointer == m_taskQueue.Length) {
m_nGetPointer = 0;
}
if (Interlocked.Decrement(ref m_numTasks) == m_taskQueue.Length - 1) {
m_getNotification.Set();
}
task.callback(task.args);
}
}
19
View Source File : ThreadTaskManager.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
private void ThreadExecutionLoop()
{
//Perform any initialization requested.
if (threadStart != null)
threadStart();
TaskEntry task;
while (true)
{
resetEvent.WaitOne();
while (true)
{
if (!taskData.TryDequeueFirst(out task))
{
bool gotSomething = false;
for (int i = 1; i < manager.workers.Count; i++)
{
if (TrySteal((index + i) % manager.workers.Count, out task))
{
gotSomething = true;
break;
}
}
if (!gotSomething)
break; //Nothing to steal and I'm broke! Guess I'll mosey on out
}
try
{
if (task.Task != null)
task.Task(task.Info);
if (Interlocked.Decrement(ref manager.tasksRemaining) == 0)
{
manager.allThreadsIdleNotifier.Set();
}
if (task.Task == null)
return;
}
catch (ArithmeticException arithmeticException)
{
throw new ArithmeticException(
"Some internal mulreplacedhreaded arithmetic has encountered an invalid state. Check for invalid enreplacedy momentums, velocities, and positions; propagating NaN's will generally trigger this exception in the getExtremePoint function.",
arithmeticException);
}
}
}
}
19
View Source File : SimpleLooper.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
private void ShutDownThread()
{
//Let the manager know that it is done with its 'task'!
if (Interlocked.Decrement(ref manager.tasksRemaining) == 0)
{
if (!manager.disposed) //Don't mess with the handle if it's already disposed.
manager.allThreadsIdleNotifier.Set();
}
//Dump out any remaining tasks in the queue.
for (int i = 0; i < taskQueue.Count; i++) //This is still safe since shutDownThread is called from within a lock(taskQueue) block.
{
taskQueue.Dequeue();
if (Interlocked.Decrement(ref manager.tasksRemaining) == 0)
{
if (!manager.disposed) //Don't mess with the handle if it's already disposed.
manager.allThreadsIdleNotifier.Set();
}
}
lock (manager.workers)
manager.workers.Remove(this);
}
19
View Source File : ParallelLooper.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
internal void OnWorkerFinish()
{
if (Interlocked.Decrement(ref workerCount) == 0)
loopFinished.Set();
}
19
View Source File : SimpleLooper.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
private void ThreadExecutionLoop()
{
//Perform any initialization requested.
if (threadStart != null)
threadStart(initializationInformation);
object information = null;
while (true)
{
Action<object> task = null;
lock (taskQueue)
{
if (taskQueue.Count > 0)
{
task = taskQueue.Dequeue();
if (task == null)
{
Dispose();
return;
}
information = taskInformationQueue.Dequeue();
}
}
if (task != null)
{
//Perform the task!
try
{
task(information);
}
catch (ArithmeticException arithmeticException)
{
throw new ArithmeticException(
"Some internal mulreplacedhreaded arithmetic has encountered an invalid state. Check for invalid enreplacedy momentums, velocities, and positions; propagating NaN's will generally trigger this exception in the getExtremePoint function.",
arithmeticException);
}
if (Interlocked.Decrement(ref manager.tasksRemaining) == 0)
{
manager.allThreadsIdleNotifier.Set();
resetEvent.WaitOne();
}
}
else
resetEvent.WaitOne();
}
}
19
View Source File : SimulationIsland.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
void BecameNonDeactivationCandidate(SimulationIslandMember member)
{
Interlocked.Decrement(ref deactivationCandidateCount);
}
19
View Source File : ThreadTaskManager.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
public void WaitForTaskCompletion()
{
//TODO: Try a WAITALL version of this
if (Interlocked.Decrement(ref tasksRemaining) == 0)
{
allThreadsIdleNotifier.Set();
}
allThreadsIdleNotifier.WaitOne();
//When it gets here, it means things are successfully idle'd.
tasksRemaining = 1;
allThreadsIdleNotifier.Reset();
}
19
View Source File : SimpleLooper.cs
License : The Unlicense
Project Creator : aeroson
License : The Unlicense
Project Creator : aeroson
public void WaitForTaskCompletion()
{
if (Interlocked.Decrement(ref tasksRemaining) == 0)
{
allThreadsIdleNotifier.Set();
}
allThreadsIdleNotifier.WaitOne();
//When it gets here, it means things are successfully idle'd.
tasksRemaining = 1;
allThreadsIdleNotifier.Reset();
}
19
View Source File : LogRecorder.cs
License : Mozilla Public License 2.0
Project Creator : agebullhu
License : Mozilla Public License 2.0
Project Creator : agebullhu
private static void WriteRecordLoop()
{
bool success = true;
try
{
if (Interlocked.Increment(ref BackIsRuning) > 1)
{
return;
}
SystemTrace(LogLevel.System, "日志开始");
int cnt = 0;
while (State != LogRecorderStatus.Shutdown)
{
//Thread.Sleep(10);//让子弹飞一会
if (State < LogRecorderStatus.Initialized || !BaseRecorder.IsInitialized || !Recorder.IsInitialized)
{
Thread.Sleep(50);
continue;
}
var array = RecordInfos.Switch();
if (array.Count == 0)
{
Thread.Sleep(50);
continue;
}
foreach (var info in array)
{
if (info == null)
continue;
try
{
info.Index = ++_id;
if (_id == ulong.MaxValue)
_id = 1;
if (!_isTextRecorder && (info.Type >= LogType.System || info.Local))
BaseRecorder.RecordLog(info);
if (Listener != null || TraceToConsole)
DoTrace(info);
}
catch (Exception ex)
{
SystemTrace(LogLevel.Error, "日志写入发生错误", ex);
}
}
try
{
Recorder.RecordLog(array.ToList());
}
catch (Exception ex)
{
SystemTrace(LogLevel.Error, "日志写入发生错误", ex);
}
if (++cnt < 1024)
continue;
GC.Collect();
cnt = 0;
}
_syncSlim.Release();
}
catch (Exception e)
{
success = false;
Console.WriteLine(e);
}
finally
{
Interlocked.Decrement(ref BackIsRuning);
SystemTrace(LogLevel.System, "日志结束");
}
if (!success)
NewRecorderThread();
}
19
View Source File : Tester.cs
License : Mozilla Public License 2.0
Project Creator : agebullhu
License : Mozilla Public License 2.0
Project Creator : agebullhu
void Async()
{
Interlocked.Increment(ref WaitCount);
GlobalContext.SetRequestContext(ZeroApplication.Config.ServiceName, Guid.NewGuid().ToString("N"));
DateTime s = DateTime.Now;
DoAsync();
Interlocked.Decrement(ref WaitCount);
var sp = (DateTime.Now - s);
Interlocked.Add(ref RunTime, sp.Ticks);
if (sp.TotalMilliseconds > 500)
Interlocked.Increment(ref TmError);
if ((Interlocked.Increment(ref ExCount) % 100) == 0)
Count();
}
19
View Source File : ApiStation.cs
License : Mozilla Public License 2.0
Project Creator : agebullhu
License : Mozilla Public License 2.0
Project Creator : agebullhu
private void ApiCall(ref ZSocket socket, ApiCallItem item)
{
using (IocScope.CreateScope())
{
Interlocked.Increment(ref CallCount);
try
{
if (LogRecorder.LogMonitor)
ApiCallByMonitor(ref socket, item);
else
ApiCallNoMonitor(ref socket, item);
}
catch (Exception ex)
{
ZeroTrace.WriteException(StationName, ex, "ApiCall", item.ApiName);
item.Result = ApiResult.InnerErrorJson;
SendResult(ref socket, item, ZeroOperatorStateType.Error);
}
finally
{
Interlocked.Decrement(ref waitCount);
}
}
}
19
View Source File : Tester.cs
License : Mozilla Public License 2.0
Project Creator : agebullhu
License : Mozilla Public License 2.0
Project Creator : agebullhu
void OnTestEnd()
{
if (Interlocked.Decrement(ref testerCount) == 0)
{
Cancel.Dispose();
Cancel = null;
}
}
19
View Source File : DiscordClient.WebSocket.cs
License : MIT License
Project Creator : Aiko-IT-Systems
License : MIT License
Project Creator : Aiko-IT-Systems
internal async Task OnHeartbeatAckAsync()
{
Interlocked.Decrement(ref this._skippedHeartbeats);
var ping = (int)(DateTime.Now - this._lastHeartbeat).TotalMilliseconds;
this.Logger.LogTrace(LoggerEvents.WebSocketReceive, "Received HEARTBEAT_ACK (OP11, {0}ms)", ping);
Volatile.Write(ref this._ping, ping);
var args = new HeartbeatEventArgs(this.ServiceProvider)
{
Ping = this.Ping,
Timestamp = DateTimeOffset.Now
};
await this._heartbeated.InvokeAsync(this, args).ConfigureAwait(false);
}
19
View Source File : RestClient.cs
License : MIT License
Project Creator : Aiko-IT-Systems
License : MIT License
Project Creator : Aiko-IT-Systems
private async Task ExecuteRequestAsync(BaseRestRequest request, RateLimitBucket bucket, TaskCompletionSource<bool> ratelimitTcs)
{
if (this._disposed)
return;
HttpResponseMessage res = default;
try
{
await this.GlobalRateLimitEvent.WaitAsync().ConfigureAwait(false);
if (bucket == null)
bucket = request.RateLimitBucket;
if (ratelimitTcs == null)
ratelimitTcs = await this.WaitForInitialRateLimit(bucket).ConfigureAwait(false);
if (ratelimitTcs == null) // ckeck rate limit only if we are not the probe request
{
var now = DateTimeOffset.UtcNow;
await bucket.TryResetLimitAsync(now).ConfigureAwait(false);
// Decrement the remaining number of requests as there can be other concurrent requests before this one finishes and has a chance to update the bucket
if (Interlocked.Decrement(ref bucket._remaining) < 0)
{
this.Logger.LogDebug(LoggerEvents.RatelimitDiag, "Request for {0} is blocked", bucket.ToString());
var delay = bucket.Reset - now;
var resetDate = bucket.Reset;
if (this.UseResetAfter)
{
delay = bucket.ResetAfter.Value;
resetDate = bucket.ResetAfterOffset;
}
if (delay < new TimeSpan(-TimeSpan.TicksPerMinute))
{
this.Logger.LogError(LoggerEvents.RatelimitDiag, "Failed to retrieve ratelimits - giving up and allowing next request for bucket");
bucket._remaining = 1;
}
if (delay < TimeSpan.Zero)
delay = TimeSpan.FromMilliseconds(100);
this.Logger.LogWarning(LoggerEvents.RatelimitPreemptive, "Pre-emptive ratelimit triggered - waiting until {0:yyyy-MM-dd HH:mm:ss zzz} ({1:c}).", resetDate, delay);
Task.Delay(delay)
.ContinueWith(_ => this.ExecuteRequestAsync(request, null, null))
.LogTaskFault(this.Logger, LogLevel.Error, LoggerEvents.RestError, "Error while executing request");
return;
}
this.Logger.LogDebug(LoggerEvents.RatelimitDiag, "Request for {0} is allowed", bucket.ToString());
}
else
this.Logger.LogDebug(LoggerEvents.RatelimitDiag, "Initial request for {0} is allowed", bucket.ToString());
var req = this.BuildRequest(request);
if(this.Debug)
this.Logger.LogTrace(LoggerEvents.Misc, await req.Content.ReadreplacedtringAsync());
var response = new RestResponse();
try
{
if (this._disposed)
return;
res = await this.HttpClient.SendAsync(req, HttpCompletionOption.ResponseContentRead, CancellationToken.None).ConfigureAwait(false);
var bts = await res.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var txt = Utilities.UTF8.GetString(bts, 0, bts.Length);
this.Logger.LogTrace(LoggerEvents.RestRx, txt);
response.Headers = res.Headers.ToDictionary(xh => xh.Key, xh => string.Join("\n", xh.Value), StringComparer.OrdinalIgnoreCase);
response.Response = txt;
response.ResponseCode = (int)res.StatusCode;
}
catch (HttpRequestException httpex)
{
this.Logger.LogError(LoggerEvents.RestError, httpex, "Request to {0} triggered an HttpException", request.Url);
request.SetFaulted(httpex);
this.FailInitialRateLimitTest(request, ratelimitTcs);
return;
}
this.UpdateBucket(request, response, ratelimitTcs);
Exception ex = null;
switch (response.ResponseCode)
{
case 400:
case 405:
ex = new BadRequestException(request, response);
break;
case 401:
case 403:
ex = new UnauthorizedException(request, response);
break;
case 404:
ex = new NotFoundException(request, response);
break;
case 413:
ex = new RequestSizeException(request, response);
break;
case 429:
ex = new RateLimitException(request, response);
// check the limit info and requeue
this.Handle429(response, out var wait, out var global);
if (wait != null)
{
if (global)
{
this.Logger.LogError(LoggerEvents.RatelimitHit, "Global ratelimit hit, cooling down");
try
{
this.GlobalRateLimitEvent.Reset();
await wait.ConfigureAwait(false);
}
finally
{
// we don't want to wait here until all the blocked requests have been run, additionally Set can never throw an exception that could be suppressed here
_ = this.GlobalRateLimitEvent.SetAsync();
}
this.ExecuteRequestAsync(request, bucket, ratelimitTcs)
.LogTaskFault(this.Logger, LogLevel.Error, LoggerEvents.RestError, "Error while retrying request");
}
else
{
this.Logger.LogError(LoggerEvents.RatelimitHit, "Ratelimit hit, requeueing request to {0}", request.Url);
await wait.ConfigureAwait(false);
this.ExecuteRequestAsync(request, bucket, ratelimitTcs)
.LogTaskFault(this.Logger, LogLevel.Error, LoggerEvents.RestError, "Error while retrying request");
}
return;
}
break;
case 500:
case 502:
case 503:
case 504:
ex = new ServerErrorException(request, response);
break;
}
if (ex != null)
request.SetFaulted(ex);
else
request.SetCompleted(response);
}
catch (Exception ex)
{
this.Logger.LogError(LoggerEvents.RestError, ex, "Request to {0} triggered an exception", request.Url);
// if something went wrong and we couldn't get rate limits for the first request here, allow the next request to run
if (bucket != null && ratelimitTcs != null && bucket._limitTesting != 0)
this.FailInitialRateLimitTest(request, ratelimitTcs);
if (!request.TrySetFaulted(ex))
throw;
}
finally
{
res?.Dispose();
// Get and decrement active requests in this bucket by 1.
_ = this.RequestQueue.TryGetValue(bucket.BucketId, out var count);
this.RequestQueue[bucket.BucketId] = Interlocked.Decrement(ref count);
// If it's 0 or less, we can remove the bucket from the active request queue,
// along with any of its past routes.
if (count <= 0)
{
foreach (var r in bucket.RouteHashes)
{
if (this.RequestQueue.ContainsKey(r))
{
_ = this.RequestQueue.TryRemove(r, out _);
}
}
}
}
}
19
View Source File : CooldownAttribute.cs
License : MIT License
Project Creator : Aiko-IT-Systems
License : MIT License
Project Creator : Aiko-IT-Systems
internal async Task<bool> DecrementUseAsync()
{
await this.UsageSemapreplaced.WaitAsync().ConfigureAwait(false);
// if we're past reset time...
var now = DateTimeOffset.UtcNow;
if (now >= this.ResetsAt)
{
// ...do the reset and set a new reset time
Interlocked.Exchange(ref this._remaining_uses, this.MaxUses);
this.ResetsAt = now + this.Reset;
}
// check if we have any uses left, if we do...
var success = false;
if (this.RemainingUses > 0)
{
// ...decrement, and return success...
Interlocked.Decrement(ref this._remaining_uses);
success = true;
}
// ...otherwise just fail
this.UsageSemapreplaced.Release();
return success;
}
19
View Source File : Amb.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
internal bool CheckDisposed()
{
return Interlocked.Decrement(ref _disposeWip) != 0;
}
19
View Source File : BufferBoundary.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDisposeSource()
{
if (Interlocked.Decrement(ref _sourceDisposeWip) != 0)
{
Dispose(_source);
return false;
}
return true;
}
19
View Source File : BufferBoundary.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDisposeOther()
{
if (Interlocked.Decrement(ref _otherDisposeWip) != 0)
{
Dispose(_other);
return false;
}
return true;
}
19
View Source File : CombineLatest.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDispose()
{
if (Interlocked.Decrement(ref _disposeWip) != 0)
{
_parent.Dispose(_source);
return false;
}
return true;
}
19
View Source File : ConcatMapEager.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDispose()
{
if (Interlocked.Decrement(ref _sourceDisposeWip) != 0)
{
Dispose(_source);
return false;
}
return true;
}
19
View Source File : ConcatMapEager.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void NextHandler(Task<bool> t)
{
if (t.IsCanceled)
{
ExceptionHelper.AddException(ref _error, new OperationCanceledException());
_sourceDone = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
}
} else
if (t.IsFaulted)
{
ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
_sourceDone = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
}
}
else if (t.Result)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(_sourceCTS.Token);
IAsyncEnumerator<TResult> src;
try
{
src = _mapper(_source.Current).GetAsyncEnumerator(cts.Token);
}
catch (Exception ex)
{
ExceptionHelper.AddException(ref _error, ex);
_sourceDone = true;
src = null;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
return;
}
}
if (src != null)
{
Interlocked.Increment(ref _disposeWip);
var inner = new InnerHandler(src, this, cts);
_inners.Enqueue(inner);
if (_disposeRequested)
{
while (_inners.TryDequeue(out var inner2))
{
inner2.Dispose();
}
}
if (TryDispose())
{
inner.MoveNext();
if (Interlocked.Decrement(ref _sourceOutstanding) != 0)
{
MoveNextSource();
}
ResumeHelper.Resume(ref _resume);
}
}
}
else
{
_sourceDone = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
}
}
}
19
View Source File : ConcatMapEager.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void InnerNextHandler(Task<bool> t)
{
if (t.IsCanceled)
{
ExceptionHelper.AddException(ref _parent._error, new OperationCanceledException());
Done = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _parent._resume);
}
} else if (t.IsFaulted)
{
ExceptionHelper.AddException(ref _parent._error, ExceptionHelper.Extract(t.Exception));
Done = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _parent._resume);
}
}
else if (t.Result)
{
Queue.Enqueue(_source.Current);
if (TryDispose())
{
if (Interlocked.Decrement(ref _outstanding) != 0)
{
MoveNext();
}
ResumeHelper.Resume(ref _parent._resume);
}
}
else
{
Done = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _parent._resume);
}
}
}
19
View Source File : Debounce.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDispose()
{
if (Interlocked.Decrement(ref _disposeWip) != 0)
{
if (_emitLast)
{
_emitLasreplacedem = default;
}
ResumeHelper.Complete(ref _disposeTask, _source.DisposeAsync());
return false;
}
return true;
}
19
View Source File : FlatMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void Handle(Task<bool> task)
{
if (task.IsCanceled)
{
AddException(new OperationCanceledException());
_done = true;
if (TryDispose())
{
Signal();
}
}
else if (task.IsFaulted)
{
AddException(task.Exception);
_done = true;
if (TryDispose())
{
Signal();
}
}
else
{
if (task.Result)
{
var v = _source.Current;
if (TryDispose())
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(_sourceCTS.Token);
IAsyncEnumerator<TResult> innerSource;
try
{
innerSource = _mapper(v)
.GetAsyncEnumerator(cts.Token);
}
catch (Exception ex)
{
_source.DisposeAsync();
AddException(ex);
_done = true;
Signal();
return;
}
var handler = new InnerHandler(this, innerSource, _prefetch, cts);
Interlocked.Increment(ref _allDisposeWip);
if (Add(handler))
{
handler.MoveNext();
if (Interlocked.Decrement(ref _outstanding) != 0)
{
MoveNext();
}
}
else
{
// This will decrement _allDisposeWip so
// that the DisposeAsync() can be released eventually
DisposeOne();
}
}
}
else
{
_done = true;
if (TryDispose())
{
Signal();
}
}
}
}
19
View Source File : FlatMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDispose()
{
if (Interlocked.Decrement(ref _dispose) != 0)
{
_parent.Dispose(_source);
return false;
}
return true;
}
19
View Source File : FlatMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void Handle(Task<bool> task)
{
if (task.IsCanceled)
{
if (TryDispose())
{
_parent.InnerError(this, new OperationCanceledException());
}
}
else if (task.IsFaulted)
{
if (TryDispose())
{
_parent.InnerError(this, task.Exception);
}
}
else
{
if (task.Result)
{
var v = _source.Current;
if (TryDispose())
{
_parent.InnerNext(this, v);
if (Interlocked.Decrement(ref _outstanding) != 0)
{
MoveNext();
}
}
}
else
{
if (TryDispose())
{
_parent.InnerComplete(this);
}
}
}
}
19
View Source File : GroupBy.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask DisposeAsync()
{
if (!_done)
{
for (; ; )
{
try
{
if (_groups.IsEmpty)
{
break;
}
if (await _source.MoveNextAsync())
{
var t = _source.Current;
var k = _keySelector(t);
var found = _groups.TryGetValue(k, out var g);
if (found)
{
await g.Next(_valueSelector(t));
}
}
else
{
foreach (var gr in _groups)
{
await gr.Value.Complete();
}
break;
}
}
catch (Exception ex)
{
foreach (var gr in _groups)
{
await gr.Value.Error(ex);
}
throw;
}
}
}
if (Interlocked.Decrement(ref _active) == 0)
{
await _source.DisposeAsync();
}
}
19
View Source File : GroupBy.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private async ValueTask Remove(Group g)
{
_groups.TryRemove(g.Key, out _);
if (Interlocked.Decrement(ref _active) == 0)
{
await _source.DisposeAsync();
}
}
19
View Source File : Latest.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDispose()
{
if (Interlocked.Decrement(ref _disposeWip) != 0)
{
ResumeHelper.Complete(ref _disposeTask, _source.DisposeAsync());
return false;
}
return true;
}
19
View Source File : Merge.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void InnerError(Exception ex)
{
ExceptionHelper.AddException(ref _error, ex);
Interlocked.Decrement(ref _done);
Signal();
}
19
View Source File : Merge.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void InnerComplete()
{
Interlocked.Decrement(ref _done);
Signal();
}
19
View Source File : Multicast.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void MoveNextSource()
{
if (Interlocked.Increment(ref _sourceWip) == 1)
{
do
{
if (Interlocked.Increment(ref _sourceDisposeWip) == 1)
{
_source.MoveNextAsync()
.AsTask()
.ContinueWith(HandleSourceAction, this);
}
else
{
break;
}
}
while (Interlocked.Decrement(ref _sourceWip) != 0);
}
}
19
View Source File : Multicast.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDisposeSource()
{
if (Interlocked.Decrement(ref _sourceDisposeWip) != 0)
{
Dispose(_source);
return false;
}
return true;
}
19
View Source File : Prefetch.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void SourceHandler(Task<bool> t)
{
var next = false;
if (t.IsFaulted)
{
_error = ExceptionHelper.Extract(t.Exception);
_done = true;
}
else if (t.IsCanceled)
{
_error = new OperationCanceledException();
_done = true;
}
else if (t.Result)
{
_queue.Enqueue(_source.Current);
next = true;
}
else
{
_done = true;
}
// release the MoveNext, just in case
if (Interlocked.Decrement(ref _disposeWip) != 0)
{
ResumeHelper.Complete(ref _disposeTask, _source.DisposeAsync());
}
else
{
if (next && Interlocked.Decrement(ref _outstanding) != 0)
{
MoveNext();
}
Signal();
}
}
19
View Source File : Sample.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDispose()
{
if (Interlocked.Decrement(ref _disposeWip) != 0)
{
Interlocked.Exchange(ref _timerLatest, EmptyHelper.EmptyIndicator);
ResumeHelper.Complete(ref _disposeTask, _source.DisposeAsync());
return false;
}
return true;
}
19
View Source File : CompletableConcat.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
internal override void Drain()
{
if (Interlocked.Increment(ref wip) == 1)
{
for (; ; )
{
var srcs = sources;
var n = srcs.Length;
if (DisposableHelper.IsDisposed(ref upstream))
{
for (int i = index; i < n; i++)
{
srcs[i] = null;
}
}
else
{
var i = index;
if (i == n)
{
DisposableHelper.WeakDispose(ref upstream);
var ex = Volatile.Read(ref errors);
if (ex != null)
{
downstream.OnError(ex);
}
else
{
downstream.OnCompleted();
}
} else
{
var src = srcs[i];
if (src == null)
{
DisposableHelper.WeakDispose(ref upstream);
ExceptionHelper.AddException(ref errors, new NullReferenceException("The " + i + "th ICompletableSource is null"));
var ex = Volatile.Read(ref errors);
downstream.OnError(ex);
continue;
}
else
{
srcs[i] = null;
index = i + 1;
src.Subscribe(this);
}
}
}
if (Interlocked.Decrement(ref wip) == 0)
{
break;
}
}
}
}
19
View Source File : CompletableMerge.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
void InnerCompleted(MergeInnerObserver sender)
{
Remove(sender);
Interlocked.Decrement(ref active);
Drain();
}
19
View Source File : CompletableMerge.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
void InnerError(MergeInnerObserver sender, Exception ex)
{
Remove(sender);
if (delayErrors)
{
ExceptionHelper.AddException(ref errors, ex);
Interlocked.Decrement(ref active);
Drain();
}
else
{
if (Interlocked.CompareExchange(ref errors, ex, null) == null)
{
Drain();
}
}
}
19
View Source File : CompletableMerge.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
internal void SubscribeAll(IEnumerable<ICompletableSource> sources)
{
Interlocked.Increment(ref active);
try
{
foreach (var src in sources)
{
var inner = new MergeInnerObserver(this);
if (!Add(inner))
{
break;
}
Interlocked.Increment(ref active);
src.Subscribe(inner);
}
}
catch (Exception ex)
{
if (delayErrors)
{
ExceptionHelper.AddException(ref errors, ex);
}
else
{
Interlocked.CompareExchange(ref errors, ex, null);
}
}
Interlocked.Decrement(ref active);
Drain();
}
19
View Source File : CompletableRedoObserver.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
internal void Drain()
{
if (Interlocked.Increment(ref wip) == 1)
{
for (; ; )
{
if (!DisposableHelper.IsDisposed(ref upstream))
{
source.Subscribe(this);
}
if (Interlocked.Decrement(ref wip) == 0)
{
break;
}
}
}
}
19
View Source File : CompletableRedoWhenObserver.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
internal void HandlerNext()
{
if (Interlocked.Increment(ref trampoline) == 1)
{
do
{
if (Interlocked.CompareExchange(ref once, 1, 0) == 0)
{
source.Subscribe(this);
}
}
while (Interlocked.Decrement(ref trampoline) != 0);
}
}
19
View Source File : WithLatestFrom.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private bool TryDisposeMain()
{
if (Interlocked.Decrement(ref _sourceDisposeWip) != 0)
{
Dispose(_source);
return false;
}
return true;
}
19
View Source File : SlimResumeTest.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
[Fact]
public async void Race()
{
for (var i = 0; i < 10_000; i++)
{
var rsm = new SlimResume();
var wip = 2;
var t1 = Task.Factory.StartNew(() =>
{
if (Interlocked.Decrement(ref wip) != 0)
{
while (Volatile.Read(ref wip) != 0) { }
}
rsm.Signal();
}, TaskCreationOptions.LongRunning);
var t2 = Task.Factory.StartNew(async () =>
{
if (Interlocked.Decrement(ref wip) != 0)
{
while (Volatile.Read(ref wip) != 0) { }
}
await rsm;
}, TaskCreationOptions.LongRunning);
await t1;
await t2;
}
}
See More Examples