History

Renamed folders and separated GSF.Media and GSF.Net into separate projects.

StephenCWills committed 14 years ago

Commit ac1ef661 parent 45bce55628 files · +733 −461Browse files

Showing the changes to Source/Libraries/GSF.Core/Threading/ManagedThreadPool.cs (1 of 628 files). Show all

Unified · Split

Source/Libraries/GSF.Core/Threading/ManagedThreadPool.cs+368−0
@@ -0,0 +1,368 @@
1//*******************************************************************************************************
2// ManagedThreadPool.cs - Gbtc
3//
4// Tennessee Valley Authority, 2009
5// No copyright is claimed pursuant to 17 USC � 105. All Other Rights Reserved.
6//
7// This software is made freely available under the TVA Open Source Agreement (see below).
8//
9// Code Modification History:
10// -----------------------------------------------------------------------------------------------------
11// 01/25/2008 - J. Ritchie Carroll
12// Initial version of source generated.
13// 09/11/2008 - J. Ritchie Carroll
14// Converted to C#.
15// 09/14/2009 - Stephen C. Wills
16// Added new header and license agreement.
17//
18//*******************************************************************************************************
19
20#region [ TVA Open Source Agreement ]
21/*
22
23 THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE,REPRODUCTION, DISTRIBUTION,
24 MODIFICATION AND REDISTRIBUTION OF CERTAIN COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE
25 TENNESSEE VALLEY AUTHORITY, A CORPORATE AGENCY AND INSTRUMENTALITY OF THE UNITED STATES GOVERNMENT
26 ("GOVERNMENT AGENCY"). GOVERNMENT AGENCY IS AN INTENDED THIRD-PARTY BENEFICIARY OF ALL SUBSEQUENT
27 DISTRIBUTIONS OR REDISTRIBUTIONS OF THE SUBJECT SOFTWARE. ANYONE WHO USES, REPRODUCES, DISTRIBUTES,
28 MODIFIES OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED HEREIN, OR ANY PART THEREOF, IS, BY THAT
29 ACTION, ACCEPTING IN FULL THE RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
30
31 Original Software Designation: openPDC
32 Original Software Title: The GSF Open Source Phasor Data Concentrator
33 User Registration Requested. Please Visit https://naspi.tva.com/Registration/
34 Point of Contact for Original Software: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>
35
36 1. DEFINITIONS
37
38 A. "Contributor" means Government Agency, as the developer of the Original Software, and any entity
39 that makes a Modification.
40
41 B. "Covered Patents" mean patent claims licensable by a Contributor that are necessarily infringed by
42 the use or sale of its Modification alone or when combined with the Subject Software.
43
44 C. "Display" means the showing of a copy of the Subject Software, either directly or by means of an
45 image, or any other device.
46
47 D. "Distribution" means conveyance or transfer of the Subject Software, regardless of means, to
48 another.
49
50 E. "Larger Work" means computer software that combines Subject Software, or portions thereof, with
51 software separate from the Subject Software that is not governed by the terms of this Agreement.
52
53 F. "Modification" means any alteration of, including addition to or deletion from, the substance or
54 structure of either the Original Software or Subject Software, and includes derivative works, as that
55 term is defined in the Copyright Statute, 17 USC � 101. However, the act of including Subject Software
56 as part of a Larger Work does not in and of itself constitute a Modification.
57
58 G. "Original Software" means the computer software first released under this Agreement by Government
59 Agency entitled openPDC, including source code, object code and accompanying documentation, if any.
60
61 H. "Recipient" means anyone who acquires the Subject Software under this Agreement, including all
62 Contributors.
63
64 I. "Redistribution" means Distribution of the Subject Software after a Modification has been made.
65
66 J. "Reproduction" means the making of a counterpart, image or copy of the Subject Software.
67
68 K. "Sale" means the exchange of the Subject Software for money or equivalent value.
69
70 L. "Subject Software" means the Original Software, Modifications, or any respective parts thereof.
71
72 M. "Use" means the application or employment of the Subject Software for any purpose.
73
74 2. GRANT OF RIGHTS
75
76 A. Under Non-Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor,
77 with respect to its own contribution to the Subject Software, hereby grants to each Recipient a
78 non-exclusive, world-wide, royalty-free license to engage in the following activities pertaining to
79 the Subject Software:
80
81 1. Use
82
83 2. Distribution
84
85 3. Reproduction
86
87 4. Modification
88
89 5. Redistribution
90
91 6. Display
92
93 B. Under Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, with
94 respect to its own contribution to the Subject Software, hereby grants to each Recipient under Covered
95 Patents a non-exclusive, world-wide, royalty-free license to engage in the following activities
96 pertaining to the Subject Software:
97
98 1. Use
99
100 2. Distribution
101
102 3. Reproduction
103
104 4. Sale
105
106 5. Offer for Sale
107
108 C. The rights granted under Paragraph B. also apply to the combination of a Contributor's Modification
109 and the Subject Software if, at the time the Modification is added by the Contributor, the addition of
110 such Modification causes the combination to be covered by the Covered Patents. It does not apply to
111 any other combinations that include a Modification.
112
113 D. The rights granted in Paragraphs A. and B. allow the Recipient to sublicense those same rights.
114 Such sublicense must be under the same terms and conditions of this Agreement.
115
116 3. OBLIGATIONS OF RECIPIENT
117
118 A. Distribution or Redistribution of the Subject Software must be made under this Agreement except for
119 additions covered under paragraph 3H.
120
121 1. Whenever a Recipient distributes or redistributes the Subject Software, a copy of this Agreement
122 must be included with each copy of the Subject Software; and
123
124 2. If Recipient distributes or redistributes the Subject Software in any form other than source code,
125 Recipient must also make the source code freely available, and must provide with each copy of the
126 Subject Software information on how to obtain the source code in a reasonable manner on or through a
127 medium customarily used for software exchange.
128
129 B. Each Recipient must ensure that the following copyright notice appears prominently in the Subject
130 Software:
131
132 No copyright is claimed pursuant to 17 USC � 105. All Other Rights Reserved.
133
134 C. Each Contributor must characterize its alteration of the Subject Software as a Modification and
135 must identify itself as the originator of its Modification in a manner that reasonably allows
136 subsequent Recipients to identify the originator of the Modification. In fulfillment of these
137 requirements, Contributor must include a file (e.g., a change log file) that describes the alterations
138 made and the date of the alterations, identifies Contributor as originator of the alterations, and
139 consents to characterization of the alterations as a Modification, for example, by including a
140 statement that the Modification is derived, directly or indirectly, from Original Software provided by
141 Government Agency. Once consent is granted, it may not thereafter be revoked.
142
143 D. A Contributor may add its own copyright notice to the Subject Software. Once a copyright notice has
144 been added to the Subject Software, a Recipient may not remove it without the express permission of
145 the Contributor who added the notice.
146
147 E. A Recipient may not make any representation in the Subject Software or in any promotional,
148 advertising or other material that may be construed as an endorsement by Government Agency or by any
149 prior Recipient of any product or service provided by Recipient, or that may seek to obtain commercial
150 advantage by the fact of Government Agency's or a prior Recipient's participation in this Agreement.
151
152 F. In an effort to track usage and maintain accurate records of the Subject Software, each Recipient,
153 upon receipt of the Subject Software, is requested to register with Government Agency by visiting the
154 following website: https://naspi.tva.com/Registration/. Recipient's name and personal information
155 shall be used for statistical purposes only. Once a Recipient makes a Modification available, it is
156 requested that the Recipient inform Government Agency at the web site provided above how to access the
157 Modification.
158
159 G. Each Contributor represents that that its Modification does not violate any existing agreements,
160 regulations, statutes or rules, and further that Contributor has sufficient rights to grant the rights
161 conveyed by this Agreement.
162
163 H. A Recipient may choose to offer, and to charge a fee for, warranty, support, indemnity and/or
164 liability obligations to one or more other Recipients of the Subject Software. A Recipient may do so,
165 however, only on its own behalf and not on behalf of Government Agency or any other Recipient. Such a
166 Recipient must make it absolutely clear that any such warranty, support, indemnity and/or liability
167 obligation is offered by that Recipient alone. Further, such Recipient agrees to indemnify Government
168 Agency and every other Recipient for any liability incurred by them as a result of warranty, support,
169 indemnity and/or liability offered by such Recipient.
170
171 I. A Recipient may create a Larger Work by combining Subject Software with separate software not
172 governed by the terms of this agreement and distribute the Larger Work as a single product. In such
173 case, the Recipient must make sure Subject Software, or portions thereof, included in the Larger Work
174 is subject to this Agreement.
175
176 J. Notwithstanding any provisions contained herein, Recipient is hereby put on notice that export of
177 any goods or technical data from the United States may require some form of export license from the
178 U.S. Government. Failure to obtain necessary export licenses may result in criminal liability under
179 U.S. laws. Government Agency neither represents that a license shall not be required nor that, if
180 required, it shall be issued. Nothing granted herein provides any such export license.
181
182 4. DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION
183
184 A. No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER
185 EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT
186 SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
187 PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
188 FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS
189 AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR
190 RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS
191 RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND
192 LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT
193 "AS IS."
194
195 B. Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST GOVERNMENT AGENCY, ITS
196 AGENTS, EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE
197 OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM
198 SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE
199 SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS GOVERNMENT AGENCY, ITS AGENTS,
200 EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY
201 LAW. THE FOREGOING RELEASE AND INDEMNIFICATION SHALL APPLY EVEN IF THE LIABILITIES, DEMANDS, DAMAGES,
202 EXPENSES OR LOSSES ARE CAUSED, OCCASIONED, OR CONTRIBUTED TO BY THE NEGLIGENCE, SOLE OR CONCURRENT, OF
203 GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE
204 IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT.
205
206 5. GENERAL TERMS
207
208 A. Termination: This Agreement and the rights granted hereunder will terminate automatically if a
209 Recipient fails to comply with these terms and conditions, and fails to cure such noncompliance within
210 thirty (30) days of becoming aware of such noncompliance. Upon termination, a Recipient agrees to
211 immediately cease use and distribution of the Subject Software. All sublicenses to the Subject
212 Software properly granted by the breaching Recipient shall survive any such termination of this
213 Agreement.
214
215 B. Severability: If any provision of this Agreement is invalid or unenforceable under applicable law,
216 it shall not affect the validity or enforceability of the remainder of the terms of this Agreement.
217
218 C. Applicable Law: This Agreement shall be subject to United States federal law only for all purposes,
219 including, but not limited to, determining the validity of this Agreement, the meaning of its
220 provisions and the rights, obligations and remedies of the parties.
221
222 D. Entire Understanding: This Agreement constitutes the entire understanding and agreement of the
223 parties relating to release of the Subject Software and may not be superseded, modified or amended
224 except by further written agreement duly executed by the parties.
225
226 E. Binding Authority: By accepting and using the Subject Software under this Agreement, a Recipient
227 affirms its authority to bind the Recipient to all terms and conditions of this Agreement and that
228 Recipient hereby agrees to all terms and conditions herein.
229
230 F. Point of Contact: Any Recipient contact with Government Agency is to be directed to the designated
231 representative as follows: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>.
232
233*/
234#endregion
235
236using System;
237using System.Threading;
238
239namespace GSF.Threading
240{
241 /// <summary>
242 /// Defines a managed thread pool
243 /// </summary>
244 /// <remarks>
245 /// This class works like the normal thread pool but provides the benefit of automatic tracking
246 /// of queued threads through the ManagedThreads collection, returns a reference to the
247 /// queued thread with the ability to dequeue and/or abort, total thread runtime and the
248 /// ability to run the queued thread in an alternate execution context
249 /// </remarks>
250 public static class ManagedThreadPool
251 {
252 /// <summary>
253 /// Queues a work item for processing on the managed thread pool
254 /// </summary>
255 /// <param name="callback">A WaitCallback representing the method to execute.</param>
256 /// <returns>Reference to queued thread</returns>
257 /// <remarks>
258 /// This differs from the normal thread pool QueueUserWorkItem function in that it does
259 /// not return a success value determing if item was queued, but rather a reference to
260 /// to the managed thread that was actually placed on the queue.
261 /// </remarks>
262 public static ManagedThread QueueUserWorkItem(ThreadStart callback)
263 {
264 if ((object)callback == null)
265 throw (new ArgumentNullException("callback"));
266
267 ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, null, null);
268
269 ManagedThreads.Queue(item);
270
271 ThreadPool.QueueUserWorkItem(HandleItem);
272
273 return item;
274 }
275
276 /// <summary>
277 /// Queues a work item for processing on the managed thread pool
278 /// </summary>
279 /// <param name="callback">A WaitCallback representing the method to execute.</param>
280 /// <returns>Reference to queued thread</returns>
281 /// <remarks>
282 /// This differs from the normal thread pool QueueUserWorkItem function in that it does
283 /// not return a success value determing if item was queued, but rather a reference to
284 /// to the managed thread that was actually placed on the queue.
285 /// </remarks>
286 public static ManagedThread QueueUserWorkItem(ParameterizedThreadStart callback)
287 {
288 return QueueUserWorkItem(callback, null);
289 }
290
291 /// <summary>
292 /// Queues a work item for processing on the managed thread pool
293 /// </summary>
294 /// <param name="callback">A WaitCallback representing the method to execute.</param>
295 /// <param name="state">An object containing data to be used by the method.</param>
296 /// <returns>Reference to queued thread</returns>
297 /// <remarks>
298 /// This differs from the normal thread pool QueueUserWorkItem function in that it does
299 /// not return a success value determing if item was queued, but rather a reference to
300 /// to the managed thread that was actually placed on the queue.
301 /// </remarks>
302 public static ManagedThread QueueUserWorkItem(ParameterizedThreadStart callback, object state)
303 {
304 if ((object)callback == null)
305 throw (new ArgumentNullException("callback"));
306
307 ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, null);
308
309 ManagedThreads.Queue(item);
310
311 ThreadPool.QueueUserWorkItem(HandleItem);
312
313 return item;
314 }
315
316 /// <summary>
317 /// Queues a work item for processing on the managed thread pool
318 /// </summary>
319 /// <param name="callback">A WaitCallback representing the method to execute.</param>
320 /// <param name="ctx">Alternate execution context in which to run the thread.</param>
321 /// <returns>Reference to queued thread</returns>
322 /// <remarks>
323 /// This differs from the normal thread pool QueueUserWorkItem function in that it does
324 /// not return a success value determing if item was queued, but rather a reference to
325 /// to the managed thread that was actually placed on the queue.
326 /// </remarks>
327 public static ManagedThread QueueUserWorkItem(ContextCallback callback, ExecutionContext ctx)
328 {
329 return QueueUserWorkItem(callback, null, ctx);
330 }
331
332 /// <summary>
333 /// Queues a work item for processing on the managed thread pool
334 /// </summary>
335 /// <param name="callback">A WaitCallback representing the method to execute.</param>
336 /// <param name="state">An object containing data to be used by the method.</param>
337 /// <param name="ctx">Alternate execution context in which to run the thread.</param>
338 /// <returns>Reference to queued thread</returns>
339 /// <remarks>
340 /// This differs from the normal thread pool QueueUserWorkItem function in that it does
341 /// not return a success value determing if item was queued, but rather a reference to
342 /// to the managed thread that was actually placed on the queue.
343 /// </remarks>
344 public static ManagedThread QueueUserWorkItem(ContextCallback callback, object state, ExecutionContext ctx)
345 {
346 if ((object)callback == null)
347 throw (new ArgumentNullException("callback"));
348
349 ManagedThread item = new ManagedThread(ThreadType.QueuedThread, callback, state, ctx);
350
351 ManagedThreads.Queue(item);
352
353 ThreadPool.QueueUserWorkItem(HandleItem);
354
355 return item;
356 }
357
358 private static void HandleItem(object state)
359 {
360 // Get next queued item
361 ManagedThread item = ManagedThreads.Pop();
362
363 // Execute callback...
364 if ((object)item != null)
365 item.HandleItem();
366 }
367 }
368}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t