WorkItemFactory.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Threading;
  3. using System.Runtime.CompilerServices;
  4. namespace Amib.Threading.Internal
  5. {
  6. #region WorkItemFactory class
  7. public class WorkItemFactory
  8. {
  9. public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemInfo workItemInfo,
  10. WaitCallback callback, object state)
  11. {
  12. ValidateCallback(callback);
  13. ValidateCallback(workItemInfo.PostExecuteWorkItemCallback);
  14. return new WorkItem(workItemsGroup, new WorkItemInfo(workItemInfo), callback, state);
  15. }
  16. public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  17. WaitCallback callback, object state)
  18. {
  19. ValidateCallback(callback);
  20. WorkItemInfo workItemInfo = new()
  21. {
  22. UseCallerCallContext = wigStartInfo.UseCallerCallContext,
  23. PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback,
  24. CallToPostExecute = wigStartInfo.CallToPostExecute,
  25. DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects,
  26. };
  27. return new WorkItem(workItemsGroup, workItemInfo, callback, state);
  28. }
  29. /// <summary>
  30. /// Create a new work item
  31. /// </summary>
  32. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  33. /// <param name="wigStartInfo">Work item group start information</param>
  34. /// <param name="callback">A callback to execute</param>
  35. /// <returns>Returns a work item</returns>
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback)
  38. {
  39. return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null);
  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. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  51. WorkItemInfo workItemInfo, WorkItemCallback callback)
  52. {
  53. return CreateWorkItem(workItemsGroup, wigStartInfo, workItemInfo, callback, null);
  54. }
  55. /// <summary>
  56. /// Create a new work item
  57. /// </summary>
  58. /// <param name="workItemsGroup">The WorkItemsGroup of this workitem</param>
  59. /// <param name="wigStartInfo">Work item group start information</param>
  60. /// <param name="callback">A callback to execute</param>
  61. /// <param name="state">
  62. /// The context object of the work item. Used for passing arguments to the work item.
  63. /// </param>
  64. /// <returns>Returns a work item</returns>
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  67. WorkItemCallback callback, object state)
  68. {
  69. ValidateCallback(callback);
  70. WorkItemInfo workItemInfo = new()
  71. {
  72. UseCallerCallContext = wigStartInfo.UseCallerCallContext,
  73. PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback,
  74. CallToPostExecute = wigStartInfo.CallToPostExecute,
  75. DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects,
  76. };
  77. return new WorkItem( workItemsGroup, workItemInfo, callback, state);
  78. }
  79. /// <summary>
  80. /// Create a new work item
  81. /// </summary>
  82. /// <param name="workItemsGroup">The work items group</param>
  83. /// <param name="wigStartInfo">Work item group start information</param>
  84. /// <param name="workItemInfo">Work item information</param>
  85. /// <param name="callback">A callback to execute</param>
  86. /// <param name="state">
  87. /// The context object of the work item. Used for passing arguments to the work item.
  88. /// </param>
  89. /// <returns>Returns a work item</returns>
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemInfo workItemInfo,
  92. WorkItemCallback callback, object state)
  93. {
  94. ValidateCallback(callback);
  95. ValidateCallback(workItemInfo.PostExecuteWorkItemCallback);
  96. WorkItem workItem = new(
  97. workItemsGroup,
  98. new WorkItemInfo(workItemInfo),
  99. callback,
  100. state);
  101. return workItem;
  102. }
  103. /// <summary>
  104. /// Create a new work item
  105. /// </summary>
  106. /// <param name="workItemsGroup">The work items group</param>
  107. /// <param name="wigStartInfo">Work item group start information</param>
  108. /// <param name="callback">A callback to execute</param>
  109. /// <param name="state">
  110. /// The context object of the work item. Used for passing arguments to the work item.
  111. /// </param>
  112. /// <param name="postExecuteWorkItemCallback">
  113. /// A delegate to call after the callback completion
  114. /// </param>
  115. /// <returns>Returns a work item</returns>
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo,
  118. WorkItemCallback callback, object state,PostExecuteWorkItemCallback postExecuteWorkItemCallback)
  119. {
  120. ValidateCallback(callback);
  121. ValidateCallback(postExecuteWorkItemCallback);
  122. WorkItemInfo workItemInfo = new()
  123. {
  124. UseCallerCallContext = wigStartInfo.UseCallerCallContext,
  125. PostExecuteWorkItemCallback = postExecuteWorkItemCallback,
  126. CallToPostExecute = wigStartInfo.CallToPostExecute,
  127. DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects
  128. };
  129. return new WorkItem( workItemsGroup, workItemInfo, callback, state);
  130. }
  131. /// <summary>
  132. /// Create a new work item
  133. /// </summary>
  134. /// <param name="workItemsGroup">The work items group</param>
  135. /// <param name="wigStartInfo">Work item group start 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. /// <param name="postExecuteWorkItemCallback">
  141. /// A delegate to call after the callback completion
  142. /// </param>
  143. /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
  144. /// <returns>Returns a work item</returns>
  145. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  146. public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup,WIGStartInfo wigStartInfo,
  147. WorkItemCallback callback, object state,
  148. PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute)
  149. {
  150. ValidateCallback(callback);
  151. ValidateCallback(postExecuteWorkItemCallback);
  152. WorkItemInfo workItemInfo = new()
  153. {
  154. UseCallerCallContext = wigStartInfo.UseCallerCallContext,
  155. PostExecuteWorkItemCallback = postExecuteWorkItemCallback,
  156. CallToPostExecute = callToPostExecute,
  157. DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects
  158. };
  159. return new WorkItem(workItemsGroup, workItemInfo, callback, state);
  160. }
  161. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  162. private static void ValidateCallback(Delegate callback)
  163. {
  164. if (callback is not null && callback.GetInvocationList().Length > 1)
  165. {
  166. throw new NotSupportedException("SmartThreadPool doesn't support delegates chains");
  167. }
  168. }
  169. }
  170. #endregion
  171. }