Skip to content
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

Unused Field #53

Open
QiAnXinCodeSafe opened this issue Nov 19, 2020 · 1 comment
Open

Unused Field #53

QiAnXinCodeSafe opened this issue Nov 19, 2020 · 1 comment

Comments

@QiAnXinCodeSafe
Copy link

private volatile long lock;

This field is never used.

@tuannh982
Copy link

The lock field is used for re-entrance lock. lock value is the the thread_id that currently acquire the current Segment instance, which is updated by the lockFieldUpdater

private static final AtomicLongFieldUpdater<Segment> lockFieldUpdater =
AtomicLongFieldUpdater.newUpdater(Segment.class, "lock");

boolean lock() {
long t = Thread.currentThread().getId();
if (t == lockFieldUpdater.get(this)) {
return false;
}
while (true) {
if (lockFieldUpdater.compareAndSet(this, 0L, t)) {
return true;
}
// yield control to other thread.
// Note: we cannot use LockSupport.parkNanos() as that does not
// provide nanosecond resolution on Windows.
Thread.yield();
}
}
void unlock(boolean wasFirst) {
if (!wasFirst) {
return;
}
long t = Thread.currentThread().getId();
boolean r = lockFieldUpdater.compareAndSet(this, t, 0L);
assert r;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants