1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
- using System . IO ;
5
- using System . IO . Pipelines ;
6
4
using System . Runtime . CompilerServices ;
7
5
using System . Text ;
8
6
using System . Threading ;
9
7
using System . Threading . Channels ;
10
8
using System . Threading . Tasks ;
11
- using Pipelines . Sockets . Unofficial ;
12
9
using Pipelines . Sockets . Unofficial . Threading ;
13
10
using static Pipelines . Sockets . Unofficial . Threading . MutexSlim ;
14
11
using static StackExchange . Redis . ConnectionMultiplexer ;
@@ -56,9 +53,7 @@ public PhysicalBridge(ServerEndPoint serverEndPoint, ConnectionType type, int ti
56
53
Name = Format . ToString ( serverEndPoint . EndPoint ) + "/" + ConnectionType . ToString ( ) ;
57
54
TimeoutMilliseconds = timeoutMilliseconds ;
58
55
_singleWriterMutex = new MutexSlim ( timeoutMilliseconds : timeoutMilliseconds ) ;
59
- _weakRefThis = new WeakReference ( this ) ;
60
56
}
61
- private readonly WeakReference _weakRefThis ;
62
57
63
58
private readonly int TimeoutMilliseconds ;
64
59
@@ -771,22 +766,15 @@ private bool PushToBacklog(Message message, bool onlyIfExists)
771
766
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
772
767
private void StartBacklogProcessor ( )
773
768
{
774
- var sched = Multiplexer . SocketManager ? . Scheduler ?? PipeScheduler . ThreadPool ;
775
769
#if DEBUG
776
770
_backlogProcessorRequestedTime = Environment . TickCount ;
777
771
#endif
778
- sched . Schedule ( s_ProcessBacklog , _weakRefThis ) ;
772
+ Task . Run ( ProcessBacklogAsync ) ;
779
773
}
780
774
#if DEBUG
781
775
private volatile int _backlogProcessorRequestedTime ;
782
776
#endif
783
777
784
- private static readonly Action < object > s_ProcessBacklog = s =>
785
- {
786
- var wr = ( WeakReference ) s ;
787
- if ( wr . Target is PhysicalBridge bridge ) bridge . ProcessBacklog ( ) ;
788
- } ;
789
-
790
778
private void CheckBacklogForTimeouts ( ) // check the head of the backlog queue, consuming anything that looks dead
791
779
{
792
780
lock ( _backlog )
@@ -810,6 +798,7 @@ private void CheckBacklogForTimeouts() // check the head of the backlog queue, c
810
798
internal enum BacklogStatus : byte
811
799
{
812
800
Inactive ,
801
+ Starting ,
813
802
Started ,
814
803
CheckingForWork ,
815
804
CheckingForTimeout ,
@@ -823,7 +812,7 @@ internal enum BacklogStatus : byte
823
812
Faulted ,
824
813
}
825
814
private volatile BacklogStatus _backlogStatus ;
826
- private void ProcessBacklog ( )
815
+ private async Task ProcessBacklogAsync ( )
827
816
{
828
817
LockToken token = default ;
829
818
try
@@ -833,12 +822,19 @@ private void ProcessBacklog()
833
822
var msToStartWorker = unchecked ( tryToAcquireTime - _backlogProcessorRequestedTime ) ;
834
823
int failureCount = 0 ;
835
824
#endif
836
- while ( true )
825
+ _backlogStatus = BacklogStatus . Starting ;
826
+ while ( true )
837
827
{
838
- // try and get the lock; if unsuccessful, check for termination
839
- token = _singleWriterMutex . TryWait ( ) ;
840
- if ( token ) break ; // got the lock
841
- lock ( _backlog ) { if ( _backlog . Count == 0 ) return ; }
828
+ // check whether the backlog is empty *before* even trying to get the lock
829
+ lock ( _backlog )
830
+ {
831
+ if ( _backlog . Count == 0 ) return ; // nothing to do
832
+ }
833
+
834
+ // try and get the lock; if unsuccessful, retry
835
+ token = await _singleWriterMutex . TryWaitAsync ( ) . ConfigureAwait ( false ) ;
836
+ if ( token . Success ) break ; // got the lock; now go do something with it
837
+
842
838
#if DEBUG
843
839
failureCount ++ ;
844
840
#endif
@@ -887,9 +883,7 @@ private void ProcessBacklog()
887
883
if ( result == WriteResult . Success )
888
884
{
889
885
_backlogStatus = BacklogStatus . Flushing ;
890
- #pragma warning disable CS0618
891
- result = physical . FlushSync ( false , timeout ) ;
892
- #pragma warning restore CS0618
886
+ result = await physical . FlushAsync ( false ) . ConfigureAwait ( false ) ;
893
887
}
894
888
895
889
_backlogStatus = BacklogStatus . MarkingInactive ;
0 commit comments