|
1 /* |
|
2 * Copyright (c) 2004-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 using System; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.Collections; |
|
22 using SymBuildParsingLib.Utils; |
|
23 using SymBuildParsingLib.Lexer; |
|
24 using SymBuildParsingLib.Token; |
|
25 |
|
26 namespace SymBuildParsingLib.Grouper |
|
27 { |
|
28 public class SymGrouper |
|
29 { |
|
30 #region Enumerations |
|
31 public enum TEvent |
|
32 { |
|
33 EEventGroupingStarted = 0, |
|
34 EEventGroupingPaused, |
|
35 EEventGroupingTokenReady, |
|
36 EEventGroupingComplete |
|
37 }; |
|
38 #endregion |
|
39 |
|
40 #region Observer interface |
|
41 public delegate void GrouperObserver( SymGrouper aGrouper, TEvent aEvent, SymToken aToken ); |
|
42 #endregion |
|
43 |
|
44 #region Events |
|
45 public event GrouperObserver GrouperObservers; |
|
46 #endregion |
|
47 |
|
48 #region Constructors & destructor |
|
49 public SymGrouper( SymLexer aLexer ) |
|
50 { |
|
51 // The mastermind implements all the logic for grouping token runs into |
|
52 // more sophisticated/logical token groupings. |
|
53 iMastermind.MastermindObservers += new SymGrouperMastermind.MastermindObserver( MastermindObserver ); |
|
54 |
|
55 // Observe the lexer for tokens |
|
56 aLexer.LexerObservers += new SymLexer.LexerObserver( LexerTokenHandler ); |
|
57 |
|
58 // Prepare worker thread |
|
59 ThreadStart threadStart = new ThreadStart( DoGrouping ); |
|
60 iWorkerThread = new Thread( threadStart ); |
|
61 iWorkerThread.Name = "SymGrouperWorkerThread"; |
|
62 iWorkerThread.IsBackground = true; |
|
63 iWorkerThread.Start(); |
|
64 } |
|
65 #endregion |
|
66 |
|
67 #region Internal methods |
|
68 private void ReportEvent( TEvent aEvent, SymToken aToken ) |
|
69 { |
|
70 if ( GrouperObservers != null ) |
|
71 { |
|
72 GrouperObservers( this, aEvent, aToken ); |
|
73 } |
|
74 } |
|
75 #endregion |
|
76 |
|
77 #region Internal threading related |
|
78 private void DoGrouping() |
|
79 { |
|
80 ReportEvent( TEvent.EEventGroupingStarted, SymToken.NullToken() ); |
|
81 |
|
82 bool lexerFinished = false; |
|
83 do |
|
84 { |
|
85 // Count how many tokens we have... |
|
86 lock( this ) |
|
87 { |
|
88 iMastermind.PerformGrouping(); |
|
89 lexerFinished = iLexerFinished; |
|
90 } |
|
91 |
|
92 // Wait until there are more items to process |
|
93 if ( lexerFinished == false ) |
|
94 { |
|
95 ReportEvent( TEvent.EEventGroupingPaused, SymToken.NullToken() ); |
|
96 iSemaphore.Wait(); |
|
97 ReportEvent( TEvent.EEventGroupingStarted, SymToken.NullToken() ); |
|
98 } |
|
99 } |
|
100 while ( lexerFinished == false ); |
|
101 |
|
102 ReportEvent( TEvent.EEventGroupingComplete, SymToken.NullToken() ); |
|
103 } |
|
104 #endregion |
|
105 |
|
106 #region Event handlers |
|
107 private void LexerTokenHandler( SymLexer aLexer, SymLexer.TEvent aEvent, SymToken aToken ) |
|
108 { |
|
109 if ( aEvent == SymLexer.TEvent.EEventLexingToken ) |
|
110 { |
|
111 // Store the token |
|
112 lock( this ) |
|
113 { |
|
114 iMastermind.EnqueueLexedToken( aToken ); |
|
115 } |
|
116 |
|
117 // and signal the worker thread if it is waiting... |
|
118 if ( iSemaphore.Count == 0 ) |
|
119 { |
|
120 iSemaphore.Signal(); |
|
121 } |
|
122 } |
|
123 else if ( aEvent == SymLexer.TEvent.EEventLexingComplete ) |
|
124 { |
|
125 lock( this ) |
|
126 { |
|
127 iLexerFinished = true; |
|
128 } |
|
129 |
|
130 // and signal the worker thread if it is waiting... |
|
131 if ( iSemaphore.Count == 0 ) |
|
132 { |
|
133 iSemaphore.Signal(); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 private void MastermindObserver( SymGrouperMastermind.TEvent aEvent, SymToken aGroupedToken ) |
|
139 { |
|
140 // We've received a token from the grouper. Pass it on to our observer to handle. |
|
141 if ( aEvent == SymGrouperMastermind.TEvent.EEventGroupTokenReady ) |
|
142 { |
|
143 ReportEvent( TEvent.EEventGroupingTokenReady, aGroupedToken ); |
|
144 } |
|
145 } |
|
146 #endregion |
|
147 |
|
148 #region Data members |
|
149 private Thread iWorkerThread; |
|
150 private SymGrouperMastermind iMastermind = new SymGrouperMastermind(); |
|
151 private SymSemaphore iSemaphore = new SymSemaphore( 0, 1 ); |
|
152 private bool iLexerFinished = false; |
|
153 #endregion |
|
154 } |
|
155 } |