001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm.core;
043:
044: import java.util.*;
045: import java.util.concurrent.CopyOnWriteArraySet;
046: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
047: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
048: import org.openide.util.RequestProcessor;
049:
050: /**
051: *
052: * @author Vladimir Kvashin
053: */
054: public class ParserThreadManager {
055:
056: private static ParserThreadManager instance;
057:
058: private static final String threadNameBase = "Code Model Parser"; // NOI18N
059: private RequestProcessor processor;
060: private Set<Wrapper> wrappers = new CopyOnWriteArraySet<Wrapper>();
061: private int currThread = 0;
062:
063: private class Wrapper implements Runnable {
064:
065: private ParserThread delegate;
066: private Thread thread;
067:
068: public Wrapper(ParserThread delegate) {
069: this .delegate = delegate;
070: }
071:
072: public void stop() {
073: assert this .delegate != null;
074: this .delegate.stop();
075: }
076:
077: public void run() {
078: try {
079: thread = Thread.currentThread();
080: thread.setName(threadNameBase + ' ' + currThread++);
081: wrappers.add(this );
082: delegate.run();
083: } catch (Throwable thr) {
084: DiagnosticExceptoins.register(thr);
085: } finally {
086: wrappers.remove(this );
087: }
088: }
089: }
090:
091: private ParserThreadManager() {
092: }
093:
094: public static synchronized ParserThreadManager instance() {
095: if (instance == null) {
096: instance = new ParserThreadManager();
097: }
098: return instance;
099: }
100:
101: public boolean isStandalone() {
102: return (processor == null);
103: }
104:
105: // package-local
106: void startup(boolean standalone) {
107:
108: ParserQueue.instance().startup();
109:
110: // int threadCount = Integer.getInteger("cnd.modelimpl.parser.wrappers",
111: // Math.max(Runtime.getRuntime().availableProcessors()-1, 1)).intValue();
112:
113: int threadCount = Integer.getInteger(
114: "cnd.modelimpl.parser.threads", // NOI18N
115: Runtime.getRuntime().availableProcessors()).intValue(); // NOI18N
116:
117: threadCount = Math.min(threadCount, 4);
118: threadCount = Math.max(threadCount, 1);
119:
120: if (!standalone) {
121: processor = new RequestProcessor(threadNameBase,
122: threadCount);
123: }
124: for (int i = 0; i < threadCount; i++) {
125: Runnable r = new Wrapper(new ParserThread());
126: if (standalone) {
127: new Thread(r).start();
128: } else {
129: processor.post(r);
130: }
131: }
132: }
133:
134: // package-local
135: void shutdown() {
136: if (TraceFlags.TRACE_MODEL_STATE)
137: System.err.println("=== ParserThreadManager.shutdown");
138:
139: for (Wrapper wrapper : wrappers) {
140: wrapper.stop();
141: }
142:
143: ParserQueue.instance().shutdown();
144: }
145:
146: public boolean isParserThread() {
147: if (isStandalone()) {
148: Thread current = Thread.currentThread();
149:
150: for (Wrapper wrapper : wrappers) {
151: if (wrapper.thread == current) {
152: return true;
153: }
154: }
155: return false;
156: } else {
157: return processor.isRequestProcessorThread();
158: }
159: }
160:
161: public void waitEmptyProjectQueue(ProjectBase prj) {
162: ParserQueue.instance().waitEmpty(prj);
163: }
164: }
|