< prev index next >

src/java.base/share/classes/jdk/internal/misc/InnocuousThread.java

Print this page

166     // Use Unsafe to access Thread group and ThreadGroup parent fields
167     static {
168         try {
169             ACC = new AccessControlContext(new ProtectionDomain[] {
170                 new ProtectionDomain(null, null)
171             });
172 
173             // Find and use topmost ThreadGroup as parent of new group
174             UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
175             Class<?> tk = Thread.class;
176             Class<?> gk = ThreadGroup.class;
177 
178             THREAD_LOCALS = UNSAFE.objectFieldOffset(tk, "threadLocals");
179             INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
180                     (tk, "inheritableThreadLocals");
181             INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset
182                 (tk, "inheritedAccessControlContext");
183             CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset
184                 (tk, "contextClassLoader");
185 
186             long tg = UNSAFE.objectFieldOffset(tk, "group");
187             long gp = UNSAFE.objectFieldOffset(gk, "parent");
188             ThreadGroup group = (ThreadGroup)
189                 UNSAFE.getReference(Thread.currentThread(), tg);
190 
191             while (group != null) {
192                 ThreadGroup parent = (ThreadGroup)UNSAFE.getReference(group, gp);
193                 if (parent == null)
194                     break;
195                 group = parent;
196             }
197             final ThreadGroup root = group;

198             if (System.getSecurityManager() == null) {
199                 INNOCUOUSTHREADGROUP = new ThreadGroup(root, "InnocuousThreadGroup");


200             } else {
201                 INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
202                     new PrivilegedAction<ThreadGroup>() {
203                         @Override

204                         public ThreadGroup run() {
205                             return new ThreadGroup(root, "InnocuousThreadGroup");
206                         }
207                     });
208             }
209         } catch (Exception e) {
210             throw new Error(e);
211         }
212     }
213 }

166     // Use Unsafe to access Thread group and ThreadGroup parent fields
167     static {
168         try {
169             ACC = new AccessControlContext(new ProtectionDomain[] {
170                 new ProtectionDomain(null, null)
171             });
172 
173             // Find and use topmost ThreadGroup as parent of new group
174             UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
175             Class<?> tk = Thread.class;
176             Class<?> gk = ThreadGroup.class;
177 
178             THREAD_LOCALS = UNSAFE.objectFieldOffset(tk, "threadLocals");
179             INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
180                     (tk, "inheritableThreadLocals");
181             INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset
182                 (tk, "inheritedAccessControlContext");
183             CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset
184                 (tk, "contextClassLoader");
185 

186             long gp = UNSAFE.objectFieldOffset(gk, "parent");
187             ThreadGroup group = Thread.currentThread().getThreadGroup();

188 
189             while (group != null) {
190                 ThreadGroup parent = (ThreadGroup)UNSAFE.getReference(group, gp);
191                 if (parent == null)
192                     break;
193                 group = parent;
194             }
195             final ThreadGroup root = group;
196 
197             if (System.getSecurityManager() == null) {
198                 @SuppressWarnings("deprecation")
199                 ThreadGroup g = new ThreadGroup(root, "InnocuousThreadGroup");
200                 INNOCUOUSTHREADGROUP = g;
201             } else {
202                 INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
203                     new PrivilegedAction<ThreadGroup>() {
204                         @Override
205                         @SuppressWarnings("deprecation")
206                         public ThreadGroup run() {
207                             return new ThreadGroup(root, "InnocuousThreadGroup");
208                         }
209                     });
210             }
211         } catch (Exception e) {
212             throw new Error(e);
213         }
214     }
215 }
< prev index next >