Changeset 145
- Timestamp:
- Jan 25, 2011, 8:51:57 PM (14 years ago)
- Location:
- MicroThreading
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/Demo.lpi
r144 r145 50 50 <TopLine Value="1"/> 51 51 <CursorPos X="1" Y="15"/> 52 <UsageCount Value="4 6"/>52 <UsageCount Value="47"/> 53 53 </Unit0> 54 54 <Unit1> … … 58 58 <ResourceBaseClass Value="Form"/> 59 59 <UnitName Value="UMainForm"/> 60 <IsVisibleTab Value="True"/> 60 61 <EditorIndex Value="0"/> 61 62 <WindowIndex Value="0"/> 62 <TopLine Value=" 182"/>63 <CursorPos X=" 28" Y="184"/>64 <UsageCount Value="4 6"/>63 <TopLine Value="84"/> 64 <CursorPos X="3" Y="88"/> 65 <UsageCount Value="47"/> 65 66 <Loaded Value="True"/> 66 67 <LoadedDesigner Value="True"/> … … 69 70 <Filename Value="../UMicroThreading.pas"/> 70 71 <UnitName Value="UMicroThreading"/> 71 <IsVisibleTab Value="True"/> 72 <EditorIndex Value="6"/> 73 <WindowIndex Value="0"/> 74 <TopLine Value="337"/> 75 <CursorPos X="17" Y="350"/> 76 <UsageCount Value="25"/> 72 <EditorIndex Value="1"/> 73 <WindowIndex Value="0"/> 74 <TopLine Value="383"/> 75 <CursorPos X="9" Y="384"/> 76 <UsageCount Value="26"/> 77 77 <Loaded Value="True"/> 78 78 </Unit2> … … 159 159 <Unit13> 160 160 <Filename Value="../../../../lazarus/lcl/include/buttons.inc"/> 161 <WindowIndex Value="0"/> 162 <TopLine Value="156"/> 163 <CursorPos X="21" Y="175"/> 164 <UsageCount Value="8"/> 161 <EditorIndex Value="4"/> 162 <WindowIndex Value="0"/> 163 <TopLine Value="317"/> 164 <CursorPos X="1" Y="341"/> 165 <UsageCount Value="10"/> 166 <Loaded Value="True"/> 165 167 </Unit13> 166 168 <Unit14> 167 169 <Filename Value="../../../../lazarus/lcl/include/buttoncontrol.inc"/> 170 <EditorIndex Value="3"/> 168 171 <WindowIndex Value="0"/> 169 172 <TopLine Value="45"/> 170 <CursorPos X="17" Y="62"/> 171 <UsageCount Value="8"/> 173 <CursorPos X="1" Y="62"/> 174 <UsageCount Value="10"/> 175 <Loaded Value="True"/> 172 176 </Unit14> 173 177 <Unit15> 174 178 <Filename Value="../../../../lazarus/lcl/include/control.inc"/> 179 <EditorIndex Value="2"/> 175 180 <WindowIndex Value="0"/> 176 181 <TopLine Value="2271"/> 177 <CursorPos X=" 4" Y="2274"/>182 <CursorPos X="19" Y="2274"/> 178 183 <UsageCount Value="11"/> 184 <Loaded Value="True"/> 179 185 </Unit15> 180 186 <Unit16> … … 289 295 <Unit31> 290 296 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysosh.inc"/> 291 <EditorIndex Value="3"/>292 297 <WindowIndex Value="0"/> 293 298 <TopLine Value="12"/> 294 299 <CursorPos X="3" Y="25"/> 295 300 <UsageCount Value="13"/> 296 <Loaded Value="True"/>297 301 </Unit31> 298 302 <Unit32> … … 354 358 <Unit40> 355 359 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/systemh.inc"/> 356 <EditorIndex Value="1"/>357 360 <WindowIndex Value="0"/> 358 361 <TopLine Value="492"/> 359 362 <CursorPos X="3" Y="504"/> 360 363 <UsageCount Value="10"/> 361 <Loaded Value="True"/>362 364 </Unit40> 363 365 <Unit41> 364 366 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/syswin.inc"/> 365 <EditorIndex Value="5"/>366 367 <WindowIndex Value="0"/> 367 368 <TopLine Value="197"/> 368 369 <CursorPos X="30" Y="208"/> 369 370 <UsageCount Value="10"/> 370 <Loaded Value="True"/>371 371 </Unit41> 372 372 <Unit42> 373 373 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/system.inc"/> 374 <EditorIndex Value="4"/>375 374 <WindowIndex Value="0"/> 376 375 <TopLine Value="737"/> 377 376 <CursorPos X="21" Y="747"/> 378 377 <UsageCount Value="10"/> 379 <Loaded Value="True"/>380 378 </Unit42> 381 379 <Unit43> 382 380 <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/innr.inc"/> 383 <EditorIndex Value="2"/>384 381 <WindowIndex Value="0"/> 385 382 <TopLine Value="96"/> 386 383 <CursorPos X="1" Y="1"/> 387 384 <UsageCount Value="10"/> 388 <Loaded Value="True"/>389 385 </Unit43> 390 386 </Units> … … 392 388 <Position1> 393 389 <Filename Value="../UMicroThreading.pas"/> 394 <Caret Line=" 322" Column="1" TopLine="315"/>390 <Caret Line="233" Column="1" TopLine="209"/> 395 391 </Position1> 396 392 <Position2> 397 393 <Filename Value="../UMicroThreading.pas"/> 398 <Caret Line=" 328" Column="1" TopLine="315"/>394 <Caret Line="234" Column="1" TopLine="210"/> 399 395 </Position2> 400 396 <Position3> 401 397 <Filename Value="../UMicroThreading.pas"/> 402 <Caret Line=" 363" Column="3" TopLine="350"/>398 <Caret Line="235" Column="1" TopLine="211"/> 403 399 </Position3> 404 400 <Position4> 405 401 <Filename Value="../UMicroThreading.pas"/> 406 <Caret Line=" 322" Column="1" TopLine="309"/>402 <Caret Line="236" Column="1" TopLine="212"/> 407 403 </Position4> 408 404 <Position5> 409 405 <Filename Value="../UMicroThreading.pas"/> 410 <Caret Line=" 326" Column="20" TopLine="309"/>406 <Caret Line="237" Column="1" TopLine="213"/> 411 407 </Position5> 412 408 <Position6> 413 409 <Filename Value="../UMicroThreading.pas"/> 414 <Caret Line=" 169" Column="1" TopLine="156"/>410 <Caret Line="239" Column="1" TopLine="215"/> 415 411 </Position6> 416 412 <Position7> 417 413 <Filename Value="../UMicroThreading.pas"/> 418 <Caret Line=" 363" Column="1" TopLine="350"/>414 <Caret Line="240" Column="1" TopLine="216"/> 419 415 </Position7> 420 416 <Position8> 421 417 <Filename Value="../UMicroThreading.pas"/> 422 <Caret Line=" 322" Column="1" TopLine="309"/>418 <Caret Line="241" Column="1" TopLine="217"/> 423 419 </Position8> 424 420 <Position9> 425 421 <Filename Value="../UMicroThreading.pas"/> 426 <Caret Line=" 330" Column="1" TopLine="309"/>422 <Caret Line="242" Column="1" TopLine="218"/> 427 423 </Position9> 428 424 <Position10> 429 425 <Filename Value="../UMicroThreading.pas"/> 430 <Caret Line=" 316" Column="1" TopLine="309"/>426 <Caret Line="243" Column="1" TopLine="219"/> 431 427 </Position10> 432 428 <Position11> 433 429 <Filename Value="../UMicroThreading.pas"/> 434 <Caret Line=" 169" Column="1" TopLine="156"/>430 <Caret Line="246" Column="1" TopLine="222"/> 435 431 </Position11> 436 432 <Position12> 437 433 <Filename Value="../UMicroThreading.pas"/> 438 <Caret Line=" 316" Column="1" TopLine="303"/>434 <Caret Line="247" Column="1" TopLine="223"/> 439 435 </Position12> 440 436 <Position13> 441 437 <Filename Value="../UMicroThreading.pas"/> 442 <Caret Line=" 330" Column="16" TopLine="317"/>438 <Caret Line="248" Column="1" TopLine="224"/> 443 439 </Position13> 444 440 <Position14> 445 441 <Filename Value="../UMicroThreading.pas"/> 446 <Caret Line=" 363" Column="1" TopLine="350"/>442 <Caret Line="249" Column="1" TopLine="225"/> 447 443 </Position14> 448 444 <Position15> 449 <Filename Value=" UMainForm.pas"/>450 <Caret Line="2 28" Column="1" TopLine="208"/>445 <Filename Value="../UMicroThreading.pas"/> 446 <Caret Line="250" Column="1" TopLine="226"/> 451 447 </Position15> 452 448 <Position16> 453 <Filename Value=" UMainForm.pas"/>454 <Caret Line="2 35" Column="1" TopLine="215"/>449 <Filename Value="../UMicroThreading.pas"/> 450 <Caret Line="252" Column="1" TopLine="228"/> 455 451 </Position16> 456 452 <Position17> 457 <Filename Value=" UMainForm.pas"/>458 <Caret Line=" 184" Column="28" TopLine="182"/>453 <Filename Value="../UMicroThreading.pas"/> 454 <Caret Line="204" Column="1" TopLine="189"/> 459 455 </Position17> 460 456 <Position18> 461 <Filename Value="../ ../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/system.inc"/>462 <Caret Line=" 660" Column="22" TopLine="656"/>457 <Filename Value="../UMicroThreading.pas"/> 458 <Caret Line="217" Column="1" TopLine="193"/> 463 459 </Position18> 464 460 <Position19> 465 461 <Filename Value="../UMicroThreading.pas"/> 466 <Caret Line=" 527" Column="78" TopLine="504"/>462 <Caret Line="188" Column="1" TopLine="173"/> 467 463 </Position19> 468 464 <Position20> 469 465 <Filename Value="../UMicroThreading.pas"/> 470 <Caret Line=" 531" Column="51" TopLine="513"/>466 <Caret Line="217" Column="1" TopLine="192"/> 471 467 </Position20> 472 468 <Position21> 473 <Filename Value="../ ../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/syswin.inc"/>474 <Caret Line=" 208" Column="27" TopLine="194"/>469 <Filename Value="../UMicroThreading.pas"/> 470 <Caret Line="188" Column="1" TopLine="173"/> 475 471 </Position21> 476 472 <Position22> 477 473 <Filename Value="../UMicroThreading.pas"/> 478 <Caret Line=" 531" Column="46" TopLine="522"/>474 <Caret Line="268" Column="7" TopLine="242"/> 479 475 </Position22> 480 476 <Position23> 481 477 <Filename Value="../UMicroThreading.pas"/> 482 <Caret Line=" 161" Column="27" TopLine="158"/>478 <Caret Line="217" Column="1" TopLine="202"/> 483 479 </Position23> 484 480 <Position24> 485 481 <Filename Value="../UMicroThreading.pas"/> 486 <Caret Line="1 95" Column="43" TopLine="180"/>482 <Caret Line="188" Column="1" TopLine="173"/> 487 483 </Position24> 488 484 <Position25> 489 485 <Filename Value="../UMicroThreading.pas"/> 490 <Caret Line=" 211" Column="43" TopLine="198"/>486 <Caret Line="480" Column="1" TopLine="465"/> 491 487 </Position25> 492 488 <Position26> 493 489 <Filename Value="../UMicroThreading.pas"/> 494 <Caret Line=" 241" Column="45" TopLine="226"/>490 <Caret Line="188" Column="1" TopLine="173"/> 495 491 </Position26> 496 492 <Position27> 497 493 <Filename Value="../UMicroThreading.pas"/> 498 <Caret Line=" 542" Column="1" TopLine="524"/>494 <Caret Line="292" Column="1" TopLine="254"/> 499 495 </Position27> 500 496 <Position28> 501 497 <Filename Value="../UMicroThreading.pas"/> 502 <Caret Line=" 538" Column="39" TopLine="525"/>498 <Caret Line="389" Column="3" TopLine="387"/> 503 499 </Position28> 504 500 <Position29> 505 501 <Filename Value="../UMicroThreading.pas"/> 506 <Caret Line=" 181" Column="1" TopLine="155"/>502 <Caret Line="384" Column="9" TopLine="383"/> 507 503 </Position29> 508 504 <Position30> 509 <Filename Value=" ../UMicroThreading.pas"/>510 <Caret Line=" 350" Column="22" TopLine="337"/>505 <Filename Value="UMainForm.pas"/> 506 <Caret Line="235" Column="1" TopLine="142"/> 511 507 </Position30> 512 508 </JumpHistory> … … 533 529 </CompilerOptions> 534 530 <Debugging> 535 <BreakPoints Count=" 5">531 <BreakPoints Count="7"> 536 532 <Item1> 537 533 <Source Value="../Coroutine.pas"/> … … 554 550 <Line Value="145"/> 555 551 </Item5> 552 <Item6> 553 <Source Value="../UMicroThreading.pas"/> 554 <Line Value="389"/> 555 </Item6> 556 <Item7> 557 <Source Value="UMainForm.pas"/> 558 <Line Value="88"/> 559 </Item7> 556 560 </BreakPoints> 557 561 <Exceptions Count="3"> -
MicroThreading/Demo/Demo.lpr
r133 r145 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, microthreading, ExceptionLogger10 Forms, UMainForm, MicroThreading, ExceptionLogger 11 11 { you can add units after this }; 12 12 -
MicroThreading/Demo/UMainForm.lfm
r144 r145 58 58 Height = 14 59 59 Top = 8 60 Width = 7460 Width = 85 61 61 Caption = 'Microthread list' 62 62 ParentColor = False … … 93 93 Height = 14 94 94 Top = 80 95 Width = 3 295 Width = 38 96 96 Caption = 'Label2' 97 97 ParentColor = False -
MicroThreading/Demo/UMainForm.pas
r144 r145 88 88 if Button1.Caption = 'Start scheduler' then begin 89 89 Button1.Caption := 'Stop scheduler'; 90 Scheduler.MicroThreads.Clear;91 90 Memo1.Clear; 92 91 Scheduler.Start; … … 159 158 I: Integer; 160 159 begin 160 Scheduler.MicroThreads.Clear; 161 161 for I := 0 to SpinEdit1.Value do 162 162 Scheduler.AddMethod(Worker); -
MicroThreading/UMicroThreading.pas
r144 r145 96 96 FStackPointer: Pointer; 97 97 FBasePointer: Pointer; 98 FSelected: TMicroThread;99 98 FExecuteCount: Integer; 100 99 FExecutedCount: Integer; … … 105 104 Scheduler: TMicroThreadScheduler; 106 105 CurrentMicroThread: TMicroThread; 107 procedure Yield (MicroThread: TMicroThread);106 procedure Yield; 108 107 constructor Create; 109 108 destructor Destroy; override; … … 154 153 StaticMicroThread: TMicroThread; 155 154 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 155 function GetMicroThreadId: Integer; 156 var 157 I: Integer; 158 CurrentStack: Pointer; 159 begin 160 asm 161 mov CurrentStack, sp 162 end; 163 with MainScheduler do begin 164 try 165 Lock.Acquire; 166 I := 0; 167 while (I < MicroThreads.Count) and 168 not ((CurrentStack >= TMicroThread(MicroThreads[I]).FStack) and 169 (CurrentStack <= (TMicroThread(MicroThreads[I]).FStack + 170 TMicroThread(MicroThreads[I]).FStackSize))) do Inc(I); 171 if I < MicroThreads.Count then begin 172 Result := TMicroThread(MicroThreads[I]).Id; 173 end else Result := -1; 174 finally 175 Lock.Release; 176 end; 177 end; 178 end; 180 179 181 180 { TMicroThreadManager } … … 187 186 FExecuteCount := Count; 188 187 FExecutedCount := 0; 189 Yield (nil);188 Yield; 190 189 Result := FExecutedCount; 191 190 end; 192 191 193 procedure TMicroThreadManager.Yield (MicroThread: TMicroThread);192 procedure TMicroThreadManager.Yield; 194 193 var 195 194 I: Integer; … … 197 196 begin 198 197 Time := Scheduler.GetNow; 199 if Assigned( MicroThread) then begin200 MicroThread.Manager := nil;201 MicroThread.FExecutionEndTime := Time;202 MicroThread.FExecutionTime := MicroThread.FExecutionTime +203 (MicroThread.FExecutionEndTime - MicroThread.FExecutionStartTime);204 if MicroThread.State = tsRunning then205 MicroThread.State := tsWaiting;198 if Assigned(CurrentMicroThread) then begin 199 CurrentMicroThread.FExecutionEndTime := Time; 200 CurrentMicroThread.FExecutionTime := CurrentMicroThread.FExecutionTime + 201 (CurrentMicroThread.FExecutionEndTime - CurrentMicroThread.FExecutionStartTime); 202 if CurrentMicroThread.State = tsRunning then 203 CurrentMicroThread.State := tsWaiting; 204 StaticMicroThread := CurrentMicroThread; 206 205 asm 207 206 // Store microthread stack 208 mov eax, MicroThread207 mov eax, StaticMicroThread 209 208 mov edx, esp 210 209 mov [eax].TMicroThread.FStackPointer, edx … … 212 211 mov [eax].TMicroThread.FBasePointer, edx 213 212 end; 214 StaticManager := MicroThread.Manager;213 StaticManager := CurrentMicroThread.Manager; 215 214 asm 216 215 // Restore scheduler stack … … 221 220 mov ebp, edx 222 221 end; 222 CurrentMicroThread.Manager := nil; 223 223 CurrentMicroThread := nil; 224 224 end; 225 225 226 FSelected := Scheduler.GetNextMicroThread;227 228 if Assigned( FSelected) and (FExecutedCount < FExecuteCount) then begin229 FSelected.Manager := Self;226 CurrentMicroThread := Scheduler.GetNextMicroThread; 227 228 if Assigned(CurrentMicroThread) and (FExecutedCount < FExecuteCount) then begin 229 CurrentMicroThread.Manager := Self; 230 230 Inc(FExecutedCount); 231 CurrentMicroThread := FSelected;232 231 asm 233 232 // Store scheduler stack … … 238 237 mov [eax].TMicroThreadManager.FBasePointer, edx 239 238 end; 240 if not FSelected.FExecuted then begin241 FSelected.FExecuted := True;242 FSelected.State := tsRunning;243 FSelected.FExecutionStartTime := Time;244 FTempPointer := FSelected.FStackPointer;239 if not CurrentMicroThread.FExecuted then begin 240 CurrentMicroThread.FExecuted := True; 241 CurrentMicroThread.State := tsRunning; 242 CurrentMicroThread.FExecutionStartTime := Time; 243 StaticMicroThread := CurrentMicroThread; 245 244 asm 246 245 // Restore microthread stack 247 mov eax, S elf248 mov edx, [eax].TMicroThread Manager.FTempPointer246 mov eax, StaticMicroThread 247 mov edx, [eax].TMicroThread.FStackPointer 249 248 mov esp, edx 250 end; 251 StaticMicroThread := FSelected; // BP will be change and Self pointer will be invalid 252 FTempPointer := FSelected.FBasePointer; 253 asm 254 mov eax, Self 255 mov edx, [eax].TMicroThreadManager.FTempPointer 249 mov edx, [eax].TMicroThread.FBasePointer 256 250 mov ebp, edx 257 251 end; … … 267 261 mov ebp, edx 268 262 end; 269 FSelected.Manager := nil; 270 FSelected.FExecutionEndTime := Time; 271 FSelected.FExecutionTime := FSelected.FExecutionTime + 272 (FSelected.FExecutionEndTime - FSelected.FExecutionStartTime); 273 FSelected.FFinished := True; 274 if FSelected.FFreeOnTerminate then begin 275 FSelected.Free; 276 end;; 263 CurrentMicroThread.Manager := nil; 264 CurrentMicroThread.FExecutionEndTime := Time; 265 CurrentMicroThread.FExecutionTime := CurrentMicroThread.FExecutionTime + 266 (CurrentMicroThread.FExecutionEndTime - CurrentMicroThread.FExecutionStartTime); 267 CurrentMicroThread.FFinished := True; 268 if CurrentMicroThread.FFreeOnTerminate then begin 269 CurrentMicroThread.Free; 270 end; 271 CurrentMicroThread := nil; 277 272 end else 278 if FSelected.State = tsWaiting then begin273 if CurrentMicroThread.State = tsWaiting then begin 279 274 // Execute selected thread 280 FSelected.State := tsRunning;281 FSelected.FExecutionStartTime := Time;282 FTempPointer := FSelected.FStackPointer;275 CurrentMicroThread.State := tsRunning; 276 CurrentMicroThread.FExecutionStartTime := Time; 277 FTempPointer := CurrentMicroThread.FStackPointer; 283 278 asm 284 279 // Restore microthread stack … … 287 282 mov esp, edx 288 283 end; 289 FTempPointer := FSelected.FBasePointer;284 FTempPointer := CurrentMicroThread.FBasePointer; 290 285 asm 291 286 mov eax, Self … … 294 289 end; 295 290 end; 291 end else begin 292 CurrentMicroThread := nil; 296 293 end; 297 294 end; … … 299 296 constructor TMicroThreadManager.Create; 300 297 begin 301 298 CurrentMicroThread := nil; 302 299 end; 303 300 … … 343 340 procedure TMicroThread.Yield; 344 341 begin 345 Manager.Yield (Self);342 Manager.Yield; 346 343 end; 347 344 … … 430 427 fpgettimeofday(@t, nil); 431 428 // Build a 64 bit microsecond tick from the seconds and microsecond longints 432 Result := (Int64(t.tv_sec) * 1000000) + t.tv_usec;429 Result := t.tv_sec + t.tv_usec / 1000000; 433 430 {$ENDIF} 434 431
Note:
See TracChangeset
for help on using the changeset viewer.