├── .gitignore ├── LICENSE ├── README.md ├── demo ├── PoolDemo.ico ├── PoolDemo.lpi ├── PoolDemo.lpr ├── mainform.lfm └── mainform.pp └── src ├── gsynclist.pp ├── hpcommons.pp ├── hptaskapi.pp ├── hpthread.pp ├── hpthreadpool.pp ├── hpwork.pp ├── hpworkengine.pp └── synclist.pp /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/lazarus 2 | # Edit at https://www.gitignore.io/?templates=lazarus 3 | 4 | ### Lazarus ### 5 | # Lazarus compiler-generated binaries (safe to delete) 6 | *.exe 7 | *.dll 8 | *.so 9 | *.dylib 10 | *.lrs 11 | *.res 12 | *.compiled 13 | *.dbg 14 | *.ppu 15 | *.o 16 | *.or 17 | *.a 18 | 19 | # Lazarus autogenerated files (duplicated info) 20 | *.rst 21 | *.rsj 22 | *.lrt 23 | 24 | # Lazarus local files (user-specific info) 25 | *.lps 26 | 27 | # Lazarus backups and unit output folders. 28 | # These can be changed by user in Lazarus/project options. 29 | backup/ 30 | *.bak 31 | lib/ 32 | 33 | # Application bundle for Mac OS 34 | *.app/ 35 | 36 | # End of https://www.gitignore.io/api/lazarus -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Version](https://img.shields.io/badge/version-v1.1-yellow.svg) 2 | ![License](https://img.shields.io/github/license/renancostab/HPThreadPool.svg) 3 | ![Lang](https://img.shields.io/github/languages/top/renancostab/HPThreadPool.svg) 4 | 5 | # HPThreadPool 6 | 7 | HPThreadPool is a High Performance Thread Pool design for Lazarus Free pascal 8 | 9 | # Design 10 | 11 | The goal is to provide a nice and simple way to work with threads without the excessive overhead of runtime thread creation, the major topics of a thread pool is covered, including: 12 | 13 | - Statics 14 | - Minimum 15 | - Maximum 16 | - Threads running 17 | - Work enqueued 18 | - Resize of thread pool 19 | - Zero sleep time when a job is available 20 | - WaitForAll and WaitForAny 21 | - Worker thread class customization (if needed) 22 | - OnTerminateWorker callback sync (Synchronized) and async (Thread context) 23 | 24 | # Quick Example 25 | 26 | ```Pascal 27 | unit Demo; 28 | 29 | interface 30 | 31 | uses 32 | HPThreadPool; 33 | 34 | type 35 | TMyJobs = class(TObject) 36 | private 37 | public 38 | class procedure MyFiboWork(AParameters: Pointer); 39 | end; 40 | 41 | implementation 42 | 43 | procedure TMyJobs.MyFiboWork(AParameters: Pointer); 44 | var 45 | I: Integer; 46 | Value: Integer; 47 | Sum: Integer = 0; 48 | NumA: Integer = 1; 49 | NumB: Integer = 0; 50 | begin 51 | Value := PInteger(AParameters)^; 52 | 53 | for I := 1 to Value do 54 | begin 55 | Sum := NumA + NumB; 56 | NumB := NumA; 57 | NumA := Sum; 58 | end; 59 | 60 | PInteger(AParameters)^ := Sum; 61 | end; 62 | 63 | // Example working with 100 jobs and a random value of fibonnaci 64 | const 65 | MAXJOBS = 100; 66 | var 67 | I: Integer; 68 | Fibo: array of Integer; 69 | Tasks: TTaskList; 70 | begin 71 | Randomize; 72 | Tasks := TTaskList.Create; 73 | SetLength(Fibo, MAXJOBS); 74 | 75 | for I := 0 to MAXJOBS - 1 do 76 | begin 77 | Fibo[I] := Random(10000); 78 | Tasks.Add(THPTask.Run(@TMyJobs.MyFiboWork, @Fibo[I])); 79 | end; 80 | 81 | THPTask.WaitForAll(Tasks.ToArray); 82 | 83 | for I := 0 to High(Fibo) do 84 | WriteLn(I, ' - ', Fibo[I]); 85 | 86 | Tasks.Free; 87 | end; 88 | -------------------------------------------------------------------------------- /demo/PoolDemo.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/demo/PoolDemo.ico -------------------------------------------------------------------------------- /demo/PoolDemo.lpi: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | <Scaled Value="True"/> 13 | <ResourceType Value="res"/> 14 | <UseXPManifest Value="True"/> 15 | <XPManifest> 16 | <DpiAware Value="True"/> 17 | </XPManifest> 18 | <Icon Value="0"/> 19 | </General> 20 | <BuildModes Count="1"> 21 | <Item1 Name="Default" Default="True"/> 22 | </BuildModes> 23 | <PublishOptions> 24 | <Version Value="2"/> 25 | <UseFileFilters Value="True"/> 26 | </PublishOptions> 27 | <RunParams> 28 | <FormatVersion Value="2"/> 29 | </RunParams> 30 | <RequiredPackages Count="2"> 31 | <Item1> 32 | <PackageName Value="virtualtreeview_package"/> 33 | </Item1> 34 | <Item2> 35 | <PackageName Value="LCL"/> 36 | </Item2> 37 | </RequiredPackages> 38 | <Units Count="10"> 39 | <Unit0> 40 | <Filename Value="PoolDemo.lpr"/> 41 | <IsPartOfProject Value="True"/> 42 | </Unit0> 43 | <Unit1> 44 | <Filename Value="mainform.pp"/> 45 | <IsPartOfProject Value="True"/> 46 | <ComponentName Value="Form1"/> 47 | <HasResources Value="True"/> 48 | <ResourceBaseClass Value="Form"/> 49 | <UnitName Value="MainForm"/> 50 | </Unit1> 51 | <Unit2> 52 | <Filename Value="..\src\gsynclist.pp"/> 53 | <IsPartOfProject Value="True"/> 54 | <UnitName Value="GSyncList"/> 55 | </Unit2> 56 | <Unit3> 57 | <Filename Value="..\src\hpcommons.pp"/> 58 | <IsPartOfProject Value="True"/> 59 | <UnitName Value="HPCommons"/> 60 | </Unit3> 61 | <Unit4> 62 | <Filename Value="..\src\hptaskapi.pp"/> 63 | <IsPartOfProject Value="True"/> 64 | <UnitName Value="HPTaskApi"/> 65 | </Unit4> 66 | <Unit5> 67 | <Filename Value="..\src\hpthread.pp"/> 68 | <IsPartOfProject Value="True"/> 69 | <UnitName Value="HPThread"/> 70 | </Unit5> 71 | <Unit6> 72 | <Filename Value="..\src\hpthreadpool.pp"/> 73 | <IsPartOfProject Value="True"/> 74 | <UnitName Value="HPThreadPool"/> 75 | </Unit6> 76 | <Unit7> 77 | <Filename Value="..\src\hpwork.pp"/> 78 | <IsPartOfProject Value="True"/> 79 | <UnitName Value="HPWork"/> 80 | </Unit7> 81 | <Unit8> 82 | <Filename Value="..\src\hpworkengine.pp"/> 83 | <IsPartOfProject Value="True"/> 84 | <UnitName Value="HPWorkEngine"/> 85 | </Unit8> 86 | <Unit9> 87 | <Filename Value="..\src\synclist.pp"/> 88 | <IsPartOfProject Value="True"/> 89 | <UnitName Value="SyncList"/> 90 | </Unit9> 91 | </Units> 92 | </ProjectOptions> 93 | <CompilerOptions> 94 | <Version Value="11"/> 95 | <PathDelim Value="\"/> 96 | <Target> 97 | <Filename Value="PoolDemo"/> 98 | </Target> 99 | <SearchPaths> 100 | <IncludeFiles Value="$(ProjOutDir)"/> 101 | <OtherUnitFiles Value="..\src"/> 102 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 103 | </SearchPaths> 104 | <Linking> 105 | <Debugging> 106 | <DebugInfoType Value="dsDwarf2Set"/> 107 | <UseHeaptrc Value="True"/> 108 | </Debugging> 109 | <Options> 110 | <Win32> 111 | <GraphicApplication Value="True"/> 112 | </Win32> 113 | </Options> 114 | </Linking> 115 | <Other> 116 | <CompilerMessages> 117 | <IgnoredMessages idx6058="True" idx3124="True" idx3123="True"/> 118 | </CompilerMessages> 119 | </Other> 120 | </CompilerOptions> 121 | <Debugging> 122 | <Exceptions Count="3"> 123 | <Item1> 124 | <Name Value="EAbort"/> 125 | </Item1> 126 | <Item2> 127 | <Name Value="ECodetoolError"/> 128 | </Item2> 129 | <Item3> 130 | <Name Value="EFOpenError"/> 131 | </Item3> 132 | </Exceptions> 133 | </Debugging> 134 | </CONFIG> 135 | -------------------------------------------------------------------------------- /demo/PoolDemo.lpr: -------------------------------------------------------------------------------- 1 | program PoolDemo; 2 | 3 | {$mode objfpc}{$H+} 4 | 5 | uses 6 | {$IFDEF UNIX}{$IFDEF UseCThreads} 7 | cthreads, 8 | {$ENDIF}{$ENDIF} 9 | Interfaces, // this includes the LCL widgetset 10 | Forms, MainForm, SyncList, HPWorkEngine, HPWork, HPThreadPool, HPThread, HPTaskApi, HPCommons, GSyncList 11 | { you can add units after this }; 12 | 13 | {$R *.res} 14 | 15 | begin 16 | RequireDerivedFormResource := True; 17 | Application.Scaled := True; 18 | Application.Initialize; 19 | Application.CreateForm(TForm1, Form1); 20 | Application.Run; 21 | end. 22 | 23 | -------------------------------------------------------------------------------- /demo/mainform.lfm: -------------------------------------------------------------------------------- 1 | object Form1: TForm1 2 | Left = 837 3 | Height = 482 4 | Top = 385 5 | Width = 523 6 | Caption = 'High Performance Thread Pool Demo' 7 | ClientHeight = 482 8 | ClientWidth = 523 9 | OnCreate = FormCreate 10 | OnDestroy = FormDestroy 11 | LCLVersion = '2.2.0.4' 12 | object Button1: TButton 13 | Left = 395 14 | Height = 25 15 | Top = 128 16 | Width = 75 17 | Caption = 'Run' 18 | OnClick = Button1Click 19 | ParentFont = False 20 | TabOrder = 0 21 | end 22 | object Memo1: TMemo 23 | Left = 8 24 | Height = 322 25 | Top = 8 26 | Width = 374 27 | Font.CharSet = ANSI_CHARSET 28 | Font.Color = clBlack 29 | Font.Height = -12 30 | Font.Name = 'Lucida Console' 31 | Font.Pitch = fpFixed 32 | Font.Quality = fqDraft 33 | ParentFont = False 34 | ReadOnly = True 35 | TabOrder = 1 36 | end 37 | object Button2: TButton 38 | Left = 395 39 | Height = 25 40 | Top = 91 41 | Width = 75 42 | Caption = 'Increment' 43 | OnClick = Button2Click 44 | ParentFont = False 45 | TabOrder = 2 46 | end 47 | object Button3: TButton 48 | Left = 395 49 | Height = 25 50 | Top = 288 51 | Width = 75 52 | Caption = 'Wait All' 53 | OnClick = Button3Click 54 | TabOrder = 3 55 | end 56 | object Button4: TButton 57 | Left = 395 58 | Height = 25 59 | Top = 56 60 | Width = 75 61 | Caption = 'Decrement' 62 | OnClick = Button4Click 63 | ParentFont = False 64 | TabOrder = 4 65 | end 66 | object Button5: TButton 67 | Left = 395 68 | Height = 25 69 | Top = 256 70 | Width = 75 71 | Caption = 'Wait Any' 72 | OnClick = Button5Click 73 | TabOrder = 5 74 | end 75 | object Memo2: TMemo 76 | Left = 8 77 | Height = 90 78 | Top = 344 79 | Width = 374 80 | ScrollBars = ssVertical 81 | TabOrder = 6 82 | end 83 | object Timer1: TTimer 84 | Enabled = False 85 | OnTimer = Timer1Timer 86 | Left = 480 87 | Top = 8 88 | end 89 | end 90 | -------------------------------------------------------------------------------- /demo/mainform.pp: -------------------------------------------------------------------------------- 1 | unit MainForm; 2 | 3 | {$mode objfpc}{$H+} 4 | 5 | interface 6 | 7 | uses 8 | Classes, 9 | SysUtils, 10 | Forms, 11 | Controls, 12 | Graphics, 13 | Dialogs, 14 | ExtCtrls, 15 | StdCtrls, 16 | HPThreadPool; 17 | 18 | type 19 | 20 | { TForm1 } 21 | 22 | TForm1 = class(TForm) 23 | Button1: TButton; 24 | Button2: TButton; 25 | Button3: TButton; 26 | Button4: TButton; 27 | Button5: TButton; 28 | Memo1: TMemo; 29 | Memo2: TMemo; 30 | Timer1: TTimer; 31 | procedure Button1Click(Sender: TObject); 32 | procedure Button2Click(Sender: TObject); 33 | procedure Button3Click(Sender: TObject); 34 | procedure Button4Click(Sender: TObject); 35 | procedure Button5Click(Sender: TObject); 36 | procedure FormCreate(Sender: TObject); 37 | procedure FormDestroy(Sender: TObject); 38 | procedure Timer1Timer(Sender: TObject); 39 | private 40 | procedure WorkerTerminate({%H-}ASender: TObject); 41 | public 42 | procedure MyWork({%H-}AParameters: Pointer); 43 | end; 44 | 45 | var 46 | Form1: TForm1; 47 | 48 | implementation 49 | 50 | {$R *.lfm} 51 | 52 | { TForm1 } 53 | 54 | procedure TForm1.Button1Click(Sender: TObject); 55 | var 56 | I: Integer; 57 | begin 58 | for I := 0 to Random(30) do 59 | THPTask.Run(@MyWork, nil); 60 | end; 61 | 62 | procedure TForm1.Button2Click(Sender: TObject); 63 | begin 64 | THPThreadPool.DefaultPool.MaxThreads := THPThreadPool.DefaultPool.MaxThreads + 1; 65 | end; 66 | 67 | procedure TForm1.Button3Click(Sender: TObject); 68 | var 69 | I: Integer; 70 | Tasks: TTaskList; 71 | begin 72 | Tasks := TTaskList.Create; 73 | for I := 0 to Random(30) do 74 | Tasks.Add(THPTask.Run(@MyWork, nil)); 75 | 76 | THPTask.WaitForAll(Tasks.ToArray); 77 | Tasks.Free; 78 | end; 79 | 80 | procedure TForm1.Button4Click(Sender: TObject); 81 | begin 82 | THPThreadPool.DefaultPool.MaxThreads := THPThreadPool.DefaultPool.MaxThreads - 1; 83 | end; 84 | 85 | procedure TForm1.Button5Click(Sender: TObject); 86 | var 87 | I: Integer; 88 | Tasks: TTaskList; 89 | begin 90 | Tasks := TTaskList.Create; 91 | for I := 0 to Random(30) do 92 | Tasks.Add(THPTask.Run(@MyWork, nil)); 93 | 94 | THPTask.WaitForAny(Tasks.ToArray); 95 | Tasks.Free; 96 | end; 97 | 98 | procedure TForm1.FormCreate(Sender: TObject); 99 | begin 100 | Randomize; 101 | Timer1.Enabled := True; 102 | THPThreadPool.DefaultPool.OnTerminateWorker := @WorkerTerminate; 103 | THPThreadPool.DefaultPool.UseSyncTerminate := True; 104 | end; 105 | 106 | procedure TForm1.FormDestroy(Sender: TObject); 107 | begin 108 | Timer1.Enabled := False; 109 | end; 110 | 111 | procedure TForm1.MyWork(AParameters: Pointer); 112 | var 113 | I: Integer; 114 | begin 115 | for I := 0 to 250 do 116 | Sleep(Random(10)); 117 | end; 118 | 119 | procedure TForm1.Timer1Timer(Sender: TObject); 120 | begin 121 | Memo1.Lines.Clear; 122 | 123 | Memo1.Lines.Add('Enqueued...: ' + THPThreadPool.DefaultPool.Enqueued.ToString()); 124 | Memo1.Lines.Add('Running....: ' + THPThreadPool.DefaultPool.Running.ToString()); 125 | Memo1.Lines.Add('Waiting....: ' + THPThreadPool.DefaultPool.Waiting.ToString()); 126 | Memo1.Lines.Add('CurrThreads: ' + THPThreadPool.DefaultPool.CurrThreads.ToString()); 127 | Memo1.Lines.Add('MaxThreads.: ' + THPThreadPool.DefaultPool.MaxThreads.ToString()); 128 | Memo1.Lines.Add('MinThreads.: ' + THPThreadPool.DefaultPool.MinThreads.ToString()); 129 | end; 130 | 131 | procedure TForm1.WorkerTerminate(ASender: TObject); 132 | begin 133 | // Beware that closing the form will raise an AV if we use GUI and don't handle 134 | // the thread concurrency when UseSyncTerminate = False 135 | 136 | // Cleanup code 137 | Memo2.Lines.Add('Terminated: ' + TThread(ASender).ThreadID.ToString()); 138 | end; 139 | 140 | end. 141 | 142 | -------------------------------------------------------------------------------- /src/gsynclist.pp: -------------------------------------------------------------------------------- 1 | (************************************************************************************************** 2 | This Source Code Form is subject to the terms of the Mozilla Public 3 | License, v. 2.0. If a copy of the MPL was not distributed with this 4 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 6 | ANY KIND, either express or implied. See the License for the specific language governing rights 7 | and limitations under the License. 8 | Project......: HPThreadPool 9 | Author.......: Renan Bellódi 10 | Original Code: GSyncList.pp 11 | ***************************************************************************************************) 12 | 13 | unit GSyncList; 14 | 15 | {$mode objfpc}{$H+} 16 | 17 | interface 18 | 19 | uses 20 | Classes, 21 | SysUtils, 22 | SyncList; 23 | 24 | type 25 | 26 | { TGSyncList } 27 | 28 | generic TGSyncList<T> = class(TObject) 29 | private 30 | FSList: TSyncList; 31 | procedure Put(AIndex: Integer; AItem: T); 32 | 33 | function GetCount: Integer; 34 | function Get(AIndex: Integer): T; 35 | public 36 | constructor Create; 37 | destructor Destroy; override; 38 | 39 | procedure Add(AItem: T); 40 | procedure Delete(AIndex: Integer); 41 | procedure Insert(AIndex: Integer; AItem: Pointer); 42 | 43 | procedure AddWithLock(AItem: T); 44 | procedure DeleteWithLock(AIndex: Integer); 45 | procedure InsertWithLock(AIndex: Integer; AItem: Pointer); 46 | 47 | procedure Lock; 48 | procedure Unlock; 49 | 50 | property Count: Integer read GetCount; 51 | property Items[AIndex: Integer]: T read Get write Put; default; 52 | end; 53 | 54 | implementation 55 | 56 | { TGSyncList } 57 | 58 | procedure TGSyncList.Put(AIndex: Integer; AItem: T); 59 | begin 60 | FSList[AIndex] := AItem; 61 | end; 62 | 63 | function TGSyncList.GetCount: Integer; 64 | begin 65 | Result := FSList.Count; 66 | end; 67 | 68 | function TGSyncList.Get(AIndex: Integer): T; 69 | begin 70 | Result := T(FSList[AIndex]); 71 | end; 72 | 73 | constructor TGSyncList.Create; 74 | begin 75 | FSList := TSyncList.Create; 76 | end; 77 | 78 | destructor TGSyncList.Destroy; 79 | begin 80 | FSList.Free; 81 | inherited Destroy; 82 | end; 83 | 84 | procedure TGSyncList.Add(AItem: T); 85 | begin 86 | FSList.Add(AItem); 87 | end; 88 | 89 | procedure TGSyncList.Delete(AIndex: Integer); 90 | begin 91 | FSList.Delete(AIndex); 92 | end; 93 | 94 | procedure TGSyncList.Insert(AIndex: Integer; AItem: Pointer); 95 | begin 96 | FSList.Insert(AIndex, AItem); 97 | end; 98 | 99 | procedure TGSyncList.AddWithLock(AItem: T); 100 | begin 101 | FSList.AddWithLock(AItem); 102 | end; 103 | 104 | procedure TGSyncList.DeleteWithLock(AIndex: Integer); 105 | begin 106 | FSList.DeleteWithLock(AIndex); 107 | end; 108 | 109 | procedure TGSyncList.InsertWithLock(AIndex: Integer; AItem: Pointer); 110 | begin 111 | FSList.InsertWithLock(AIndex, AItem); 112 | end; 113 | 114 | procedure TGSyncList.Lock; 115 | begin 116 | FSList.Lock; 117 | end; 118 | 119 | procedure TGSyncList.Unlock; 120 | begin 121 | FSList.Unlock; 122 | end; 123 | 124 | end. 125 | 126 | -------------------------------------------------------------------------------- /src/hpcommons.pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/src/hpcommons.pp -------------------------------------------------------------------------------- /src/hptaskapi.pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/src/hptaskapi.pp -------------------------------------------------------------------------------- /src/hpthread.pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/src/hpthread.pp -------------------------------------------------------------------------------- /src/hpthreadpool.pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/src/hpthreadpool.pp -------------------------------------------------------------------------------- /src/hpwork.pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/src/hpwork.pp -------------------------------------------------------------------------------- /src/hpworkengine.pp: -------------------------------------------------------------------------------- 1 | (************************************************************************************************** 2 | This Source Code Form is subject to the terms of the Mozilla Public 3 | License, v. 2.0. If a copy of the MPL was not distributed with this 4 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 6 | ANY KIND, either express or implied. See the License for the specific language governing rights 7 | and limitations under the License. 8 | Project......: HPThreadPool 9 | Author.......: Renan Bellódi 10 | Original Code: HPWorkEngine.pp 11 | ***************************************************************************************************) 12 | 13 | unit HPWorkEngine; 14 | 15 | {$mode objfpc}{$H+} 16 | 17 | interface 18 | 19 | uses 20 | Classes, 21 | SysUtils, 22 | SyncObjs, 23 | HPWork; 24 | 25 | type 26 | 27 | THPWork = HPWork.THPWork; 28 | 29 | { THPWorkEngine } 30 | 31 | THPWorkEngine = class(TObject) 32 | private 33 | FEvent: TEvent; 34 | FWorkList: THPWorkList; 35 | FShutdown: Boolean; 36 | FCount: Integer; 37 | function GetCount: Integer; 38 | public 39 | constructor Create; 40 | destructor Destroy; override; 41 | 42 | procedure AddWork(AWork: THPWork); 43 | procedure ShutDown; 44 | 45 | function WaitWork: THPWork; 46 | property Enqueued: Integer read GetCount; 47 | end; 48 | 49 | implementation 50 | 51 | { THPWorkEngine } 52 | 53 | function THPWorkEngine.GetCount: Integer; 54 | begin 55 | Result := FWorkList.Count; 56 | end; 57 | 58 | constructor THPWorkEngine.Create; 59 | begin 60 | FEvent := TEvent.Create(nil, True, False, EmptyStr); 61 | FWorkList := THPWorkList.Create; 62 | FShutdown := False; 63 | FCount := 0; 64 | end; 65 | 66 | destructor THPWorkEngine.Destroy; 67 | begin 68 | FEvent.Free; 69 | FWorkList.Free; 70 | inherited Destroy; 71 | end; 72 | 73 | procedure THPWorkEngine.AddWork(AWork: THPWork); 74 | begin 75 | FWorkList.AddWithLock(AWork); 76 | FCount := FWorkList.Count; 77 | FEvent.SetEvent; 78 | end; 79 | 80 | procedure THPWorkEngine.ShutDown; 81 | begin 82 | FShutdown := True; 83 | FEvent.SetEvent; 84 | end; 85 | 86 | function THPWorkEngine.WaitWork: THPWork; 87 | begin 88 | Result := nil; 89 | if (FShutdown) or (FEvent.WaitFor(INFINITE) <> wrSignaled) then 90 | Exit; 91 | 92 | FWorkList.Lock; 93 | try 94 | if FWorkList.Count = 0 then 95 | Exit; 96 | 97 | Result := FWorkList[0]; 98 | FWorkList.Delete(0); 99 | FCount := FWorkList.Count; 100 | 101 | if FWorkList.Count = 0 then 102 | FEvent.ResetEvent; 103 | finally 104 | FWorkList.Unlock; 105 | end; 106 | end; 107 | 108 | end. 109 | 110 | -------------------------------------------------------------------------------- /src/synclist.pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renancostab/HPThreadPool/762d876c657a6b4bfc79bef804fc5ae1e79ecf9b/src/synclist.pp --------------------------------------------------------------------------------