Changeset 142 for MicroThreading
- Timestamp:
- Jan 24, 2011, 4:02:40 PM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/Demo.lpi
r141 r142 42 42 </Item3> 43 43 </RequiredPackages> 44 <Units Count=" 28">44 <Units Count="38"> 45 45 <Unit0> 46 46 <Filename Value="Demo.lpr"/> … … 50 50 <TopLine Value="1"/> 51 51 <CursorPos X="1" Y="15"/> 52 <UsageCount Value=" 39"/>52 <UsageCount Value="40"/> 53 53 </Unit0> 54 54 <Unit1> … … 58 58 <ResourceBaseClass Value="Form"/> 59 59 <UnitName Value="UMainForm"/> 60 <IsVisibleTab Value="True"/>61 60 <EditorIndex Value="0"/> 62 61 <WindowIndex Value="0"/> 63 <TopLine Value="1 76"/>64 <CursorPos X=" 42" Y="177"/>65 <UsageCount Value=" 39"/>62 <TopLine Value="147"/> 63 <CursorPos X="29" Y="148"/> 64 <UsageCount Value="40"/> 66 65 <Loaded Value="True"/> 67 66 <LoadedDesigner Value="True"/> … … 70 69 <Filename Value="../UMicroThreading.pas"/> 71 70 <UnitName Value="UMicroThreading"/> 72 <EditorIndex Value="10"/> 73 <WindowIndex Value="0"/> 74 <TopLine Value="183"/> 75 <CursorPos X="20" Y="194"/> 76 <UsageCount Value="20"/> 71 <IsVisibleTab Value="True"/> 72 <EditorIndex Value="3"/> 73 <WindowIndex Value="0"/> 74 <TopLine Value="152"/> 75 <CursorPos X="32" Y="165"/> 76 <UsageCount Value="21"/> 77 77 <Loaded Value="True"/> 78 78 </Unit2> … … 120 120 <Filename Value="../../ExceptionLogger/UStackTrace.pas"/> 121 121 <UnitName Value="UStackTrace"/> 122 <EditorIndex Value="8"/>123 122 <WindowIndex Value="0"/> 124 123 <TopLine Value="56"/> 125 124 <CursorPos X="24" Y="77"/> 126 125 <UsageCount Value="20"/> 127 <Loaded Value="True"/>128 126 <DefaultSyntaxHighlighter Value="Delphi"/> 129 127 </Unit8> … … 131 129 <Filename Value="../../ExceptionLogger/CustomLineInfo.pas"/> 132 130 <UnitName Value="CustomLineInfo"/> 133 <EditorIndex Value="9"/>134 131 <WindowIndex Value="0"/> 135 132 <TopLine Value="264"/> 136 133 <CursorPos X="37" Y="141"/> 137 134 <UsageCount Value="20"/> 138 <Loaded Value="True"/>139 135 <DefaultSyntaxHighlighter Value="Delphi"/> 140 136 </Unit9> … … 185 181 <Filename Value="../Other/Coroutine.pas"/> 186 182 <UnitName Value="Coroutine"/> 187 <EditorIndex Value="6"/>188 183 <WindowIndex Value="0"/> 189 184 <TopLine Value="290"/> 190 185 <CursorPos X="36" Y="306"/> 191 <UsageCount Value="20"/> 192 <Loaded Value="True"/> 186 <UsageCount Value="21"/> 193 187 </Unit16> 194 188 <Unit17> … … 230 224 <Filename Value="../MicroThreading.pas"/> 231 225 <UnitName Value="MicroThreading"/> 232 <EditorIndex Value="7"/>233 226 <WindowIndex Value="0"/> 234 227 <TopLine Value="1"/> 235 228 <CursorPos X="25" Y="9"/> 236 <UsageCount Value="12"/> 237 <Loaded Value="True"/> 229 <UsageCount Value="13"/> 238 230 </Unit22> 239 231 <Unit23> … … 243 235 <TopLine Value="38"/> 244 236 <CursorPos X="29" Y="50"/> 245 <UsageCount Value="1 2"/>237 <UsageCount Value="13"/> 246 238 <Loaded Value="True"/> 247 239 </Unit23> … … 252 244 <TopLine Value="161"/> 253 245 <CursorPos X="11" Y="184"/> 254 <UsageCount Value="1 2"/>246 <UsageCount Value="13"/> 255 247 <Loaded Value="True"/> 256 248 </Unit24> 257 249 <Unit25> 258 250 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/dati.inc"/> 259 <EditorIndex Value="5"/>260 251 <WindowIndex Value="0"/> 261 252 <TopLine Value="254"/> 262 253 <CursorPos X="12" Y="258"/> 263 <UsageCount Value="12"/> 264 <Loaded Value="True"/> 254 <UsageCount Value="13"/> 265 255 </Unit25> 266 256 <Unit26> 267 257 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysutils.pp"/> 268 258 <UnitName Value="sysutils"/> 269 <EditorIndex Value="3"/>270 259 <WindowIndex Value="0"/> 271 260 <TopLine Value="581"/> 272 261 <CursorPos X="11" Y="585"/> 273 <UsageCount Value="12"/> 274 <Loaded Value="True"/> 262 <UsageCount Value="13"/> 275 263 </Unit26> 276 264 <Unit27> 277 265 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/wininc/redef.inc"/> 278 <EditorIndex Value="4"/>279 266 <WindowIndex Value="0"/> 280 267 <TopLine Value="525"/> 281 268 <CursorPos X="11" Y="538"/> 282 <UsageCount Value="12"/> 283 <Loaded Value="True"/> 269 <UsageCount Value="13"/> 284 270 </Unit27> 271 <Unit28> 272 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/packages/fcl-base/src/syncobjs.pp"/> 273 <UnitName Value="syncobjs"/> 274 <EditorIndex Value="10"/> 275 <WindowIndex Value="0"/> 276 <TopLine Value="57"/> 277 <CursorPos X="16" Y="70"/> 278 <UsageCount Value="10"/> 279 <Loaded Value="True"/> 280 </Unit28> 281 <Unit29> 282 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/threadh.inc"/> 283 <EditorIndex Value="11"/> 284 <WindowIndex Value="0"/> 285 <TopLine Value="18"/> 286 <CursorPos X="50" Y="31"/> 287 <UsageCount Value="10"/> 288 <Loaded Value="True"/> 289 </Unit29> 290 <Unit30> 291 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/thread.inc"/> 292 <EditorIndex Value="13"/> 293 <WindowIndex Value="0"/> 294 <TopLine Value="109"/> 295 <CursorPos X="3" Y="112"/> 296 <UsageCount Value="10"/> 297 <Loaded Value="True"/> 298 </Unit30> 299 <Unit31> 300 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysosh.inc"/> 301 <EditorIndex Value="12"/> 302 <WindowIndex Value="0"/> 303 <TopLine Value="12"/> 304 <CursorPos X="3" Y="25"/> 305 <UsageCount Value="10"/> 306 <Loaded Value="True"/> 307 </Unit31> 308 <Unit32> 309 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/> 310 <EditorIndex Value="5"/> 311 <WindowIndex Value="0"/> 312 <TopLine Value="1507"/> 313 <CursorPos X="3" Y="1469"/> 314 <UsageCount Value="10"/> 315 <Loaded Value="True"/> 316 </Unit32> 317 <Unit33> 318 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/tthread.inc"/> 319 <EditorIndex Value="7"/> 320 <WindowIndex Value="0"/> 321 <TopLine Value="96"/> 322 <CursorPos X="15" Y="98"/> 323 <UsageCount Value="10"/> 324 <Loaded Value="True"/> 325 </Unit33> 326 <Unit34> 327 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/wininc/func.inc"/> 328 <EditorIndex Value="9"/> 329 <WindowIndex Value="0"/> 330 <TopLine Value="130"/> 331 <CursorPos X="10" Y="143"/> 332 <UsageCount Value="10"/> 333 <Loaded Value="True"/> 334 </Unit34> 335 <Unit35> 336 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/wininc/defines.inc"/> 337 <EditorIndex Value="8"/> 338 <WindowIndex Value="0"/> 339 <TopLine Value="1923"/> 340 <CursorPos X="6" Y="1936"/> 341 <UsageCount Value="10"/> 342 <Loaded Value="True"/> 343 </Unit35> 344 <Unit36> 345 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classes.inc"/> 346 <EditorIndex Value="6"/> 347 <WindowIndex Value="0"/> 348 <TopLine Value="109"/> 349 <CursorPos X="6" Y="115"/> 350 <UsageCount Value="10"/> 351 <Loaded Value="True"/> 352 </Unit36> 353 <Unit37> 354 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/objpash.inc"/> 355 <EditorIndex Value="4"/> 356 <WindowIndex Value="0"/> 357 <TopLine Value="181"/> 358 <CursorPos X="21" Y="194"/> 359 <UsageCount Value="10"/> 360 <Loaded Value="True"/> 361 </Unit37> 285 362 </Units> 286 363 <JumpHistory Count="30" HistoryIndex="29"> 287 364 <Position1> 288 <Filename Value=" UMainForm.pas"/>289 <Caret Line=" 41" Column="24" TopLine="28"/>365 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/threadh.inc"/> 366 <Caret Line="63" Column="35" TopLine="49"/> 290 367 </Position1> 291 368 <Position2> 292 <Filename Value=" UMainForm.pas"/>293 <Caret Line=" 182" Column="45" TopLine="169"/>369 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/threadh.inc"/> 370 <Caret Line="31" Column="50" TopLine="18"/> 294 371 </Position2> 295 372 <Position3> 296 373 <Filename Value="../UMicroThreading.pas"/> 297 <Caret Line=" 8" Column="57" TopLine="1"/>374 <Caret Line="24" Column="7" TopLine="23"/> 298 375 </Position3> 299 376 <Position4> 300 377 <Filename Value="../UMicroThreading.pas"/> 301 <Caret Line=" 71" Column="20" TopLine="49"/>378 <Caret Line="152" Column="4" TopLine="180"/> 302 379 </Position4> 303 380 <Position5> 304 381 <Filename Value="../UMicroThreading.pas"/> 305 <Caret Line=" 97" Column="24" TopLine="75"/>382 <Caret Line="189" Column="1" TopLine="180"/> 306 383 </Position5> 307 384 <Position6> 308 385 <Filename Value="../UMicroThreading.pas"/> 309 <Caret Line="1 19" Column="38" TopLine="97"/>386 <Caret Line="190" Column="1" TopLine="180"/> 310 387 </Position6> 311 388 <Position7> 312 389 <Filename Value="../UMicroThreading.pas"/> 313 <Caret Line="1 26" Column="61" TopLine="104"/>390 <Caret Line="185" Column="1" TopLine="180"/> 314 391 </Position7> 315 392 <Position8> 316 393 <Filename Value="../UMicroThreading.pas"/> 317 <Caret Line="18 2" Column="34" TopLine="147"/>394 <Caret Line="186" Column="1" TopLine="180"/> 318 395 </Position8> 319 396 <Position9> 320 397 <Filename Value="../UMicroThreading.pas"/> 321 <Caret Line=" 277" Column="45" TopLine="258"/>398 <Caret Line="191" Column="1" TopLine="180"/> 322 399 </Position9> 323 400 <Position10> 324 401 <Filename Value="../UMicroThreading.pas"/> 325 <Caret Line="1 36" Column="14" TopLine="120"/>402 <Caret Line="149" Column="1" TopLine="136"/> 326 403 </Position10> 327 404 <Position11> 328 <Filename Value=" UMainForm.pas"/>329 <Caret Line="1 91" Column="91" TopLine="173"/>405 <Filename Value="../UMicroThreading.pas"/> 406 <Caret Line="189" Column="1" TopLine="176"/> 330 407 </Position11> 331 408 <Position12> 332 <Filename Value=" UMainForm.pas"/>333 <Caret Line="1 79" Column="40" TopLine="165"/>409 <Filename Value="../UMicroThreading.pas"/> 410 <Caret Line="190" Column="1" TopLine="176"/> 334 411 </Position12> 335 412 <Position13> 336 413 <Filename Value="../UMicroThreading.pas"/> 337 <Caret Line="1 93" Column="1" TopLine="181"/>414 <Caret Line="185" Column="1" TopLine="176"/> 338 415 </Position13> 339 416 <Position14> 340 417 <Filename Value="../UMicroThreading.pas"/> 341 <Caret Line="1 1" Column="56" TopLine="1"/>418 <Caret Line="186" Column="1" TopLine="176"/> 342 419 </Position14> 343 420 <Position15> 344 421 <Filename Value="../UMicroThreading.pas"/> 345 <Caret Line="1 5" Column="33" TopLine="1"/>422 <Caret Line="191" Column="1" TopLine="176"/> 346 423 </Position15> 347 424 <Position16> 348 <Filename Value=" UMainForm.pas"/>349 <Caret Line=" 202" Column="91" TopLine="183"/>425 <Filename Value="../UMicroThreading.pas"/> 426 <Caret Line="149" Column="1" TopLine="136"/> 350 427 </Position16> 351 428 <Position17> 352 429 <Filename Value="../UMicroThreading.pas"/> 353 <Caret Line=" 42" Column="22" TopLine="22"/>430 <Caret Line="156" Column="1" TopLine="136"/> 354 431 </Position17> 355 432 <Position18> 356 433 <Filename Value="../UMicroThreading.pas"/> 357 <Caret Line=" 92" Column="1" TopLine="92"/>434 <Caret Line="157" Column="1" TopLine="136"/> 358 435 </Position18> 359 436 <Position19> 360 <Filename Value=" UMainForm.pas"/>361 <Caret Line=" 89" Column="19" TopLine="72"/>437 <Filename Value="../UMicroThreading.pas"/> 438 <Caret Line="158" Column="1" TopLine="136"/> 362 439 </Position19> 363 440 <Position20> 364 441 <Filename Value="../UMicroThreading.pas"/> 365 <Caret Line=" 75" Column="22" TopLine="62"/>442 <Caret Line="189" Column="1" TopLine="176"/> 366 443 </Position20> 367 444 <Position21> 368 <Filename Value=" UMainForm.pas"/>369 <Caret Line=" 89" Column="22" TopLine="76"/>445 <Filename Value="../UMicroThreading.pas"/> 446 <Caret Line="190" Column="1" TopLine="176"/> 370 447 </Position21> 371 448 <Position22> 372 449 <Filename Value="../UMicroThreading.pas"/> 373 <Caret Line=" 175" Column="9" TopLine="175"/>450 <Caret Line="409" Column="21" TopLine="387"/> 374 451 </Position22> 375 452 <Position23> 376 453 <Filename Value="../UMicroThreading.pas"/> 377 <Caret Line=" 79" Column="20" TopLine="64"/>454 <Caret Line="206" Column="18" TopLine="201"/> 378 455 </Position23> 379 456 <Position24> 380 457 <Filename Value="../UMicroThreading.pas"/> 381 <Caret Line=" 172" Column="23" TopLine="170"/>458 <Caret Line="21" Column="15" TopLine="7"/> 382 459 </Position24> 383 460 <Position25> 384 461 <Filename Value="../UMicroThreading.pas"/> 385 <Caret Line=" 349" Column="1" TopLine="1"/>462 <Caret Line="123" Column="50" TopLine="110"/> 386 463 </Position25> 387 464 <Position26> 388 465 <Filename Value="../UMicroThreading.pas"/> 389 <Caret Line="1 92" Column="17" TopLine="179"/>466 <Caret Line="165" Column="29" TopLine="145"/> 390 467 </Position26> 391 468 <Position27> 392 469 <Filename Value="../UMicroThreading.pas"/> 393 <Caret Line="1 1" Column="66" TopLine="1"/>470 <Caret Line="148" Column="31" TopLine="135"/> 394 471 </Position27> 395 472 <Position28> 396 <Filename Value=" UMainForm.pas"/>397 <Caret Line=" 43" Column="1" TopLine="30"/>473 <Filename Value="../UMicroThreading.pas"/> 474 <Caret Line="165" Column="26" TopLine="152"/> 398 475 </Position28> 399 476 <Position29> 400 <Filename Value=" UMainForm.pas"/>401 <Caret Line=" 83" Column="1" TopLine="70"/>477 <Filename Value="../UMicroThreading.pas"/> 478 <Caret Line="148" Column="31" TopLine="148"/> 402 479 </Position29> 403 480 <Position30> 404 <Filename Value=" UMainForm.pas"/>405 <Caret Line="15 4" Column="16" TopLine="141"/>481 <Filename Value="../UMicroThreading.pas"/> 482 <Caret Line="150" Column="31" TopLine="135"/> 406 483 </Position30> 407 484 </JumpHistory> … … 439 516 <Item3> 440 517 <Source Value="umainform.pas"/> 441 <Line Value="5 5"/>518 <Line Value="58"/> 442 519 </Item3> 443 520 <Item4> -
MicroThreading/Demo/UMainForm.lfm
r140 r142 15 15 Height = 25 16 16 Top = 13 17 Width = 1 1917 Width = 103 18 18 Caption = 'Start scheduler' 19 19 OnClick = Button1Click … … 64 64 object Memo1: TMemo 65 65 Left = 10 66 Height = 41967 Top = 8866 Height = 379 67 Top = 128 68 68 Width = 294 69 69 Anchors = [akTop, akLeft, akBottom] … … 72 72 end 73 73 object Button2: TButton 74 Left = 17674 Left = 224 75 75 Height = 25 76 Top = 1376 Top = 96 77 77 Width = 75 78 78 Caption = 'Button2' … … 83 83 Left = 10 84 84 Height = 25 85 Top = 5785 Top = 96 86 86 Width = 75 87 87 Caption = 'Coroutine' … … 92 92 Left = 10 93 93 Height = 14 94 Top = 4094 Top = 80 95 95 Width = 32 96 96 Caption = 'Label2' 97 97 ParentColor = False 98 end 99 object Button4: TButton 100 Left = 136 101 Height = 25 102 Top = 12 103 Width = 75 104 Caption = 'Start workers' 105 OnClick = Button4Click 106 TabOrder = 5 107 end 108 object SpinEdit1: TSpinEdit 109 Left = 217 110 Height = 21 111 Top = 13 112 Width = 82 113 MaxValue = 100000 114 MinValue = 1 115 TabOrder = 6 116 Value = 20 98 117 end 99 118 object Timer1: TTimer -
MicroThreading/Demo/UMainForm.pas
r141 r142 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 ComCtrls, ExtCtrls, UMicroThreading, Coroutine, DateUtils;9 ComCtrls, ExtCtrls, Spin, UMicroThreading, Coroutine, DateUtils; 10 10 11 11 type … … 23 23 Button2: TButton; 24 24 Button3: TButton; 25 Button4: TButton; 25 26 Label1: TLabel; 26 27 Label2: TLabel; 27 28 ListView1: TListView; 28 29 Memo1: TMemo; 30 SpinEdit1: TSpinEdit; 29 31 Timer1: TTimer; 30 32 procedure Button1Click(Sender: TObject); 31 33 procedure Button2Click(Sender: TObject); 32 34 procedure Button3Click(Sender: TObject); 35 procedure Button4Click(Sender: TObject); 33 36 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 34 37 procedure FormCreate(Sender: TObject); … … 70 73 begin 71 74 Scheduler := TMicroThreadScheduler.Create; 72 Scheduler.FreeMicroThreadOnFinish := False;73 75 Test := TTest.Create; 74 76 DoubleBuffered := True; … … 84 86 Scheduler.MicroThreads.Clear; 85 87 Memo1.Clear; 86 for I := 0 to 20 do87 Scheduler.Add('Worker', Worker);88 88 Scheduler.Start; 89 89 end else begin … … 149 149 begin 150 150 Test.Invoke; 151 end; 152 153 procedure TForm1.Button4Click(Sender: TObject); 154 var 155 I: Integer; 156 begin 157 for I := 0 to SpinEdit1.Value do 158 Scheduler.AddMethod(Worker); 151 159 end; 152 160 -
MicroThreading/MicroThreading.lpk
r140 r142 3 3 <Package Version="3"> 4 4 <Name Value="MicroThreading"/> 5 <Author Value="Chronos"/> 5 6 <CompilerOptions> 6 7 <Version Value="9"/> … … 13 14 </Other> 14 15 </CompilerOptions> 16 <Description Value="Micro threading subsystem. "/> 17 <License Value="GNU/GPL"/> 18 <Version Minor="1"/> 15 19 <Files Count="2"> 16 20 <Item1> -
MicroThreading/UMicroThreading.pas
r141 r142 11 11 Classes, SysUtils, Contnrs, SyncObjs, DateUtils, Dialogs, Forms; 12 12 13 const 14 DefaultStackSize = $4000; 15 13 16 type 14 17 TMicroThread = class; 15 18 TMicroThreadScheduler = class; 16 19 17 TStartEvent = procedure(MicroThread: TMicroThread) of object; 18 19 TMicroThreadState = (tsReady, tsRunning, tsWaiting, tsBlocked, tsSuspended, 20 tsSleeping, tsFinished); 20 TMicroThreadState = (tsWaiting, tsRunning, tsBlocked, tsSuspended, 21 tsSleeping); 22 21 23 22 24 { TMicroThread } … … 24 26 TMicroThread = class 25 27 private 26 F Method: TStartEvent;28 FFreeOnTerminate: Boolean; 27 29 FStack: Pointer; 28 30 FStackSize: Integer; … … 33 35 FBasePointer: Pointer; 34 36 FWakeupTime: TDateTime; 37 FTerminated: Boolean; 38 FExecuted: Boolean; // At first go through Execute method, then switch context 39 FFinished: Boolean; 35 40 public 36 41 Id: Integer; … … 39 44 State: TMicroThreadState; 40 45 Scheduler: TMicroThreadScheduler; 46 procedure Execute; virtual; 47 48 // Internal execution 41 49 procedure Yield; 42 50 procedure Sleep(Duration: TDateTime); 43 constructor Create; 51 function WaitForSignal(Signal: TEvent): TWaitResult; 52 53 // External execution 54 procedure WaitFor; 55 procedure Terminate; 56 procedure Start; 57 procedure Stop; 58 59 constructor Create(CreateSuspended: Boolean; 60 const StackSize: SizeUInt = DefaultStackSize); 44 61 destructor Destroy; override; 45 property Method: TStartEvent read FMethod write FMethod;46 62 property ExecutionTime: TDateTime read FExecutionTime; 63 property FreeOnTerminate: Boolean read FFreeOnTerminate 64 write FFreeOnTerminate; 65 property Terminated: Boolean read FTerminated; 66 end; 67 68 TMicroThreadEvent = procedure(MicroThread: TMicroThread) of object; 69 70 { TMicroThreadMethod } 71 72 TMicroThreadMethod = class(TMicroThread) 73 Method: TMicroThreadEvent; 74 procedure Execute; override; 47 75 end; 48 76 … … 55 83 TMicroThreadScheduler = class 56 84 private 57 FFreeMicroThreadOnFinish: Boolean;58 85 ThreadPool: TThreadPool; 59 86 RoundRobinIndex: Integer; … … 67 94 FExecutedCount: Integer; 68 95 FTerminated: Boolean; 96 FThreadPoolSize: Integer; 69 97 function GetMicroThreadCount: Integer; 98 function GetThreadPoolSize: Integer; 99 procedure SetThreadPoolSize(const AValue: Integer); 70 100 procedure Yield(MicroThread: TMicroThread); 71 101 public 72 102 MicroThreads: TObjectList; // TList<TMicroThread> 73 103 Lock: TCriticalSection; 104 CurrentMicroThread: TMicroThread; 74 105 function GetNow: TDateTime; 75 function Add(Name: string; Method: TStartEvent): TMicroThread; 106 function Add(MicroThread: TMicroThread): Integer; 107 function AddMethod(Method: TMicroThreadEvent): Integer; 76 108 constructor Create; 77 109 destructor Destroy; override; … … 80 112 procedure Stop; 81 113 property MicroThreadCount: Integer read GetMicroThreadCount; 82 property FreeMicroThreadOnFinish: Boolean read FFreeMicroThreadOnFinish 83 write FFreeMicroThreadOnFinish; 84 end; 114 property ThreadPoolSize: Integer read GetThreadPoolSize 115 write SetThreadPoolSize; 116 end; 117 118 var 119 MainScheduler: TMicroThreadScheduler; 85 120 86 121 const 87 MicroThreadStateText: array[TMicroThreadState] of string = (' Ready', 'Running',88 ' Waiting', 'Blocked', 'Suspended', 'Sleeping', 'Finished');122 MicroThreadStateText: array[TMicroThreadState] of string = ('Waiting', 123 'Running', 'Blocked', 'Suspended', 'Sleeping'); 89 124 90 125 implementation 91 126 127 { TMicroThreadMethod } 128 129 procedure TMicroThreadMethod.Execute; 130 begin 131 inherited Execute; 132 Method(Self); 133 end; 134 92 135 93 136 { TMicroThread } 94 137 138 procedure TMicroThread.Execute; 139 begin 140 141 end; 142 95 143 procedure TMicroThread.Yield; 96 144 begin 97 145 Scheduler.Yield(Self); 146 end; 147 148 procedure TMicroThread.WaitFor; 149 begin 150 while not FFinished do begin 151 Sleep(1); 152 end; 98 153 end; 99 154 … … 105 160 end; 106 161 107 constructor TMicroThread.Create; 108 begin 109 FStackSize := $10000; 162 function TMicroThread.WaitForSignal(Signal: TEvent): TWaitResult; 163 begin 164 repeat 165 Result := Signal.WaitFor(1); 166 Sleep(1); 167 until Result <> wrTimeout; 168 end; 169 170 constructor TMicroThread.Create(CreateSuspended: Boolean; 171 const StackSize: SizeUInt = DefaultStackSize); 172 begin 173 FStackSize := StackSize; 110 174 FStack := GetMem(FStackSize); 111 175 FBasePointer := FStack + FStackSize; 112 176 FStackPointer := FBasePointer - 20; 113 177 FExecutionTime := 0; 178 FTerminated := False; 179 if CreateSuspended then 180 State := tsSuspended; 181 FFreeOnTerminate := True; 182 end; 183 184 procedure TMicroThread.Terminate; 185 begin 186 FTerminated := True; 114 187 end; 115 188 116 189 destructor TMicroThread.Destroy; 117 190 begin 191 Terminate; 192 WaitFor; 193 // Microthread is finished, remove it from queue 194 try 195 Scheduler.Lock.Acquire; 196 Scheduler.MicroThreads.Delete(Scheduler.MicroThreads.IndexOf(Self)); 197 finally 198 Scheduler.Lock.Release; 199 end; 118 200 FreeMem(FStack); 119 201 inherited Destroy; 202 end; 203 204 procedure TMicroThread.Start; 205 begin 206 State := tsWaiting; 207 end; 208 209 procedure TMicroThread.Stop; 210 begin 211 State := tsSuspended; 120 212 end; 121 213 … … 142 234 end; 143 235 144 function TMicroThreadScheduler.Add(Name: string; Method: TStartEvent 145 ): TMicroThread; 146 var 147 NewMicroThread: TMicroThread; 148 begin 149 NewMicroThread := TMicroThread.Create; 236 function TMicroThreadScheduler.Add(MicroThread: TMicroThread): Integer; 237 begin 238 Inc(LastId); 239 MicroThread.Scheduler := Self; 240 MicroThread.Id := LastId; 241 Result := MicroThreads.Add(MicroThread); 242 end; 243 244 function TMicroThreadScheduler.AddMethod(Method: TMicroThreadEvent): Integer; 245 var 246 NewMicroThread: TMicroThreadMethod; 247 begin 248 NewMicroThread := TMicroThreadMethod.Create(False); 249 NewMicroThread.Method := Method; 150 250 NewMicroThread.Scheduler := Self; 151 NewMicroThread.Name := Name; 152 NewMicroThread.Method := Method; 153 Inc(LastId); 154 NewMicroThread.Id := LastId; 155 MicroThreads.Add(NewMicroThread); 251 Result := Add(NewMicroThread); 156 252 end; 157 253 … … 161 257 MicroThreads := TObjectList.Create; 162 258 ThreadPool := TThreadPool.Create; 163 FFreeMicroThreadOnFinish := True;164 259 {$IFDEF Windows} 165 260 QueryPerformanceFrequency(FFrequency); … … 235 330 mov ebp, edx 236 331 end; 332 CurrentMicroThread := nil; 237 333 end; 238 334 … … 245 341 if RoundRobinIndex >= MicroThreads.Count then 246 342 RoundRobinIndex := 0; 247 while (I < MicroThreads.Count) and (TMicroThread(MicroThreads[RoundRobinIndex]).State <> tsReady) and248 (TMicroThread(MicroThreads[RoundRobinIndex]).State <> tsWaiting) do begin343 while (I < MicroThreads.Count) and 344 (TMicroThread(MicroThreads[RoundRobinIndex]).State <> tsWaiting) do begin 249 345 // WakeUp sleeping threads 250 346 if (TMicroThread(MicroThreads[RoundRobinIndex]).State = tsSleeping) and … … 268 364 if Assigned(FSelected) and (FExecutedCount < FExecuteCount) then begin 269 365 Inc(FExecutedCount); 366 CurrentMicroThread := FSelected; 270 367 asm 271 368 // Store scheduler stack … … 276 373 mov [eax].TMicroThreadScheduler.FMainBasePointer, edx 277 374 end; 278 if FSelected.State = tsReady then begin 375 if not FSelected.FExecuted then begin 376 FSelected.FExecuted := True; 279 377 FSelected.State := tsRunning; 280 378 FSelected.FExecutionStartTime := Time; … … 293 391 mov ebp, edx 294 392 end; 295 StaticMicroThread. Method(StaticMicroThread);393 StaticMicroThread.Execute; 296 394 //FSelected.Method(FSelected); 297 395 StaticScheduler := StaticMicroThread.Scheduler; … … 307 405 FSelected.FExecutionTime := FSelected.FExecutionTime + 308 406 (FSelected.FExecutionEndTime - FSelected.FExecutionStartTime); 309 if FFreeMicroThreadOnFinish then begin 310 // Microthread is finished, remove it from queue 311 try 312 Lock.Acquire; 313 MicroThreads.Delete(MicroThreads.IndexOf(FSelected)); 314 finally 315 Lock.Release; 316 end; 317 end else FSelected.State := tsFinished; 407 FSelected.FFinished := True; 408 if FSelected.FFreeOnTerminate then begin 409 FSelected.Free; 410 end;; 318 411 end else 319 412 if FSelected.State = tsWaiting then begin … … 348 441 end; 349 442 443 function TMicroThreadScheduler.GetThreadPoolSize: Integer; 444 begin 445 Result := FThreadPoolSize; 446 end; 447 448 procedure TMicroThreadScheduler.SetThreadPoolSize(const AValue: Integer); 449 begin 450 FThreadPoolSize := AValue; 451 end; 452 453 initialization 454 455 MainScheduler := TMicroThreadScheduler.Create; 456 457 finalization 458 459 MainScheduler.Free; 460 350 461 end. 351 462
Note:
See TracChangeset
for help on using the changeset viewer.