-
Notifications
You must be signed in to change notification settings - Fork 31
Implement proper thread-safety for concurrent ObjectMarker #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jasonk000
wants to merge
3
commits into
master
Choose a base branch
from
jkoch/object-marker-concurrency
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
216 changes: 216 additions & 0 deletions
216
plugins/org.eclipse.mat.report/src/org/eclipse/mat/collect/ConcurrentBitField.java
This file contains hidden or 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,216 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2008, 2024 SAP AG, IBM Corporation and others. | ||
| * All rights reserved. This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Jason Koch (Netflix, Inc) - implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.mat.collect; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicLongArray; | ||
|
|
||
| /** | ||
| * This class manages huge bit fields. It is much faster than | ||
| * {@link java.util.BitSet} and was specifically developed to be used with huge | ||
| * bit sets in ISnapshot (e.g. needed in virtual GC traces). Out of performance | ||
| * reasons no method does any parameter checking, i.e. only valid values are | ||
| * expected. This is a fully thread-safe/concurrent implementation. | ||
| */ | ||
| public final class ConcurrentBitField | ||
| { | ||
|
|
||
| private final AtomicLongArray bits; | ||
| private final int size; | ||
|
|
||
| private static final int SHIFT = 0x6; | ||
| private static final int MASK = 0x3f; | ||
|
|
||
| /** | ||
| * Creates a bit field with the given number of bits. Size is expected to be | ||
| * positive. | ||
| * @param size the maximum size of the BitField | ||
| */ | ||
| public ConcurrentBitField(int size) | ||
| { | ||
| if (size <= 0) | ||
| { | ||
| throw new IllegalArgumentException("size must be > 0"); | ||
| } | ||
| this.size = size; | ||
| this.bits = new AtomicLongArray((((size) - 1) >>> SHIFT) + 1); | ||
| } | ||
|
|
||
| public ConcurrentBitField(boolean[] bits) | ||
| { | ||
| if (bits.length == 0) | ||
| { | ||
| throw new IllegalArgumentException("bits must have at least one element"); | ||
| } | ||
| this.size = bits.length; | ||
| this.bits = new AtomicLongArray((((size) - 1) >>> SHIFT) + 1); | ||
|
|
||
| for (int i = 0; i < size; i++) | ||
| { | ||
| if (bits[i]) | ||
| set(i); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the bit on the given index. Index is expected to be in range - out | ||
| * of performance reasons no checks are done! | ||
| * @param index The 0-based index into the BitField. | ||
| */ | ||
| public final void set(int index) | ||
| { | ||
| final int slot = index >>> SHIFT; | ||
| final long flag = (1L << (index & MASK)); | ||
|
|
||
| while (true) | ||
| { | ||
| final long existing = bits.get(slot); | ||
| final long next = existing | flag; | ||
| if (next == existing) | ||
| { return; } | ||
|
|
||
| if (bits.compareAndSet(slot, existing, next)) | ||
| { return; } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clears the bit on the given index. Index is expected to be in range - out | ||
| * of performance reasons no checks are done! | ||
| * @param index The 0-based index into the BitField. | ||
| */ | ||
| public final void clear(int index) | ||
| { | ||
| final int slot = index >>> SHIFT; | ||
| final long flag = (1L << (index & MASK)); | ||
|
|
||
| while (true) | ||
| { | ||
| final long existing = bits.get(slot); | ||
| final long next = existing & (~flag); | ||
| if (next == existing) | ||
| { return; } | ||
|
|
||
| if (bits.compareAndSet(slot, existing, next)) | ||
| { return; } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compare and set the value atomically. NB multiple underlying CAS | ||
| * might be competing, but only once ever for the same bit. | ||
| * @param index | ||
| * @return true if successful. False return indicates that the actual value | ||
| * was not equal to the expected value. | ||
| */ | ||
| public final boolean compareAndSet(int index, boolean expectedValue, boolean newValue) { | ||
| int slot = index >>> SHIFT; | ||
| long flag = (1L << (index & MASK)); | ||
|
|
||
| // We need to do a two-pass here | ||
| // Load the value, then update the mask, and if then attempt a CAS | ||
| // there are two possibilities for CAS failure: | ||
| // (1) someone changed the flag we are interested in | ||
| // (2) someone changed a different flag in the block | ||
| // Therefore, we need to use full compare and exchange rather than | ||
| // compare and set. | ||
|
|
||
| while (true) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appreciate any extra eyes on this!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A colleague suggested adding a jcstress, did so in 9499a38. |
||
| { | ||
| final long existing = bits.get(slot); | ||
| final boolean currentBit = (existing & flag) != 0; | ||
|
|
||
| // expected bit does not match | ||
| if (currentBit != expectedValue) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| final long nextValue = newValue | ||
| ? (existing | flag) | ||
| : (existing & ~flag); | ||
|
|
||
| // we know that expected matches, and now next matches, then done | ||
| if (nextValue == existing) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| final long witness = bits.compareAndExchange(slot, existing, nextValue); | ||
|
|
||
| // cas succeeded | ||
| if (witness == existing) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // cas failed, but why? | ||
| // check the returned value, and, if it was changed by someone else | ||
| // the CAS fails | ||
| boolean witnessBit = (witness & flag) != 0L; | ||
| if (witnessBit != expectedValue) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // otherwise, we know that the bit was OK but some other bits in the | ||
| // slot changed we can safely retry | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the bit on the given index. Index is expected to be in range - out | ||
| * of performance reasons no checks are done! | ||
| * @param index The 0-based index into the BitField. | ||
| * @return true if the BitField was set, false if it was cleared or never set. | ||
| */ | ||
| public final boolean get(int index) { | ||
| final int slot = index >>> SHIFT; | ||
| final long flag = (1L << (index & MASK)); | ||
|
|
||
| final long existing = bits.get(slot); | ||
| return (existing & flag) != 0; | ||
| } | ||
|
|
||
| /** | ||
| * The size of the bitfield. | ||
| * @return | ||
| */ | ||
| public final int size() { | ||
| return this.size; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the full array. Note that this is _not_ a thread-safe snapshot. | ||
| * @return | ||
| */ | ||
| public final boolean[] toBooleanArrayNonAtomic() { | ||
| final boolean[] result = new boolean[size]; | ||
| intoBooleanArrayNonAtomic(result); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the full array. Note that this is _not_ a thread-safe snapshot. | ||
| * @param output array to fill | ||
| */ | ||
| public final void intoBooleanArrayNonAtomic(boolean[] output) { | ||
| if (output.length != size) | ||
| { | ||
| throw new IllegalArgumentException("output length must match bitset length"); | ||
| } | ||
| for (int i = 0; i < size; i++) | ||
| { | ||
| output[i] = get(i); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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.
This was required to support compareAndExchange. If we cannot do xchg, it can be done with compareAndSet, but involves additional CAS operations.