Changeset 160 for MicroThreading
- Timestamp:
- Feb 1, 2011, 10:06:01 AM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading
- Property svn:ignore
-
old new 1 1 lib 2 backup
-
- Property svn:ignore
-
MicroThreading/Demo
- Property svn:ignore
-
old new 2 2 lib 3 3 Demo.exe 4 backup
-
- Property svn:ignore
-
MicroThreading/Demo/Demo.lpi
r159 r160 39 39 </Item2> 40 40 </RequiredPackages> 41 <Units Count=" 69">41 <Units Count="79"> 42 42 <Unit0> 43 43 <Filename Value="Demo.lpr"/> 44 44 <IsPartOfProject Value="True"/> 45 45 <UnitName Value="Demo"/> 46 <EditorIndex Value=" 4"/>46 <EditorIndex Value="17"/> 47 47 <WindowIndex Value="0"/> 48 48 <TopLine Value="1"/> 49 49 <CursorPos X="34" Y="16"/> 50 <UsageCount Value=" 89"/>50 <UsageCount Value="92"/> 51 51 <Loaded Value="True"/> 52 52 </Unit0> … … 57 57 <ResourceBaseClass Value="Form"/> 58 58 <UnitName Value="UMainForm"/> 59 <EditorIndex Value="2"/> 60 <WindowIndex Value="0"/> 61 <TopLine Value="100"/> 62 <CursorPos X="45" Y="122"/> 63 <UsageCount Value="89"/> 59 <IsVisibleTab Value="True"/> 60 <EditorIndex Value="9"/> 61 <WindowIndex Value="0"/> 62 <TopLine Value="345"/> 63 <CursorPos X="29" Y="355"/> 64 <UsageCount Value="92"/> 64 65 <Loaded Value="True"/> 65 66 <LoadedDesigner Value="True"/> … … 70 71 <EditorIndex Value="0"/> 71 72 <WindowIndex Value="0"/> 72 <TopLine Value=" 317"/>73 <CursorPos X=" 26" Y="334"/>74 <UsageCount Value="4 6"/>73 <TopLine Value="544"/> 74 <CursorPos X="1" Y="557"/> 75 <UsageCount Value="47"/> 75 76 <Loaded Value="True"/> 76 77 </Unit2> … … 113 114 <CursorPos X="1" Y="8"/> 114 115 <UsageCount Value="8"/> 115 <DefaultSyntaxHighlighter Value="Delphi"/>116 116 </Unit7> 117 117 <Unit8> … … 122 122 <CursorPos X="24" Y="77"/> 123 123 <UsageCount Value="17"/> 124 <DefaultSyntaxHighlighter Value="Delphi"/>125 124 </Unit8> 126 125 <Unit9> … … 131 130 <CursorPos X="37" Y="141"/> 132 131 <UsageCount Value="17"/> 133 <DefaultSyntaxHighlighter Value="Delphi"/>134 132 </Unit9> 135 133 <Unit10> … … 251 249 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysutils.pp"/> 252 250 <UnitName Value="sysutils"/> 253 <WindowIndex Value="0"/> 254 <TopLine Value="581"/> 255 <CursorPos X="11" Y="585"/> 256 <UsageCount Value="10"/> 251 <EditorIndex Value="14"/> 252 <WindowIndex Value="0"/> 253 <TopLine Value="1034"/> 254 <CursorPos X="14" Y="1037"/> 255 <UsageCount Value="11"/> 256 <Loaded Value="True"/> 257 257 </Unit26> 258 258 <Unit27> … … 308 308 <Unit34> 309 309 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/wininc/func.inc"/> 310 <WindowIndex Value="0"/> 311 <TopLine Value="130"/> 312 <CursorPos X="10" Y="143"/> 313 <UsageCount Value="11"/> 310 <EditorIndex Value="15"/> 311 <WindowIndex Value="0"/> 312 <TopLine Value="147"/> 313 <CursorPos X="1" Y="158"/> 314 <UsageCount Value="12"/> 315 <Loaded Value="True"/> 314 316 </Unit34> 315 317 <Unit35> … … 336 338 <Unit38> 337 339 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/osutilsh.inc"/> 340 <EditorIndex Value="13"/> 338 341 <WindowIndex Value="0"/> 339 342 <TopLine Value="14"/> 340 <CursorPos X="17" Y="27"/> 341 <UsageCount Value="12"/> 343 <CursorPos X="11" Y="27"/> 344 <UsageCount Value="13"/> 345 <Loaded Value="True"/> 342 346 </Unit38> 343 347 <Unit39> … … 407 411 <Filename Value="../UPlatform.pas"/> 408 412 <UnitName Value="UPlatform"/> 409 <EditorIndex Value=" 1"/>413 <EditorIndex Value="8"/> 410 414 <WindowIndex Value="0"/> 411 415 <TopLine Value="23"/> 412 416 <CursorPos X="31" Y="38"/> 413 <UsageCount Value="2 6"/>417 <UsageCount Value="27"/> 414 418 <Loaded Value="True"/> 415 419 </Unit48> … … 552 556 <ResourceBaseClass Value="Form"/> 553 557 <UnitName Value="UMicroThreadList"/> 554 <IsVisibleTab Value="True"/> 555 <EditorIndex Value="3"/> 556 <WindowIndex Value="0"/> 557 <TopLine Value="1"/> 558 <CursorPos X="29" Y="9"/> 559 <UsageCount Value="10"/> 558 <EditorIndex Value="16"/> 559 <WindowIndex Value="0"/> 560 <TopLine Value="1"/> 561 <CursorPos X="44" Y="17"/> 562 <UsageCount Value="11"/> 560 563 <Loaded Value="True"/> 561 564 <LoadedDesigner Value="True"/> … … 568 571 <UsageCount Value="10"/> 569 572 </Unit68> 573 <Unit69> 574 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/objpas/sysutils/sysutilh.inc"/> 575 <EditorIndex Value="12"/> 576 <WindowIndex Value="0"/> 577 <TopLine Value="100"/> 578 <CursorPos X="16" Y="119"/> 579 <UsageCount Value="11"/> 580 <Loaded Value="True"/> 581 </Unit69> 582 <Unit70> 583 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/lcl/interfaces/win32/win32object.inc"/> 584 <EditorIndex Value="7"/> 585 <WindowIndex Value="0"/> 586 <TopLine Value="600"/> 587 <CursorPos X="1" Y="613"/> 588 <UsageCount Value="10"/> 589 <Loaded Value="True"/> 590 </Unit70> 591 <Unit71> 592 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/objpas/classes/classesh.inc"/> 593 <EditorIndex Value="10"/> 594 <WindowIndex Value="0"/> 595 <TopLine Value="1483"/> 596 <CursorPos X="51" Y="1490"/> 597 <UsageCount Value="10"/> 598 <Loaded Value="True"/> 599 </Unit71> 600 <Unit72> 601 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/win/tthread.inc"/> 602 <EditorIndex Value="11"/> 603 <WindowIndex Value="0"/> 604 <TopLine Value="30"/> 605 <CursorPos X="3" Y="15"/> 606 <UsageCount Value="10"/> 607 <Loaded Value="True"/> 608 </Unit72> 609 <Unit73> 610 <Filename Value="../UThreadEx.pas"/> 611 <UnitName Value="UThreadEx"/> 612 <EditorIndex Value="1"/> 613 <WindowIndex Value="0"/> 614 <TopLine Value="1"/> 615 <CursorPos X="20" Y="1"/> 616 <UsageCount Value="20"/> 617 <Loaded Value="True"/> 618 </Unit73> 619 <Unit74> 620 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/inc/systemh.inc"/> 621 <EditorIndex Value="5"/> 622 <WindowIndex Value="0"/> 623 <TopLine Value="982"/> 624 <CursorPos X="10" Y="995"/> 625 <UsageCount Value="10"/> 626 <Loaded Value="True"/> 627 </Unit74> 628 <Unit75> 629 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/inc/system.inc"/> 630 <EditorIndex Value="6"/> 631 <WindowIndex Value="0"/> 632 <TopLine Value="692"/> 633 <CursorPos X="1" Y="694"/> 634 <UsageCount Value="10"/> 635 <Loaded Value="True"/> 636 </Unit75> 637 <Unit76> 638 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/objpas/sysutils/osutilsh.inc"/> 639 <EditorIndex Value="4"/> 640 <WindowIndex Value="0"/> 641 <TopLine Value="14"/> 642 <CursorPos X="11" Y="27"/> 643 <UsageCount Value="10"/> 644 <Loaded Value="True"/> 645 </Unit76> 646 <Unit77> 647 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/inc/threadh.inc"/> 648 <EditorIndex Value="2"/> 649 <WindowIndex Value="0"/> 650 <TopLine Value="57"/> 651 <CursorPos X="5" Y="70"/> 652 <UsageCount Value="10"/> 653 <Loaded Value="True"/> 654 </Unit77> 655 <Unit78> 656 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.5.1/fpc/2.5.1/source/rtl/inc/thread.inc"/> 657 <EditorIndex Value="3"/> 658 <WindowIndex Value="0"/> 659 <TopLine Value="169"/> 660 <CursorPos X="33" Y="184"/> 661 <UsageCount Value="10"/> 662 <Loaded Value="True"/> 663 </Unit78> 570 664 </Units> 571 <JumpHistory Count=" 21" HistoryIndex="20">665 <JumpHistory Count="30" HistoryIndex="29"> 572 666 <Position1> 573 667 <Filename Value="UMainForm.pas"/> 574 <Caret Line=" 147" Column="10" TopLine="147"/>668 <Caret Line="355" Column="1" TopLine="339"/> 575 669 </Position1> 576 670 <Position2> 577 <Filename Value=" UMainForm.pas"/>578 <Caret Line=" 10" Column="26" TopLine="1"/>671 <Filename Value="../UThreadEx.pas"/> 672 <Caret Line="37" Column="1" TopLine="24"/> 579 673 </Position2> 580 674 <Position3> 581 <Filename Value=" UMainForm.pas"/>582 <Caret Line=" 16" Column="48" TopLine="1"/>675 <Filename Value="../UThreadEx.pas"/> 676 <Caret Line="38" Column="1" TopLine="24"/> 583 677 </Position3> 584 678 <Position4> 585 <Filename Value="../U MicroThreading.pas"/>586 <Caret Line=" 227" Column="20" TopLine="206"/>679 <Filename Value="../UThreadEx.pas"/> 680 <Caret Line="39" Column="1" TopLine="24"/> 587 681 </Position4> 588 682 <Position5> 589 <Filename Value="../U MicroThreading.pas"/>590 <Caret Line=" 21" Column="33" TopLine="1"/>683 <Filename Value="../UThreadEx.pas"/> 684 <Caret Line="41" Column="1" TopLine="24"/> 591 685 </Position5> 592 686 <Position6> 593 <Filename Value=" UMainForm.pas"/>594 <Caret Line=" 90" Column="26" TopLine="85"/>687 <Filename Value="../UThreadEx.pas"/> 688 <Caret Line="42" Column="1" TopLine="24"/> 595 689 </Position6> 596 690 <Position7> 597 <Filename Value=" UMainForm.pas"/>598 <Caret Line=" 5" Column="15" TopLine="1"/>691 <Filename Value="../UThreadEx.pas"/> 692 <Caret Line="44" Column="1" TopLine="24"/> 599 693 </Position7> 600 694 <Position8> 601 <Filename Value=" UMainForm.pas"/>602 <Caret Line=" 150" Column="28" TopLine="147"/>695 <Filename Value="../UThreadEx.pas"/> 696 <Caret Line="45" Column="1" TopLine="24"/> 603 697 </Position8> 604 698 <Position9> 605 <Filename Value=" UMainForm.pas"/>606 <Caret Line=" 85" Column="15" TopLine="72"/>699 <Filename Value="../UMicroThreading.pas"/> 700 <Caret Line="130" Column="17" TopLine="117"/> 607 701 </Position9> 608 702 <Position10> 609 703 <Filename Value="UMainForm.pas"/> 610 <Caret Line=" 151" Column="15" TopLine="138"/>704 <Caret Line="378" Column="1" TopLine="365"/> 611 705 </Position10> 612 706 <Position11> 613 <Filename Value=" UMainForm.pas"/>614 <Caret Line=" 93" Column="30" TopLine="80"/>707 <Filename Value="../UMicroThreading.pas"/> 708 <Caret Line="552" Column="1" TopLine="539"/> 615 709 </Position11> 616 710 <Position12> 617 711 <Filename Value="UMainForm.pas"/> 618 <Caret Line=" 123" Column="59" TopLine="110"/>712 <Caret Line="352" Column="1" TopLine="339"/> 619 713 </Position12> 620 714 <Position13> 621 715 <Filename Value="UMainForm.pas"/> 622 <Caret Line=" 122" Column="1" TopLine="110"/>716 <Caret Line="353" Column="1" TopLine="339"/> 623 717 </Position13> 624 718 <Position14> 625 <Filename Value=" ../UMicroThreading.pas"/>626 <Caret Line=" 217" Column="1" TopLine="197"/>719 <Filename Value="UMainForm.pas"/> 720 <Caret Line="354" Column="1" TopLine="339"/> 627 721 </Position14> 628 722 <Position15> 629 <Filename Value=" ../UMicroThreading.pas"/>630 <Caret Line=" 250" Column="20" TopLine="230"/>723 <Filename Value="UMainForm.pas"/> 724 <Caret Line="355" Column="1" TopLine="339"/> 631 725 </Position15> 632 726 <Position16> 633 <Filename Value=" UMainForm.pas"/>634 <Caret Line=" 150" Column="16" TopLine="145"/>727 <Filename Value="../UThreadEx.pas"/> 728 <Caret Line="37" Column="1" TopLine="27"/> 635 729 </Position16> 636 730 <Position17> 637 <Filename Value=" UMainForm.pas"/>638 <Caret Line=" 93" Column="25" TopLine="80"/>731 <Filename Value="../UThreadEx.pas"/> 732 <Caret Line="38" Column="1" TopLine="27"/> 639 733 </Position17> 640 734 <Position18> 641 <Filename Value="../U MicroThreading.pas"/>642 <Caret Line="3 38" Column="3" TopLine="336"/>735 <Filename Value="../UThreadEx.pas"/> 736 <Caret Line="39" Column="1" TopLine="27"/> 643 737 </Position18> 644 738 <Position19> 645 <Filename Value=" UMainForm.pas"/>646 <Caret Line=" 5" Column="74" TopLine="3"/>739 <Filename Value="../UThreadEx.pas"/> 740 <Caret Line="41" Column="1" TopLine="27"/> 647 741 </Position19> 648 742 <Position20> 649 <Filename Value="../U MicroThreading.pas"/>650 <Caret Line=" 208" Column="22" TopLine="204"/>743 <Filename Value="../UThreadEx.pas"/> 744 <Caret Line="42" Column="1" TopLine="27"/> 651 745 </Position20> 652 746 <Position21> 747 <Filename Value="../UThreadEx.pas"/> 748 <Caret Line="44" Column="1" TopLine="27"/> 749 </Position21> 750 <Position22> 751 <Filename Value="../UThreadEx.pas"/> 752 <Caret Line="1" Column="20" TopLine="1"/> 753 </Position22> 754 <Position23> 653 755 <Filename Value="../UMicroThreading.pas"/> 654 <Caret Line="336" Column="36" TopLine="314"/> 655 </Position21> 756 <Caret Line="619" Column="24" TopLine="607"/> 757 </Position23> 758 <Position24> 759 <Filename Value="../UMicroThreading.pas"/> 760 <Caret Line="29" Column="33" TopLine="13"/> 761 </Position24> 762 <Position25> 763 <Filename Value="UMainForm.pas"/> 764 <Caret Line="378" Column="1" TopLine="365"/> 765 </Position25> 766 <Position26> 767 <Filename Value="../UMicroThreading.pas"/> 768 <Caret Line="557" Column="1" TopLine="544"/> 769 </Position26> 770 <Position27> 771 <Filename Value="UMainForm.pas"/> 772 <Caret Line="352" Column="1" TopLine="339"/> 773 </Position27> 774 <Position28> 775 <Filename Value="UMainForm.pas"/> 776 <Caret Line="353" Column="1" TopLine="339"/> 777 </Position28> 778 <Position29> 779 <Filename Value="UMainForm.pas"/> 780 <Caret Line="354" Column="1" TopLine="339"/> 781 </Position29> 782 <Position30> 783 <Filename Value="UMainForm.pas"/> 784 <Caret Line="355" Column="1" TopLine="339"/> 785 </Position30> 656 786 </JumpHistory> 657 787 </ProjectOptions> … … 663 793 <SearchPaths> 664 794 <IncludeFiles Value="$(ProjOutDir)"/> 795 <OtherUnitFiles Value=".."/> 665 796 <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> 666 797 </SearchPaths> … … 692 823 </CompilerOptions> 693 824 <Debugging> 694 <BreakPoints Count=" 1">825 <BreakPoints Count="7"> 695 826 <Item1> 696 827 <Source Value="../UMicroThreading.pas"/> 697 <Line Value="25 0"/>828 <Line Value="253"/> 698 829 </Item1> 830 <Item2> 831 <Source Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysutils.pp"/> 832 <Line Value="1036"/> 833 </Item2> 834 <Item3> 835 <Source Value="../UMicroThreading.pas"/> 836 <Line Value="591"/> 837 </Item3> 838 <Item4> 839 <Source Value="../UMicroThreading.pas"/> 840 <Line Value="590"/> 841 </Item4> 842 <Item5> 843 <Source Value="UMainForm.pas"/> 844 <Line Value="378"/> 845 </Item5> 846 <Item6> 847 <Source Value="../UMicroThreading.pas"/> 848 <Line Value="557"/> 849 </Item6> 850 <Item7> 851 <Source Value="UMainForm.pas"/> 852 <Line Value="355"/> 853 </Item7> 699 854 </BreakPoints> 700 855 <Watches Count="2"> -
MicroThreading/Demo/UMainForm.lfm
r159 r160 1 1 object MainForm: TMainForm 2 Left = 2 372 Left = 282 3 3 Height = 533 4 Top = 1024 Top = 94 5 5 Width = 816 6 6 Caption = 'MicroThreading demo' … … 16 16 Top = 8 17 17 Width = 802 18 ActivePage = TabSheet 218 ActivePage = TabSheet1 19 19 Anchors = [akTop, akLeft, akRight, akBottom] 20 TabIndex = 020 TabIndex = 1 21 21 TabOrder = 0 22 22 object TabSheet2: TTabSheet … … 336 336 ParentColor = False 337 337 end 338 object Button5: TButton 339 Left = 8 340 Height = 25 341 Top = 80 342 Width = 115 343 Caption = 'Raise exception' 344 OnClick = Button5Click 345 TabOrder = 6 346 end 338 347 end 339 348 end -
MicroThreading/Demo/UMainForm.pas
r159 r160 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 9 ComCtrls, ExtCtrls, Spin, UMicroThreading, DateUtils, UPlatform, 10 UMicroThreadList ;10 UMicroThreadList, UThreadEx; 11 11 12 12 type … … 29 29 Button3: TButton; 30 30 Button4: TButton; 31 Button5: TButton; 31 32 ButtonAddWorkers: TButton; 32 33 ButtonClearMicroThreads: TButton; … … 72 73 procedure Button3Click(Sender: TObject); 73 74 procedure Button4Click(Sender: TObject); 75 procedure Button5Click(Sender: TObject); 74 76 procedure ButtonSchedulerStartStopClick(Sender: TObject); 75 77 procedure Button2Click(Sender: TObject); … … 92 94 private 93 95 MicroThreadList: TMicroThreadList; 96 LastException: Exception; 97 LastExceptionSender: TObject; 94 98 procedure WorkerSubRoutine; 99 procedure ShowException(Sender: TObject; E: Exception); 100 procedure DoShowException; 95 101 public 96 102 DoWriteToMemo: Boolean; 97 103 DoSleep: Boolean; 104 RaiseException: Boolean; 98 105 SleepDuration: Integer; 99 106 DoWaitForEvent: Boolean; … … 121 128 Event := TMicroThreadEvent.Create; 122 129 MicroThreadList := TMicroThreadList.Create(Self); 130 UMicroThreading.ExceptionHandler := ShowException; 123 131 end; 124 132 … … 150 158 begin 151 159 MicroThreadList.Form.Show; 160 end; 161 162 procedure TMainForm.Button5Click(Sender: TObject); 163 begin 164 RaiseException := True; 152 165 end; 153 166 … … 335 348 end; 336 349 350 procedure TMainForm.ShowException(Sender: TObject; E: Exception); 351 begin 352 LastException := E; 353 LastExceptionSender := Sender; 354 if MainThreadID <> ThreadID then 355 TThread.Synchronize(TThreadEx.CurrentThread, DoShowException) 356 else DoShowException; 357 end; 358 359 procedure TMainForm.DoShowException; 360 begin 361 ShowMessage('Exception "' + LastException.Message + '" in class "' + 362 LastExceptionSender.ClassName + '"') 363 end; 364 337 365 procedure TWorker.Execute; 338 366 var … … 346 374 if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond); 347 375 if MainForm.DoSleep then MTSleep(MainForm.SleepDuration * OneMillisecond); 376 if MainForm.RaiseException then begin 377 MainForm.RaiseException := False; 378 raise Exception.Create('Exception from microthread'); 379 end; 348 380 //WorkerSubRoutine; 349 381 Completion := I / MainForm.Iterations; -
MicroThreading/MicroThreading.lpk
r159 r160 17 17 <License Value="GNU/GPL"/> 18 18 <Version Minor="1"/> 19 <Files Count=" 4">19 <Files Count="5"> 20 20 <Item1> 21 21 <Filename Value="UMicroThreading.pas"/> … … 35 35 <UnitName Value="UMicroThreadList"/> 36 36 </Item4> 37 <Item5> 38 <Filename Value="UThreadEx.pas"/> 39 <UnitName Value="UThreadEx"/> 40 </Item5> 37 41 </Files> 38 42 <Type Value="RunAndDesignTime"/> -
MicroThreading/MicroThreading.pas
r159 r160 8 8 9 9 uses 10 UMicroThreading, UPlatform, UMicroThreadList, LazarusPackageIntf;10 UMicroThreading, UPlatform, UMicroThreadList, UThreadEx, LazarusPackageIntf; 11 11 12 12 implementation -
MicroThreading/UMicroThreading.pas
r159 r160 19 19 {$ENDIF}{$ENDIF} 20 20 Classes, ExtCtrls, SysUtils, Contnrs, SyncObjs, DateUtils, Dialogs, Forms, 21 UPlatform, UMicroThreadList ;21 UPlatform, UMicroThreadList, UThreadEx; 22 22 23 23 const 24 DefaultStackSize = $40000; 24 DefaultStackSize = $4000; 25 26 resourcestring 27 SStackOverflow = 'Microthread %d stack error. Pointer %s , range < %s ; %s >'; 28 SNilThreadReference = 'Can''t release nil thread.'; 29 SManagerMicroThreadRunning = 'Manager already have running microthread'; 25 30 26 31 type … … 124 129 { TMicroThreadThread } 125 130 126 TMicroThreadThread = class(TThread )131 TMicroThreadThread = class(TThreadEx) 127 132 Manager: TMicroThreadManager; 128 133 State: TMicroThreadThreadState; … … 171 176 FRoundRobinIndex: Integer; 172 177 FLastId: Integer; 173 FFrequency: Int64;174 178 FMainThreadTerminated: Boolean; 175 179 FMicroThreads: TObjectList; // TList<TMicroThread> … … 196 200 function Add(MicroThread: TMicroThread): Integer; 197 201 function AddMethod(Method: TMicroThreadMethod): Integer; 198 function FindCurrentThread: TThread;199 202 procedure Remove(MicroThread: TMicroThread; Free: Boolean = True); 200 203 constructor Create; … … 217 220 public 218 221 Form: TMicroThreadListForm; 219 constructor Create(AOwner: TComponent); 222 constructor Create(AOwner: TComponent); override; 220 223 end; 221 224 … … 258 261 function GetMicroThreadId: Integer; 259 262 var 260 I: Integer; 261 CurrentStack: Pointer; 262 begin 263 asm 264 mov CurrentStack, sp 265 end; 266 with MainScheduler do begin 267 try 268 FMicroThreadsLock.Acquire; 269 I := 0; 270 while (I < FMicroThreads.Count) and 271 not ((CurrentStack >= TMicroThread(FMicroThreads[I]).FStack) and 272 (CurrentStack <= (TMicroThread(FMicroThreads[I]).FStack + 273 TMicroThread(FMicroThreads[I]).FStackSize))) do Inc(I); 274 if I < FMicroThreads.Count then begin 275 Result := TMicroThread(FMicroThreads[I]).FId; 276 end else Result := -1; 277 finally 278 FMicroThreadsLock.Release; 279 end; 280 end; 263 MT: TMicroThread; 264 begin 265 MT := GetCurrentMicroThread; 266 if Assigned(MT) then Result := MT.Id else Result := -1; 281 267 end; 282 268 283 269 function GetCurrentMicroThread: TMicroThread; 284 270 var 285 I: Integer;271 Thread: TThread; 286 272 begin 287 273 with MainScheduler do … … 289 275 FMicroThreadsLock.Acquire; 290 276 if MainThreadID = ThreadID then Result := MainThreadManager.CurrentMicroThread 291 else Result := TMicroThreadThread(MainScheduler.FindCurrentThread).Manager.CurrentMicroThread; 277 else begin 278 Thread := TThreadEx.CurrentThread; 279 if Assigned(Thread) then 280 Result := TMicroThreadThread(Thread).Manager.CurrentMicroThread 281 else Result := nil; 282 end; 292 283 finally 293 284 FMicroThreadsLock.Release; … … 309 300 begin 310 301 if GetCurrentThreadId <> MainThreadID then begin 311 Thread := MainScheduler.FindCurrentThread;302 Thread := TThreadEx.CurrentThread; 312 303 if Assigned(Thread) then TThread.Synchronize(Thread, Method) 313 304 else raise Exception.Create('Can''t determine thread for id ' + IntToStr(GetCurrentThreadId)); … … 483 474 // but virtual methods can be called only statically 484 475 // Then static method CallExecute is calling virtual method Execute 485 476 call TMicroThread.CallExecute 486 477 487 478 // Restore manager stack … … 555 546 ExecutedCount: Integer; 556 547 begin 557 inherited Execute;558 548 try 559 549 repeat … … 599 589 except 600 590 on E: Exception do 601 ExceptionHandler(Self, E);591 if Assigned(ExceptionHandler) then ExceptionHandler(Self, E); 602 592 end; 603 593 asm … … 632 622 begin 633 623 if not ((FStackPointer > FStack) and (FStackPointer < (FStack + FStackSize))) 634 then raise EStackOverflow.Create(Format('Microthread %d stack error', [FId])); 624 then raise EStackOverflow.Create(Format(SStackOverflow, 625 [FId, IntToHex(Integer(FStackPointer), 8), IntToHex(Integer(FStack), 8), 626 IntToHex(Integer(FStack + FStackSize), 8)])); 635 627 end; 636 628 … … 766 758 NewMicroThread.FScheduler := Self; 767 759 Result := Add(NewMicroThread); 768 end;769 770 function TMicroThreadScheduler.FindCurrentThread: TThread;771 var772 I: Integer;773 begin774 try775 FThreadPoolLock.Acquire;776 I := 0;777 while (I < FThreadPool.Count) and (TMicroThreadThread(FThreadPool[I]).ThreadID <> ThreadID) do Inc(I);778 if I < FThreadPool.Count then Result := TMicroThreadThread(FThreadPool[I])779 else Result := nil;780 finally781 FThreadPoolLock.Release;782 end;783 760 end; 784 761 … … 951 928 if I < FMicroThreads.Count then begin 952 929 if Assigned(Manager.FCurrentMicroThread) then 953 raise Exception.Create( 'Manager have already have running microthread');930 raise Exception.Create(SManagerMicroThreadRunning); 954 931 Selected := TMicroThread(FMicroThreads[FRoundRobinIndex]); 955 932 Selected.FState := tsRunning; … … 965 942 begin 966 943 if not Assigned(MicroThread) then 967 raise Exception.Create( 'Can''t release nil thread.');944 raise Exception.Create(SNilThreadReference); 968 945 try 969 946 FMicroThreadsLock.Acquire;
Note:
See TracChangeset
for help on using the changeset viewer.