@@ -653,30 +653,25 @@ private void Release(bool exclusive) {
653653 }
654654 }
655655
656- #if ! NET5_0_OR_GREATER
657- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
658- private static int InterlockedOr ( ref int location1 , int value ) {
659- int current = location1 ;
656+ private int InterlockedOrState ( int value ) {
657+ #if NET5_0_OR_GREATER
658+ return Interlocked . Or ( ref _state , value ) ;
659+ #else
660+ int current = _state ;
660661 while ( true ) {
661662 int newValue = current | value ;
662- int oldValue = Interlocked . CompareExchange ( ref location1 , newValue , current ) ;
663+ int oldValue = Interlocked . CompareExchange ( ref _state , newValue , current ) ;
663664 if ( oldValue == current ) {
664665 return oldValue ;
665666 }
666667 current = oldValue ;
667668 }
668- }
669669#endif
670+ }
670671
671672 public void close ( ) {
672673 // close is idempotent; it must never block
673- #if NET5_0_OR_GREATER
674- if ( ( Interlocked . Or ( ref _state , StateBits . Closed ) & StateBits . Closed ) != StateBits . Closed ) {
675- #else
676- #pragma warning disable CS0420 // A reference to a volatile field will not be treated as volatile
677- if ( ( InterlockedOr ( ref _state , StateBits . Closed ) & StateBits . Closed ) != StateBits . Closed ) {
678- #pragma warning restore CS0420 // A reference to a volatile field will not be treated as volatile
679- #endif
674+ if ( ( InterlockedOrState ( StateBits . Closed ) & StateBits . Closed ) != StateBits . Closed ) {
680675 // freshly closed, release the construction time reference
681676 Release ( exclusive : false ) ;
682677 }
@@ -1332,13 +1327,7 @@ public MmapBuffer(MmapDefault mmap, BufferFlags flags) {
13321327 _mmap = mmap ;
13331328 _flags = flags ;
13341329 _locker = new MmapLocker ( mmap ) ;
1335- #if NET5_0_OR_GREATER
1336- Interlocked . Or ( ref mmap . _state , StateBits . Exporting ) ;
1337- #else
1338- #pragma warning disable CS0420 // A reference to a volatile field will not be treated as volatile
1339- InterlockedOr ( ref mmap . _state , StateBits . Exporting ) ;
1340- #pragma warning restore CS0420 // A reference to a volatile field will not be treated as volatile
1341- #endif
1330+ mmap . InterlockedOrState ( StateBits . Exporting ) ;
13421331 _handle = _mmap . _view . SafeMemoryMappedViewHandle ;
13431332 ItemCount = _mmap . __len__ ( ) is int i ? i : throw new NotImplementedException ( ) ;
13441333 }
0 commit comments