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 org.netbeans.modules.cnd.apt.support.APTPreprocHandler;
045: import org.netbeans.modules.cnd.modelimpl.debug.Diagnostic;
046: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
047: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
048:
049: /**
050: *
051: * @author vk155633
052: */
053: public class ParserThread implements Runnable {
054: private boolean stopped = false;
055:
056: public void stop() {
057: this .stopped = true;
058: }
059:
060: public void run() {
061: if (TraceFlags.TRACE_PARSER_QUEUE)
062: trace("started"); // NOI18N
063: ParserQueue queue = ParserQueue.instance();
064: while (!stopped) {
065: if (TraceFlags.TRACE_PARSER_QUEUE)
066: trace("polling queue"); // NOI18N
067: try {
068: ParserQueue.Entry entry = queue.poll();
069: if (entry == null) {
070: if (TraceFlags.TRACE_PARSER_QUEUE)
071: trace("waiting"); // NOI18N
072: queue.waitReady();
073: } else {
074: FileImpl file = entry.getFile();
075: if (TraceFlags.TRACE_PARSER_QUEUE) {
076: trace("parsing started: "
077: + entry
078: .toString(TraceFlags.TRACE_PARSER_QUEUE_DETAILS)); // NOI18N
079: }
080: Diagnostic.StopWatch stw = (TraceFlags.TIMING_PARSE_PER_FILE_FLAT && !file
081: .isParsed()) ? new Diagnostic.StopWatch()
082: : null;
083: APTPreprocHandler preprocHandler = null;
084: try {
085: if (!file.getProjectImpl().isDisposing()) { // just in case check
086: APTPreprocHandler.State state = entry
087: .getPreprocState();
088: if (state != null) {
089: // init from entry
090: preprocHandler = file.getProjectImpl()
091: .createEmptyPreprocHandler(
092: file.getBuffer()
093: .getFile());
094: if (TraceFlags.TRACE_PARSER_QUEUE) {
095: System.err
096: .println("before ensureParse on "
097: + file
098: .getAbsolutePath()
099: + ParserQueue
100: .tracePreprocState(state));
101: }
102: preprocHandler.setState(state);
103: }
104: file.ensureParsed(preprocHandler);
105: }
106: } catch (Throwable thr) {
107: DiagnosticExceptoins.register(thr);
108: } finally {
109: if (stw != null)
110: stw.stopAndReport("parsing "
111: + file.getAbsolutePath()); // NOI18N
112: try {
113: queue.onFileParsingFinished(file);
114: if (TraceFlags.TRACE_PARSER_QUEUE)
115: trace("parsing done: "
116: + file.getAbsolutePath()); // NOI18N
117: Notificator.instance().flush();
118: if (TraceFlags.TRACE_PARSER_QUEUE)
119: trace("model event flushed"); // NOI18N
120: } catch (Throwable thr) {
121: thr.printStackTrace(System.err);
122: }
123: }
124: }
125: } catch (InterruptedException ex) {
126: if (TraceFlags.TRACE_PARSER_QUEUE)
127: trace("interrupted"); // NOI18N
128: break;
129: }
130: }
131: if (TraceFlags.TRACE_PARSER_QUEUE)
132: trace(stopped ? "stopped" : "finished"); // NOI18N
133: }
134:
135: private void trace(String text) {
136: System.err.println(Thread.currentThread().getName() + ": "
137: + text);
138: }
139:
140: }
|