Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new lock type in TimerQueue #103104

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/libraries/System.Private.CoreLib/src/System/Threading/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ private bool EnsureTimerFiresBy(uint requestedDuration)
// need to look at the long list because the current time will be <= _currentAbsoluteThreshold.
private const int ShortTimersThresholdMilliseconds = 333;

// Lock shared by the TimerQueue instances
eduardo-vp marked this conversation as resolved.
Show resolved Hide resolved
internal Lock SharedLock { get; } = new Lock();

// Fire any timers that have expired, and update the native timer to schedule the rest of them.
// We're in a thread pool work item here, and if there are multiple timers to be fired, we want
// to queue all but the first one. The first may can then be invoked synchronously or queued,
Expand All @@ -181,7 +184,7 @@ private void FireNextTimers()
// are queued to the ThreadPool.
TimerQueueTimer? timerToFireOnThisThread = null;

lock (this)
lock (SharedLock)
{
// Since we got here, that means our previous timer has fired.
_isTimerScheduled = false;
Expand Down Expand Up @@ -540,7 +543,7 @@ internal bool Change(uint dueTime, uint period)
{
bool success;

lock (_associatedTimerQueue)
lock (_associatedTimerQueue.SharedLock)
{
if (_canceled)
{
Expand All @@ -567,7 +570,7 @@ internal bool Change(uint dueTime, uint period)

public void Dispose()
{
lock (_associatedTimerQueue)
lock (_associatedTimerQueue.SharedLock)
{
if (!_canceled)
{
Expand All @@ -584,7 +587,7 @@ public bool Dispose(WaitHandle toSignal)
bool success;
bool shouldSignal = false;

lock (_associatedTimerQueue)
lock (_associatedTimerQueue.SharedLock)
{
if (_canceled)
{
Expand All @@ -608,7 +611,7 @@ public bool Dispose(WaitHandle toSignal)

public ValueTask DisposeAsync()
{
lock (_associatedTimerQueue)
lock (_associatedTimerQueue.SharedLock)
{
object? notifyWhenNoCallbacksRunning = _notifyWhenNoCallbacksRunning;

Expand Down Expand Up @@ -668,9 +671,9 @@ public ValueTask DisposeAsync()

internal void Fire(bool isThreadPool = false)
{
bool canceled = false;
bool canceled;

lock (_associatedTimerQueue)
lock (_associatedTimerQueue.SharedLock)
{
canceled = _canceled;
if (!canceled)
Expand All @@ -683,7 +686,7 @@ internal void Fire(bool isThreadPool = false)
CallCallback(isThreadPool);

bool shouldSignal;
lock (_associatedTimerQueue)
lock (_associatedTimerQueue.SharedLock)
{
_callbacksRunning--;
shouldSignal = _canceled && _callbacksRunning == 0 && _notifyWhenNoCallbacksRunning != null;
Expand Down Expand Up @@ -943,7 +946,7 @@ public static long ActiveCount
long count = 0;
foreach (TimerQueue queue in TimerQueue.Instances)
{
lock (queue)
lock (queue.SharedLock)
{
count += queue.ActiveCount;
}
Expand Down
Loading