Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Expose Interlocked.MemoryBarrierProcessWide#17512

Merged
jkotas merged 3 commits into
dotnet:masterfrom
jkotas:MemoryBarrierProcessWide
Mar 27, 2017
Merged

Expose Interlocked.MemoryBarrierProcessWide#17512
jkotas merged 3 commits into
dotnet:masterfrom
jkotas:MemoryBarrierProcessWide

Conversation

@jkotas
Copy link
Copy Markdown
Member

@jkotas jkotas commented Mar 25, 2017

@jkotas
Copy link
Copy Markdown
Member Author

jkotas commented Mar 25, 2017

Depends on dotnet/coreclr#10476

@jkotas jkotas force-pushed the MemoryBarrierProcessWide branch from 2ab1235 to 787d190 Compare March 25, 2017 01:35
{
// Taking this lock on the same thread repeatedly is very fast because of it has no interlocked operations.
// Switching the thread where the lock is taken is expensive because of allocation and FlushProcessWriteBuffers.
class AsymmetricLock
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: private

internal bool Taken;
}

LockCookie _current = new LockCookie(-1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: private


LockCookie _current = new LockCookie(-1);

// Returning LockCookie to call Exit on is the fastest implementation because of it works naturally with the RCU pattern.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: because of it => because it

private LockCookie EnterSlow()
{
// Attempt to steal the ownership. Take a regular lock to make sure that only thread is trying to steal it at a time.
lock (this)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: only thread => only one thread

@stephentoub
Copy link
Copy Markdown
Member

@dotnet-bot test this please

// we do not need here. We really just need to make sure that the compiler won't reorder the read with the above write.
// RyuJIT won't reorder them today, but more advanced optimizers might.
//
if (Volatile.Read(ref _current) == entry)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this defeating the purpose a little bit? The point of sys_membar is that you do not need to match fences on the write side with fences on the read side.
I see that it is here to force no reordering by JIT, but perhaps any other [MethodImpl(noinline)] method would achieve the same without implied fencing?

Copy link
Copy Markdown
Member Author

@jkotas jkotas Mar 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on whether on you are optimizing for x86/x64 or arm/arm64.

Volatile on x86/x64 does regular load without explicit barrier. It will be faster than the noinline variant.

Volatile on arm/arm64 has explicit barrier. Then it is about measuring whether the explicit barrier is more expensive compared to call overhead. I think they will be pretty close, but I do not have a arm/arm64 machine around to check it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this is a test, no barrier seems more appropriate.
Real implementation should take whatever is fastest, of course.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have changed it to no barrier. Thanks for the feedback.

@VSadov
Copy link
Copy Markdown
Member

VSadov commented Mar 25, 2017

LGTM

@stephentoub
Copy link
Copy Markdown
Member

@dotnet-bot test this please

@jkotas jkotas merged commit 147b103 into dotnet:master Mar 27, 2017
@jkotas jkotas deleted the MemoryBarrierProcessWide branch March 27, 2017 17:19
ViktorHofer pushed a commit to ViktorHofer/corefx that referenced this pull request Mar 28, 2017
@karelz karelz modified the milestone: 2.0.0 Mar 31, 2017
dotnet-bot pushed a commit that referenced this pull request Apr 13, 2018
)

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
ahsonkhan pushed a commit that referenced this pull request Apr 13, 2018
) (#29082)

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants