Changeset 154 for MicroThreading
- Timestamp:
- Jan 27, 2011, 2:15:57 PM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/Demo.lpi
r153 r154 39 39 </Item2> 40 40 </RequiredPackages> 41 <Units Count="6 2">41 <Units Count="63"> 42 42 <Unit0> 43 43 <Filename Value="Demo.lpr"/> 44 44 <IsPartOfProject Value="True"/> 45 45 <UnitName Value="Demo"/> 46 <EditorIndex Value="4"/> 46 47 <WindowIndex Value="0"/> 47 48 <TopLine Value="1"/> 48 <CursorPos X="5" Y="9"/> 49 <UsageCount Value="68"/> 49 <CursorPos X="21" Y="18"/> 50 <UsageCount Value="69"/> 51 <Loaded Value="True"/> 50 52 </Unit0> 51 53 <Unit1> … … 57 59 <EditorIndex Value="0"/> 58 60 <WindowIndex Value="0"/> 59 <TopLine Value=" 1"/>60 <CursorPos X=" 15" Y="67"/>61 <UsageCount Value="6 8"/>61 <TopLine Value="263"/> 62 <CursorPos X="8" Y="276"/> 63 <UsageCount Value="69"/> 62 64 <Loaded Value="True"/> 63 65 <LoadedDesigner Value="True"/> … … 67 69 <UnitName Value="UMicroThreading"/> 68 70 <IsVisibleTab Value="True"/> 69 <EditorIndex Value=" 1"/>70 <WindowIndex Value="0"/> 71 <TopLine Value=" 446"/>72 <CursorPos X=" 6" Y="450"/>71 <EditorIndex Value="2"/> 72 <WindowIndex Value="0"/> 73 <TopLine Value="51"/> 74 <CursorPos X="22" Y="54"/> 73 75 <UsageCount Value="37"/> 74 76 <Loaded Value="True"/> … … 293 295 <Unit32> 294 296 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/> 295 <EditorIndex Value=" 5"/>297 <EditorIndex Value="8"/> 296 298 <WindowIndex Value="0"/> 297 299 <TopLine Value="1467"/> … … 302 304 <Unit33> 303 305 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/tthread.inc"/> 304 <EditorIndex Value=" 6"/>306 <EditorIndex Value="9"/> 305 307 <WindowIndex Value="0"/> 306 308 <TopLine Value="52"/> … … 325 327 <Unit36> 326 328 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classes.inc"/> 327 <EditorIndex Value=" 7"/>329 <EditorIndex Value="10"/> 328 330 <WindowIndex Value="0"/> 329 331 <TopLine Value="124"/> … … 341 343 <Unit38> 342 344 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/osutilsh.inc"/> 343 <EditorIndex Value=" 2"/>345 <EditorIndex Value="5"/> 344 346 <WindowIndex Value="0"/> 345 347 <TopLine Value="14"/> 346 <CursorPos X="1 1" Y="27"/>348 <CursorPos X="17" Y="27"/> 347 349 <UsageCount Value="13"/> 348 350 <Loaded Value="True"/> … … 357 359 <Unit40> 358 360 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/systemh.inc"/> 359 <WindowIndex Value="0"/> 360 <TopLine Value="492"/> 361 <CursorPos X="3" Y="504"/> 362 <UsageCount Value="9"/> 361 <EditorIndex Value="3"/> 362 <WindowIndex Value="0"/> 363 <TopLine Value="480"/> 364 <CursorPos X="3" Y="493"/> 365 <UsageCount Value="10"/> 366 <Loaded Value="True"/> 363 367 </Unit40> 364 368 <Unit41> … … 414 418 <Filename Value="../UPlatform.pas"/> 415 419 <UnitName Value="UPlatform"/> 416 <EditorIndex Value=" 9"/>417 <WindowIndex Value="0"/> 418 <TopLine Value="1 "/>419 <CursorPos X="1 4" Y="1"/>420 <EditorIndex Value="12"/> 421 <WindowIndex Value="0"/> 422 <TopLine Value="19"/> 423 <CursorPos X="1" Y="41"/> 420 424 <UsageCount Value="17"/> 421 425 <Loaded Value="True"/> … … 481 485 <Unit57> 482 486 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/lists.inc"/> 483 <EditorIndex Value=" 8"/>487 <EditorIndex Value="11"/> 484 488 <WindowIndex Value="0"/> 485 489 <TopLine Value="590"/> … … 490 494 <Unit58> 491 495 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32callback.inc"/> 492 <EditorIndex Value=" 4"/>496 <EditorIndex Value="7"/> 493 497 <WindowIndex Value="0"/> 494 498 <TopLine Value="1086"/> … … 508 512 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/customtimer.pas"/> 509 513 <UnitName Value="CustomTimer"/> 510 <EditorIndex Value=" 3"/>514 <EditorIndex Value="6"/> 511 515 <WindowIndex Value="0"/> 512 516 <TopLine Value="40"/> … … 517 521 <Unit61> 518 522 <Filename Value="../ReadMe.txt"/> 519 <EditorIndex Value="1 0"/>523 <EditorIndex Value="13"/> 520 524 <WindowIndex Value="0"/> 521 525 <TopLine Value="1"/> … … 525 529 <DefaultSyntaxHighlighter Value="None"/> 526 530 </Unit61> 531 <Unit62> 532 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/forms.pp"/> 533 <UnitName Value="Forms"/> 534 <EditorIndex Value="1"/> 535 <WindowIndex Value="0"/> 536 <TopLine Value="57"/> 537 <CursorPos X="3" Y="70"/> 538 <UsageCount Value="10"/> 539 <Loaded Value="True"/> 540 </Unit62> 527 541 </Units> 528 542 <JumpHistory Count="30" HistoryIndex="29"> 529 543 <Position1> 530 544 <Filename Value="../UMicroThreading.pas"/> 531 <Caret Line="2 79" Column="19" TopLine="267"/>545 <Caret Line="250" Column="1" TopLine="228"/> 532 546 </Position1> 533 547 <Position2> 534 548 <Filename Value="../UMicroThreading.pas"/> 535 <Caret Line=" 295" Column="1" TopLine="270"/>549 <Caret Line="472" Column="1" TopLine="459"/> 536 550 </Position2> 537 551 <Position3> 538 552 <Filename Value="../UMicroThreading.pas"/> 539 <Caret Line=" 289" Column="1" TopLine="270"/>553 <Caret Line="473" Column="1" TopLine="459"/> 540 554 </Position3> 541 555 <Position4> 542 556 <Filename Value="../UMicroThreading.pas"/> 543 <Caret Line=" 290" Column="1" TopLine="270"/>557 <Caret Line="474" Column="1" TopLine="459"/> 544 558 </Position4> 545 559 <Position5> 546 560 <Filename Value="../UMicroThreading.pas"/> 547 <Caret Line=" 291" Column="1" TopLine="270"/>561 <Caret Line="459" Column="1" TopLine="446"/> 548 562 </Position5> 549 563 <Position6> 550 564 <Filename Value="../UMicroThreading.pas"/> 551 <Caret Line="2 92" Column="1" TopLine="270"/>565 <Caret Line="267" Column="1" TopLine="254"/> 552 566 </Position6> 553 567 <Position7> 554 568 <Filename Value="../UMicroThreading.pas"/> 555 <Caret Line=" 598" Column="50" TopLine="587"/>569 <Caret Line="268" Column="1" TopLine="254"/> 556 570 </Position7> 557 571 <Position8> 558 <Filename Value="../U MicroThreading.pas"/>559 <Caret Line="29 1" Column="40" TopLine="278"/>572 <Filename Value="../UPlatform.pas"/> 573 <Caret Line="29" Column="1" TopLine="16"/> 560 574 </Position8> 561 575 <Position9> 562 <Filename Value="../U MicroThreading.pas"/>563 <Caret Line=" 280" Column="1" TopLine="272"/>576 <Filename Value="../UPlatform.pas"/> 577 <Caret Line="31" Column="1" TopLine="16"/> 564 578 </Position9> 565 579 <Position10> 566 <Filename Value="../U MicroThreading.pas"/>567 <Caret Line=" 288" Column="26" TopLine="272"/>580 <Filename Value="../UPlatform.pas"/> 581 <Caret Line="40" Column="1" TopLine="18"/> 568 582 </Position10> 569 583 <Position11> 570 <Filename Value="../U MicroThreading.pas"/>571 <Caret Line=" 285" Column="35" TopLine="272"/>584 <Filename Value="../UPlatform.pas"/> 585 <Caret Line="41" Column="1" TopLine="19"/> 572 586 </Position11> 573 587 <Position12> 574 588 <Filename Value="../UMicroThreading.pas"/> 575 <Caret Line="2 88" Column="1" TopLine="272"/>589 <Caret Line="269" Column="1" TopLine="254"/> 576 590 </Position12> 577 591 <Position13> 578 592 <Filename Value="../UMicroThreading.pas"/> 579 <Caret Line="2 89" Column="1" TopLine="272"/>593 <Caret Line="270" Column="1" TopLine="260"/> 580 594 </Position13> 581 595 <Position14> 582 596 <Filename Value="../UMicroThreading.pas"/> 583 <Caret Line="2 90" Column="1" TopLine="272"/>597 <Caret Line="273" Column="1" TopLine="260"/> 584 598 </Position14> 585 599 <Position15> 586 600 <Filename Value="../UMicroThreading.pas"/> 587 <Caret Line="2 91" Column="1" TopLine="272"/>601 <Caret Line="274" Column="1" TopLine="260"/> 588 602 </Position15> 589 603 <Position16> 590 604 <Filename Value="../UMicroThreading.pas"/> 591 <Caret Line="2 92" Column="1" TopLine="272"/>605 <Caret Line="275" Column="1" TopLine="260"/> 592 606 </Position16> 593 607 <Position17> 594 608 <Filename Value="../UMicroThreading.pas"/> 595 <Caret Line="2 93" Column="1" TopLine="272"/>609 <Caret Line="276" Column="1" TopLine="260"/> 596 610 </Position17> 597 611 <Position18> 598 612 <Filename Value="../UMicroThreading.pas"/> 599 <Caret Line="2 96" Column="1" TopLine="274"/>613 <Caret Line="277" Column="1" TopLine="260"/> 600 614 </Position18> 601 615 <Position19> 602 616 <Filename Value="../UMicroThreading.pas"/> 603 <Caret Line="2 97" Column="1" TopLine="275"/>617 <Caret Line="278" Column="1" TopLine="260"/> 604 618 </Position19> 605 619 <Position20> 606 620 <Filename Value="../UMicroThreading.pas"/> 607 <Caret Line="2 98" Column="1" TopLine="276"/>621 <Caret Line="281" Column="1" TopLine="260"/> 608 622 </Position20> 609 623 <Position21> 610 624 <Filename Value="../UMicroThreading.pas"/> 611 <Caret Line="2 99" Column="1" TopLine="277"/>625 <Caret Line="282" Column="1" TopLine="260"/> 612 626 </Position21> 613 627 <Position22> 614 628 <Filename Value="../UMicroThreading.pas"/> 615 <Caret Line=" 471" Column="1" TopLine="458"/>629 <Caret Line="283" Column="1" TopLine="261"/> 616 630 </Position22> 617 631 <Position23> 618 632 <Filename Value="../UMicroThreading.pas"/> 619 <Caret Line="28 5" Column="32" TopLine="275"/>633 <Caret Line="284" Column="1" TopLine="262"/> 620 634 </Position23> 621 635 <Position24> 622 <Filename Value="../ ../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/>623 <Caret Line=" 1488" Column="37" TopLine="1467"/>636 <Filename Value="../UMicroThreading.pas"/> 637 <Caret Line="286" Column="1" TopLine="264"/> 624 638 </Position24> 625 639 <Position25> 626 <Filename Value=" ../UMicroThreading.pas"/>627 <Caret Line=" 71" Column="5" TopLine="55"/>640 <Filename Value="UMainForm.pas"/> 641 <Caret Line="276" Column="1" TopLine="263"/> 628 642 </Position25> 629 643 <Position26> 630 644 <Filename Value="../UMicroThreading.pas"/> 631 <Caret Line=" 125" Column="1" TopLine="110"/>645 <Caret Line="249" Column="1" TopLine="236"/> 632 646 </Position26> 633 647 <Position27> 634 <Filename Value=" UMainForm.pas"/>635 <Caret Line=" 68" Column="29" TopLine="54"/>648 <Filename Value="../UMicroThreading.pas"/> 649 <Caret Line="250" Column="1" TopLine="236"/> 636 650 </Position27> 637 651 <Position28> 638 <Filename Value=" ../UMicroThreading.pas"/>639 <Caret Line=" 566" Column="20" TopLine="561"/>652 <Filename Value="UMainForm.pas"/> 653 <Caret Line="276" Column="8" TopLine="263"/> 640 654 </Position28> 641 655 <Position29> 642 656 <Filename Value="../UMicroThreading.pas"/> 643 <Caret Line=" 567" Column="23" TopLine="554"/>657 <Caret Line="250" Column="29" TopLine="245"/> 644 658 </Position29> 645 659 <Position30> 646 660 <Filename Value="../UMicroThreading.pas"/> 647 <Caret Line=" 579" Column="51" TopLine="560"/>661 <Caret Line="64" Column="17" TopLine="51"/> 648 662 </Position30> 649 663 </JumpHistory> -
MicroThreading/Demo/UMainForm.pas
r153 r154 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 ComCtrls, ExtCtrls, Spin, UMicroThreading, Coroutine,DateUtils, UPlatform;9 ComCtrls, ExtCtrls, Spin, UMicroThreading, DateUtils, UPlatform; 10 10 11 11 type … … 67 67 procedure Worker(MicroThread: TMicroThread); 68 68 procedure WorkerDoWrite; 69 procedure WorkerSubRoutine; 69 70 public 70 71 Iterations: Integer; 71 Scheduler: TMicroThreadScheduler;72 72 end; 73 73 … … 85 85 procedure TMainForm.FormCreate(Sender: TObject); 86 86 begin 87 Scheduler := TMicroThreadScheduler.Create;88 87 DoubleBuffered := True; 89 88 ListView1.DoubleBuffered := True; … … 97 96 if ButtonSchedulerStartStop.Caption = 'Start scheduler' then begin 98 97 ButtonSchedulerStartStop.Caption := 'Stop scheduler'; 99 Scheduler.ThreadPoolSize := SpinEdit2.Value;100 Scheduler.Active := True;98 MainScheduler.ThreadPoolSize := SpinEdit2.Value; 99 MainScheduler.Active := True; 101 100 end else begin 102 101 ButtonSchedulerStartStop.Caption := 'Start scheduler'; 103 Scheduler.Active := False;102 MainScheduler.Active := False; 104 103 end; 105 104 end; … … 169 168 //Scheduler.FMicroThreads.Clear; 170 169 for I := 0 to SpinEdit1.Value - 1 do 171 Scheduler.AddMethod(Worker);170 MainScheduler.AddMethod(Worker); 172 171 end; 173 172 … … 197 196 begin 198 197 try 199 Scheduler.MicroThreadsLock.Acquire;200 Scheduler.MicroThreads.Clear;198 MainScheduler.MicroThreadsLock.Acquire; 199 MainScheduler.MicroThreads.Clear; 201 200 finally 202 Scheduler.MicroThreadsLock.Release;201 MainScheduler.MicroThreadsLock.Release; 203 202 end; 204 203 end; … … 206 205 procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject); 207 206 begin 208 Scheduler.UseMainThread := CheckBoxUseMainThread.Checked;207 MainScheduler.UseMainThread := CheckBoxUseMainThread.Checked; 209 208 end; 210 209 211 210 procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction); 212 211 begin 213 Scheduler.Active := False;212 MainScheduler.Active := False; 214 213 end; 215 214 216 215 procedure TMainForm.FormDestroy(Sender: TObject); 217 216 begin 218 Scheduler.Free;217 MainScheduler.Free; 219 218 end; 220 219 … … 227 226 begin 228 227 try 229 Scheduler.MicroThreadsLock.Acquire;230 if Item.Index < Scheduler.MicroThreads.Count then231 with TMicroThread( Scheduler.MicroThreads[Item.Index]) do begin228 MainScheduler.MicroThreadsLock.Acquire; 229 if Item.Index < MainScheduler.MicroThreads.Count then 230 with TMicroThread(MainScheduler.MicroThreads[Item.Index]) do begin 232 231 Item.Caption := IntToStr(Id); 233 232 Item.SubItems.Add(Name); … … 240 239 end; 241 240 finally 242 Scheduler.MicroThreadsLock.Release;241 MainScheduler.MicroThreadsLock.Release; 243 242 end; 244 243 end; … … 246 245 procedure TMainForm.SpinEdit2Change(Sender: TObject); 247 246 begin 248 Scheduler.ThreadPoolSize := SpinEdit2.Value;247 MainScheduler.ThreadPoolSize := SpinEdit2.Value; 249 248 end; 250 249 … … 256 255 procedure TMainForm.TimerRedrawTimer(Sender: TObject); 257 256 begin 258 if ListView1.Items.Count <> Scheduler.MicroThreadCount then259 ListView1.Items.Count := Scheduler.MicroThreadCount;257 if ListView1.Items.Count <> MainScheduler.MicroThreadCount then 258 ListView1.Items.Count := MainScheduler.MicroThreadCount; 260 259 ListView1.Items[-1]; 261 260 ListView1.Refresh; 262 261 Label2.Caption := DateTimeToStr(NowPrecise) + ' ' + 263 262 FloatToStr(Frac(NowPrecise / OneSecond)); 264 Label9.Caption := IntToStr( Scheduler.ThreadPoolCount);265 Label10.Caption := IntToStr( Scheduler.MicroThreadCount);263 Label9.Caption := IntToStr(MainScheduler.ThreadPoolCount); 264 Label10.Caption := IntToStr(MainScheduler.MicroThreadCount); 266 265 end; 267 266 … … 271 270 ButtonAddWorkers.Click; 272 271 ButtonSchedulerStartStop.Click; 272 end; 273 274 procedure TMainForm.WorkerSubRoutine; 275 begin 276 MTSleep(1 * OneMillisecond); 273 277 end; 274 278 … … 287 291 // FloatToStr(ExecutionTime)); 288 292 Completion := I / Iterations; 289 // Sleep(1 * Id * OneMillisecond);293 //MTSleep(1 * Id * OneMillisecond); 290 294 Yield; 295 WorkerSubRoutine; 291 296 end; 292 297 end; -
MicroThreading/UMicroThreading.pas
r153 r154 30 30 TMicroThreadState = (tsWaiting, tsRunning, tsBlocked, tsSuspended, 31 31 tsSleeping); 32 33 { TMicroThreadEvent } 34 35 TMicroThreadEvent = class 36 private 37 FAutoReset: Boolean; 38 FSignaled: Boolean; 39 FMicroThreads: TObjectList; 40 FMicroThreadsLock: TCriticalSection; 41 public 42 procedure SetEvent; 43 procedure ResetEvent; 44 procedure WaitFor(Duration: TDateTime); 45 constructor Create; 46 destructor Destroy; override; 47 property Signaled: Boolean read FSignaled; 48 property AutoReset: Boolean read FAutoReset write FAutoReset; 49 end; 32 50 33 51 { TMicroThread } … … 48 66 FFinished: Boolean; 49 67 FSuspended: Boolean; 68 FBlocked: Boolean; 50 69 FState: TMicroThreadState; 51 70 FScheduler: TMicroThreadScheduler; 52 71 FManager: TMicroThreadManager; 53 72 FId: Integer; 73 FBlockCondition: Boolean; 54 74 procedure CallExecute; 55 75 function GetStackUsed: Integer; … … 58 78 Name: string; 59 79 Priority: Integer; 60 Completion: Single; // Can be used for progress information in range <0, 1>80 Completion: Single; // Can be used for progress information usually in range <0, 1> 61 81 procedure Execute; virtual; 62 82 63 83 procedure Yield; 64 procedure Sleep(Duration: TDateTime);65 function WaitFor Signal(Signal: TEvent): TWaitResult;84 procedure MTSleep(Duration: TDateTime); // No conflicting name to global Sleep procedure 85 function WaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult; 66 86 procedure WaitFor; 67 87 procedure Terminate; … … 86 106 end; 87 107 88 TMicroThread Event= procedure(MicroThread: TMicroThread) of object;89 90 { TMicroThread Method}91 92 TMicroThread Method= class(TMicroThread)93 Method: TMicroThread Event;108 TMicroThreadMethod = procedure(MicroThread: TMicroThread) of object; 109 110 { TMicroThreadSimple } 111 112 TMicroThreadSimple = class(TMicroThread) 113 Method: TMicroThreadMethod; 94 114 procedure Execute; override; 95 115 end; … … 166 186 public 167 187 function Add(MicroThread: TMicroThread): Integer; 168 function AddMethod(Method: TMicroThreadEvent): Integer; 188 function AddMethod(Method: TMicroThreadMethod): Integer; 189 procedure Remove(MicroThread: TMicroThread); 169 190 constructor Create; 170 191 destructor Destroy; override; … … 187 208 MicroThreadStateText: array[TMicroThreadState] of string = ('Waiting', 188 209 'Running', 'Blocked', 'Suspended', 'Sleeping'); 210 211 function GetCurrentMicroThread: TMicroThread; 212 procedure MTSleep(Duration: TDateTime); 213 function MTWaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult; 189 214 190 215 implementation … … 218 243 end; 219 244 end; 245 end; 246 247 function GetCurrentMicroThread: TMicroThread; 248 var 249 I: Integer; 250 begin 251 with MainScheduler do 252 try 253 FThreadPoolLock.Acquire; 254 if MainThreadID = ThreadID then Result := MainThreadManager.CurrentMicroThread 255 else begin 256 I := 0; 257 while (I < FThreadPool.Count) and (TMicroThreadThread(FThreadPool[I]).ThreadID <> ThreadID) do Inc(I); 258 if I < FThreadPool.Count then Result := TMicroThreadThread(FThreadPool[I]).Manager.CurrentMicroThread 259 else Result := nil; 260 end; 261 finally 262 FThreadPoolLock.Release; 263 end; 264 end; 265 266 procedure MTSleep(Duration: TDateTime); 267 var 268 MT: TMicroThread; 269 begin 270 MT := GetCurrentMicroThread; 271 if Assigned(MT) then MT.MTSleep(Duration) 272 else Sleep(Trunc(Duration / OneMillisecond)); 273 end; 274 275 function MTWaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult; 276 var 277 MT: TMicroThread; 278 begin 279 MT := GetCurrentMicroThread; 280 if Assigned(MT) then Result := MT.WaitForEvent(Event, Duration); 281 // else Result := Event.WaitFor(Trunc(Duration / OneMillisecond)); 282 end; 283 284 { TMicroThreadMethod } 285 286 procedure TMicroThreadEvent.SetEvent; 287 var 288 I: Integer; 289 begin 290 for I := 0 to FMicroThreads.Count - 1 do 291 TMicroThread(FMicroThreads[I]).FBlockCondition := True; 292 if not FAutoReset then FSignaled := True; 293 end; 294 295 procedure TMicroThreadEvent.ResetEvent; 296 begin 297 FSignaled := False; 298 end; 299 300 procedure TMicroThreadEvent.WaitFor(Duration: TDateTime); 301 var 302 MT: TMicroThread; 303 begin 304 MT := GetCurrentMicroThread; 305 if Assigned(MT) then MT.WaitForEvent(Self, Duration); 306 end; 307 308 constructor TMicroThreadEvent.Create; 309 begin 310 FMicroThreads := TObjectList.Create; 311 FMicroThreads.OwnsObjects := False; 312 FMicroThreadsLock := TCriticalSection.Create; 313 end; 314 315 destructor TMicroThreadEvent.Destroy; 316 begin 317 FMicroThreadsLock.Free; 318 FMicroThreads.Free; 319 inherited Destroy; 220 320 end; 221 321 … … 390 490 end; 391 491 392 { TMicroThread Method}393 394 procedure TMicroThread Method.Execute;492 { TMicroThreadSimple } 493 494 procedure TMicroThreadSimple.Execute; 395 495 begin 396 496 inherited Execute; … … 433 533 if GetMicroThreadId <> -1 then 434 534 while not FFinished do begin 435 Sleep(1);436 end; 437 end; 438 439 procedure TMicroThread. Sleep(Duration: TDateTime);535 MTSleep(1); 536 end; 537 end; 538 539 procedure TMicroThread.MTSleep(Duration: TDateTime); 440 540 begin 441 541 FWakeUpTime := NowPrecise + Duration; … … 444 544 end; 445 545 446 function TMicroThread.WaitForSignal(Signal: TEvent): TWaitResult; 447 begin 546 function TMicroThread.WaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult; 547 begin 548 try 549 Event.FMicroThreadsLock.Acquire; 550 Event.FMicroThreads.Add(Self); 551 finally 552 Event.FMicroThreadsLock.Release; 553 end; 554 FBlocked := True; 555 Yield; 448 556 repeat 449 Result := Signal.WaitFor(1); 450 Sleep(1); 557 if FState = tsBlocked then MTSleep(1); 451 558 until Result <> wrTimeout; 559 try 560 Event.FMicroThreadsLock.Acquire; 561 Event.FMicroThreads.Remove(Self); 562 finally 563 Event.FMicroThreadsLock.Release; 564 end; 452 565 end; 453 566 … … 520 633 end; 521 634 522 function TMicroThreadScheduler.AddMethod(Method: TMicroThread Event): Integer;523 var 524 NewMicroThread: TMicroThread Method;525 begin 526 NewMicroThread := TMicroThread Method.Create(False);635 function TMicroThreadScheduler.AddMethod(Method: TMicroThreadMethod): Integer; 636 var 637 NewMicroThread: TMicroThreadSimple; 638 begin 639 NewMicroThread := TMicroThreadSimple.Create(False); 527 640 NewMicroThread.Method := Method; 528 641 NewMicroThread.FScheduler := Self; 529 642 Result := Add(NewMicroThread); 643 end; 644 645 procedure TMicroThreadScheduler.Remove(MicroThread: TMicroThread); 646 begin 647 try 648 FMicroThreadsLock.Acquire; 649 FMicroThreads.Remove(MicroThread); 650 finally 651 FMicroThreadsLock.Release; 652 end; 530 653 end; 531 654 … … 664 787 if (TMicroThread(FMicroThreads[FRoundRobinIndex]).FState = tsSleeping) and 665 788 (TMicroThread(FMicroThreads[FRoundRobinIndex]).FWakeupTime < CurrentTime) then 666 TMicroThread(FMicroThreads[FRoundRobinIndex]).FState := tsWaiting else 667 begin 789 TMicroThread(FMicroThreads[FRoundRobinIndex]).FState := tsWaiting 790 else 791 if (TMicroThread(FMicroThreads[FRoundRobinIndex]).FState = tsBlocked) and 792 (TMicroThread(FMicroThreads[FRoundRobinIndex]).FBlockCondition) then 793 TMicroThread(FMicroThreads[FRoundRobinIndex]).FState := tsWaiting 794 else begin 668 795 // Go to next thread 669 796 Inc(I); … … 748 875 initialization 749 876 750 //StaticManagers := TObjectList.Create; 751 //MainScheduler := TMicroThreadScheduler.Create; 877 MainScheduler := TMicroThreadScheduler.Create; 752 878 753 879 finalization 754 880 755 //MainScheduler.Free; 756 //StaticManagers.Free; 881 MainScheduler.Free; 757 882 758 883 end.
Note:
See TracChangeset
for help on using the changeset viewer.