corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
4 *
5 * - Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 //#define SMARTHREADPOOL_REF
28  
29 using System;
30 using System.IO;
31 using System.Reflection;
32  
33 #if SMARTHREADPOOL_REF
34 using Amib.Threading;
35 #else
36 using System.Threading;
37 #endif
38  
39 namespace OpenMetaverse
40 {
41  
42 // Use statically referenced SmartThreadPool.dll
43 #if SMARTHREADPOOL_REF
44 public static class WorkPool
45 {
46 internal static SmartThreadPool Pool = null;
47  
48 public static bool Init(bool useSmartThredPool)
49 {
50 if (Pool == null)
51 {
52 STPStartInfo param = new STPStartInfo();
53 param.MinWorkerThreads = 2;
54 param.MaxWorkerThreads = 50;
55 param.ThreadPoolName = "LibOpenMetaverse Main ThreadPool";
56 param.AreThreadsBackground = true;
57  
58 Pool = new SmartThreadPool(param);
59 }
60 return true;
61 }
62  
63 public static void Shutdown()
64 {
65 if (Pool != null)
66 {
67 Pool.Shutdown();
68 Pool = null;
69 }
70 }
71  
72 public static void QueueUserWorkItem(System.Threading.WaitCallback callback)
73 {
74 if (Pool != null)
75 {
76 Pool.QueueWorkItem(state => { callback.Invoke(state); return null; });
77 }
78 else
79 {
80 System.Threading.ThreadPool.QueueUserWorkItem(state => callback.Invoke(state));
81 }
82 }
83  
84 public static void QueueUserWorkItem(System.Threading.WaitCallback callback, object state)
85 {
86 if (Pool != null)
87 {
88 Pool.QueueWorkItem(sync => { callback.Invoke(sync); return null; }, state);
89 }
90 else
91 {
92 System.Threading.ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync), state);
93 }
94 }
95 }
96  
97 #else
98  
99 // Try to load SmartThreadPool.dll during initialization
100 // Fallback to System.Threading.ThreadPool if that fails
101 public static class WorkPoolDynamic
102 {
103 internal static object Pool = null;
104  
105 private static Type SmartThreadPoolType;
106 private static Type WorkItemCallbackType;
107 private static MethodInfo QueueWorkItemFunc, QueueWorkItemFunc2;
108 private static MethodInfo ShutdownFunc;
109 private static Func<System.Threading.WaitCallback, object, object> Invoker;
110  
111 public static bool Init()
112 {
113 try
114 {
115 string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
116 Assembly assembly = Assembly.LoadFile(Path.Combine(dir, "SmartThreadPool.dll"));
117 Type STPStartInfo = assembly.GetType("Amib.Threading.STPStartInfo");
118 SmartThreadPoolType = assembly.GetType("Amib.Threading.SmartThreadPool");
119 WorkItemCallbackType = assembly.GetType("Amib.Threading.WorkItemCallback");
120 var param = Activator.CreateInstance(STPStartInfo);
121 STPStartInfo.GetProperty("MinWorkerThreads").SetValue(param, 2, null);
122 STPStartInfo.GetProperty("MaxWorkerThreads").SetValue(param, 50, null);
123 STPStartInfo.GetProperty("ThreadPoolName").SetValue(param, "LibOpenMetaverse Main ThreadPool", null);
124 STPStartInfo.GetProperty("AreThreadsBackground").SetValue(param, true, null);
125 STPStartInfo.GetProperty("MinWorkerThreads").SetValue(param, 2, null);
126 Pool = Activator.CreateInstance(SmartThreadPoolType, new object[] { param });
127 QueueWorkItemFunc = SmartThreadPoolType.GetMethod("QueueWorkItem", new Type[] { WorkItemCallbackType });
128 QueueWorkItemFunc2 = SmartThreadPoolType.GetMethod("QueueWorkItem", new Type[] { WorkItemCallbackType, typeof(object) });
129 ShutdownFunc = SmartThreadPoolType.GetMethod("Shutdown", new Type[] { });
130  
131 Invoker = (inv, state) =>
132 {
133 inv.Invoke(state);
134 return null;
135 };
136  
137 return true;
138 }
139 catch
140 {
141 Pool = null;
142 return false;
143 }
144 }
145  
146 public static void Shutdown()
147 {
148 if (Pool != null)
149 {
150 ShutdownFunc.Invoke(Pool, null);
151 Pool = null;
152 }
153 }
154  
155  
156 public static void QueueUserWorkItem(System.Threading.WaitCallback callback)
157 {
158 if (Pool != null)
159 {
160 QueueWorkItemFunc.Invoke(Pool, new object[] { Delegate.CreateDelegate(WorkItemCallbackType, callback, Invoker.Method) });
161 }
162 else
163 {
164 System.Threading.ThreadPool.QueueUserWorkItem(state => callback.Invoke(state));
165 }
166 }
167  
168 public static void QueueUserWorkItem(System.Threading.WaitCallback callback, object state)
169 {
170 if (Pool != null)
171 {
172 QueueWorkItemFunc2.Invoke(Pool, new object[] { Delegate.CreateDelegate(WorkItemCallbackType, callback, Invoker.Method), state });
173 }
174 else
175 {
176 System.Threading.ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync), state);
177 }
178 }
179 }
180  
181  
182 public static class WorkPool
183 {
184 private static bool UseSmartThreadPool = false;
185  
186 public static bool Init(bool useSmartThredPool)
187 {
188 if (useSmartThredPool)
189 {
190 if (WorkPoolDynamic.Init())
191 {
192 UseSmartThreadPool = true;
193 return true;
194 }
195 return false;
196 }
197 return true;
198 }
199  
200 public static void Shutdown()
201 {
202 if (UseSmartThreadPool)
203 {
204 WorkPoolDynamic.Shutdown();
205 UseSmartThreadPool = false;
206 }
207 }
208  
209 public static void QueueUserWorkItem(System.Threading.WaitCallback callback)
210 {
211 if (UseSmartThreadPool)
212 {
213 WorkPoolDynamic.QueueUserWorkItem(sync => callback.Invoke(sync));
214 }
215 else
216 {
217 ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync));
218 }
219 }
220  
221 public static void QueueUserWorkItem(System.Threading.WaitCallback callback, object state)
222 {
223 if (UseSmartThreadPool)
224 {
225 WorkPoolDynamic.QueueUserWorkItem(sync => callback.Invoke(sync), state);
226 }
227 else
228 {
229 ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync), state);
230 }
231 }
232 }
233 #endif
234 }