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