Changeset 159
- Timestamp:
- Jan 31, 2011, 2:16:21 PM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 2 added
- 1 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/Demo.lpi
r158 r159 39 39 </Item2> 40 40 </RequiredPackages> 41 <Units Count="6 7">41 <Units Count="69"> 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 49 <CursorPos X="34" Y="16"/> 49 50 <UsageCount Value="89"/> 51 <Loaded Value="True"/> 50 52 </Unit0> 51 53 <Unit1> … … 55 57 <ResourceBaseClass Value="Form"/> 56 58 <UnitName Value="UMainForm"/> 57 <EditorIndex Value=" 5"/>58 <WindowIndex Value="0"/> 59 <TopLine Value=" 351"/>60 <CursorPos X=" 54" Y="366"/>59 <EditorIndex Value="2"/> 60 <WindowIndex Value="0"/> 61 <TopLine Value="100"/> 62 <CursorPos X="45" Y="122"/> 61 63 <UsageCount Value="89"/> 62 64 <Loaded Value="True"/> … … 66 68 <Filename Value="../UMicroThreading.pas"/> 67 69 <UnitName Value="UMicroThreading"/> 68 <IsVisibleTab Value="True"/>69 70 <EditorIndex Value="0"/> 70 71 <WindowIndex Value="0"/> 71 <TopLine Value="31 9"/>72 <CursorPos X=" 9" Y="330"/>72 <TopLine Value="317"/> 73 <CursorPos X="26" Y="334"/> 73 74 <UsageCount Value="46"/> 74 75 <Loaded Value="True"/> … … 142 143 <Unit11> 143 144 <Filename Value="/usr/share/fpcsrc/2.4.0/rtl/inc/systemh.inc"/> 144 <EditorIndex Value="1"/>145 145 <WindowIndex Value="0"/> 146 146 <TopLine Value="469"/> 147 147 <CursorPos X="3" Y="484"/> 148 148 <UsageCount Value="12"/> 149 <Loaded Value="True"/>150 149 </Unit11> 151 150 <Unit12> … … 194 193 <Unit18> 195 194 <Filename Value="/usr/share/fpcsrc/2.4.0/rtl/objpas/classes/classesh.inc"/> 196 <EditorIndex Value="3"/>197 195 <WindowIndex Value="0"/> 198 196 <TopLine Value="1"/> 199 197 <CursorPos X="50" Y="9"/> 200 198 <UsageCount Value="12"/> 201 <Loaded Value="True"/>202 199 </Unit18> 203 200 <Unit19> … … 227 224 <WindowIndex Value="0"/> 228 225 <TopLine Value="1"/> 229 <CursorPos X=" 25" Y="9"/>226 <CursorPos X="63" Y="10"/> 230 227 <UsageCount Value="10"/> 231 228 </Unit22> … … 298 295 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/> 299 296 <WindowIndex Value="0"/> 300 <TopLine Value=" 302"/>301 <CursorPos X=" 19" Y="319"/>297 <TopLine Value="1638"/> 298 <CursorPos X="28" Y="1645"/> 302 299 <UsageCount Value="20"/> 303 300 </Unit32> … … 410 407 <Filename Value="../UPlatform.pas"/> 411 408 <UnitName Value="UPlatform"/> 412 <EditorIndex Value=" 4"/>409 <EditorIndex Value="1"/> 413 410 <WindowIndex Value="0"/> 414 411 <TopLine Value="23"/> … … 510 507 <TopLine Value="1"/> 511 508 <CursorPos X="17" Y="14"/> 512 <UsageCount Value=" 9"/>509 <UsageCount Value="10"/> 513 510 <DefaultSyntaxHighlighter Value="None"/> 514 511 </Unit61> … … 517 514 <UnitName Value="Forms"/> 518 515 <WindowIndex Value="0"/> 519 <TopLine Value=" 57"/>520 <CursorPos X=" 3" Y="70"/>521 <UsageCount Value=" 9"/>516 <TopLine Value="1688"/> 517 <CursorPos X="1" Y="1701"/> 518 <UsageCount Value="10"/> 522 519 </Unit62> 523 520 <Unit63> … … 538 535 <Unit65> 539 536 <Filename Value="/usr/share/fpcsrc/2.4.0/rtl/inc/system.inc"/> 540 <EditorIndex Value="2"/>541 537 <WindowIndex Value="0"/> 542 538 <TopLine Value="671"/> 543 539 <CursorPos X="1" Y="673"/> 544 540 <UsageCount Value="11"/> 545 <Loaded Value="True"/>546 541 </Unit65> 547 542 <Unit66> 548 543 <Filename Value="/usr/share/fpcsrc/2.4.0/rtl/inc/objpash.inc"/> 549 <EditorIndex Value="6"/>550 544 <WindowIndex Value="0"/> 551 545 <TopLine Value="160"/> 552 546 <CursorPos X="23" Y="175"/> 553 547 <UsageCount Value="11"/> 548 </Unit66> 549 <Unit67> 550 <Filename Value="../UMicroThreadList.pas"/> 551 <ComponentName Value="MicroThreadListForm"/> 552 <ResourceBaseClass Value="Form"/> 553 <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"/> 554 560 <Loaded Value="True"/> 555 </Unit66> 561 <LoadedDesigner Value="True"/> 562 </Unit67> 563 <Unit68> 564 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/include/customform.inc"/> 565 <WindowIndex Value="0"/> 566 <TopLine Value="2100"/> 567 <CursorPos X="1" Y="2113"/> 568 <UsageCount Value="10"/> 569 </Unit68> 556 570 </Units> 557 <JumpHistory Count=" 30" HistoryIndex="29">571 <JumpHistory Count="21" HistoryIndex="20"> 558 572 <Position1> 559 <Filename Value=" ../UMicroThreading.pas"/>560 <Caret Line=" 615" Column="1" TopLine="599"/>573 <Filename Value="UMainForm.pas"/> 574 <Caret Line="147" Column="10" TopLine="147"/> 561 575 </Position1> 562 576 <Position2> 563 <Filename Value=" ../UMicroThreading.pas"/>564 <Caret Line=" 614" Column="1" TopLine="599"/>577 <Filename Value="UMainForm.pas"/> 578 <Caret Line="10" Column="26" TopLine="1"/> 565 579 </Position2> 566 580 <Position3> 567 <Filename Value=" ../UMicroThreading.pas"/>568 <Caret Line=" 615" Column="1" TopLine="599"/>581 <Filename Value="UMainForm.pas"/> 582 <Caret Line="16" Column="48" TopLine="1"/> 569 583 </Position3> 570 584 <Position4> 571 585 <Filename Value="../UMicroThreading.pas"/> 572 <Caret Line=" 614" Column="1" TopLine="599"/>586 <Caret Line="227" Column="20" TopLine="206"/> 573 587 </Position4> 574 588 <Position5> 575 589 <Filename Value="../UMicroThreading.pas"/> 576 <Caret Line=" 615" Column="1" TopLine="599"/>590 <Caret Line="21" Column="33" TopLine="1"/> 577 591 </Position5> 578 592 <Position6> 579 <Filename Value=" ../UMicroThreading.pas"/>580 <Caret Line=" 614" Column="1" TopLine="599"/>593 <Filename Value="UMainForm.pas"/> 594 <Caret Line="90" Column="26" TopLine="85"/> 581 595 </Position6> 582 596 <Position7> 583 <Filename Value=" ../UMicroThreading.pas"/>584 <Caret Line=" 615" Column="1" TopLine="599"/>597 <Filename Value="UMainForm.pas"/> 598 <Caret Line="5" Column="15" TopLine="1"/> 585 599 </Position7> 586 600 <Position8> 587 <Filename Value=" ../UMicroThreading.pas"/>588 <Caret Line=" 614" Column="1" TopLine="599"/>601 <Filename Value="UMainForm.pas"/> 602 <Caret Line="150" Column="28" TopLine="147"/> 589 603 </Position8> 590 604 <Position9> 591 <Filename Value=" ../UMicroThreading.pas"/>592 <Caret Line=" 615" Column="1" TopLine="599"/>605 <Filename Value="UMainForm.pas"/> 606 <Caret Line="85" Column="15" TopLine="72"/> 593 607 </Position9> 594 608 <Position10> 595 <Filename Value=" ../UMicroThreading.pas"/>596 <Caret Line=" 616" Column="1" TopLine="599"/>609 <Filename Value="UMainForm.pas"/> 610 <Caret Line="151" Column="15" TopLine="138"/> 597 611 </Position10> 598 612 <Position11> 599 <Filename Value=" ../UMicroThreading.pas"/>600 <Caret Line=" 618" Column="1" TopLine="599"/>613 <Filename Value="UMainForm.pas"/> 614 <Caret Line="93" Column="30" TopLine="80"/> 601 615 </Position11> 602 616 <Position12> 603 <Filename Value=" ../UMicroThreading.pas"/>604 <Caret Line=" 620" Column="1" TopLine="599"/>617 <Filename Value="UMainForm.pas"/> 618 <Caret Line="123" Column="59" TopLine="110"/> 605 619 </Position12> 606 620 <Position13> 607 <Filename Value=" ../UMicroThreading.pas"/>608 <Caret Line=" 325" Column="3" TopLine="321"/>621 <Filename Value="UMainForm.pas"/> 622 <Caret Line="122" Column="1" TopLine="110"/> 609 623 </Position13> 610 624 <Position14> 611 625 <Filename Value="../UMicroThreading.pas"/> 612 <Caret Line=" 321" Column="69" TopLine="319"/>626 <Caret Line="217" Column="1" TopLine="197"/> 613 627 </Position14> 614 628 <Position15> 615 629 <Filename Value="../UMicroThreading.pas"/> 616 <Caret Line=" 326" Column="1" TopLine="319"/>630 <Caret Line="250" Column="20" TopLine="230"/> 617 631 </Position15> 618 632 <Position16> 619 <Filename Value=" ../UMicroThreading.pas"/>620 <Caret Line=" 327" Column="1" TopLine="319"/>633 <Filename Value="UMainForm.pas"/> 634 <Caret Line="150" Column="16" TopLine="145"/> 621 635 </Position16> 622 636 <Position17> 623 <Filename Value=" ../UMicroThreading.pas"/>624 <Caret Line=" 411" Column="22" TopLine="403"/>637 <Filename Value="UMainForm.pas"/> 638 <Caret Line="93" Column="25" TopLine="80"/> 625 639 </Position17> 626 640 <Position18> 627 641 <Filename Value="../UMicroThreading.pas"/> 628 <Caret Line=" 863" Column="10" TopLine="841"/>642 <Caret Line="338" Column="3" TopLine="336"/> 629 643 </Position18> 630 644 <Position19> 631 <Filename Value=" ../UMicroThreading.pas"/>632 <Caret Line=" 861" Column="1" TopLine="841"/>645 <Filename Value="UMainForm.pas"/> 646 <Caret Line="5" Column="74" TopLine="3"/> 633 647 </Position19> 634 648 <Position20> 635 649 <Filename Value="../UMicroThreading.pas"/> 636 <Caret Line=" 862" Column="1" TopLine="841"/>650 <Caret Line="208" Column="22" TopLine="204"/> 637 651 </Position20> 638 652 <Position21> 639 653 <Filename Value="../UMicroThreading.pas"/> 640 <Caret Line=" 863" Column="1" TopLine="841"/>654 <Caret Line="336" Column="36" TopLine="314"/> 641 655 </Position21> 642 <Position22>643 <Filename Value="../UMicroThreading.pas"/>644 <Caret Line="861" Column="1" TopLine="841"/>645 </Position22>646 <Position23>647 <Filename Value="../UMicroThreading.pas"/>648 <Caret Line="843" Column="33" TopLine="841"/>649 </Position23>650 <Position24>651 <Filename Value="../UMicroThreading.pas"/>652 <Caret Line="325" Column="5" TopLine="319"/>653 </Position24>654 <Position25>655 <Filename Value="../UMicroThreading.pas"/>656 <Caret Line="326" Column="1" TopLine="319"/>657 </Position25>658 <Position26>659 <Filename Value="../UMicroThreading.pas"/>660 <Caret Line="327" Column="1" TopLine="319"/>661 </Position26>662 <Position27>663 <Filename Value="../UMicroThreading.pas"/>664 <Caret Line="328" Column="1" TopLine="319"/>665 </Position27>666 <Position28>667 <Filename Value="../UMicroThreading.pas"/>668 <Caret Line="329" Column="1" TopLine="319"/>669 </Position28>670 <Position29>671 <Filename Value="../UMicroThreading.pas"/>672 <Caret Line="328" Column="1" TopLine="319"/>673 </Position29>674 <Position30>675 <Filename Value="../UMicroThreading.pas"/>676 <Caret Line="329" Column="1" TopLine="319"/>677 </Position30>678 656 </JumpHistory> 679 657 </ProjectOptions> … … 714 692 </CompilerOptions> 715 693 <Debugging> 716 <BreakPoints Count=" 2">694 <BreakPoints Count="1"> 717 695 <Item1> 718 696 <Source Value="../UMicroThreading.pas"/> 719 <Line Value=" 869"/>697 <Line Value="250"/> 720 698 </Item1> 721 <Item2>722 <Source Value="../UMicroThreading.pas"/>723 <Line Value="325"/>724 </Item2>725 699 </BreakPoints> 726 700 <Watches Count="2"> -
MicroThreading/Demo/UMainForm.lfm
r158 r159 1 1 object MainForm: TMainForm 2 Left = 2 242 Left = 237 3 3 Height = 533 4 Top = 984 Top = 102 5 5 Width = 816 6 6 Caption = 'MicroThreading demo' … … 16 16 Top = 8 17 17 Width = 802 18 ActivePage = TabSheet 118 ActivePage = TabSheet2 19 19 Anchors = [akTop, akLeft, akRight, akBottom] 20 TabIndex = 120 TabIndex = 0 21 21 TabOrder = 0 22 22 object TabSheet2: TTabSheet 23 23 Caption = 'Scheduler state' 24 ClientHeight = 492 25 ClientWidth = 798 26 object ListView1: TListView 27 Left = 222 28 Height = 459 29 Top = 24 30 Width = 571 31 Anchors = [akTop, akLeft, akRight, akBottom] 32 Columns = < 33 item 34 Caption = 'Id' 35 Width = 40 36 end 37 item 38 Caption = 'Usage' 39 end 40 item 41 Caption = 'Priority' 42 end 43 item 44 Caption = 'State' 45 Width = 60 46 end 47 item 48 Caption = 'Block state' 49 end 50 item 51 Caption = 'Execution time' 52 Width = 70 53 end 54 item 55 Caption = 'Execution count' 56 end 57 item 58 Caption = 'Completion' 59 end 60 item 61 Caption = 'Used stack' 62 Width = 178 63 end 64 item 65 Caption = 'Name' 66 end> 67 OwnerData = True 68 ReadOnly = True 69 RowSelect = True 70 TabOrder = 0 71 ViewStyle = vsReport 72 OnData = ListView1Data 73 end 74 object Label1: TLabel 75 Left = 222 76 Height = 14 77 Top = 7 78 Width = 76 79 Caption = 'Microthreads:' 80 ParentColor = False 81 end 24 ClientHeight = 493 25 ClientWidth = 794 82 26 object GroupBox1: TGroupBox 83 27 Left = 6 … … 86 30 Width = 205 87 31 Caption = 'State' 88 ClientHeight = 6 632 ClientHeight = 63 89 33 ClientWidth = 201 90 TabOrder = 134 TabOrder = 0 91 35 object Label5: TLabel 92 36 Left = 9 93 37 Height = 14 94 38 Top = 4 95 Width = 1 3539 Width = 117 96 40 Caption = 'Logical processor count:' 97 41 ParentColor = False … … 109 53 Height = 14 110 54 Top = 20 111 Width = 7855 Width = 69 112 56 Caption = 'Thread count:' 113 57 ParentColor = False … … 117 61 Height = 14 118 62 Top = 36 119 Width = 10763 Width = 95 120 64 Caption = 'Micro thread count:' 121 65 ParentColor = False … … 141 85 Height = 14 142 86 Top = 52 143 Width = 8387 Width = 73 144 88 Caption = 'Main thread id:' 145 89 ParentColor = False … … 160 104 Width = 206 161 105 Caption = 'Control' 162 ClientHeight = 1 11106 ClientHeight = 108 163 107 ClientWidth = 202 164 TabOrder = 2108 TabOrder = 1 165 109 object ButtonSchedulerStartStop: TButton 166 110 Left = 8 … … 176 120 Height = 14 177 121 Top = 46 178 Width = 78122 Width = 69 179 123 Caption = 'Thread count:' 180 124 ParentColor = False … … 190 134 object CheckBoxUseMainThread: TCheckBox 191 135 Left = 10 192 Height = 20136 Height = 17 193 137 Top = 64 194 Width = 112138 Width = 98 195 139 Caption = 'Use main thread' 196 140 OnChange = CheckBoxUseMainThreadChange … … 200 144 object ListView2: TListView 201 145 Left = 6 202 Height = 23 6146 Height = 237 203 147 Top = 247 204 148 Width = 206 … … 213 157 end> 214 158 OwnerData = True 215 TabOrder = 3159 TabOrder = 2 216 160 ViewStyle = vsReport 217 161 OnData = ListView2Data … … 221 165 Height = 14 222 166 Top = 233 223 Width = 50167 Width = 44 224 168 Caption = 'Threads:' 225 169 ParentColor = False 170 end 171 object Button4: TButton 172 Left = 226 173 Height = 25 174 Top = 17 175 Width = 123 176 Caption = 'Show manager' 177 OnClick = Button4Click 178 TabOrder = 3 226 179 end 227 180 end 228 181 object TabSheet1: TTabSheet 229 182 Caption = 'Job control' 230 ClientHeight = 49 2231 ClientWidth = 79 8183 ClientHeight = 493 184 ClientWidth = 794 232 185 object ButtonAddWorkers: TButton 233 186 Left = 10 … … 252 205 Height = 14 253 206 Top = 14 254 Width = 3 7207 Width = 34 255 208 Caption = 'Count:' 256 209 ParentColor = False … … 269 222 object Memo1: TMemo 270 223 Left = 316 271 Height = 24 3224 Height = 244 272 225 Top = 14 273 226 Width = 294 … … 289 242 Height = 14 290 243 Top = 38 291 Width = 5 7244 Width = 52 292 245 Caption = 'Iterations:' 293 246 ParentColor = False … … 309 262 Width = 292 310 263 Caption = 'Do inside jobs' 311 ClientHeight = 1 22264 ClientHeight = 119 312 265 ClientWidth = 288 313 266 TabOrder = 6 314 267 object CheckBox1: TCheckBox 315 268 Left = 4 316 Height = 20269 Height = 17 317 270 Top = 9 318 Width = 76271 Width = 66 319 272 Caption = 'Use sleep' 320 273 OnChange = CheckBox1Change … … 334 287 Height = 14 335 288 Top = 15 336 Width = 1 7289 Width = 14 337 290 Caption = 'ms' 338 291 ParentColor = False … … 340 293 object CheckBox2: TCheckBox 341 294 Left = 5 342 Height = 20295 Height = 17 343 296 Top = 32 344 Width = 1 26297 Width = 112 345 298 Caption = 'Write test to memo' 346 299 OnChange = CheckBox2Change … … 349 302 object CheckBox3: TCheckBox 350 303 Left = 5 351 Height = 20304 Height = 17 352 305 Top = 56 353 Width = 9 8306 Width = 90 354 307 Caption = 'Wait for event' 355 308 OnChange = CheckBox3Change … … 379 332 Height = 14 380 333 Top = 56 381 Width = 1 7334 Width = 14 382 335 Caption = 'ms' 383 336 ParentColor = False -
MicroThreading/Demo/UMainForm.pas
r158 r159 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 ComCtrls, ExtCtrls, Spin, UMicroThreading, DateUtils, UPlatform; 9 ComCtrls, ExtCtrls, Spin, UMicroThreading, DateUtils, UPlatform, 10 UMicroThreadList; 10 11 11 12 type 12 13 13 TMainForm = class; 14 14 … … 28 28 Button2: TButton; 29 29 Button3: TButton; 30 Button4: TButton; 30 31 ButtonAddWorkers: TButton; 31 32 ButtonClearMicroThreads: TButton; … … 40 41 GroupBox2: TGroupBox; 41 42 GroupBox3: TGroupBox; 42 Label1: TLabel;43 43 Label10: TLabel; 44 44 Label11: TLabel; … … 57 57 Label8: TLabel; 58 58 Label9: TLabel; 59 ListView1: TListView;60 59 ListView2: TListView; 61 60 Memo1: TMemo; … … 72 71 procedure Button1Click(Sender: TObject); 73 72 procedure Button3Click(Sender: TObject); 73 procedure Button4Click(Sender: TObject); 74 74 procedure ButtonSchedulerStartStopClick(Sender: TObject); 75 75 procedure Button2Click(Sender: TObject); … … 85 85 procedure FormDestroy(Sender: TObject); 86 86 procedure FormShow(Sender: TObject); 87 procedure ListView1Data(Sender: TObject; Item: TListItem);88 87 procedure ListView2Data(Sender: TObject; Item: TListItem); 89 88 procedure SpinEdit2Change(Sender: TObject); … … 92 91 procedure TimerRedrawTimer(Sender: TObject); 93 92 private 93 MicroThreadList: TMicroThreadList; 94 94 procedure WorkerSubRoutine; 95 95 public … … 117 117 begin 118 118 DoubleBuffered := True; 119 ListView1.DoubleBuffered := True;119 // ListView1.DoubleBuffered := True; 120 120 Label6.Caption := IntToStr(GetLogicalProcessorCount); 121 121 Event := TMicroThreadEvent.Create; 122 MicroThreadList := TMicroThreadList.Create(Self); 122 123 end; 123 124 … … 144 145 begin 145 146 Event.SetEvent; 147 end; 148 149 procedure TMainForm.Button4Click(Sender: TObject); 150 begin 151 MicroThreadList.Form.Show; 146 152 end; 147 153 … … 268 274 procedure TMainForm.FormDestroy(Sender: TObject); 269 275 begin 276 MicroThreadList.Free; 270 277 MainScheduler.Active := False; 271 278 Event.Free; … … 279 286 ButtonSchedulerStartStop.Click; 280 287 Label16.Caption := IntToStr(MainThreadID); 281 end;282 283 procedure TMainForm.ListView1Data(Sender: TObject; Item: TListItem);284 begin285 try286 MainScheduler.MicroThreadsLock.Acquire;287 if Item.Index < MainScheduler.MicroThreads.Count then288 with TMicroThread(MainScheduler.MicroThreads[Item.Index]) do begin289 Item.Caption := IntToStr(Id);290 Item.SubItems.Add('');291 Item.SubItems.Add(IntToStr(Priority));292 Item.SubItems.Add(MicroThreadStateText[State]);293 Item.SubItems.Add(MicroThreadBlockStateText[BlockState]);294 Item.SubItems.Add(FloatToStr(ExecutionTime));295 Item.SubItems.Add(IntToStr(ExecutionCount));296 Item.SubItems.Add(IntToStr(Trunc(Completion * 100)) + '%');297 Item.SubItems.Add(IntToStr(StackUsed));298 Item.SubItems.Add(Name);299 end;300 finally301 MainScheduler.MicroThreadsLock.Release;302 end;303 288 end; 304 289 … … 334 319 procedure TMainForm.TimerRedrawTimer(Sender: TObject); 335 320 begin 336 if ListView1.Items.Count <> MainScheduler.MicroThreadCount then337 ListView1.Items.Count := MainScheduler.MicroThreadCount;338 ListView1.Items[-1];339 ListView1.Refresh;340 341 321 if ListView2.Items.Count <> MainScheduler.ThreadPoolCount then 342 322 ListView2.Items.Count := MainScheduler.ThreadPoolCount; -
MicroThreading/MicroThreading.lpk
r153 r159 20 20 <Item1> 21 21 <Filename Value="UMicroThreading.pas"/> 22 <HasRegisterProc Value="True"/> 22 23 <UnitName Value="UMicroThreading"/> 23 24 </Item1> 24 25 <Item2> 25 <Filename Value=" Other/Coroutine.pas"/>26 <UnitName Value=" Coroutine"/>26 <Filename Value="UPlatform.pas"/> 27 <UnitName Value="UPlatform"/> 27 28 </Item2> 28 29 <Item3> 29 <Filename Value=" UPlatform.pas"/>30 < UnitName Value="UPlatform"/>30 <Filename Value="ReadMe.txt"/> 31 <Type Value="Binary"/> 31 32 </Item3> 32 33 <Item4> 33 <Filename Value=" ReadMe.txt"/>34 < Type Value="Binary"/>34 <Filename Value="UMicroThreadList.pas"/> 35 <UnitName Value="UMicroThreadList"/> 35 36 </Item4> 36 37 </Files> -
MicroThreading/MicroThreading.pas
r149 r159 8 8 9 9 uses 10 UMicroThreading, Coroutine, UPlatform, LazarusPackageIntf;10 UMicroThreading, UPlatform, UMicroThreadList, LazarusPackageIntf; 11 11 12 12 implementation … … 14 14 procedure Register; 15 15 begin 16 RegisterUnit('UMicroThreading', @UMicroThreading.Register); 16 17 end; 17 18 -
MicroThreading/UMicroThreading.pas
r158 r159 18 18 cthreads, 19 19 {$ENDIF}{$ENDIF} 20 Classes, ExtCtrls, SysUtils, Contnrs, SyncObjs, DateUtils, Dialogs, Forms, UPlatform; 20 Classes, ExtCtrls, SysUtils, Contnrs, SyncObjs, DateUtils, Dialogs, Forms, 21 UPlatform, UMicroThreadList; 21 22 22 23 const 23 DefaultStackSize = $4000 ;24 DefaultStackSize = $40000; 24 25 25 26 type … … 191 192 procedure UpdateThreadPoolSize; 192 193 procedure MainThreadStart(Sender: TObject); 194 procedure MainThreadTick(Data: PtrInt); 193 195 public 194 196 function Add(MicroThread: TMicroThread): Integer; 195 197 function AddMethod(Method: TMicroThreadMethod): Integer; 196 procedure Remove(MicroThread: TMicroThread); 198 function FindCurrentThread: TThread; 199 procedure Remove(MicroThread: TMicroThread; Free: Boolean = True); 197 200 constructor Create; 198 201 destructor Destroy; override; … … 210 213 end; 211 214 215 TMicroThreadList = class(TComponent) 216 private 217 public 218 Form: TMicroThreadListForm; 219 constructor Create(AOwner: TComponent); 220 end; 221 222 TMicroThreadExceptionEvent = procedure(Sender: TObject; E: Exception) of object; 223 212 224 var 213 225 MainScheduler: TMicroThreadScheduler; 226 ExceptionHandler: TMicroThreadExceptionEvent; 214 227 215 228 const … … 223 236 function GetCurrentMicroThread: TMicroThread; 224 237 procedure MTSleep(Duration: TDateTime); 238 procedure MTSynchronize(Method: TThreadMethod); 225 239 function MTWaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult; 226 240 procedure Log(Text: string); 241 procedure Register; 227 242 228 243 const … … 235 250 // StaticManager: TMicroThreadManager; 236 251 // StaticMicroThread: TMicroThread; 252 253 procedure Register; 254 begin 255 RegisterComponents('MicroThreading', [TMicroThreadList]); 256 end; 237 257 238 258 function GetMicroThreadId: Integer; … … 267 287 with MainScheduler do 268 288 try 269 F ThreadPoolLock.Acquire;289 FMicroThreadsLock.Acquire; 270 290 if MainThreadID = ThreadID then Result := MainThreadManager.CurrentMicroThread 271 else begin 272 I := 0; 273 while (I < FThreadPool.Count) and (TMicroThreadThread(FThreadPool[I]).ThreadID <> ThreadID) do Inc(I); 274 if I < FThreadPool.Count then Result := TMicroThreadThread(FThreadPool[I]).Manager.CurrentMicroThread 275 else Result := nil; 276 end; 277 finally 278 FThreadPoolLock.Release; 291 else Result := TMicroThreadThread(MainScheduler.FindCurrentThread).Manager.CurrentMicroThread; 292 finally 293 FMicroThreadsLock.Release; 279 294 end; 280 295 end; … … 289 304 end; 290 305 306 procedure MTSynchronize(Method: TThreadMethod); 307 var 308 Thread: TThread; 309 begin 310 if GetCurrentThreadId <> MainThreadID then begin 311 Thread := MainScheduler.FindCurrentThread; 312 if Assigned(Thread) then TThread.Synchronize(Thread, Method) 313 else raise Exception.Create('Can''t determine thread for id ' + IntToStr(GetCurrentThreadId)); 314 end else Method; 315 end; 316 291 317 function MTWaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult; 292 318 var … … 294 320 begin 295 321 MT := GetCurrentMicroThread; 296 if Assigned(MT) then Result := MT.WaitForEvent(Event, Duration); 322 if Assigned(MT) then Result := MT.WaitForEvent(Event, Duration) 323 else raise Exception.Create('Not in thread'); 297 324 // else Result := Event.WaitFor(Trunc(Duration / OneMillisecond)); 298 325 end; … … 316 343 end; 317 344 end; 345 346 { TMicroThreadList } 347 348 constructor TMicroThreadList.Create(AOwner: TComponent); 349 begin 350 inherited; 351 Form := TMicroThreadListForm.Create(Self); 352 end; 353 354 318 355 319 356 { TMicroThreadMethod } … … 360 397 destructor TMicroThreadEvent.Destroy; 361 398 begin 362 MainScheduler.FEvents.Delete(MainScheduler.FEvents.IndexOf(Self)); 399 try 400 MainScheduler.FEvents.OwnsObjects := False; 401 MainScheduler.FEvents.Delete(MainScheduler.FEvents.IndexOf(Self)); 402 finally 403 MainScheduler.FEvents.OwnsObjects := True; 404 end; 363 405 FMicroThreadsLock.Free; 364 406 FMicroThreads.Free; … … 441 483 // but virtual methods can be called only statically 442 484 // Then static method CallExecute is calling virtual method Execute 443 call TMicroThread.CallExecute485 call TMicroThread.CallExecute 444 486 445 487 // Restore manager stack … … 522 564 until Terminated; 523 565 except 524 on E: Exception do ;525 //ExceptionHandler(E);566 on E: Exception do 567 if Assigned(ExceptionHandler) then ExceptionHandler(Self, E); 526 568 end; 527 569 end; … … 549 591 end; 550 592 551 552 593 { TMicroThread } 553 594 554 595 procedure TMicroThread.CallExecute; 555 596 begin 556 Execute; 597 try 598 Execute; 599 except 600 on E: Exception do 601 ExceptionHandler(Self, E); 602 end; 557 603 asm 558 604 mov ecx, Self … … 631 677 end; 632 678 Yield; 679 if FBlockTime < NowPrecise then 680 Result := wrTimeout else Result := wrSignaled; 681 633 682 try 634 683 Event.FMicroThreadsLock.Acquire; … … 669 718 destructor TMicroThread.Destroy; 670 719 begin 720 MainScheduler.Remove(Self, False); 671 721 //Terminate; 672 722 //WaitFor; … … 718 768 end; 719 769 720 procedure TMicroThreadScheduler.Remove(MicroThread: TMicroThread); 770 function TMicroThreadScheduler.FindCurrentThread: TThread; 771 var 772 I: Integer; 773 begin 774 try 775 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 finally 781 FThreadPoolLock.Release; 782 end; 783 end; 784 785 procedure TMicroThreadScheduler.Remove(MicroThread: TMicroThread; 786 Free: Boolean = True); 721 787 begin 722 788 try 723 789 FMicroThreadsLock.Acquire; 790 if not Free then FMicroThreads.OwnsObjects := False; 724 791 FMicroThreads.Remove(MicroThread); 792 FMicroThreads.OwnsObjects := True; 725 793 finally 726 794 FMicroThreadsLock.Release; … … 824 892 825 893 procedure TMicroThreadScheduler.MainThreadStart(Sender: TObject); 826 var827 Executed: Integer;828 894 begin 829 895 FMainThreadStarter.Enabled := False; 830 896 FMainThreadTerminated := False; 831 repeat 832 Executed := FMainThreadManager.Execute(1); 833 Application.ProcessMessages; 834 if Executed = 0 then Sleep(1); 835 until (FState <> ssRunning) or (not FUseMainThread); 836 FMainThreadTerminated := True; 897 Application.QueueAsyncCall(MainThreadTick, 0); 898 end; 899 900 procedure TMicroThreadScheduler.MainThreadTick(Data: PtrInt); 901 var 902 Executed: Integer; 903 begin 904 Executed := FMainThreadManager.Execute(1); 905 if Executed = 0 then Sleep(1); 906 // If not terminated then queue next tick else terminate 907 if (FState = ssRunning) and FUseMainThread then 908 Application.QueueAsyncCall(MainThreadTick, 0) 909 else FMainThreadTerminated := True; 837 910 end; 838 911
Note:
See TracChangeset
for help on using the changeset viewer.