KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ParallelCompilerPass


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.base.CompilerObject;
29
30 import java.util.*;
31
32 public abstract class ParallelCompilerPass extends CompilerObject implements CompilationUnitPass {
33     public ParallelCompilerPass(JavaCompiler compiler) {
34         super(compiler);
35     }
36
37     public abstract String JavaDoc getDisplayName();
38     
39     public double getWorkEstimate() { return 1.0; }
40     
41     public int getNumThreads() { return getOptions().procs; }
42
43     private List cuList = new LinkedList();
44
45     private int finished = 0;
46
47     public synchronized CompilationUnit getWorkCu() {
48         if (cuList.size() == 0) return null;
49         return (CompilationUnit)cuList.remove(0);
50     }
51
52     public synchronized void setFinished() {
53         finished += 1;
54     }
55
56     public void transformWorld() {
57         if (getDisplayName() != null) getCompiler().beginSection(getDisplayName());
58         for (Iterator i = getWorld().getCompilationUnits().iterator(); i.hasNext(); ) {
59             CompilationUnit cu = (CompilationUnit)i.next();
60             cuList.add(cu);
61         }
62
63         runThreads();
64     }
65
66     class PassThread extends Thread JavaDoc {
67         public void run() {
68             CompilationUnit cu = getWorkCu();
69             while (cu != null) {
70                 transform(cu);
71                 getCompiler().completedFile(ParallelCompilerPass.this, cu);
72                 cu = getWorkCu();
73             }
74             setFinished();
75         }
76     }
77
78     void runThreads() {
79         for (int i = 0; i < getNumThreads(); i++) {
80             new PassThread().start();
81         }
82
83         Thread JavaDoc myThread = Thread.currentThread();
84
85         while (finished < getNumThreads()) {
86             try {
87                 myThread.sleep(100);
88             } catch (InterruptedException JavaDoc ie) {
89                 break;
90             }
91         }
92     }
93
94     
95     public void transform(CompilationUnit cu) {
96         for (Iterator j = cu.getDefinedTypes().iterator(); j.hasNext(); ) {
97             TypeDec td = (TypeDec)j.next();
98             transform(td);
99         }
100     }
101     
102     public void transform(TypeDec td) {
103     }
104 }
105
106
Popular Tags