This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
spinlock for GC #1447
Merged
Merged
spinlock for GC #1447
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* SpinLock for runtime internal usage. | ||
* | ||
* Copyright: Copyright Digital Mars 2015 -. | ||
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0). | ||
* Authors: Martin Nowak | ||
* Source: $(DRUNTIMESRC core/internal/_spinlock.d) | ||
*/ | ||
module core.internal.spinlock; | ||
|
||
import core.atomic, core.thread; | ||
|
||
shared struct SpinLock | ||
{ | ||
/// for how long is the lock usually contended | ||
enum Contention : ubyte | ||
{ | ||
brief, | ||
medium, | ||
lengthy, | ||
} | ||
|
||
@trusted @nogc nothrow: | ||
this(Contention contention) | ||
{ | ||
this.contention = contention; | ||
} | ||
|
||
void lock() | ||
{ | ||
if (cas(&val, size_t(0), size_t(1))) | ||
return; | ||
// Try to reduce the chance of another cas failure | ||
// TTAS lock (https://en.wikipedia.org/wiki/Test_and_test-and-set) | ||
immutable step = 1 << contention; | ||
while (true) | ||
{ | ||
for (size_t n; atomicLoad!(MemoryOrder.raw)(val); n += step) | ||
yield(n); | ||
if (cas(&val, size_t(0), size_t(1))) | ||
return; | ||
} | ||
} | ||
|
||
void unlock() | ||
{ | ||
atomicStore!(MemoryOrder.rel)(val, size_t(0)); | ||
} | ||
|
||
/// yield with backoff | ||
void yield(size_t k) | ||
{ | ||
if (k < pauseThresh) | ||
return pause(); | ||
else if (k < 32) | ||
return Thread.yield(); | ||
Thread.sleep(1.msecs); | ||
} | ||
|
||
private: | ||
version (D_InlineAsm_X86) | ||
enum X86 = true; | ||
else version (D_InlineAsm_X86_64) | ||
enum X86 = true; | ||
else | ||
enum X86 = false; | ||
|
||
static if (X86) | ||
{ | ||
enum pauseThresh = 16; | ||
void pause() | ||
{ | ||
asm @trusted @nogc nothrow | ||
{ | ||
// pause instruction | ||
rep; | ||
nop; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
enum pauseThresh = 4; | ||
void pause() | ||
{ | ||
} | ||
} | ||
|
||
size_t val; | ||
Contention contention; | ||
} | ||
|
||
// aligned to cacheline to avoid false sharing | ||
shared align(64) struct AlignedSpinLock | ||
{ | ||
this(SpinLock.Contention contention) | ||
{ | ||
impl = shared(SpinLock)(contention); | ||
} | ||
|
||
SpinLock impl; | ||
alias impl this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think restructuring this function as a do/while so there's only one cas() might generate tighter code. But not urgent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to avoid accessing the
contention
field.