aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/java/lang/ref
diff options
context:
space:
mode:
authorTom Tromey <tromey@gcc.gnu.org>2007-01-09 19:58:05 +0000
committerTom Tromey <tromey@gcc.gnu.org>2007-01-09 19:58:05 +0000
commit97b8365cafc3a344a22d3980b8ed885f5c6d8357 (patch)
tree996a5f57d4a68c53473382e45cb22f574cb3e4db /libjava/classpath/java/lang/ref
parentc648dedbde727ca3f883bb5fd773aa4af70d3369 (diff)
downloadgcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.zip
gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.tar.gz
gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.tar.bz2
Merged gcj-eclipse branch to trunk.
From-SVN: r120621
Diffstat (limited to 'libjava/classpath/java/lang/ref')
-rw-r--r--libjava/classpath/java/lang/ref/PhantomReference.java10
-rw-r--r--libjava/classpath/java/lang/ref/Reference.java23
-rw-r--r--libjava/classpath/java/lang/ref/ReferenceQueue.java65
-rw-r--r--libjava/classpath/java/lang/ref/SoftReference.java12
-rw-r--r--libjava/classpath/java/lang/ref/WeakReference.java10
5 files changed, 69 insertions, 51 deletions
diff --git a/libjava/classpath/java/lang/ref/PhantomReference.java b/libjava/classpath/java/lang/ref/PhantomReference.java
index 4d929c2..67e97d3 100644
--- a/libjava/classpath/java/lang/ref/PhantomReference.java
+++ b/libjava/classpath/java/lang/ref/PhantomReference.java
@@ -1,5 +1,5 @@
/* java.lang.ref.PhantomReference
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -46,8 +46,8 @@ package java.lang.ref;
*
* @author Jochen Hoenicke
*/
-public class PhantomReference
- extends Reference
+public class PhantomReference<T>
+ extends Reference<T>
{
/**
* Creates a new phantom reference.
@@ -56,7 +56,7 @@ public class PhantomReference
* finalized. This mustn't be <code>null</code>.
* @exception NullPointerException if q is null.
*/
- public PhantomReference(Object referent, ReferenceQueue q)
+ public PhantomReference(T referent, ReferenceQueue<? super T> q)
{
super(referent, q);
}
@@ -66,7 +66,7 @@ public class PhantomReference
* @return <code>null</code>, since the refered object may be
* finalized and thus not accessible.
*/
- public Object get()
+ public T get()
{
return null;
}
diff --git a/libjava/classpath/java/lang/ref/Reference.java b/libjava/classpath/java/lang/ref/Reference.java
index 4b6a3ad..ce224b8 100644
--- a/libjava/classpath/java/lang/ref/Reference.java
+++ b/libjava/classpath/java/lang/ref/Reference.java
@@ -1,5 +1,5 @@
/* java.lang.ref.Reference
- Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -70,19 +70,19 @@ package java.lang.ref;
* @author Jochen Hoenicke
* @see java.util.WeakHashMap
*/
-public abstract class Reference
+public abstract class Reference<T>
{
/**
* The underlying object. This field is handled in a special way by
* the garbage collector.
*/
- Object referent;
+ T referent;
/**
* The queue this reference is registered on. This is null, if this
* wasn't registered to any queue or reference was already enqueued.
*/
- ReferenceQueue queue;
+ volatile ReferenceQueue<? super T> queue;
/**
* Link to the next entry on the queue. If this is null, this
@@ -91,7 +91,7 @@ public abstract class Reference
* (not to null, that value is used to mark a not enqueued
* reference).
*/
- Reference nextOnQueue;
+ volatile Reference nextOnQueue;
/**
* This lock should be taken by the garbage collector, before
@@ -106,7 +106,7 @@ public abstract class Reference
* class in a different package.
* @param ref the object we refer to.
*/
- Reference(Object ref)
+ Reference(T ref)
{
referent = ref;
}
@@ -119,7 +119,7 @@ public abstract class Reference
* @param q the reference queue to register on.
* @exception NullPointerException if q is null.
*/
- Reference(Object ref, ReferenceQueue q)
+ Reference(T ref, ReferenceQueue<? super T> q)
{
if (q == null)
throw new NullPointerException();
@@ -132,7 +132,7 @@ public abstract class Reference
* @return the object, this reference refers to, or null if the
* reference was cleared.
*/
- public Object get()
+ public T get()
{
synchronized (lock)
{
@@ -166,11 +166,10 @@ public abstract class Reference
*/
public boolean enqueue()
{
- if (queue != null && nextOnQueue == null)
+ ReferenceQueue q = queue;
+ if (q != null)
{
- queue.enqueue(this);
- queue = null;
- return true;
+ return q.enqueue(this);
}
return false;
}
diff --git a/libjava/classpath/java/lang/ref/ReferenceQueue.java b/libjava/classpath/java/lang/ref/ReferenceQueue.java
index f4729f2..2816287 100644
--- a/libjava/classpath/java/lang/ref/ReferenceQueue.java
+++ b/libjava/classpath/java/lang/ref/ReferenceQueue.java
@@ -1,5 +1,5 @@
/* java.lang.ref.ReferenceQueue
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2004, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -50,7 +50,7 @@ package java.lang.ref;
* @author Jochen Hoenicke
* @see Reference#enqueue()
*/
-public class ReferenceQueue
+public class ReferenceQueue<T>
{
/**
* This is a linked list of references. If this is null, the list is
@@ -60,7 +60,13 @@ public class ReferenceQueue
* itself (not to null, since <code>nextOnQueue</code> is used to
* determine if a reference is enqueued).
*/
- private Reference first;
+ private Reference<? extends T> first;
+
+ /**
+ * This is the lock that protects our linked list and is used to signal
+ * a thread waiting in remove().
+ */
+ private final Object lock = new Object();
/**
* Creates a new empty reference queue.
@@ -76,7 +82,7 @@ public class ReferenceQueue
* @return a reference on the queue, if there is one,
* <code>null</code> otherwise.
*/
- public synchronized Reference poll()
+ public Reference<? extends T> poll()
{
return dequeue();
}
@@ -84,29 +90,41 @@ public class ReferenceQueue
/**
* This is called by reference to enqueue itself on this queue.
* @param ref the reference that should be enqueued.
+ * @return true if successful, false if not.
*/
- synchronized void enqueue(Reference ref)
- {
- /* last reference will point to itself */
- ref.nextOnQueue = first == null ? ref : first;
- first = ref;
- /* this wakes only one remove thread. */
- notify();
+ final boolean enqueue(Reference<? extends T> ref)
+ {
+ synchronized (lock)
+ {
+ if (ref.queue != this)
+ return false;
+
+ /* last reference will point to itself */
+ ref.nextOnQueue = first == null ? ref : first;
+ ref.queue = null;
+ first = ref;
+ /* this wakes only one remove thread. */
+ lock.notify();
+ return true;
+ }
}
/**
* Remove a reference from the queue, if there is one.
* @return the first element of the queue, or null if there isn't any.
*/
- private Reference dequeue()
+ private Reference<? extends T> dequeue()
{
- if (first == null)
- return null;
-
- Reference result = first;
- first = (first == first.nextOnQueue) ? null : first.nextOnQueue;
- result.nextOnQueue = null;
- return result;
+ synchronized (lock)
+ {
+ if (first == null)
+ return null;
+
+ Reference<? extends T> result = first;
+ first = (first == first.nextOnQueue) ? null : first.nextOnQueue;
+ result.nextOnQueue = null;
+ return result;
+ }
}
/**
@@ -118,12 +136,13 @@ public class ReferenceQueue
* <code>null</code> if timeout period expired.
* @exception InterruptedException if the wait was interrupted.
*/
- public synchronized Reference remove(long timeout)
+ public Reference<? extends T> remove(long timeout)
throws InterruptedException
{
- if (first == null)
+ synchronized (lock)
{
- wait(timeout);
+ if (first == null)
+ lock.wait(timeout);
}
return dequeue();
@@ -137,7 +156,7 @@ public class ReferenceQueue
* @return the reference removed from the queue.
* @exception InterruptedException if the wait was interrupted.
*/
- public Reference remove()
+ public Reference<? extends T> remove()
throws InterruptedException
{
return remove(0L);
diff --git a/libjava/classpath/java/lang/ref/SoftReference.java b/libjava/classpath/java/lang/ref/SoftReference.java
index 97395ea..077dc17 100644
--- a/libjava/classpath/java/lang/ref/SoftReference.java
+++ b/libjava/classpath/java/lang/ref/SoftReference.java
@@ -1,5 +1,5 @@
/* java.lang.ref.SoftReference
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -47,14 +47,14 @@ package java.lang.ref;
*
* @author Jochen Hoenicke
*/
-public class SoftReference
- extends Reference
+public class SoftReference<T>
+ extends Reference<T>
{
/**
* Create a new soft reference, that is not registered to any queue.
* @param referent the object we refer to.
*/
- public SoftReference(Object referent)
+ public SoftReference(T referent)
{
super(referent);
}
@@ -65,7 +65,7 @@ public class SoftReference
* @param q the reference queue to register on.
* @exception NullPointerException if q is null.
*/
- public SoftReference(Object referent, ReferenceQueue q)
+ public SoftReference(T referent, ReferenceQueue<? super T> q)
{
super(referent, q);
}
@@ -75,7 +75,7 @@ public class SoftReference
* @return the object, this reference refers to, or null if the
* reference was cleared.
*/
- public Object get()
+ public T get()
{
/* Why is this overloaded???
* Maybe for a kind of LRU strategy. */
diff --git a/libjava/classpath/java/lang/ref/WeakReference.java b/libjava/classpath/java/lang/ref/WeakReference.java
index b4018fb..563563b 100644
--- a/libjava/classpath/java/lang/ref/WeakReference.java
+++ b/libjava/classpath/java/lang/ref/WeakReference.java
@@ -1,5 +1,5 @@
/* java.lang.ref.WeakReference
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -54,14 +54,14 @@ package java.lang.ref;
* @author Jochen Hoenicke
* @see java.util.WeakHashMap
*/
-public class WeakReference
- extends Reference
+public class WeakReference<T>
+ extends Reference<T>
{
/**
* Create a new weak reference, that is not registered to any queue.
* @param referent the object we refer to.
*/
- public WeakReference(Object referent)
+ public WeakReference(T referent)
{
super(referent);
}
@@ -72,7 +72,7 @@ public class WeakReference
* @param q the reference queue to register on.
* @exception NullPointerException if q is null.
*/
- public WeakReference(Object referent, ReferenceQueue q)
+ public WeakReference(T referent, ReferenceQueue<? super T> q)
{
super(referent, q);
}