Skip to content

Commit

Permalink
Add Interlocked unsigned and bitwise operations (#32216)
Browse files Browse the repository at this point in the history
* Add Interlocked unsigned and bitwise operations

* Address PR feedback

* Delete dead code for internal CompareExchange(..., ref bool)

* Remove Xor
  • Loading branch information
stephentoub authored Feb 14, 2020
1 parent dcedd95 commit 4107a4c
Show file tree
Hide file tree
Showing 18 changed files with 818 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
<Compile Include="$(BclSourcesRoot)\System\Threading\ClrThreadPoolBoundHandle.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\ClrThreadPoolBoundHandleOverlapped.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\ClrThreadPoolPreAllocatedOverlapped.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Interlocked.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Interlocked.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Monitor.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Overlapped.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\StackCrawlMark.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28902.138
Expand All @@ -9,6 +8,7 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Private.CoreLib.Shar
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\..\libraries\System.Private.CoreLib\src\System.Private.CoreLib.Shared.projitems*{3da06c3a-2e7b-4cb7-80ed-9b12916013f9}*SharedItemsImports = 5
..\..\..\libraries\System.Private.CoreLib\src\System.Private.CoreLib.Shared.projitems*{845c8b26-350b-4e63-bd11-2c8150444e28}*SharedItemsImports = 13
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

Large diffs are not rendered by default.

This file was deleted.

16 changes: 0 additions & 16 deletions src/coreclr/src/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,22 +1648,6 @@ FCIMPL3(INT32, COMInterlocked::CompareExchange, INT32* location, INT32 value, IN
}
FCIMPLEND

FCIMPL4(INT32, COMInterlocked::CompareExchangeReliableResult, INT32* location, INT32 value, INT32 comparand, CLR_BOOL* succeeded)
{
FCALL_CONTRACT;

if( NULL == location) {
FCThrow(kNullReferenceException);
}

INT32 result = FastInterlockCompareExchange((LONG*)location, value, comparand);
if (result == comparand)
*succeeded = true;

return result;
}
FCIMPLEND

FCIMPL3_IVV(INT64, COMInterlocked::CompareExchange64, INT64* location, INT64 value, INT64 comparand)
{
FCALL_CONTRACT;
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/src/vm/comutilnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ class COMInterlocked
static FCDECL2_IV(INT64, Exchange64, INT64 *location, INT64 value);
static FCDECL2(LPVOID, ExchangePointer, LPVOID* location, LPVOID value);
static FCDECL3(INT32, CompareExchange, INT32* location, INT32 value, INT32 comparand);
static FCDECL4(INT32, CompareExchangeReliableResult, INT32* location, INT32 value, INT32 comparand, CLR_BOOL* succeeded);
static FCDECL3_IVV(INT64, CompareExchange64, INT64* location, INT64 value, INT64 comparand);
static FCDECL3(LPVOID, CompareExchangePointer, LPVOID* location, LPVOID value, LPVOID comparand);
static FCDECL2_IV(float, ExchangeFloat, float *location, float value);
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/src/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ FCFuncStart(gInterlockedFuncs)
FCFuncElementSig("CompareExchange", &gsig_SM_RefDbl_Dbl_Dbl_RetDbl, COMInterlocked::CompareExchangeDouble)
FCFuncElementSig("CompareExchange", &gsig_SM_RefFlt_Flt_Flt_RetFlt, COMInterlocked::CompareExchangeFloat)
FCFuncElementSig("CompareExchange", &gsig_SM_RefObj_Obj_Obj_RetObj, COMInterlocked::CompareExchangeObject)
FCFuncElementSig("CompareExchange", &gsig_SM_RefInt_Int_Int_RefBool_RetInt, COMInterlocked::CompareExchangeReliableResult)
FCFuncElementSig("CompareExchange", &gsig_SM_RefIntPtr_IntPtr_IntPtr_RetIntPtr, COMInterlocked::CompareExchangePointer)
FCIntrinsicSig("ExchangeAdd", &gsig_SM_RefInt_Int_RetInt, COMInterlocked::ExchangeAdd32, CORINFO_INTRINSIC_InterlockedXAdd32)
FCIntrinsicSig("ExchangeAdd", &gsig_SM_RefLong_Long_RetLong, COMInterlocked::ExchangeAdd64, CORINFO_INTRINSIC_InterlockedXAdd64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public bool this[int bit]
get => (Volatile.Read(ref _data) & bit) == bit;
set
{
while (true)
if (value)
{
int oldValue = _data;
int newValue = value ? oldValue | bit : oldValue &= ~bit;
if (Interlocked.CompareExchange(ref _data, newValue, oldValue) == oldValue)
{
break;
}
Interlocked.Or(ref _data, bit);
}
else
{
Interlocked.And(ref _data, ~bit);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\ExecutionContext.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\IOCompletionCallback.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\IThreadPoolWorkItem.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\Interlocked.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LazyInitializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LazyThreadSafetyMode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\LockRecursionException.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,7 @@ internal static int RoundUpToPowerOf2(int i)
if (!_frozenForEnqueues) // flag used to ensure we don't increase the Tail more than once if frozen more than once
{
_frozenForEnqueues = true;

// Increase the tail by FreezeOffset, spinning until we're successful in doing so.
int tail = _headAndTail.Tail;
while (true)
{
int oldTail = Interlocked.CompareExchange(ref _headAndTail.Tail, tail + FreezeOffset, tail);
if (oldTail == tail)
{
break;
}
tail = oldTail;
}
Interlocked.Add(ref _headAndTail.Tail, FreezeOffset);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,8 @@ public void SetHandleAsInvalid()
{
Debug.Assert(_fullyInitialized);

// Attempt to set closed state (low order bit of the _state field).
// Might have to attempt these repeatedly, if the operation suffers
// interference from an AddRef or Release.
int oldState, newState;
do
{
oldState = _state;
newState = oldState | StateBits.Closed;
} while (Interlocked.CompareExchange(ref _state, newState, oldState) != oldState);
// Set closed state (low order bit of the _state field).
Interlocked.Or(ref _state, StateBits.Closed);

GC.SuppressFinalize(this);
}
Expand Down
Loading

0 comments on commit 4107a4c

Please sign in to comment.