Changeset 147
- Timestamp:
- Jan 26, 2011, 8:06:25 AM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/Demo.lpi
r146 r147 42 42 </Item3> 43 43 </RequiredPackages> 44 <Units Count="4 4">44 <Units Count="46"> 45 45 <Unit0> 46 46 <Filename Value="Demo.lpr"/> … … 50 50 <TopLine Value="1"/> 51 51 <CursorPos X="1" Y="15"/> 52 <UsageCount Value="4 8"/>52 <UsageCount Value="49"/> 53 53 </Unit0> 54 54 <Unit1> … … 60 60 <EditorIndex Value="0"/> 61 61 <WindowIndex Value="0"/> 62 <TopLine Value=" 218"/>63 <CursorPos X="1 7" Y="230"/>64 <UsageCount Value="4 8"/>62 <TopLine Value="82"/> 63 <CursorPos X="1" Y="95"/> 64 <UsageCount Value="49"/> 65 65 <Loaded Value="True"/> 66 66 <LoadedDesigner Value="True"/> … … 69 69 <Filename Value="../UMicroThreading.pas"/> 70 70 <UnitName Value="UMicroThreading"/> 71 <IsVisibleTab Value="True"/> 72 <EditorIndex Value="1"/> 73 <WindowIndex Value="0"/> 74 <TopLine Value="259"/> 75 <CursorPos X="11" Y="278"/> 71 <EditorIndex Value="2"/> 72 <WindowIndex Value="0"/> 73 <TopLine Value="500"/> 74 <CursorPos X="1" Y="517"/> 76 75 <UsageCount Value="27"/> 77 76 <Loaded Value="True"/> … … 238 237 <Unit24> 239 238 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/datih.inc"/> 240 <WindowIndex Value="0"/> 241 <TopLine Value="161"/> 242 <CursorPos X="11" Y="184"/> 239 <EditorIndex Value="4"/> 240 <WindowIndex Value="0"/> 241 <TopLine Value="106"/> 242 <CursorPos X="10" Y="119"/> 243 243 <UsageCount Value="16"/> 244 <Loaded Value="True"/> 244 245 </Unit24> 245 246 <Unit25> … … 296 297 <Unit32> 297 298 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/> 298 <WindowIndex Value="0"/> 299 <TopLine Value="1507"/> 300 <CursorPos X="3" Y="1469"/> 301 <UsageCount Value="13"/> 299 <EditorIndex Value="5"/> 300 <WindowIndex Value="0"/> 301 <TopLine Value="1504"/> 302 <CursorPos X="1" Y="1510"/> 303 <UsageCount Value="13"/> 304 <Loaded Value="True"/> 302 305 </Unit32> 303 306 <Unit33> … … 324 327 <Unit36> 325 328 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classes.inc"/> 329 <EditorIndex Value="6"/> 326 330 <WindowIndex Value="0"/> 327 331 <TopLine Value="109"/> 328 <CursorPos X="6" Y="115"/> 329 <UsageCount Value="13"/> 332 <CursorPos X="3" Y="111"/> 333 <UsageCount Value="13"/> 334 <Loaded Value="True"/> 330 335 </Unit36> 331 336 <Unit37> … … 378 383 <UsageCount Value="10"/> 379 384 </Unit43> 385 <Unit44> 386 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/include/spinedit.inc"/> 387 <EditorIndex Value="1"/> 388 <WindowIndex Value="0"/> 389 <TopLine Value="221"/> 390 <CursorPos X="1" Y="235"/> 391 <UsageCount Value="10"/> 392 <Loaded Value="True"/> 393 </Unit44> 394 <Unit45> 395 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/> 396 <IsVisibleTab Value="True"/> 397 <EditorIndex Value="3"/> 398 <WindowIndex Value="0"/> 399 <TopLine Value="387"/> 400 <CursorPos X="1" Y="400"/> 401 <UsageCount Value="10"/> 402 <Loaded Value="True"/> 403 </Unit45> 380 404 </Units> 381 <JumpHistory Count="30" HistoryIndex=" 19">405 <JumpHistory Count="30" HistoryIndex="29"> 382 406 <Position1> 383 407 <Filename Value="../UMicroThreading.pas"/> 384 <Caret Line=" 272" Column="1" TopLine="250"/>408 <Caret Line="579" Column="1" TopLine="566"/> 385 409 </Position1> 386 410 <Position2> 387 411 <Filename Value="../UMicroThreading.pas"/> 388 <Caret Line=" 273" Column="1" TopLine="251"/>412 <Caret Line="580" Column="1" TopLine="566"/> 389 413 </Position2> 390 414 <Position3> 391 415 <Filename Value="../UMicroThreading.pas"/> 392 <Caret Line=" 274" Column="1" TopLine="252"/>416 <Caret Line="581" Column="1" TopLine="566"/> 393 417 </Position3> 394 418 <Position4> 395 419 <Filename Value="../UMicroThreading.pas"/> 396 <Caret Line=" 397" Column="1" TopLine="384"/>420 <Caret Line="582" Column="1" TopLine="566"/> 397 421 </Position4> 398 422 <Position5> 399 423 <Filename Value="../UMicroThreading.pas"/> 400 <Caret Line=" 398" Column="1" TopLine="384"/>424 <Caret Line="583" Column="1" TopLine="566"/> 401 425 </Position5> 402 426 <Position6> 403 <Filename Value=" UMainForm.pas"/>404 <Caret Line=" 230" Column="17" TopLine="218"/>427 <Filename Value="../UMicroThreading.pas"/> 428 <Caret Line="584" Column="1" TopLine="566"/> 405 429 </Position6> 406 430 <Position7> 407 431 <Filename Value="../UMicroThreading.pas"/> 408 <Caret Line=" 256" Column="1" TopLine="243"/>432 <Caret Line="142" Column="26" TopLine="125"/> 409 433 </Position7> 410 434 <Position8> 411 <Filename Value=" ../UMicroThreading.pas"/>412 <Caret Line=" 259" Column="1" TopLine="243"/>435 <Filename Value="UMainForm.pas"/> 436 <Caret Line="95" Column="17" TopLine="87"/> 413 437 </Position8> 414 438 <Position9> 415 439 <Filename Value="../UMicroThreading.pas"/> 416 <Caret Line=" 262" Column="1" TopLine="243"/>440 <Caret Line="512" Column="3" TopLine="507"/> 417 441 </Position9> 418 442 <Position10> 419 <Filename Value=" ../UMicroThreading.pas"/>420 <Caret Line=" 263" Column="1" TopLine="243"/>443 <Filename Value="UMainForm.pas"/> 444 <Caret Line="95" Column="17" TopLine="87"/> 421 445 </Position10> 422 446 <Position11> 423 447 <Filename Value="../UMicroThreading.pas"/> 424 <Caret Line=" 264" Column="1" TopLine="249"/>448 <Caret Line="513" Column="1" TopLine="507"/> 425 449 </Position11> 426 450 <Position12> 427 451 <Filename Value="../UMicroThreading.pas"/> 428 <Caret Line=" 265" Column="1" TopLine="249"/>452 <Caret Line="514" Column="1" TopLine="507"/> 429 453 </Position12> 430 454 <Position13> 431 455 <Filename Value="../UMicroThreading.pas"/> 432 <Caret Line=" 266" Column="1" TopLine="249"/>456 <Caret Line="327" Column="3" TopLine="323"/> 433 457 </Position13> 434 458 <Position14> 435 459 <Filename Value="../UMicroThreading.pas"/> 436 <Caret Line=" 268" Column="1" TopLine="249"/>460 <Caret Line="513" Column="1" TopLine="500"/> 437 461 </Position14> 438 462 <Position15> 439 463 <Filename Value="../UMicroThreading.pas"/> 440 <Caret Line=" 269" Column="1" TopLine="249"/>464 <Caret Line="514" Column="1" TopLine="500"/> 441 465 </Position15> 442 466 <Position16> 443 467 <Filename Value="../UMicroThreading.pas"/> 444 <Caret Line=" 270" Column="1" TopLine="249"/>468 <Caret Line="516" Column="1" TopLine="500"/> 445 469 </Position16> 446 470 <Position17> 447 471 <Filename Value="../UMicroThreading.pas"/> 448 <Caret Line=" 271" Column="1" TopLine="249"/>472 <Caret Line="517" Column="1" TopLine="500"/> 449 473 </Position17> 450 474 <Position18> 451 475 <Filename Value="../UMicroThreading.pas"/> 452 <Caret Line=" 272" Column="1" TopLine="250"/>476 <Caret Line="328" Column="1" TopLine="315"/> 453 477 </Position18> 454 478 <Position19> 455 479 <Filename Value="../UMicroThreading.pas"/> 456 <Caret Line=" 273" Column="1" TopLine="251"/>480 <Caret Line="330" Column="1" TopLine="315"/> 457 481 </Position19> 458 482 <Position20> 459 483 <Filename Value="../UMicroThreading.pas"/> 460 <Caret Line=" 274" Column="1" TopLine="252"/>484 <Caret Line="331" Column="1" TopLine="315"/> 461 485 </Position20> 462 486 <Position21> 463 487 <Filename Value="../UMicroThreading.pas"/> 464 <Caret Line="3 97" Column="1" TopLine="384"/>488 <Caret Line="332" Column="1" TopLine="315"/> 465 489 </Position21> 466 490 <Position22> 467 <Filename Value=" ../UMicroThreading.pas"/>468 <Caret Line=" 400" Column="44" TopLine="384"/>491 <Filename Value="UMainForm.pas"/> 492 <Caret Line="95" Column="17" TopLine="87"/> 469 493 </Position22> 470 494 <Position23> 471 <Filename Value=" ../UMicroThreading.pas"/>472 <Caret Line=" 397" Column="1" TopLine="384"/>495 <Filename Value="UMainForm.pas"/> 496 <Caret Line="235" Column="19" TopLine="224"/> 473 497 </Position23> 474 498 <Position24> 475 499 <Filename Value="../UMicroThreading.pas"/> 476 <Caret Line=" 398" Column="1" TopLine="384"/>500 <Caret Line="513" Column="1" TopLine="500"/> 477 501 </Position24> 478 502 <Position25> 479 <Filename Value=" ../UMicroThreading.pas"/>480 <Caret Line=" 399" Column="1" TopLine="384"/>503 <Filename Value="UMainForm.pas"/> 504 <Caret Line="244" Column="9" TopLine="224"/> 481 505 </Position25> 482 506 <Position26> 483 <Filename Value="../ UMicroThreading.pas"/>484 <Caret Line="400" Column="1 8" TopLine="384"/>507 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/> 508 <Caret Line="400" Column="1" TopLine="387"/> 485 509 </Position26> 486 510 <Position27> 487 511 <Filename Value="../UMicroThreading.pas"/> 488 <Caret Line=" 398" Column="1" TopLine="384"/>512 <Caret Line="517" Column="1" TopLine="500"/> 489 513 </Position27> 490 514 <Position28> 491 <Filename Value=" ../UMicroThreading.pas"/>492 <Caret Line=" 399" Column="1" TopLine="384"/>515 <Filename Value="UMainForm.pas"/> 516 <Caret Line="95" Column="1" TopLine="82"/> 493 517 </Position28> 494 518 <Position29> 495 519 <Filename Value="../UMicroThreading.pas"/> 496 <Caret Line=" 400" Column="1" TopLine="384"/>520 <Caret Line="517" Column="1" TopLine="500"/> 497 521 </Position29> 498 522 <Position30> 499 <Filename Value="../ UMicroThreading.pas"/>500 <Caret Line=" 274" Column="15" TopLine="261"/>523 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/> 524 <Caret Line="394" Column="30" TopLine="387"/> 501 525 </Position30> 502 526 </JumpHistory> … … 534 558 <Item3> 535 559 <Source Value="umainform.pas"/> 536 <Line Value="6 2"/>560 <Line Value="65"/> 537 561 </Item3> 538 562 <Item4> -
MicroThreading/Demo/UMainForm.lfm
r146 r147 1 1 object Form1: TForm1 2 Left = 23 12 Left = 235 3 3 Height = 510 4 Top = 4 84 Top = 44 5 5 Width = 798 6 6 Caption = 'MicroThreading demo' … … 68 68 object Memo1: TMemo 69 69 Left = 10 70 Height = 37971 Top = 12870 Height = 267 71 Top = 240 72 72 Width = 294 73 73 Anchors = [akTop, akLeft, akBottom] … … 78 78 Left = 224 79 79 Height = 25 80 Top = 9680 Top = 208 81 81 Width = 75 82 82 Caption = 'Button2' … … 87 87 Left = 10 88 88 Height = 25 89 Top = 9689 Top = 208 90 90 Width = 75 91 91 Caption = 'Coroutine' … … 96 96 Left = 10 97 97 Height = 14 98 Top = 8098 Top = 192 99 99 Width = 32 100 100 Caption = 'Label2' … … 102 102 end 103 103 object Button4: TButton 104 Left = 1 20104 Left = 10 105 105 Height = 25 106 Top = 12106 Top = 48 107 107 Width = 88 108 108 Caption = 'Start workers' … … 111 111 end 112 112 object SpinEdit1: TSpinEdit 113 Left = 21 7113 Left = 216 114 114 Height = 21 115 Top = 13115 Top = 48 116 116 Width = 82 117 117 MaxValue = 100000 118 118 MinValue = 1 119 119 TabOrder = 6 120 Value = 20 120 Value = 200 121 121 end 122 122 object Button5: TButton 123 123 Left = 168 124 124 Height = 25 125 Top = 48125 Top = 160 126 126 Width = 131 127 127 Caption = 'Get max TThread count' … … 132 132 Left = 88 133 133 Height = 25 134 Top = 48134 Top = 160 135 135 Width = 75 136 136 Caption = 'Button6' 137 137 OnClick = Button6Click 138 138 TabOrder = 8 139 end 140 object SpinEdit2: TSpinEdit 141 Left = 216 142 Height = 21 143 Top = 13 144 Width = 82 145 TabOrder = 9 146 Value = 1 147 end 148 object Label3: TLabel 149 Left = 120 150 Height = 14 151 Top = 20 152 Width = 69 153 Caption = 'Thread count:' 154 ParentColor = False 155 end 156 object Label4: TLabel 157 Left = 104 158 Height = 14 159 Top = 48 160 Width = 92 161 Caption = 'Microthread count:' 162 ParentColor = False 139 163 end 140 164 object Timer1: TTimer -
MicroThreading/Demo/UMainForm.pas
r146 r147 28 28 Label1: TLabel; 29 29 Label2: TLabel; 30 Label3: TLabel; 31 Label4: TLabel; 30 32 ListView1: TListView; 31 33 Memo1: TMemo; 32 34 SpinEdit1: TSpinEdit; 35 SpinEdit2: TSpinEdit; 33 36 Timer1: TTimer; 34 37 procedure Button1Click(Sender: TObject); … … 89 92 Button1.Caption := 'Stop scheduler'; 90 93 Memo1.Clear; 94 Scheduler.ThreadPoolSize := SpinEdit2.Value; 91 95 Scheduler.Start; 92 96 end else begin 93 97 Button1.Caption := 'Start scheduler'; 94 98 Scheduler.Stop; 99 Scheduler.ThreadPoolSize := 0; 95 100 end; 96 101 end; … … 227 232 var 228 233 I: Integer; 234 Q: Integer; 229 235 const 230 236 TotalSteps = 100; … … 233 239 Memo1.Lines.Add('Worker ' + IntToStr(Id)); 234 240 for I := 0 to TotalSteps - 1 do begin 235 Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' + 236 FloatToStr(ExecutionTime)); 241 Q := 0; 242 while Q < 1000000 do Inc(Q); 243 //Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' + 244 // FloatToStr(ExecutionTime)); 237 245 Completion := I / TotalSteps; 238 246 //Sleep(1 * Id * OneMillisecond); -
MicroThreading/UMicroThreading.pas
r146 r147 34 34 FStackSize: Integer; 35 35 FBasePointer: Pointer; 36 FWake upTime: TDateTime;36 FWakeUpTime: TDateTime; 37 37 FTerminated: Boolean; 38 38 FExecuted: Boolean; // At first go through Execute method, then switch context 39 39 FFinished: Boolean; 40 FSuspended: Boolean; 41 FState: TMicroThreadState; 42 FScheduler: TMicroThreadScheduler; 43 FManager: TMicroThreadManager; 44 FId: Integer; 40 45 public 41 Id: Integer;42 46 Name: string; 43 47 Priority: Integer; 44 State: TMicroThreadState;45 Manager: TMicroThreadManager;46 Scheduler: TMicroThreadScheduler;47 48 Completion: Single; // Can be used for progress information in range <0, 1> 48 49 procedure Execute; virtual; 49 50 50 // Internal execution51 51 procedure Yield; 52 52 procedure Sleep(Duration: TDateTime); 53 53 function WaitForSignal(Signal: TEvent): TWaitResult; 54 55 // External execution56 54 procedure WaitFor; 57 55 procedure Terminate; 58 56 procedure Start; 59 procedure Stop; 57 procedure Resume; 58 procedure Suspend; 60 59 61 60 constructor Create(CreateSuspended: Boolean; 62 61 const StackSize: SizeUInt = DefaultStackSize); 63 62 destructor Destroy; override; 63 property Id: Integer read FId; 64 property State: TMicroThreadState read FState; 64 65 property ExecutionTime: TDateTime read FExecutionTime; 65 66 property FreeOnTerminate: Boolean read FFreeOnTerminate 66 67 write FFreeOnTerminate; 67 68 property Terminated: Boolean read FTerminated; 69 property Scheduler: TMicroThreadScheduler read FScheduler; 70 property Manager: TMicroThreadManager read FManager; 68 71 end; 69 72 … … 80 83 81 84 TMicroThreadSchedulerPoolThread = class(TThread) 82 Scheduler: TMicroThreadScheduler;83 85 Manager: TMicroThreadManager; 84 86 procedure Execute; override; 87 constructor Create(CreateSuspended: Boolean; 88 const StackSize: SizeUInt = DefaultStackSize); 89 destructor Destroy; override; 85 90 end; 86 91 87 92 TThreadPool = class(TObjectList) 88 89 93 end; 90 94 … … 116 120 ThreadPool: TThreadPool; 117 121 RoundRobinIndex: Integer; 118 LastId: Integer;122 FLastId: Integer; 119 123 FFrequency: Int64; 120 FThreadPoolSize: Integer;121 124 FTerminated: Boolean; 122 125 function GetMicroThreadCount: Integer; … … 171 174 TMicroThread(MicroThreads[I]).FStackSize))) do Inc(I); 172 175 if I < MicroThreads.Count then begin 173 Result := TMicroThread(MicroThreads[I]). Id;176 Result := TMicroThread(MicroThreads[I]).FId; 174 177 end else Result := -1; 175 178 finally … … 194 197 var 195 198 I: Integer; 196 Time: TDateTime;197 begin 198 Time := Scheduler.GetNow;199 CurrentTime: TDateTime; 200 begin 201 CurrentTime := Scheduler.GetNow; 199 202 if Assigned(CurrentMicroThread) then begin 200 CurrentMicroThread.FExecutionEndTime := Time;203 CurrentMicroThread.FExecutionEndTime := CurrentTime; 201 204 CurrentMicroThread.FExecutionTime := CurrentMicroThread.FExecutionTime + 202 205 (CurrentMicroThread.FExecutionEndTime - CurrentMicroThread.FExecutionStartTime); 203 if CurrentMicroThread. State = tsRunning then204 CurrentMicroThread. State := tsWaiting;206 if CurrentMicroThread.FState = tsRunning then 207 CurrentMicroThread.FState := tsWaiting; 205 208 StaticMicroThread := CurrentMicroThread; 206 209 asm … … 212 215 mov [eax].TMicroThread.FBasePointer, edx 213 216 end; 214 StaticManager := CurrentMicroThread. Manager;217 StaticManager := CurrentMicroThread.FManager; 215 218 asm 216 219 // Restore scheduler stack … … 221 224 mov ebp, edx 222 225 end; 223 CurrentMicroThread. Manager := nil;226 CurrentMicroThread.FManager := nil; 224 227 CurrentMicroThread := nil; 225 228 end; … … 228 231 229 232 if Assigned(CurrentMicroThread) and (FExecutedCount < FExecuteCount) then begin 230 CurrentMicroThread. Manager := Self;233 CurrentMicroThread.FManager := Self; 231 234 Inc(FExecutedCount); 232 235 asm … … 240 243 if not CurrentMicroThread.FExecuted then begin 241 244 CurrentMicroThread.FExecuted := True; 242 CurrentMicroThread. State := tsRunning;243 CurrentMicroThread.FExecutionStartTime := Time;245 CurrentMicroThread.FState := tsRunning; 246 CurrentMicroThread.FExecutionStartTime := CurrentTime; 244 247 StaticMicroThread := CurrentMicroThread; 245 248 asm … … 257 260 end; 258 261 //FSelected.Method(FSelected); 259 StaticManager := CurrentMicroThread. Manager;262 StaticManager := CurrentMicroThread.FManager; 260 263 asm 261 264 // Restore scheduler stack … … 266 269 mov ebp, edx 267 270 end; 268 CurrentMicroThread. Manager := nil;269 CurrentMicroThread.FExecutionEndTime := Time;271 CurrentMicroThread.FManager := nil; 272 CurrentMicroThread.FExecutionEndTime := CurrentTime; 270 273 CurrentMicroThread.FExecutionTime := CurrentMicroThread.FExecutionTime + 271 274 (CurrentMicroThread.FExecutionEndTime - CurrentMicroThread.FExecutionStartTime); … … 285 288 if CurrentMicroThread.State = tsWaiting then begin 286 289 // Execute selected thread 287 CurrentMicroThread. State := tsRunning;288 CurrentMicroThread.FExecutionStartTime := Time;290 CurrentMicroThread.FState := tsRunning; 291 CurrentMicroThread.FExecutionStartTime := CurrentTime; 289 292 FTempPointer := CurrentMicroThread.FStackPointer; 290 293 asm … … 334 337 end; 335 338 339 constructor TMicroThreadSchedulerPoolThread.Create(CreateSuspended: Boolean; 340 const StackSize: SizeUInt); 341 begin 342 inherited; 343 Manager := TMicroThreadManager.Create; 344 end; 345 346 destructor TMicroThreadSchedulerPoolThread.Destroy; 347 begin 348 Manager.Free; 349 inherited Destroy; 350 end; 351 336 352 { TMicroThreadMethod } 337 353 … … 352 368 procedure TMicroThread.Yield; 353 369 begin 354 Manager.Yield;370 FManager.Yield; 355 371 end; 356 372 … … 365 381 procedure TMicroThread.Sleep(Duration: TDateTime); 366 382 begin 367 FWakeUpTime := Scheduler.GetNow + Duration;368 State := tsSleeping;383 FWakeUpTime := FScheduler.GetNow + Duration; 384 FState := tsSleeping; 369 385 Yield; 370 386 end; … … 387 403 FExecutionTime := 0; 388 404 FTerminated := False; 389 if CreateSuspended then 390 State := tsSuspended; 405 if CreateSuspended then begin 406 FState := tsSuspended; 407 FSuspended := True; 408 end else FSuspended := False; 391 409 FFreeOnTerminate := True; 392 410 end; … … 395 413 begin 396 414 FTerminated := True; 415 end; 416 417 procedure TMicroThread.Start; 418 begin 419 FState := tsWaiting; 397 420 end; 398 421 … … 405 428 end; 406 429 407 procedure TMicroThread.Start; 408 begin 409 State := tsWaiting; 410 end; 411 412 procedure TMicroThread.Stop; 413 begin 414 State := tsSuspended; 430 procedure TMicroThread.Resume; 431 begin 432 FSuspended := False; 433 if FState = tsSuspended then 434 FState := tsWaiting; 435 end; 436 437 procedure TMicroThread.Suspend; 438 begin 439 FSuspended := True; 440 //Yield; 415 441 end; 416 442 … … 440 466 function TMicroThreadScheduler.Add(MicroThread: TMicroThread): Integer; 441 467 begin 442 Inc( LastId);443 MicroThread. Scheduler := Self;444 MicroThread. Id :=LastId;468 Inc(FLastId); 469 MicroThread.FScheduler := Self; 470 MicroThread.FId := FLastId; 445 471 Result := MicroThreads.Add(MicroThread); 446 472 end; … … 452 478 NewMicroThread := TMicroThreadMethod.Create(False); 453 479 NewMicroThread.Method := Method; 454 NewMicroThread. Scheduler := Self;480 NewMicroThread.FScheduler := Self; 455 481 Result := Add(NewMicroThread); 456 482 end; … … 482 508 var 483 509 Executed: Integer; 510 I: Integer; 484 511 begin 485 512 FTerminated := False; 513 for I := 0 to ThreadPool.Count - 1 do 514 TMicroThreadSchedulerPoolThread(ThreadPool[I]).Start; 486 515 repeat 487 516 Executed := MainThreadManager.Execute(10); … … 492 521 493 522 procedure TMicroThreadScheduler.Stop; 494 begin 523 var 524 I: Integer; 525 begin 526 for I := 0 to ThreadPool.Count - 1 do 527 TMicroThreadSchedulerPoolThread(ThreadPool[I]).Terminate; 495 528 FTerminated := True; 496 529 end; … … 499 532 var 500 533 I: Integer; 501 begin 534 CurrentTime: TDateTime; 535 begin 536 CurrentTime := GetNow; 502 537 Result := nil; 503 538 try … … 510 545 (TMicroThread(MicroThreads[RoundRobinIndex]).State <> tsWaiting) do begin 511 546 // WakeUp sleeping threads 512 if (TMicroThread(MicroThreads[RoundRobinIndex]). State = tsSleeping) and513 (TMicroThread(MicroThreads[RoundRobinIndex]).FWakeupTime < Time) then514 TMicroThread(MicroThreads[RoundRobinIndex]). State := tsWaiting else547 if (TMicroThread(MicroThreads[RoundRobinIndex]).FState = tsSleeping) and 548 (TMicroThread(MicroThreads[RoundRobinIndex]).FWakeupTime < CurrentTime) then 549 TMicroThread(MicroThreads[RoundRobinIndex]).FState := tsWaiting else 515 550 begin 516 551 // Go to next thread … … 541 576 function TMicroThreadScheduler.GetThreadPoolSize: Integer; 542 577 begin 543 Result := FThreadPoolSize;578 Result := ThreadPool.Count; 544 579 end; 545 580 546 581 procedure TMicroThreadScheduler.SetThreadPoolSize(const AValue: Integer); 547 begin 548 FThreadPoolSize := AValue; 582 var 583 I: Integer; 584 NewThread: TMicroThreadSchedulerPoolThread; 585 begin 586 if AValue > ThreadPool.Count then begin 587 ThreadPool.Capacity := AValue; 588 while ThreadPool.Count < AValue do begin 589 NewThread := TMicroThreadSchedulerPoolThread.Create(True); 590 NewThread.Manager.Scheduler := Self; 591 ThreadPool.Add(NewThread); 592 end; 593 end else 594 ThreadPool.Count := AValue; 549 595 end; 550 596
Note:
See TracChangeset
for help on using the changeset viewer.