aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/utils/DirectBufferCleanup.java
blob: 378cd76581abb354c1617e6dc065b5456b196f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gl4java.utils;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.nio.*;
import java.util.HashMap;
import java.util.Map;

/** Provides a cleanup mechanism for direct buffers instantiated via
    the JNI entry point <code>NewDirectByteBuffer</code>. A direct
    buffer can be <i>registered</i> with a DirectBufferCleanup. When
    that buffer is reclaimed by the garbage collector, the callback
    associated with that DirectBufferCleanup is called on the address
    the direct buffer was associated with. */

public class DirectBufferCleanup {

  // Put DirectBufferCleanup.c into its own library if extracting just
  // this mechanism
  //  static {
  //    System.loadLibrary("DirectBufferCleanup");
  //  }

  public static interface Callback {
    public void cleanup(long addr);
  }

  public DirectBufferCleanup(Callback cb) {
    this.cb = cb;
    refToAddrMap = new HashMap();
    queue = new ReferenceQueue();
    start();
  }

  /** The DirectBufferCleanup contains an internal thread which is
      started automatically upon construction. This method starts the
      thread again if it is manually stopped via the {@link #stop}
      method. */
  public synchronized void start() {
    if (t == null) {
      t = new Thread(new Runnable() {
          public void run() {
            while (!done) {
              try {
                Reference r = queue.remove();
                Long addr = (Long) refToAddrMap.remove(r);
                cb.cleanup(addr.longValue());
                r.clear();
              } catch (InterruptedException e) {
              }
            }
            t = null;
          }
        });
      done = false;
      t.start();
    }
  }

  /** Stops the internal thread of this DirectBufferCleanup. Should
      not typically be necessary. */
  public synchronized void stop() {
    done = true;
    while (t != null) {
      try {
        wait();
      } catch (InterruptedException e) {
      }
    }
  }

  /** Registers the given buffer (which must be a direct buffer) for
      later cleanup when it is reclaimed by the garbage collector.
   
      @throw IllegalArgumentException if the passed buffer is not
      direct.
  */

  public void register(Buffer buf) throws IllegalArgumentException {
    try {
      long addr = getDirectBufferAddress(buf);
      if (addr == 0) throw new IllegalArgumentException();
      refToAddrMap.put(new PhantomReference(buf, queue),
                       new Long(addr));
    } catch (ClassCastException e) {
      throw new IllegalArgumentException();
    }
  }

  //----------------------------------------------------------------------
  // Internals only below this point
  //

  private Callback cb;

  // Maps PhantomReferences to addresses
  private Map refToAddrMap;

  // Reference queue which gets notified
  private ReferenceQueue queue;

  // Thread watching reference queue
  private volatile Thread t;

  // Native method providing direct buffer address via JNI
  private static native long getDirectBufferAddress(Buffer buf);

  private volatile boolean done = false;
}