| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package org.jpype.ref; |
| 17 |
|
| 18 |
import java.lang.ref.PhantomReference; |
| 19 |
import java.lang.ref.ReferenceQueue; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
final public class JPypeReferenceQueue extends ReferenceQueue |
| 33 |
{ |
| 34 |
|
| 35 |
private final static JPypeReferenceQueue INSTANCE = new JPypeReferenceQueue(); |
| 36 |
private JPypeReferenceSet hostReferences; |
| 37 |
private boolean isStopped = false; |
| 38 |
private Thread queueThread; |
| 39 |
private Object queueStopMutex = new Object(); |
| 40 |
private PhantomReference sentinel = null; |
| 41 |
|
| 42 |
public static JPypeReferenceQueue getInstance() |
| 43 |
{ |
| 44 |
return INSTANCE; |
| 45 |
} |
| 46 |
|
| 47 |
private JPypeReferenceQueue() |
| 48 |
{ |
| 49 |
super(); |
| 50 |
this.hostReferences = new JPypeReferenceSet(); |
| 51 |
addSentinel(); |
| 52 |
JPypeReferenceNative.removeHostReference(0, 0); |
| 53 |
try |
| 54 |
{ |
| 55 |
JPypeReferenceNative.init(this, getClass().getDeclaredMethod("registerRef", Object.class, Long.TYPE, Long.TYPE)); |
| 56 |
} catch (NoSuchMethodException | SecurityException ex) |
| 57 |
{ |
| 58 |
throw new RuntimeException(ex); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void registerRef(Object javaObject, long host, long cleanup) |
| 74 |
{ |
| 75 |
if (cleanup == 0) |
| 76 |
return; |
| 77 |
if (isStopped) |
| 78 |
{ |
| 79 |
JPypeReferenceNative.removeHostReference(host, cleanup); |
| 80 |
} else |
| 81 |
{ |
| 82 |
JPypeReference ref = new JPypeReference(this, javaObject, host, cleanup); |
| 83 |
hostReferences.add(ref); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void start() |
| 91 |
{ |
| 92 |
isStopped = false; |
| 93 |
queueThread = new Thread(new Worker(), "Python Reference Queue"); |
| 94 |
queueThread.setDaemon(true); |
| 95 |
queueThread.start(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public void stop() |
| 104 |
{ |
| 105 |
try |
| 106 |
{ |
| 107 |
synchronized (queueStopMutex) |
| 108 |
{ |
| 109 |
synchronized (this) |
| 110 |
{ |
| 111 |
isStopped = true; |
| 112 |
queueThread.interrupt(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
queueStopMutex.wait(10000); |
| 117 |
} |
| 118 |
} catch (InterruptedException ex) |
| 119 |
{ |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
hostReferences.flush(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public boolean isRunning() |
| 133 |
{ |
| 134 |
return !isStopped; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public int getQueueSize() |
| 143 |
{ |
| 144 |
return this.hostReferences.size(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private class Worker implements Runnable |
| 152 |
{ |
| 153 |
|
| 154 |
@Override |
| 155 |
public void run() |
| 156 |
{ |
| 157 |
while (!isStopped) |
| 158 |
{ |
| 159 |
try |
| 160 |
{ |
| 161 |
|
| 162 |
|
| 163 |
JPypeReference ref = (JPypeReference) remove(250); |
| 164 |
if (ref == sentinel) |
| 165 |
{ |
| 166 |
addSentinel(); |
| 167 |
JPypeReferenceNative.wake(); |
| 168 |
continue; |
| 169 |
} |
| 170 |
if (ref != null) |
| 171 |
{ |
| 172 |
long hostRef = ref.hostReference; |
| 173 |
long cleanup = ref.cleanup; |
| 174 |
hostReferences.remove(ref); |
| 175 |
JPypeReferenceNative.removeHostReference(hostRef, cleanup); |
| 176 |
} |
| 177 |
} catch (InterruptedException ex) |
| 178 |
{ |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
synchronized (queueStopMutex) |
| 184 |
{ |
| 185 |
queueStopMutex.notifyAll(); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
final void addSentinel() |
| 191 |
{ |
| 192 |
sentinel = new JPypeReference(this, new byte[0], 0, 0); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
} |