Debian Codegraph @

native/jpype_module/src/main/java/org/jpype/ref/JPypeReferenceQueue.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 **************************************************************************** */
16 package org.jpype.ref;
17
18 import java.lang.ref.PhantomReference;
19 import java.lang.ref.ReferenceQueue;
20
21 /**
22 * Reference queue holds the life of python objects to be as long as java items.
23 * <p>
24 * Any java class that holds a pointer into python needs to have a reference so
25 * that python object does not go away if the references in python fall to zero.
26 * JPype will add an extra reference to the object which is removed when the
27 * python object dies.
28 *
29 * @author smenard
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 * (internal) Binds the lifetime of a Python object to a Java object.
64 * <p>
65 * JPype adds an extra reference to a PyObject* and then calls this method to
66 * hold that reference until the Java object is garbage collected.
67 *
68 * @param javaObject is the object to bind the lifespan to.
69 * @param host is the pointer to the host object.
70 * @param cleanup is the pointer to the function to call to delete the
71 * resource.
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 * Start the threading queue.
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 * Stops the reference queue.
100 * <p>
101 * This is called by jpype when the jvm shuts down.
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 // wait for the thread to finish ...
116 queueStopMutex.wait(10000);
117 }
118 } catch (InterruptedException ex)
119 {
120 // who cares ...
121 }
122
123 // Empty the queue.
124 hostReferences.flush();
125 }
126
127 /**
128 * Checks the status of the reference queue.
129 *
130 * @return true is the queue is running.
131 */
132 public boolean isRunning()
133 {
134 return !isStopped;
135 }
136
137 /**
138 * Get the number of items in the reference queue.
139 *
140 * @return the number of python resources held.
141 */
142 public int getQueueSize()
143 {
144 return this.hostReferences.size();
145 }
146
147 //<editor-fold desc="internal" defaultstate="collapsed">
148 /**
149 * Thread to monitor the queue and delete resources.
150 */
151 private class Worker implements Runnable
152 {
153
154 @Override
155 public void run()
156 {
157 while (!isStopped)
158 {
159 try
160 {
161 // Check if a ref has been queued. and check if the thread has been
162 // stopped every 0.25 seconds
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 // don't know why ... don't really care ...
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 //</editor-fold>
196 }