Changeset 162 for MicroThreading
- Timestamp:
- Feb 7, 2011, 1:04:27 PM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/Demo.lpi
r161 r162 31 31 </local> 32 32 </RunParams> 33 <RequiredPackages Count=" 2">33 <RequiredPackages Count="3"> 34 34 <Item1> 35 <PackageName Value="Microthreading"/> 35 <PackageName Value="LCLBase"/> 36 <MinVersion Major="1" Release="1" Valid="True"/> 36 37 </Item1> 37 38 <Item2> 39 <PackageName Value="Microthreading"/> 40 </Item2> 41 <Item3> 38 42 <PackageName Value="LCL"/> 39 </Item 2>43 </Item3> 40 44 </RequiredPackages> 41 <Units Count="2 4">45 <Units Count="28"> 42 46 <Unit0> 43 47 <Filename Value="Demo.lpr"/> … … 55 59 <ResourceBaseClass Value="Form"/> 56 60 <UnitName Value="UMainForm"/> 57 <EditorIndex Value=" 1"/>58 <WindowIndex Value="0"/> 59 <TopLine Value="3 69"/>60 <CursorPos X=" 1" Y="379"/>61 <EditorIndex Value="6"/> 62 <WindowIndex Value="0"/> 63 <TopLine Value="372"/> 64 <CursorPos X="21" Y="378"/> 61 65 <UsageCount Value="270"/> 62 66 <Loaded Value="True"/> 67 <LoadedDesigner Value="True"/> 63 68 </Unit1> 64 69 <Unit2> … … 68 73 <EditorIndex Value="0"/> 69 74 <WindowIndex Value="0"/> 70 <TopLine Value=" 500"/>71 <CursorPos X="1 " Y="517"/>75 <TopLine Value="976"/> 76 <CursorPos X="18" Y="986"/> 72 77 <UsageCount Value="136"/> 73 78 <Loaded Value="True"/> … … 131 136 <ResourceBaseClass Value="Form"/> 132 137 <UnitName Value="UMicroThreadList"/> 133 <WindowIndex Value="0"/> 134 <TopLine Value="1"/> 135 <CursorPos X="44" Y="17"/> 136 <UsageCount Value="21"/> 138 <EditorIndex Value="4"/> 139 <WindowIndex Value="0"/> 140 <TopLine Value="83"/> 141 <CursorPos X="84" Y="94"/> 142 <UsageCount Value="23"/> 143 <Loaded Value="True"/> 144 <LoadedDesigner Value="True"/> 137 145 </Unit10> 138 146 <Unit11> … … 228 236 <UsageCount Value="16"/> 229 237 </Unit23> 238 <Unit24> 239 <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/FPC/packages/fcl-base/src/syncobjs.pp"/> 240 <UnitName Value="syncobjs"/> 241 <EditorIndex Value="5"/> 242 <WindowIndex Value="0"/> 243 <TopLine Value="35"/> 244 <CursorPos X="18" Y="45"/> 245 <UsageCount Value="12"/> 246 <Loaded Value="True"/> 247 </Unit24> 248 <Unit25> 249 <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/Lazarus/lcl/include/customlistview.inc"/> 250 <EditorIndex Value="1"/> 251 <WindowIndex Value="0"/> 252 <TopLine Value="548"/> 253 <CursorPos X="1" Y="561"/> 254 <UsageCount Value="11"/> 255 <Loaded Value="True"/> 256 </Unit25> 257 <Unit26> 258 <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/Lazarus/lcl/include/listitem.inc"/> 259 <EditorIndex Value="2"/> 260 <WindowIndex Value="0"/> 261 <TopLine Value="798"/> 262 <CursorPos X="1" Y="821"/> 263 <UsageCount Value="11"/> 264 <Loaded Value="True"/> 265 </Unit26> 266 <Unit27> 267 <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/Lazarus/lcl/include/listitems.inc"/> 268 <EditorIndex Value="3"/> 269 <WindowIndex Value="0"/> 270 <TopLine Value="65"/> 271 <CursorPos X="29" Y="75"/> 272 <UsageCount Value="11"/> 273 <Loaded Value="True"/> 274 </Unit27> 230 275 </Units> 231 276 <JumpHistory Count="30" HistoryIndex="29"> 232 277 <Position1> 233 278 <Filename Value="../UMicroThreading.pas"/> 234 <Caret Line=" 462" Column="1" TopLine="451"/>279 <Caret Line="371" Column="16" TopLine="350"/> 235 280 </Position1> 236 281 <Position2> 237 282 <Filename Value="../UMicroThreading.pas"/> 238 <Caret Line=" 463" Column="1" TopLine="451"/>283 <Caret Line="652" Column="38" TopLine="631"/> 239 284 </Position2> 240 285 <Position3> 241 286 <Filename Value="../UMicroThreading.pas"/> 242 <Caret Line=" 434" Column="1" TopLine="423"/>287 <Caret Line="746" Column="12" TopLine="725"/> 243 288 </Position3> 244 289 <Position4> 245 290 <Filename Value="../UMicroThreading.pas"/> 246 <Caret Line=" 462" Column="1" TopLine="451"/>291 <Caret Line="750" Column="31" TopLine="729"/> 247 292 </Position4> 248 293 <Position5> 249 294 <Filename Value="../UMicroThreading.pas"/> 250 <Caret Line=" 463" Column="1" TopLine="451"/>295 <Caret Line="753" Column="26" TopLine="732"/> 251 296 </Position5> 252 297 <Position6> 253 298 <Filename Value="../UMicroThreading.pas"/> 254 <Caret Line=" 545" Column="1" TopLine="534"/>299 <Caret Line="935" Column="13" TopLine="914"/> 255 300 </Position6> 256 301 <Position7> 257 302 <Filename Value="../UMicroThreading.pas"/> 258 <Caret Line=" 429" Column="1" TopLine="418"/>303 <Caret Line="983" Column="31" TopLine="968"/> 259 304 </Position7> 260 305 <Position8> 261 306 <Filename Value="../UMicroThreading.pas"/> 262 <Caret Line=" 430" Column="1" TopLine="418"/>307 <Caret Line="1013" Column="24" TopLine="992"/> 263 308 </Position8> 264 309 <Position9> 265 310 <Filename Value="../UMicroThreading.pas"/> 266 <Caret Line=" 574" Column="1" TopLine="563"/>311 <Caret Line="8" Column="19" TopLine="1"/> 267 312 </Position9> 268 313 <Position10> 269 314 <Filename Value="../UMicroThreading.pas"/> 270 <Caret Line=" 575" Column="1" TopLine="563"/>315 <Caret Line="652" Column="51" TopLine="639"/> 271 316 </Position10> 272 317 <Position11> 273 318 <Filename Value="../UMicroThreading.pas"/> 274 <Caret Line=" 576" Column="1" TopLine="563"/>319 <Caret Line="645" Column="28" TopLine="639"/> 275 320 </Position11> 276 321 <Position12> 277 322 <Filename Value="../UMicroThreading.pas"/> 278 <Caret Line=" 572" Column="1" TopLine="563"/>323 <Caret Line="653" Column="43" TopLine="639"/> 279 324 </Position12> 280 325 <Position13> 281 326 <Filename Value="../UMicroThreading.pas"/> 282 <Caret Line=" 573" Column="1" TopLine="563"/>327 <Caret Line="479" Column="1" TopLine="466"/> 283 328 </Position13> 284 329 <Position14> 285 330 <Filename Value="../UMicroThreading.pas"/> 286 <Caret Line=" 434" Column="1" TopLine="423"/>331 <Caret Line="712" Column="1" TopLine="699"/> 287 332 </Position14> 288 333 <Position15> 289 334 <Filename Value="../UMicroThreading.pas"/> 290 <Caret Line=" 462" Column="1" TopLine="451"/>335 <Caret Line="582" Column="18" TopLine="571"/> 291 336 </Position15> 292 337 <Position16> 293 338 <Filename Value="../UMicroThreading.pas"/> 294 <Caret Line=" 463" Column="1" TopLine="451"/>339 <Caret Line="583" Column="65" TopLine="571"/> 295 340 </Position16> 296 341 <Position17> 297 342 <Filename Value="../UMicroThreading.pas"/> 298 <Caret Line="5 45" Column="30" TopLine="534"/>343 <Caret Line="582" Column="20" TopLine="571"/> 299 344 </Position17> 300 345 <Position18> 301 346 <Filename Value="../UMicroThreading.pas"/> 302 <Caret Line=" 429" Column="1" TopLine="418"/>347 <Caret Line="583" Column="1" TopLine="571"/> 303 348 </Position18> 304 349 <Position19> 305 350 <Filename Value="../UMicroThreading.pas"/> 306 <Caret Line=" 430" Column="1" TopLine="418"/>351 <Caret Line="711" Column="1" TopLine="698"/> 307 352 </Position19> 308 353 <Position20> 309 354 <Filename Value="../UMicroThreading.pas"/> 310 <Caret Line=" 574" Column="1" TopLine="563"/>355 <Caret Line="712" Column="1" TopLine="698"/> 311 356 </Position20> 312 357 <Position21> 313 358 <Filename Value="../UMicroThreading.pas"/> 314 <Caret Line=" 575" Column="1" TopLine="563"/>359 <Caret Line="713" Column="1" TopLine="698"/> 315 360 </Position21> 316 361 <Position22> 317 362 <Filename Value="../UMicroThreading.pas"/> 318 <Caret Line=" 513" Column="34" TopLine="504"/>363 <Caret Line="714" Column="1" TopLine="698"/> 319 364 </Position22> 320 365 <Position23> 321 366 <Filename Value="../UMicroThreading.pas"/> 322 <Caret Line=" 517" Column="1" TopLine="504"/>367 <Caret Line="715" Column="1" TopLine="698"/> 323 368 </Position23> 324 369 <Position24> 325 <Filename Value="../UMicroThread ing.pas"/>326 <Caret Line=" 545" Column="1" TopLine="534"/>370 <Filename Value="../UMicroThreadList.pas"/> 371 <Caret Line="95" Column="54" TopLine="83"/> 327 372 </Position24> 328 373 <Position25> 329 <Filename Value=" UMainForm.pas"/>330 <Caret Line=" 379" Column="1" TopLine="369"/>374 <Filename Value="../UMicroThreading.pas"/> 375 <Caret Line="240" Column="45" TopLine="227"/> 331 376 </Position25> 332 377 <Position26> 333 378 <Filename Value="../UMicroThreading.pas"/> 334 <Caret Line=" 613" Column="1" TopLine="602"/>379 <Caret Line="984" Column="40" TopLine="979"/> 335 380 </Position26> 336 381 <Position27> 337 382 <Filename Value="../UMicroThreading.pas"/> 338 <Caret Line=" 428" Column="6" TopLine="417"/>383 <Caret Line="992" Column="38" TopLine="979"/> 339 384 </Position27> 340 385 <Position28> 341 386 <Filename Value="../UMicroThreading.pas"/> 342 <Caret Line=" 434" Column="3" TopLine="504"/>387 <Caret Line="989" Column="55" TopLine="976"/> 343 388 </Position28> 344 389 <Position29> 345 390 <Filename Value="../UMicroThreading.pas"/> 346 <Caret Line=" 579" Column="1" TopLine="568"/>391 <Caret Line="990" Column="24" TopLine="976"/> 347 392 </Position29> 348 393 <Position30> 349 394 <Filename Value="../UMicroThreading.pas"/> 350 <Caret Line=" 435" Column="1" TopLine="424"/>395 <Caret Line="989" Column="29" TopLine="976"/> 351 396 </Position30> 352 397 </JumpHistory> … … 389 434 </CompilerOptions> 390 435 <Debugging> 391 <BreakPoints Count=" 2">436 <BreakPoints Count="3"> 392 437 <Item1> 393 438 <Source Value="../UMicroThreading.pas"/> 394 <Line Value="2 61"/>439 <Line Value="280"/> 395 440 </Item1> 396 441 <Item2> … … 398 443 <Line Value="1036"/> 399 444 </Item2> 445 <Item3> 446 <Source Value="../UMicroThreading.pas"/> 447 <Line Value="321"/> 448 </Item3> 400 449 </BreakPoints> 401 450 <Watches Count="2"> -
MicroThreading/Demo/UMainForm.lfm
r160 r162 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 … … 26 26 object GroupBox1: TGroupBox 27 27 Left = 6 28 Height = 8128 Height = 123 29 29 Top = 143 30 30 Width = 205 31 31 Caption = 'State' 32 ClientHeight = 6332 ClientHeight = 105 33 33 ClientWidth = 201 34 34 TabOrder = 0 … … 142 142 end 143 143 end 144 object ListView2: TListView145 Left = 6146 Height = 237147 Top = 247148 Width = 206149 Anchors = [akTop, akLeft, akBottom]150 Columns = <151 item152 Caption = 'Id'153 end154 item155 Caption = 'State'156 Width = 135157 end>158 OwnerData = True159 TabOrder = 2160 ViewStyle = vsReport161 OnData = ListView2Data162 end163 object Label13: TLabel164 Left = 7165 Height = 14166 Top = 233167 Width = 44168 Caption = 'Threads:'169 ParentColor = False170 end171 144 object Button4: TButton 172 145 Left = 226 … … 176 149 Caption = 'Show manager' 177 150 OnClick = Button4Click 178 TabOrder = 3151 TabOrder = 2 179 152 end 180 153 end … … 195 168 Left = 10 196 169 Height = 25 197 Top = 2 23170 Top = 266 198 171 Width = 104 199 172 Caption = 'Clear jobs' … … 230 203 end 231 204 object Button1: TButton 232 Left = 230233 Height = 25 234 Top = 2 39205 Left = 316 206 Height = 25 207 Top = 266 235 208 Width = 75 236 209 Caption = 'Clear memo' … … 258 231 object GroupBox3: TGroupBox 259 232 Left = 10 260 Height = 1 37233 Height = 185 261 234 Top = 74 262 235 Width = 292 263 236 Caption = 'Do inside jobs' 264 ClientHeight = 1 19237 ClientHeight = 167 265 238 ClientWidth = 288 266 239 TabOrder = 6 … … 280 253 Width = 90 281 254 MaxValue = 100000 255 OnChange = CheckBox1Change 282 256 TabOrder = 1 283 257 Value = 100 … … 345 319 TabOrder = 6 346 320 end 321 object CheckBox4: TCheckBox 322 Left = 6 323 Height = 17 324 Top = 110 325 Width = 123 326 Caption = 'Wait in critical section' 327 OnChange = CheckBox4Change 328 TabOrder = 7 329 end 330 object SpinEdit6: TSpinEdit 331 Left = 186 332 Height = 21 333 Top = 104 334 Width = 70 335 MaxValue = 100000 336 OnChange = CheckBox4Change 337 TabOrder = 8 338 Value = 100 339 end 340 object Label18: TLabel 341 Left = 264 342 Height = 14 343 Top = 110 344 Width = 14 345 Caption = 'ms' 346 ParentColor = False 347 end 347 348 end 348 349 end -
MicroThreading/Demo/UMainForm.pas
r161 r162 20 20 MainForm: TMainForm; 21 21 procedure DoWriteToMemo; 22 constructor Create(CreateSuspended: Boolean; 23 const StackSize: SizeUInt = DefaultStackSize); 24 destructor Destroy; override; 22 25 end; 23 26 … … 38 41 CheckBox2: TCheckBox; 39 42 CheckBox3: TCheckBox; 43 CheckBox4: TCheckBox; 40 44 CheckBoxUseMainThread: TCheckBox; 41 45 GroupBox1: TGroupBox; … … 45 49 Label11: TLabel; 46 50 Label12: TLabel; 47 Label13: TLabel;48 51 Label14: TLabel; 49 52 Label15: TLabel; 50 53 Label16: TLabel; 51 54 Label17: TLabel; 55 Label18: TLabel; 52 56 Label2: TLabel; 53 57 Label3: TLabel; … … 58 62 Label8: TLabel; 59 63 Label9: TLabel; 60 ListView2: TListView;61 64 Memo1: TMemo; 62 65 PageControl1: TPageControl; … … 66 69 SpinEdit4: TSpinEdit; 67 70 SpinEdit5: TSpinEdit; 71 SpinEdit6: TSpinEdit; 68 72 TabSheet1: TTabSheet; 69 73 TabSheet2: TTabSheet; … … 83 87 procedure CheckBox2Change(Sender: TObject); 84 88 procedure CheckBox3Change(Sender: TObject); 89 procedure CheckBox4Change(Sender: TObject); 85 90 procedure CheckBoxUseMainThreadChange(Sender: TObject); 86 91 procedure FormCreate(Sender: TObject); … … 91 96 procedure SpinEdit3Change(Sender: TObject); 92 97 procedure SpinEdit5Change(Sender: TObject); 98 procedure SpinEdit6Change(Sender: TObject); 93 99 procedure TimerRedrawTimer(Sender: TObject); 94 100 private 95 101 MicroThreadList: TMicroThreadList; 102 Lock: TMicroThreadCriticalSection; 96 103 LastException: Exception; 97 104 LastExceptionSender: TObject; … … 102 109 DoWriteToMemo: Boolean; 103 110 DoSleep: Boolean; 111 DoCriticalSection: Boolean; 104 112 RaiseException: Boolean; 105 113 SleepDuration: Integer; 114 CriticalSectionSleepDuration: Integer; 106 115 DoWaitForEvent: Boolean; 107 116 Event: TMicroThreadEvent; … … 129 138 MicroThreadList := TMicroThreadList.Create(Self); 130 139 UMicroThreading.ExceptionHandler := ShowException; 140 Lock := TMicroThreadCriticalSection.Create; 131 141 end; 132 142 … … 280 290 end; 281 291 292 procedure TMainForm.CheckBox4Change(Sender: TObject); 293 begin 294 CriticalSectionSleepDuration := SpinEdit4.Value; 295 DoCriticalSection := CheckBox4.Checked; 296 end; 297 282 298 procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject); 283 299 begin … … 290 306 MainScheduler.Active := False; 291 307 Event.Free; 308 Lock.Free; 292 309 end; 293 310 … … 303 320 procedure TMainForm.ListView2Data(Sender: TObject; Item: TListItem); 304 321 begin 305 if Item.Index < MainScheduler.ThreadPoolCount then306 try307 MainScheduler.ThreadPoolLock.Acquire;308 with TMicroThreadThread(MainScheduler.ThreadPool[Item.Index]) do begin309 Item.Caption := IntToStr(ThreadID);310 Item.SubItems.Add(MicroThreadThreadStateText[State]);311 end;312 finally313 MainScheduler.ThreadPoolLock.Release;314 end;315 322 end; 316 323 … … 330 337 end; 331 338 339 procedure TMainForm.SpinEdit6Change(Sender: TObject); 340 begin 341 end; 342 332 343 procedure TMainForm.TimerRedrawTimer(Sender: TObject); 333 344 begin 334 if ListView2.Items.Count <> MainScheduler.ThreadPoolCount then335 ListView2.Items.Count := MainScheduler.ThreadPoolCount;336 ListView2.Items[-1];337 ListView2.Refresh;338 339 345 Label2.Caption := DateTimeToStr(NowPrecise) + ' ' + 340 346 FloatToStr(Frac(NowPrecise / OneSecond)); … … 369 375 begin 370 376 for I := 0 to MainForm.Iterations - 1 do begin 371 try372 377 Q := 0; 373 while Q < 100 do Inc(Q);378 while Q < 100000 do Inc(Q); 374 379 if MainForm.DoWriteToMemo then Synchronize(DoWriteToMemo); 375 380 if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond); … … 379 384 raise Exception.Create('Exception from microthread'); 380 385 end; 386 if MainForm.DoCriticalSection then begin 387 try 388 MainForm.Lock.Acquire; 389 MTSleep(MainForm.CriticalSectionSleepDuration * OneMillisecond); 390 finally 391 MainForm.Lock.Release; 392 end; 393 end; 381 394 //WorkerSubRoutine; 382 395 Completion := I / MainForm.Iterations; 383 396 Yield; 384 385 except386 Q := 0;387 raise Exception.Create('Exception from microthread');388 end;389 397 end; 390 398 end; … … 395 403 end; 396 404 405 constructor TWorker.Create(CreateSuspended: Boolean; const StackSize: SizeUInt); 406 begin 407 inherited; 408 end; 409 410 destructor TWorker.Destroy; 411 begin 412 inherited Destroy; 413 end; 414 397 415 end. 398 416 -
MicroThreading/MicroThreading.lpk
r160 r162 5 5 <Author Value="Chronos"/> 6 6 <CompilerOptions> 7 <Version Value=" 9"/>7 <Version Value="10"/> 8 8 <SearchPaths> 9 9 <OtherUnitFiles Value="Other"/> … … 41 41 </Files> 42 42 <Type Value="RunAndDesignTime"/> 43 <RequiredPkgs Count=" 2">43 <RequiredPkgs Count="4"> 44 44 <Item1> 45 <PackageName Value="LCL "/>45 <PackageName Value="LCLBase"/> 46 46 </Item1> 47 47 <Item2> 48 <PackageName Value="LCLBase"/> 49 </Item2> 50 <Item3> 51 <PackageName Value="LCL"/> 52 </Item3> 53 <Item4> 48 54 <PackageName Value="FCL"/> 49 55 <MinVersion Major="1" Valid="True"/> 50 </Item 2>56 </Item4> 51 57 </RequiredPkgs> 52 58 <UsageOptions> -
MicroThreading/UMicroThreadList.lfm
r159 r162 1 1 object MicroThreadListForm: TMicroThreadListForm 2 Left = 2672 Left = 367 3 3 Height = 423 4 4 Top = 118 … … 10 10 OnShow = FormShow 11 11 LCLVersion = '0.9.31' 12 object ListView1: TListView 13 Left = 8 14 Height = 410 15 Top = 8 16 Width = 557 17 Anchors = [akTop, akLeft, akRight, akBottom] 18 Columns = < 19 item 20 Caption = 'Id' 21 Width = 40 22 end 23 item 24 Caption = 'Usage' 25 end 26 item 27 Caption = 'Priority' 28 Width = 40 29 end 30 item 31 Caption = 'State' 32 Width = 60 33 end 34 item 35 Caption = 'Block state' 36 end 37 item 38 Caption = 'Execution time' 39 Width = 70 40 end 41 item 42 Caption = 'Tick count' 43 end 44 item 45 Caption = 'Completion' 46 end 47 item 48 Caption = 'Used stack' 49 end 50 item 51 Caption = 'Name' 52 Width = 70 53 end> 54 OwnerData = True 55 ReadOnly = True 56 RowSelect = True 12 object Panel1: TPanel 13 Left = 0 14 Height = 248 15 Top = 0 16 Width = 569 17 Align = alTop 18 BevelOuter = bvNone 19 ClientHeight = 248 20 ClientWidth = 569 57 21 TabOrder = 0 58 ViewStyle = vsReport 59 OnData = ListView1Data 22 object Label1: TLabel 23 Left = 8 24 Height = 14 25 Top = 8 26 Width = 67 27 Caption = 'Microthreads:' 28 ParentColor = False 29 end 30 object ListView1: TListView 31 Left = 8 32 Height = 218 33 Top = 24 34 Width = 557 35 Anchors = [akTop, akLeft, akRight, akBottom] 36 Columns = < 37 item 38 Caption = 'Id' 39 Width = 40 40 end 41 item 42 Caption = 'Usage' 43 end 44 item 45 Caption = 'Priority' 46 Width = 40 47 end 48 item 49 Caption = 'State' 50 Width = 60 51 end 52 item 53 Caption = 'Block state' 54 end 55 item 56 Caption = 'Execution time' 57 Width = 70 58 end 59 item 60 Caption = 'Tick count' 61 end 62 item 63 Caption = 'Completion' 64 end 65 item 66 Caption = 'Used stack' 67 end 68 item 69 Caption = 'Name' 70 Width = 70 71 end> 72 OwnerData = True 73 ReadOnly = True 74 RowSelect = True 75 TabOrder = 0 76 ViewStyle = vsReport 77 OnData = ListView1Data 78 end 79 end 80 object Panel2: TPanel 81 Left = 0 82 Height = 170 83 Top = 253 84 Width = 569 85 Align = alClient 86 BevelOuter = bvNone 87 ClientHeight = 170 88 ClientWidth = 569 89 TabOrder = 1 90 object ListView2: TListView 91 Left = 8 92 Height = 142 93 Top = 19 94 Width = 557 95 Anchors = [akTop, akLeft, akRight, akBottom] 96 Columns = < 97 item 98 Caption = 'Id' 99 end 100 item 101 Caption = 'State' 102 Width = 100 103 end 104 item 105 Caption = 'Microthread Id' 106 Width = 70 107 end 108 item 109 Caption = 'Loop duration' 110 Width = 70 111 end> 112 OwnerData = True 113 ReadOnly = True 114 RowSelect = True 115 TabOrder = 0 116 ViewStyle = vsReport 117 OnData = ListView2Data 118 end 119 object Label2: TLabel 120 Left = 8 121 Height = 14 122 Top = 3 123 Width = 44 124 Caption = 'Threads:' 125 ParentColor = False 126 end 127 end 128 object Splitter1: TSplitter 129 Cursor = crVSplit 130 Left = 0 131 Height = 5 132 Top = 248 133 Width = 569 134 Align = alTop 135 ResizeAnchor = akTop 60 136 end 61 137 object TimerRedraw: TTimer -
MicroThreading/UMicroThreadList.pas
r159 r162 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, 9 ExtCtrls ;9 ExtCtrls, StdCtrls, DateUtils; 10 10 11 11 type … … 14 14 15 15 TMicroThreadListForm = class(TForm) 16 Label1: TLabel; 17 Label2: TLabel; 16 18 ListView1: TListView; 19 ListView2: TListView; 20 Panel1: TPanel; 21 Panel2: TPanel; 22 Splitter1: TSplitter; 17 23 TimerRedraw: TTimer; 18 24 procedure FormHide(Sender: TObject); 19 25 procedure FormShow(Sender: TObject); 20 26 procedure ListView1Data(Sender: TObject; Item: TListItem); 27 procedure ListView2Data(Sender: TObject; Item: TListItem); 21 28 procedure TimerRedrawTimer(Sender: TObject); 22 29 private … … 36 43 37 44 procedure TMicroThreadListForm.TimerRedrawTimer(Sender: TObject); 45 var 46 ThreadCount: Integer; 38 47 begin 39 48 if ListView1.Items.Count <> MainScheduler.MicroThreadCount then … … 41 50 ListView1.Items[-1]; 42 51 ListView1.Refresh; 52 53 ThreadCount := MainScheduler.ThreadPoolCount; 54 if MainScheduler.UseMainThread then Inc(ThreadCount); 55 if ListView2.Items.Count <> ThreadCount then 56 ListView2.Items.Count := ThreadCount; 57 ListView2.Items[-1]; 58 ListView2.Refresh; 43 59 end; 44 60 … … 66 82 end; 67 83 84 procedure TMicroThreadListForm.ListView2Data(Sender: TObject; Item: TListItem); 85 var 86 Increment: Integer; 87 begin 88 if MainScheduler.UseMainThread then Increment := 1 89 else Increment := 0; 90 91 if Item.Index < (MainScheduler.ThreadPoolCount + Increment) then begin 92 if MainScheduler.UseMainThread and (Item.Index = 0) then begin 93 Item.Caption := IntToStr(MainThreadID); 94 Item.SubItems.Add(''); 95 Item.SubItems.Add(IntToStr(MainScheduler.MainThreadManager.GetCurrentMicroThreadId)); 96 Item.SubItems.Add(FloatToStr(MainScheduler.MainThreadManager.LoopDuration / OneMillisecond) + ' ms'); 97 end else 98 try 99 MainScheduler.ThreadPoolLock.Acquire; 100 with TMicroThreadThread(MainScheduler.ThreadPool[Item.Index - Increment]) do begin 101 Item.Caption := IntToStr(ThreadID); 102 Item.SubItems.Add(MicroThreadThreadStateText[State]); 103 Item.SubItems.Add(IntToStr(Manager.GetCurrentMicroThreadId)); 104 Item.SubItems.Add(FloatToStr(Manager.LoopDuration / OneMillisecond) + ' ms'); 105 end; 106 finally 107 MainScheduler.ThreadPoolLock.Release; 108 end; 109 end; 110 end; 111 68 112 procedure TMicroThreadListForm.FormShow(Sender: TObject); 69 113 begin -
MicroThreading/UMicroThreading.pas
r161 r162 1 (* Not implemented yet 1 // Date: 2010-02-07 2 3 (* 4 Not implemented yet 2 5 - Stack limit checking 3 6 - measurement of cpu usage by micro threads 4 - microthread critical sections (no low level cpu blocking) 5 - wait for single and multiple objects 6 - micro thread priorty 7 - wait for multiple objects 8 - micro thread priority 7 9 *) 8 10 … … 31 33 SCantDetermineThreadID = 'Can''t determine thread for id %d'; 32 34 SNotInThread = 'Not in thread'; 35 SReleaseNotAcquiredLock = 'Release not acquired lock'; 33 36 34 37 … … 41 44 TMicroThreadBlockState = (tbsNone, tbsSleeping, tbsWaitFor, tbsTerminating, 42 45 tbsTerminated); 46 47 { TMicroThreadCriticalSection } 48 49 TMicroThreadCriticalSection = class 50 Lock: TCriticalSection; 51 Counter: Integer; 52 procedure Acquire; 53 procedure Release; 54 constructor Create; 55 destructor Destroy; override; 56 end; 43 57 44 58 { TMicroThreadEvent } … … 53 67 procedure SetEvent; 54 68 procedure ResetEvent; 55 procedure WaitFor(Duration: TDateTime);69 function WaitFor(Duration: TDateTime): TWaitResult; 56 70 constructor Create; 57 71 destructor Destroy; override; … … 70 84 FExecutionCount: Integer; 71 85 FStack: Pointer; 72 FStackPointer: Pointer; 86 FStackPointer: Pointer; // Register SP 73 87 FStackSize: Integer; 74 FBasePointer: Pointer; 88 FBasePointer: Pointer; // Register BP 75 89 FExceptObjectStack: PExceptObject; 76 90 FExceptAddrStack: PExceptAddr; … … 160 174 FThread: TMicroThreadThread; 161 175 FId: Integer; 176 FLoopDuration: TDateTime; 177 FLoopStart: TDateTime; 162 178 procedure SetCurrentMicroThread(const AValue: TMicroThread); 163 179 function Execute(Count: Integer): Integer; … … 170 186 destructor Destroy; override; 171 187 property Scheduler: TMicroThreadScheduler read FScheduler; 188 property LoopDuration: TDateTime read FLoopDuration; 189 function GetCurrentMicroThreadId: Integer; 172 190 end; 173 191 … … 206 224 procedure MainThreadTick(Data: PtrInt); 207 225 public 226 BurstCount: Integer; 208 227 function Add(MicroThread: TMicroThread): Integer; 209 228 function AddMethod(Method: TMicroThreadMethod): Integer; … … 343 362 end; 344 363 364 { TMicroThreadCriticalSection } 365 366 procedure TMicroThreadCriticalSection.Acquire; 367 begin 368 try 369 Lock.Acquire; 370 while Counter > 0 do begin 371 try 372 Lock.Release; 373 MTSleep(1 * OneMillisecond); 374 finally 375 Lock.Acquire; 376 end; 377 end; 378 Inc(Counter); 379 finally 380 Lock.Release; 381 end; 382 end; 383 384 procedure TMicroThreadCriticalSection.Release; 385 begin 386 try 387 Lock.Acquire; 388 if Counter > 0 then Dec(Counter) 389 else raise Exception.Create(SReleaseNotAcquiredLock); 390 finally 391 Lock.Release; 392 end; 393 end; 394 395 constructor TMicroThreadCriticalSection.Create; 396 begin 397 Lock := TCriticalSection.Create; 398 end; 399 400 destructor TMicroThreadCriticalSection.Destroy; 401 begin 402 Acquire; 403 Lock.Free; 404 inherited Destroy; 405 end; 406 345 407 { TMicroThreadList } 346 408 … … 363 425 for I := 0 to FMicroThreads.Count - 1 do 364 426 with TMicroThread(FMicroThreads[I]) do begin 365 if (FState = tsBlocked) and (FBlockState = tbsWaitFor) then 427 if (FState = tsBlocked) and (FBlockState = tbsWaitFor) then begin 366 428 FState := tsWaiting; 429 FBlockTime := 0; // Set signaled state using block time variable 430 end; 367 431 end; 368 432 if not FAutoReset then FSignaled := True; … … 377 441 end; 378 442 379 procedure TMicroThreadEvent.WaitFor(Duration: TDateTime);443 function TMicroThreadEvent.WaitFor(Duration: TDateTime): TWaitResult; 380 444 var 381 445 MT: TMicroThread; 382 446 begin 383 447 MT := GetCurrentMicroThread; 384 if Assigned(MT) then MT.WaitForEvent(Self, Duration); 448 if Assigned(MT) then Result := MT.WaitForEvent(Self, Duration) 449 else Result := wrSignaled; 385 450 end; 386 451 … … 422 487 function TMicroThreadManager.Execute(Count: Integer): Integer; 423 488 begin 489 FLoopStart := NowPrecise; 424 490 FStack := StackBottom; 425 491 FStackSize := StackBottom + StackLength; … … 428 494 Yield; 429 495 Result := FExecutedCount; 496 FLoopDuration := NowPrecise - FLoopStart; 430 497 end; 431 498 … … 489 556 mov ebp, ebx 490 557 // We want to call virtual method Execute 491 // but virtual methods can be called only statically558 // but methods can be called only statically from assembler 492 559 // Then static method CallExecute is calling virtual method Execute 493 560 call TMicroThread.CallExecute … … 515 582 FMicroThreadsLock.Acquire; 516 583 FMicroThreads.Delete(FMicroThreads.IndexOf(FCurrentMicroThread)); 517 FCurrentMicroThread .Manager:= nil;584 FCurrentMicroThread := nil; 518 585 finally 519 586 FMicroThreadsLock.Release; … … 562 629 end; 563 630 631 function TMicroThreadManager.GetCurrentMicroThreadId: Integer; 632 begin 633 try 634 FScheduler.FMicroThreadsLock.Acquire; 635 if Assigned(FCurrentMicroThread) then 636 Result := FCurrentMicroThread.Id 637 else Result := 0; 638 finally 639 FScheduler.FMicroThreadsLock.Release; 640 end; 641 end; 642 564 643 { TMicroThreadThread } 565 644 … … 571 650 repeat 572 651 State := ttsRunning; 573 ExecutedCount := Manager.Execute( 10);652 ExecutedCount := Manager.Execute(MainScheduler.BurstCount); 574 653 State := ttsReady; 575 654 if ExecutedCount = 0 then Sleep(1); … … 691 770 end; 692 771 Yield; 693 if FBlockTime < NowPrecise then694 Result := wrTimeoutelse Result := wrSignaled;772 if (FBlockTime <> 0) and (FBlockTime < NowPrecise) then Result := wrTimeout 773 else Result := wrSignaled; 695 774 696 775 try … … 811 890 FMainThreadManager.FScheduler := Self; 812 891 UseMainThread := False; 892 BurstCount := 100; 813 893 end; 814 894 … … 900 980 var 901 981 Executed: Integer; 982 StartTime: TDateTime; 983 Duration: TDateTime; 902 984 begin 903 985 // try 904 Executed := FMainThreadManager.Execute(1); 905 if Executed = 0 then Sleep(1); 986 Duration := 100 * OneMillisecond; 987 StartTime := NowPrecise; 988 Executed := -1; 989 while (Executed <> 0) and ((NowPrecise - StartTime) < Duration) do begin 990 Executed := FMainThreadManager.Execute(BurstCount); 991 end; 992 //if Executed = 0 then Sleep(1); 906 993 // If not terminated then queue next tick else terminate 907 994 if (FState = ssRunning) and FUseMainThread then
Note:
See TracChangeset
for help on using the changeset viewer.