-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
47 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2025-present, pull-vert and Jayo contributors. | ||
* Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package jayo.internal; | ||
|
||
import jayo.tools.BasicLock; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.VarHandle; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
public final class RealBasicLock implements BasicLock { | ||
@SuppressWarnings("FieldMayBeFinal") | ||
private volatile @Nullable Thread lockingThread = null; | ||
|
||
// VarHandle mechanics | ||
private static final VarHandle LOCKING_THREAD; | ||
|
||
static { | ||
try { | ||
final var l = MethodHandles.lookup(); | ||
LOCKING_THREAD = l.findVarHandle(RealBasicLock.class, "lockingThread", Thread.class); | ||
} catch (ReflectiveOperationException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void lock() { | ||
final var lockingThread = (Thread) LOCKING_THREAD.getAndSetRelease(this, Thread.currentThread()); | ||
if (lockingThread != null) { | ||
LockSupport.park(); | ||
} | ||
} | ||
|
||
@Override | ||
public void unlock() { | ||
final var currentThread = Thread.currentThread(); | ||
final var lockingThread = (Thread) LOCKING_THREAD.compareAndExchangeRelease(this, currentThread, null); | ||
if (lockingThread != currentThread) { | ||
LockSupport.unpark(lockingThread); | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,44 @@ | ||
/* | ||
* Copyright (c) 2025-present, pull-vert and Jayo contributors. | ||
* Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package jayo.tools; | ||
|
||
import jayo.internal.RealBasicLock; | ||
import org.jspecify.annotations.NonNull; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
/** | ||
* A basic lock that only supports {@link #lock()} and {@link #unlock()}. It is not reentrant and only support 2 | ||
* concurrent threads. | ||
*/ | ||
public interface BasicLock extends Lock { | ||
static BasicLock create() { | ||
return new RealBasicLock(); | ||
} | ||
|
||
@Override | ||
default void lockInterruptibly() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
default boolean tryLock() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
default boolean tryLock(long time, @NonNull TimeUnit unit) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
default Condition newCondition() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |