Interfaces and the foundation for a ReentrantLock implementation

This commit is contained in:
Mike Jensen 2014-03-03 16:04:56 -07:00
parent b5dd74c3d8
commit 7dd799476a
4 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,24 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent.locks;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public interface Condition {
public void await();
public boolean await(long time, TimeUnit unit);
public long awaitNanos(long nanosTimeout);
public void awaitUninterruptibly();
public boolean awaitUntil(Date deadline);
public void signal();
public void signalAll();
}

View File

@ -0,0 +1,22 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent.locks;
import java.util.concurrent.TimeUnit;
public interface Lock {
public void lock();
public void lockInterruptibly() throws InterruptedException;
public Condition newCondition();
public boolean tryLock();
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
public void unlock();
}

View File

@ -0,0 +1,89 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent.locks;
import sun.misc.Unsafe;
public class LockSupport {
private LockSupport() {
// can't construct
}
private static final Unsafe unsafe;
private static final long parkBlockerOffset;
static {
unsafe = Unsafe.getUnsafe();
try {
parkBlockerOffset = unsafe.objectFieldOffset(java.lang.Thread.class.getDeclaredField("parkBlocker"));
} catch (Exception ex) {
throw new Error(ex);
}
}
private static void setBlocker(Thread t, Object arg) {
unsafe.putObject(t, parkBlockerOffset, arg);
}
public static void unpark(Thread thread) {
if (thread != null) {
unsafe.unpark(thread);
}
}
public static void park(Object blocker) {
doParkNanos(blocker, 0L);
}
public static void parkNanos(Object blocker, long nanos) {
if (nanos <= 0) {
return;
}
doParkNanos(blocker, nanos);
}
private static void doParkNanos(Object blocker, long nanos) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
unsafe.park(false, nanos);
setBlocker(t, null);
}
public static void parkUntil(Object blocker, long deadline) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
unsafe.park(true, deadline);
setBlocker(t, null);
}
public static Object getBlocker(Thread t) {
if (t == null) {
throw new NullPointerException();
}
return unsafe.getObjectVolatile(t, parkBlockerOffset);
}
public static void park() {
unsafe.park(false, 0L);
}
public static void parkNanos(long nanos) {
if (nanos > 0) {
unsafe.park(false, nanos);
}
}
public static void parkUntil(long deadline) {
unsafe.park(true, deadline);
}
}

View File

@ -0,0 +1,16 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent.locks;
public interface ReadWriteLock {
public Lock readLock();
public Lock writeLock();
}