xref: /aosp_15_r20/external/fbjni/java/com/facebook/jni/DestructorThread.java (revision 65c59e023c5336bbd4a23be7af78407e3d80e7e7)
1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.facebook.jni;
18 
19 import java.lang.ref.PhantomReference;
20 import java.lang.ref.ReferenceQueue;
21 import java.util.concurrent.atomic.AtomicReference;
22 
23 /**
24  * A thread which invokes the "destruct" routine for objects after they have been garbage collected.
25  *
26  * <p>An object which needs to be destructed should create a static subclass of {@link Destructor}.
27  * Once the referent object is garbage collected, the DestructorThread will callback to the {@link
28  * Destructor#destruct()} method.
29  *
30  * <p>The underlying thread in DestructorThread starts when the first Destructor is constructed and
31  * then runs indefinitely.
32  */
33 public class DestructorThread {
34 
35   /**
36    * N.B The Destructor <b>SHOULD NOT</b> refer back to its referent object either explicitly or
37    * implicitly (for example, as a non-static inner class). This will create a reference cycle where
38    * the referent object will never be garbage collected.
39    */
40   public abstract static class Destructor extends PhantomReference<Object> {
41 
42     private Destructor next;
43     private Destructor previous;
44 
Destructor(Object referent)45     public Destructor(Object referent) {
46       super(referent, sReferenceQueue);
47       sDestructorStack.push(this);
48     }
49 
Destructor()50     private Destructor() {
51       super(null, sReferenceQueue);
52     }
53 
54     /** Callback which is invoked when the original object has been garbage collected. */
destruct()55     protected abstract void destruct();
56   }
57 
58   /** A list to keep all active Destructors in memory confined to the Destructor thread. */
59   private static final DestructorList sDestructorList;
60 
61   /** A thread safe stack where new Destructors are placed before being add to sDestructorList. */
62   private static final DestructorStack sDestructorStack;
63 
64   private static final ReferenceQueue sReferenceQueue;
65   private static final Thread sThread;
66 
67   static {
68     sDestructorStack = new DestructorStack();
69     sReferenceQueue = new ReferenceQueue();
70     sDestructorList = new DestructorList();
71     sThread =
72         new Thread("HybridData DestructorThread") {
73           @Override
74           public void run() {
75             while (true) {
76               try {
77                 Destructor current = (Destructor) sReferenceQueue.remove();
78                 current.destruct();
79 
80                 // If current is in the sDestructorStack,
81                 // transfer all the Destructors in the stack to the list.
82                 if (current.previous == null) {
83                   sDestructorStack.transferAllToList();
84                 }
85 
86                 DestructorList.drop(current);
87               } catch (InterruptedException e) {
88                 // Continue. This thread should never be terminated.
89               }
90             }
91           }
92         };
93 
sThread.start()94     sThread.start();
95   }
96 
97   private static class Terminus extends Destructor {
98     @Override
destruct()99     protected void destruct() {
100       throw new IllegalStateException("Cannot destroy Terminus Destructor.");
101     }
102   }
103 
104   /** This is a thread safe, lock-free Treiber-like Stack of Destructors. */
105   private static class DestructorStack {
106     private final AtomicReference<Destructor> mHead = new AtomicReference<>();
107 
push(Destructor newHead)108     public void push(Destructor newHead) {
109       Destructor oldHead;
110       do {
111         oldHead = mHead.get();
112         newHead.next = oldHead;
113       } while (!mHead.compareAndSet(oldHead, newHead));
114     }
115 
transferAllToList()116     public void transferAllToList() {
117       Destructor current = mHead.getAndSet(null);
118       while (current != null) {
119         Destructor next = current.next;
120         sDestructorList.enqueue(current);
121         current = next;
122       }
123     }
124   }
125 
126   /** A doubly-linked list of Destructors. */
127   private static class DestructorList {
128     private final Destructor mHead;
129 
DestructorList()130     public DestructorList() {
131       mHead = new Terminus();
132       mHead.next = new Terminus();
133       mHead.next.previous = mHead;
134     }
135 
enqueue(Destructor current)136     public void enqueue(Destructor current) {
137       current.next = mHead.next;
138       mHead.next = current;
139 
140       current.next.previous = current;
141       current.previous = mHead;
142     }
143 
drop(Destructor current)144     private static void drop(Destructor current) {
145       current.next.previous = current.previous;
146       current.previous.next = current.next;
147     }
148   }
149 }
150