diff options
Diffstat (limited to 'ThirdParty/SmartThreadPool/WorkItemFactory.cs')
-rw-r--r-- | ThirdParty/SmartThreadPool/WorkItemFactory.cs | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/ThirdParty/SmartThreadPool/WorkItemFactory.cs b/ThirdParty/SmartThreadPool/WorkItemFactory.cs new file mode 100644 index 0000000..471eb20 --- /dev/null +++ b/ThirdParty/SmartThreadPool/WorkItemFactory.cs | |||
@@ -0,0 +1,343 @@ | |||
1 | using System; | ||
2 | |||
3 | namespace Amib.Threading.Internal | ||
4 | { | ||
5 | #region WorkItemFactory class | ||
6 | |||
7 | public class WorkItemFactory | ||
8 | { | ||
9 | /// <summary> | ||
10 | /// Create a new work item | ||
11 | /// </summary> | ||
12 | /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param> | ||
13 | /// <param name="wigStartInfo">Work item group start information</param> | ||
14 | /// <param name="callback">A callback to execute</param> | ||
15 | /// <returns>Returns a work item</returns> | ||
16 | public static WorkItem CreateWorkItem( | ||
17 | IWorkItemsGroup workItemsGroup, | ||
18 | WIGStartInfo wigStartInfo, | ||
19 | WorkItemCallback callback) | ||
20 | { | ||
21 | return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null); | ||
22 | } | ||
23 | |||
24 | /// <summary> | ||
25 | /// Create a new work item | ||
26 | /// </summary> | ||
27 | /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param> | ||
28 | /// <param name="wigStartInfo">Work item group start information</param> | ||
29 | /// <param name="callback">A callback to execute</param> | ||
30 | /// <param name="workItemPriority">The priority of the work item</param> | ||
31 | /// <returns>Returns a work item</returns> | ||
32 | public static WorkItem CreateWorkItem( | ||
33 | IWorkItemsGroup workItemsGroup, | ||
34 | WIGStartInfo wigStartInfo, | ||
35 | WorkItemCallback callback, | ||
36 | WorkItemPriority workItemPriority) | ||
37 | { | ||
38 | return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null, workItemPriority); | ||
39 | } | ||
40 | |||
41 | /// <summary> | ||
42 | /// Create a new work item | ||
43 | /// </summary> | ||
44 | /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param> | ||
45 | /// <param name="wigStartInfo">Work item group start information</param> | ||
46 | /// <param name="workItemInfo">Work item info</param> | ||
47 | /// <param name="callback">A callback to execute</param> | ||
48 | /// <returns>Returns a work item</returns> | ||
49 | public static WorkItem CreateWorkItem( | ||
50 | IWorkItemsGroup workItemsGroup, | ||
51 | WIGStartInfo wigStartInfo, | ||
52 | WorkItemInfo workItemInfo, | ||
53 | WorkItemCallback callback) | ||
54 | { | ||
55 | return CreateWorkItem( | ||
56 | workItemsGroup, | ||
57 | wigStartInfo, | ||
58 | workItemInfo, | ||
59 | callback, | ||
60 | null); | ||
61 | } | ||
62 | |||
63 | /// <summary> | ||
64 | /// Create a new work item | ||
65 | /// </summary> | ||
66 | /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param> | ||
67 | /// <param name="wigStartInfo">Work item group start information</param> | ||
68 | /// <param name="callback">A callback to execute</param> | ||
69 | /// <param name="state"> | ||
70 | /// The context object of the work item. Used for passing arguments to the work item. | ||
71 | /// </param> | ||
72 | /// <returns>Returns a work item</returns> | ||
73 | public static WorkItem CreateWorkItem( | ||
74 | IWorkItemsGroup workItemsGroup, | ||
75 | WIGStartInfo wigStartInfo, | ||
76 | WorkItemCallback callback, | ||
77 | object state) | ||
78 | { | ||
79 | ValidateCallback(callback); | ||
80 | |||
81 | WorkItemInfo workItemInfo = new WorkItemInfo(); | ||
82 | workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; | ||
83 | workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; | ||
84 | workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; | ||
85 | workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; | ||
86 | workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; | ||
87 | workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; | ||
88 | |||
89 | WorkItem workItem = new WorkItem( | ||
90 | workItemsGroup, | ||
91 | workItemInfo, | ||
92 | callback, | ||
93 | state); | ||
94 | return workItem; | ||
95 | } | ||
96 | |||
97 | /// <summary> | ||
98 | /// Create a new work item | ||
99 | /// </summary> | ||
100 | /// <param name="workItemsGroup">The work items group</param> | ||
101 | /// <param name="wigStartInfo">Work item group start information</param> | ||
102 | /// <param name="callback">A callback to execute</param> | ||
103 | /// <param name="state"> | ||
104 | /// The context object of the work item. Used for passing arguments to the work item. | ||
105 | /// </param> | ||
106 | /// <param name="workItemPriority">The work item priority</param> | ||
107 | /// <returns>Returns a work item</returns> | ||
108 | public static WorkItem CreateWorkItem( | ||
109 | IWorkItemsGroup workItemsGroup, | ||
110 | WIGStartInfo wigStartInfo, | ||
111 | WorkItemCallback callback, | ||
112 | object state, | ||
113 | WorkItemPriority workItemPriority) | ||
114 | { | ||
115 | ValidateCallback(callback); | ||
116 | |||
117 | WorkItemInfo workItemInfo = new WorkItemInfo(); | ||
118 | workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; | ||
119 | workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; | ||
120 | workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; | ||
121 | workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; | ||
122 | workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; | ||
123 | workItemInfo.WorkItemPriority = workItemPriority; | ||
124 | |||
125 | WorkItem workItem = new WorkItem( | ||
126 | workItemsGroup, | ||
127 | workItemInfo, | ||
128 | callback, | ||
129 | state); | ||
130 | |||
131 | return workItem; | ||
132 | } | ||
133 | |||
134 | /// <summary> | ||
135 | /// Create a new work item | ||
136 | /// </summary> | ||
137 | /// <param name="workItemsGroup">The work items group</param> | ||
138 | /// <param name="wigStartInfo">Work item group start information</param> | ||
139 | /// <param name="workItemInfo">Work item information</param> | ||
140 | /// <param name="callback">A callback to execute</param> | ||
141 | /// <param name="state"> | ||
142 | /// The context object of the work item. Used for passing arguments to the work item. | ||
143 | /// </param> | ||
144 | /// <returns>Returns a work item</returns> | ||
145 | public static WorkItem CreateWorkItem( | ||
146 | IWorkItemsGroup workItemsGroup, | ||
147 | WIGStartInfo wigStartInfo, | ||
148 | WorkItemInfo workItemInfo, | ||
149 | WorkItemCallback callback, | ||
150 | object state) | ||
151 | { | ||
152 | ValidateCallback(callback); | ||
153 | ValidateCallback(workItemInfo.PostExecuteWorkItemCallback); | ||
154 | |||
155 | WorkItem workItem = new WorkItem( | ||
156 | workItemsGroup, | ||
157 | new WorkItemInfo(workItemInfo), | ||
158 | callback, | ||
159 | state); | ||
160 | |||
161 | return workItem; | ||
162 | } | ||
163 | |||
164 | /// <summary> | ||
165 | /// Create a new work item | ||
166 | /// </summary> | ||
167 | /// <param name="workItemsGroup">The work items group</param> | ||
168 | /// <param name="wigStartInfo">Work item group start information</param> | ||
169 | /// <param name="callback">A callback to execute</param> | ||
170 | /// <param name="state"> | ||
171 | /// The context object of the work item. Used for passing arguments to the work item. | ||
172 | /// </param> | ||
173 | /// <param name="postExecuteWorkItemCallback"> | ||
174 | /// A delegate to call after the callback completion | ||
175 | /// </param> | ||
176 | /// <returns>Returns a work item</returns> | ||
177 | public static WorkItem CreateWorkItem( | ||
178 | IWorkItemsGroup workItemsGroup, | ||
179 | WIGStartInfo wigStartInfo, | ||
180 | WorkItemCallback callback, | ||
181 | object state, | ||
182 | PostExecuteWorkItemCallback postExecuteWorkItemCallback) | ||
183 | { | ||
184 | ValidateCallback(callback); | ||
185 | ValidateCallback(postExecuteWorkItemCallback); | ||
186 | |||
187 | WorkItemInfo workItemInfo = new WorkItemInfo(); | ||
188 | workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; | ||
189 | workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; | ||
190 | workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; | ||
191 | workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; | ||
192 | workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; | ||
193 | workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; | ||
194 | |||
195 | WorkItem workItem = new WorkItem( | ||
196 | workItemsGroup, | ||
197 | workItemInfo, | ||
198 | callback, | ||
199 | state); | ||
200 | |||
201 | return workItem; | ||
202 | } | ||
203 | |||
204 | /// <summary> | ||
205 | /// Create a new work item | ||
206 | /// </summary> | ||
207 | /// <param name="workItemsGroup">The work items group</param> | ||
208 | /// <param name="wigStartInfo">Work item group start information</param> | ||
209 | /// <param name="callback">A callback to execute</param> | ||
210 | /// <param name="state"> | ||
211 | /// The context object of the work item. Used for passing arguments to the work item. | ||
212 | /// </param> | ||
213 | /// <param name="postExecuteWorkItemCallback"> | ||
214 | /// A delegate to call after the callback completion | ||
215 | /// </param> | ||
216 | /// <param name="workItemPriority">The work item priority</param> | ||
217 | /// <returns>Returns a work item</returns> | ||
218 | public static WorkItem CreateWorkItem( | ||
219 | IWorkItemsGroup workItemsGroup, | ||
220 | WIGStartInfo wigStartInfo, | ||
221 | WorkItemCallback callback, | ||
222 | object state, | ||
223 | PostExecuteWorkItemCallback postExecuteWorkItemCallback, | ||
224 | WorkItemPriority workItemPriority) | ||
225 | { | ||
226 | ValidateCallback(callback); | ||
227 | ValidateCallback(postExecuteWorkItemCallback); | ||
228 | |||
229 | WorkItemInfo workItemInfo = new WorkItemInfo(); | ||
230 | workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; | ||
231 | workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; | ||
232 | workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; | ||
233 | workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; | ||
234 | workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; | ||
235 | workItemInfo.WorkItemPriority = workItemPriority; | ||
236 | |||
237 | WorkItem workItem = new WorkItem( | ||
238 | workItemsGroup, | ||
239 | workItemInfo, | ||
240 | callback, | ||
241 | state); | ||
242 | |||
243 | return workItem; | ||
244 | } | ||
245 | |||
246 | /// <summary> | ||
247 | /// Create a new work item | ||
248 | /// </summary> | ||
249 | /// <param name="workItemsGroup">The work items group</param> | ||
250 | /// <param name="wigStartInfo">Work item group start information</param> | ||
251 | /// <param name="callback">A callback to execute</param> | ||
252 | /// <param name="state"> | ||
253 | /// The context object of the work item. Used for passing arguments to the work item. | ||
254 | /// </param> | ||
255 | /// <param name="postExecuteWorkItemCallback"> | ||
256 | /// A delegate to call after the callback completion | ||
257 | /// </param> | ||
258 | /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param> | ||
259 | /// <returns>Returns a work item</returns> | ||
260 | public static WorkItem CreateWorkItem( | ||
261 | IWorkItemsGroup workItemsGroup, | ||
262 | WIGStartInfo wigStartInfo, | ||
263 | WorkItemCallback callback, | ||
264 | object state, | ||
265 | PostExecuteWorkItemCallback postExecuteWorkItemCallback, | ||
266 | CallToPostExecute callToPostExecute) | ||
267 | { | ||
268 | ValidateCallback(callback); | ||
269 | ValidateCallback(postExecuteWorkItemCallback); | ||
270 | |||
271 | WorkItemInfo workItemInfo = new WorkItemInfo(); | ||
272 | workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; | ||
273 | workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; | ||
274 | workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; | ||
275 | workItemInfo.CallToPostExecute = callToPostExecute; | ||
276 | workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; | ||
277 | workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; | ||
278 | |||
279 | WorkItem workItem = new WorkItem( | ||
280 | workItemsGroup, | ||
281 | workItemInfo, | ||
282 | callback, | ||
283 | state); | ||
284 | |||
285 | return workItem; | ||
286 | } | ||
287 | |||
288 | /// <summary> | ||
289 | /// Create a new work item | ||
290 | /// </summary> | ||
291 | /// <param name="workItemsGroup">The work items group</param> | ||
292 | /// <param name="wigStartInfo">Work item group start information</param> | ||
293 | /// <param name="callback">A callback to execute</param> | ||
294 | /// <param name="state"> | ||
295 | /// The context object of the work item. Used for passing arguments to the work item. | ||
296 | /// </param> | ||
297 | /// <param name="postExecuteWorkItemCallback"> | ||
298 | /// A delegate to call after the callback completion | ||
299 | /// </param> | ||
300 | /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param> | ||
301 | /// <param name="workItemPriority">The work item priority</param> | ||
302 | /// <returns>Returns a work item</returns> | ||
303 | public static WorkItem CreateWorkItem( | ||
304 | IWorkItemsGroup workItemsGroup, | ||
305 | WIGStartInfo wigStartInfo, | ||
306 | WorkItemCallback callback, | ||
307 | object state, | ||
308 | PostExecuteWorkItemCallback postExecuteWorkItemCallback, | ||
309 | CallToPostExecute callToPostExecute, | ||
310 | WorkItemPriority workItemPriority) | ||
311 | { | ||
312 | |||
313 | ValidateCallback(callback); | ||
314 | ValidateCallback(postExecuteWorkItemCallback); | ||
315 | |||
316 | WorkItemInfo workItemInfo = new WorkItemInfo(); | ||
317 | workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; | ||
318 | workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; | ||
319 | workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; | ||
320 | workItemInfo.CallToPostExecute = callToPostExecute; | ||
321 | workItemInfo.WorkItemPriority = workItemPriority; | ||
322 | workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; | ||
323 | |||
324 | WorkItem workItem = new WorkItem( | ||
325 | workItemsGroup, | ||
326 | workItemInfo, | ||
327 | callback, | ||
328 | state); | ||
329 | |||
330 | return workItem; | ||
331 | } | ||
332 | |||
333 | private static void ValidateCallback(Delegate callback) | ||
334 | { | ||
335 | if (callback != null && callback.GetInvocationList().Length > 1) | ||
336 | { | ||
337 | throw new NotSupportedException("SmartThreadPool doesn't support delegates chains"); | ||
338 | } | ||
339 | } | ||
340 | } | ||
341 | |||
342 | #endregion | ||
343 | } | ||