mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 12:04:39 +02:00
8048983: Enhance thread contexts in JAXP
Reviewed-by: chegar, lancea, dfuchs, hawtin
This commit is contained in:
parent
2b912ed601
commit
7d455864c8
2 changed files with 151 additions and 93 deletions
|
@ -0,0 +1,94 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Oracle designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
package com.sun.org.apache.xml.internal.utils;
|
||||||
|
|
||||||
|
import sun.misc.Unsafe;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a combination of ThreadControllerWrapper's inner class SafeThread
|
||||||
|
* that was introduced as a fix for CR 6607339
|
||||||
|
* and sun.misc.ManagedLocalsThread, a thread that has it's thread locals, and
|
||||||
|
* inheritable thread locals erased on construction. Except the run method,
|
||||||
|
* it is identical to sun.misc.ManagedLocalsThread.
|
||||||
|
*/
|
||||||
|
public class SafeThread extends Thread {
|
||||||
|
|
||||||
|
private static final Unsafe UNSAFE;
|
||||||
|
private static final long THREAD_LOCALS;
|
||||||
|
private static final long INHERITABLE_THREAD_LOCALS;
|
||||||
|
|
||||||
|
private volatile boolean ran = false;
|
||||||
|
|
||||||
|
public SafeThread(Runnable target) {
|
||||||
|
super(target);
|
||||||
|
eraseThreadLocals();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SafeThread(Runnable target, String name) {
|
||||||
|
super(target, name);
|
||||||
|
eraseThreadLocals();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SafeThread(ThreadGroup group, Runnable target, String name) {
|
||||||
|
super(group, target, name);
|
||||||
|
eraseThreadLocals();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void run() {
|
||||||
|
if (Thread.currentThread() != this) {
|
||||||
|
throw new IllegalStateException("The run() method in a"
|
||||||
|
+ " SafeThread cannot be called from another thread.");
|
||||||
|
}
|
||||||
|
synchronized (this) {
|
||||||
|
if (!ran) {
|
||||||
|
ran = true;
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException("The run() method in a"
|
||||||
|
+ " SafeThread cannot be called more than once.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops all thread locals (and inherited thread locals).
|
||||||
|
*/
|
||||||
|
public final void eraseThreadLocals() {
|
||||||
|
UNSAFE.putObject(this, THREAD_LOCALS, null);
|
||||||
|
UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
UNSAFE = Unsafe.getUnsafe();
|
||||||
|
Class<?> t = Thread.class;
|
||||||
|
try {
|
||||||
|
THREAD_LOCALS = UNSAFE.objectFieldOffset(t.getDeclaredField("threadLocals"));
|
||||||
|
INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset(t.getDeclaredField("inheritableThreadLocals"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,11 +3,12 @@
|
||||||
* DO NOT REMOVE OR ALTER!
|
* DO NOT REMOVE OR ALTER!
|
||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
* Copyright 1999-2004 The Apache Software Foundation.
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
*
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* this work for additional information regarding copyright ownership.
|
||||||
* you may not use this file except in compliance with the License.
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
* You may obtain a copy of the License at
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
|
@ -17,110 +18,73 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
* $Id: ThreadControllerWrapper.java,v 1.2.4.1 2005/09/15 08:15:59 suresh_emailid Exp $
|
|
||||||
*/
|
|
||||||
package com.sun.org.apache.xml.internal.utils;
|
package com.sun.org.apache.xml.internal.utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A utility class that wraps the ThreadController, which is used
|
* A utility class that wraps the ThreadController, which is used by
|
||||||
* by IncrementalSAXSource for the incremental building of DTM.
|
* IncrementalSAXSource for the incremental building of DTM.
|
||||||
*/
|
*/
|
||||||
public class ThreadControllerWrapper
|
public class ThreadControllerWrapper {
|
||||||
{
|
|
||||||
|
|
||||||
/** The ThreadController pool */
|
|
||||||
private static ThreadController m_tpool = new ThreadController();
|
|
||||||
|
|
||||||
public static Thread runThread(Runnable runnable, int priority)
|
|
||||||
{
|
|
||||||
return m_tpool.run(runnable, priority);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void waitThread(Thread worker, Runnable task)
|
|
||||||
throws InterruptedException
|
|
||||||
{
|
|
||||||
m_tpool.waitThread(worker, task);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thread controller utility class for incremental SAX source. Must
|
|
||||||
* be overriden with a derived class to support thread pooling.
|
|
||||||
*
|
|
||||||
* All thread-related stuff is in this class.
|
|
||||||
*/
|
|
||||||
public static class ThreadController
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class was introduced as a fix for CR 6607339.
|
* The ThreadController pool
|
||||||
*/
|
*/
|
||||||
final class SafeThread extends Thread {
|
private static ThreadController m_tpool = new ThreadController();
|
||||||
private volatile boolean ran = false;
|
|
||||||
|
|
||||||
public SafeThread(Runnable target) {
|
public static Thread runThread(Runnable runnable, int priority) {
|
||||||
super(target);
|
return m_tpool.run(runnable, priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void run() {
|
public static void waitThread(Thread worker, Runnable task)
|
||||||
if (Thread.currentThread() != this) {
|
throws InterruptedException {
|
||||||
throw new IllegalStateException("The run() method in a"
|
m_tpool.waitThread(worker, task);
|
||||||
+ " SafeThread cannot be called from another thread.");
|
|
||||||
}
|
|
||||||
synchronized (this) {
|
|
||||||
if (!ran) {
|
|
||||||
ran = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new IllegalStateException("The run() method in a"
|
|
||||||
+ " SafeThread cannot be called more than once.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
super.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will get a thread from the pool, execute the task
|
* Thread controller utility class for incremental SAX source. Must be
|
||||||
* and return the thread to the pool.
|
* overridden with a derived class to support thread pooling.
|
||||||
*
|
*
|
||||||
* The return value is used only to wait for completion
|
* All thread-related stuff is in this class.
|
||||||
*
|
|
||||||
*
|
|
||||||
* NEEDSDOC @param task
|
|
||||||
* @param priority if >0 the task will run with the given priority
|
|
||||||
* ( doesn't seem to be used in xalan, since it's allways the default )
|
|
||||||
* @return The thread that is running the task, can be used
|
|
||||||
* to wait for completion
|
|
||||||
*/
|
*/
|
||||||
public Thread run(Runnable task, int priority)
|
public static class ThreadController {
|
||||||
{
|
|
||||||
|
|
||||||
Thread t = new SafeThread(task);
|
/**
|
||||||
|
* Will get a thread from the pool, execute the task and return the
|
||||||
|
* thread to the pool.
|
||||||
|
*
|
||||||
|
* The return value is used only to wait for completion
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param task the Runnable
|
||||||
|
*
|
||||||
|
* @param priority if >0 the task will run with the given priority (
|
||||||
|
* doesn't seem to be used in xalan, since it's always the default )
|
||||||
|
* @return The thread that is running the task, can be used to wait for
|
||||||
|
* completion
|
||||||
|
*/
|
||||||
|
public Thread run(Runnable task, int priority) {
|
||||||
|
|
||||||
t.start();
|
Thread t = new SafeThread(task);
|
||||||
|
t.start();
|
||||||
|
|
||||||
// if( priority > 0 )
|
//if( priority > 0 )
|
||||||
// t.setPriority( priority );
|
// t.setPriority( priority );
|
||||||
return t;
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait until the task is completed on the worker thread.
|
||||||
|
*
|
||||||
|
* @param worker worker thread
|
||||||
|
* @param task the Runnable
|
||||||
|
*
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
|
public void waitThread(Thread worker, Runnable task)
|
||||||
|
throws InterruptedException {
|
||||||
|
|
||||||
|
// This should wait until the transformThread is considered not alive.
|
||||||
|
worker.join();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Wait until the task is completed on the worker
|
|
||||||
* thread.
|
|
||||||
*
|
|
||||||
* NEEDSDOC @param worker
|
|
||||||
* NEEDSDOC @param task
|
|
||||||
*
|
|
||||||
* @throws InterruptedException
|
|
||||||
*/
|
|
||||||
public void waitThread(Thread worker, Runnable task)
|
|
||||||
throws InterruptedException
|
|
||||||
{
|
|
||||||
|
|
||||||
// This should wait until the transformThread is considered not alive.
|
|
||||||
worker.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue