~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Projects.Formats.MSBuild/MonoDevelop.Projects.Formats.MSBuild/BuildEngine.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
using System.Runtime.Remoting;
32
32
using System.Collections.Generic;
33
33
using System.Collections;
 
34
using Microsoft.Build.BuildEngine;
 
35
using Microsoft.Build.Framework;
34
36
 
35
37
namespace MonoDevelop.Projects.Formats.MSBuild
36
38
{
37
39
        public class BuildEngine: MarshalByRefObject, IBuildEngine
38
40
        {
 
41
                static AutoResetEvent wordDoneEvent = new AutoResetEvent (false);
 
42
                static ThreadStart workDelegate;
 
43
                static object workLock = new object ();
 
44
                static Thread workThread;
 
45
                static Exception workError;
 
46
 
39
47
                ManualResetEvent doneEvent = new ManualResetEvent (false);
40
 
                
 
48
                Dictionary<string,Engine> engines = new Dictionary<string, Engine> ();
 
49
 
41
50
                public void Dispose ()
42
51
                {
43
52
                        doneEvent.Set ();
46
55
                internal WaitHandle WaitHandle {
47
56
                        get { return doneEvent; }
48
57
                }
49
 
                
 
58
 
50
59
                public IProjectBuilder LoadProject (string file, string binDir)
51
60
                {
52
 
                        return new ProjectBuilder (file, binDir);
 
61
                        return new ProjectBuilder (this, GetEngine (binDir), file);
53
62
                }
54
63
                
55
64
                public void UnloadProject (IProjectBuilder pb)
56
65
                {
 
66
                        ((ProjectBuilder)pb).Dispose ();
57
67
                        RemotingServices.Disconnect ((MarshalByRefObject) pb);
58
68
                }
59
69
                
61
71
                {
62
72
                        return null;
63
73
                }
 
74
 
 
75
                Engine GetEngine (string binDir)
 
76
                {
 
77
                        Engine engine = null;
 
78
                        RunSTA (delegate {
 
79
                                if (!engines.TryGetValue (binDir, out engine)) {
 
80
                                        engine = new Engine (binDir);
 
81
                                        engine.GlobalProperties.SetProperty ("BuildingInsideVisualStudio", "true");
 
82
                                        
 
83
                                        //we don't have host compilers in MD, and this is set to true by some of the MS targets
 
84
                                        //which causes it to always run the CoreCompile task if BuildingInsideVisualStudio is also
 
85
                                        //true, because the VS in-process compiler would take care of the deps tracking
 
86
                                        engine.GlobalProperties.SetProperty ("UseHostCompilerIfAvailable", "false");
 
87
                                        engines [binDir] = engine;
 
88
                                }
 
89
                        });
 
90
                        return engine;
 
91
                }
 
92
 
 
93
                internal void UnloadProject (string file)
 
94
                {
 
95
                        RunSTA (delegate {
 
96
                                foreach (var engine in engines.Values) {
 
97
                                        var loadedProj = engine.GetLoadedProject (file);
 
98
                                        if (loadedProj != null)
 
99
                                                engine.UnloadProject (loadedProj);
 
100
                                }
 
101
                        });
 
102
                }
 
103
 
 
104
                internal static void RunSTA (ThreadStart ts)
 
105
                {
 
106
                        lock (workLock) {
 
107
                                lock (threadLock) {
 
108
                                        workDelegate = ts;
 
109
                                        workError = null;
 
110
                                        if (workThread == null) {
 
111
                                                workThread = new Thread (STARunner);
 
112
                                                workThread.SetApartmentState (ApartmentState.STA);
 
113
                                                workThread.IsBackground = true;
 
114
                                                workThread.Start ();
 
115
                                        }
 
116
                                        else
 
117
                                                // Awaken the existing thread
 
118
                                                Monitor.Pulse (threadLock);
 
119
                                }
 
120
                                wordDoneEvent.WaitOne ();
 
121
                        }
 
122
                        if (workError != null)
 
123
                                throw new Exception ("MSBuild operation failed", workError);
 
124
                }
 
125
 
 
126
                static object threadLock = new object ();
 
127
                
 
128
                static void STARunner ()
 
129
                {
 
130
                        lock (threadLock) {
 
131
                                do {
 
132
                                        try {
 
133
                                                workDelegate ();
 
134
                                        }
 
135
                                        catch (Exception ex) {
 
136
                                                workError = ex;
 
137
                                        }
 
138
                                        wordDoneEvent.Set ();
 
139
                                }
 
140
                                while (Monitor.Wait (threadLock, 60000));
 
141
                                
 
142
                                workThread = null;
 
143
                        }
 
144
                }
64
145
        }
65
146
}
 
 
b'\\ No newline at end of file'