Changeset 170 for MicroThreading/UMicroThreading.pas
- Timestamp:
- Feb 21, 2011, 2:00:03 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/UMicroThreading.pas
r169 r170 53 53 TMicroThreadCriticalSection = class 54 54 private 55 FId: Integer; 55 56 FMicroThreads: TObjectList; 56 Lock: TCriticalSection;57 //Lock: TCriticalSection; 57 58 FCounter: Integer; 58 59 public … … 61 62 constructor Create; 62 63 destructor Destroy; override; 64 property Id: Integer read FId; 63 65 end; 64 66 … … 208 210 FThreadPoolSize: Integer; 209 211 FRoundRobinIndex: Integer; 210 FLastId: Integer; 212 FMicroThreadLastId: Integer; 213 FCriticalSectionLastId: Integer; 211 214 FMainThreadTerminated: Boolean; 212 215 FMicroThreads: TObjectList; // TList<TMicroThread> … … 232 235 procedure MainThreadStart(Sender: TObject); 233 236 procedure MainThreadTick(Data: PtrInt); 237 function GetCriticalSectionId: Integer; 234 238 public 235 239 BurstCount: Integer; 236 240 function Add(MicroThread: TMicroThread): Integer; 237 function AddMethod(Method: TProcedureOfObject; WaitForFinish: Boolean = True): Integer; 241 function AddMethod(Method: TProcedureOfObject; 242 WaitForFinish: Boolean = True; ThreadName: string = ''): Integer; 238 243 procedure Remove(MicroThread: TMicroThread; Free: Boolean = True); 239 244 constructor Create; … … 282 287 const 283 288 LogFileName: string = 'Log.txt'; 289 LogEnabled: Boolean = False; 284 290 285 291 implementation … … 350 356 LogFile: TextFile; 351 357 begin 358 if LogEnabled then 352 359 try 353 360 LogLock.Acquire; … … 373 380 try 374 381 MainScheduler.FMicroThreadsLock.Acquire; 375 Lock.Acquire; 382 {$IFDEF DebugCriticalSection} 383 Log('CriticalSection(' + IntToStr(FId) + ') Acquire start, MicroThread: ' + IntToStr(MT.Id) + '(' + MT.Name + ')'); 384 {$ENDIF} 385 //Lock.Acquire; 376 386 Inc(FCounter); 387 {$IFDEF DebugCriticalSection} 388 Log('CriticalSection(' + IntToStr(FId) + ') Acquire Counter: ' + IntToStr(FCounter)); 389 {$ENDIF} 377 390 if FCounter > 1 then begin 378 391 FMicroThreads.Add(MT); … … 380 393 MT.FStatePending := tsBlocked; 381 394 try 382 Lock.Release;395 //Lock.Release; 383 396 MainScheduler.FMicroThreadsLock.Release; 384 397 MT.Yield; 385 398 finally 386 399 MainScheduler.FMicroThreadsLock.Acquire; 387 Lock.Acquire;400 //Lock.Acquire; 388 401 end; 389 402 end; 390 403 finally 391 Lock.Release; 404 {$IFDEF DebugCriticalSection} 405 Log('CriticalSection(' + IntToStr(FId) + ') Acquire end: Id:' + IntToStr(MT.Id) + ' Name:' + MT.Name); 406 {$ENDIF} 407 //Lock.Release; 392 408 MainScheduler.FMicroThreadsLock.Release; 393 409 end else … … 396 412 397 413 procedure TMicroThreadCriticalSection.Release; 414 var 415 MT: TMicroThread; 398 416 begin 399 417 try 400 418 MainScheduler.FMicroThreadsLock.Acquire; 401 Lock.Acquire; 419 {$IFDEF DebugCriticalSection} 420 MT := GetCurrentMicroThread; 421 if Assigned(MT) then 422 Log('CriticalSection(' + IntToStr(FId) + ') Release start: Id:' + IntToStr(MT.Id) + ' Name:' + MT.Name) 423 else Log('CriticalSection(' + IntToStr(FId) + ') Release start: no microthread'); 424 {$ENDIF} 425 //Lock.Acquire; 402 426 Dec(FCounter); 427 {$IFDEF DebugCriticalSection} 428 Log('CriticalSection(' + IntToStr(FId) + ') Release Counter: ' + IntToStr(FCounter)); 429 {$ENDIF} 403 430 if FMicroThreads.Count > 0 then begin 404 431 // Release one waiting micro thread … … 410 437 raise EMicroThreadError.Create(SCriticalSectionDecrement); 411 438 finally 412 Lock.Release; 439 {$IFDEF DebugCriticalSection} 440 Log('CriticalSection(' + IntToStr(FId) + ') Release end: Id:' + IntToStr(MT.Id) + ' Name:' + MT.Name); 441 {$ENDIF} 442 //Lock.Release; 413 443 MainScheduler.FMicroThreadsLock.Release; 414 444 end; … … 417 447 constructor TMicroThreadCriticalSection.Create; 418 448 begin 419 Lock := TCriticalSection.Create;449 //Lock := TCriticalSection.Create; 420 450 FMicroThreads := TObjectList.Create; 421 451 FMicroThreads.OwnsObjects := False; 452 FId := MainScheduler.GetCriticalSectionId; 422 453 end; 423 454 … … 426 457 try 427 458 MainScheduler.FMicroThreadsLock.Acquire; 428 Lock.Acquire;459 //Lock.Acquire; 429 460 430 461 while FMicroThreads.Count > 0 do begin … … 434 465 end; 435 466 finally 436 Lock.Release;467 //Lock.Release; 437 468 MainScheduler.FMicroThreadsLock.Release; 438 469 end; 439 470 FMicroThreads.Free; 440 Lock.Free;471 //Lock.Free; 441 472 inherited Destroy; 442 473 end; … … 488 519 Exit; 489 520 end; 490 FMicroThreads.Add( Self);521 FMicroThreads.Add(MT); 491 522 MT.FBlockTime := NowPrecise + Duration; 492 523 MT.FBlockState := tbsWaitFor; … … 798 829 procedure TMicroThread.Yield; 799 830 begin 800 //if not Assigned(FManager) then801 //raise EMicroThreadError.Create(SManagerReferenceLost);831 if not Assigned(FManager) then 832 raise EMicroThreadError.Create(SManagerReferenceLost); 802 833 if FStatePending = tsNone then 803 834 FStatePending := tsWaiting; … … 896 927 try 897 928 FMicroThreadsLock.Acquire; 898 Inc(FLastId); 929 Inc(FMicroThreadLastId); 930 MicroThread.FId := FMicroThreadLastId; 899 931 MicroThread.FScheduler := Self; 900 MicroThread.FId := FLastId;901 932 Result := FMicroThreads.Add(MicroThread); 902 933 finally … … 905 936 end; 906 937 907 function TMicroThreadScheduler.AddMethod(Method: TProcedureOfObject; WaitForFinish: Boolean): Integer; 938 function TMicroThreadScheduler.AddMethod(Method: TProcedureOfObject; 939 WaitForFinish: Boolean = True; ThreadName: string = ''): Integer; 908 940 var 909 941 NewMicroThread: TMicroThreadSimple; … … 912 944 try 913 945 NewMicroThread := TMicroThreadSimple.Create(True); 946 NewMicroThread.Name := ThreadName; 914 947 NewMicroThread.Method := Method; 915 948 NewMicroThread.FScheduler := Self; … … 1083 1116 // end; 1084 1117 FMainThreadOutsideStart := NowPrecise; 1118 end; 1119 1120 function TMicroThreadScheduler.GetCriticalSectionId: Integer; 1121 begin 1122 Inc(FCriticalSectionLastId); 1123 Result := FCriticalSectionLastId; 1085 1124 end; 1086 1125
Note:
See TracChangeset
for help on using the changeset viewer.