Skip to content

Commit

Permalink
Use -1 to report success and allow using element #1 in the SyncTable
Browse files Browse the repository at this point in the history
  • Loading branch information
VSadov committed Dec 15, 2022
1 parent 367acbb commit 43a354d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private static void MonitorEnter(object obj, ref bool lockTaken)
{
// Inlined Monitor.Enter with a few tweaks
int resultOrIndex = ObjectHeader.Acquire(obj);
if (resultOrIndex == 1)
if (resultOrIndex < 0)
{
lockTaken = true;
return;
Expand Down Expand Up @@ -49,7 +49,7 @@ private static void MonitorEnterStatic(IntPtr pEEType, ref bool lockTaken)
// Inlined Monitor.Enter with a few tweaks
object obj = GetStaticLockObject(pEEType);
int resultOrIndex = ObjectHeader.Acquire(obj);
if (resultOrIndex == 1)
if (resultOrIndex < 0)
{
lockTaken = true;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static Condition GetCondition(object obj)
public static void Enter(object obj)
{
int resultOrIndex = ObjectHeader.Acquire(obj);
if (resultOrIndex == 1)
if (resultOrIndex < 0)
return;

Lock lck = resultOrIndex == 0 ?
Expand Down Expand Up @@ -70,7 +70,7 @@ public static void Enter(object obj, ref bool lockTaken)
public static bool TryEnter(object obj)
{
int resultOrIndex = ObjectHeader.TryAcquire(obj);
if (resultOrIndex == 1)
if (resultOrIndex < 0)
return true;

if (resultOrIndex == 0)
Expand All @@ -97,7 +97,7 @@ public static bool TryEnter(object obj, int millisecondsTimeout)
throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);

int resultOrIndex = ObjectHeader.TryAcquire(obj);
if (resultOrIndex == 1)
if (resultOrIndex < 0)
return true;

Lock lck = resultOrIndex == 0 ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ public static unsafe void SetSyncEntryIndex(int* pHeader, int syncIndex)
//

// Returs:
// 1 - success
// 0 - failed
// syncIndex - retry with the Lock
// -1 - success
// 0 - failed
// syncIndex - retry with the Lock
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int Acquire(object obj)
{
return TryAcquire(obj, oneShot: false);
}

// 1 - success
// -1 - success
// 0 - failed
// syncIndex - retry with the Lock
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -286,14 +286,14 @@ public static unsafe int TryAcquire(object obj, bool oneShot = true)
{
if (Interlocked.CompareExchange(ref *pHeader, oldBits | currentThreadID, oldBits) == oldBits)
{
return 1;
return -1;
}
}
else if (GetSyncEntryIndex(oldBits, out int syncIndex))
{
if (SyncTable.GetLockObject(syncIndex).TryAcquireOneShot(currentThreadID))
{
return 1;
return -1;
}

// has sync entry -> slow path
Expand All @@ -306,7 +306,7 @@ public static unsafe int TryAcquire(object obj, bool oneShot = true)
}

// handling uncommon cases here - recursive lock, contention, retries
// 1 - success
// -1 - success
// 0 - failed
// syncIndex - retry with the Lock
private static unsafe int TryAcquireUncommon(object obj, bool oneShot)
Expand Down Expand Up @@ -341,7 +341,7 @@ private static unsafe int TryAcquireUncommon(object obj, bool oneShot)
int newBits = oldBits | currentThreadID;
if (Interlocked.CompareExchange(ref *pHeader, newBits, oldBits) == oldBits)
{
return 1;
return -1;
}

// contention on a lock that noone owned,
Expand Down Expand Up @@ -370,7 +370,7 @@ private static unsafe int TryAcquireUncommon(object obj, bool oneShot)
{
if (Interlocked.CompareExchange(ref *pHeader, newBits, oldBits) == oldBits)
{
return 1;
return -1;
}

// rare contention on owned lock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static class SyncTable
/// </summary>
#if DEBUG
// Exercise table expansion more frequently in debug builds
private const int InitialSize = 2;
private const int InitialSize = 1;
#else
private const int InitialSize = 1 << 7;
#endif
Expand All @@ -78,11 +78,11 @@ internal static class SyncTable
private static int s_freeEntryList;

/// <summary>
/// The index of the lowest never used entry. We skip the 0th and 1st entries and start with 2.
/// The index of the lowest never used entry. We skip the 0th entry and start with 1.
/// If all entries have been used, s_unusedEntryIndex == s_entries.Length. This counter
/// never decreases.
/// </summary>
private static int s_unusedEntryIndex = 2;
private static int s_unusedEntryIndex = 1;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ref Entry UnsafeEntryRef(int i)
Expand Down

0 comments on commit 43a354d

Please sign in to comment.