Ignore:
Timestamp:
Feb 8, 2011, 11:05:30 AM (14 years ago)
Author:
george
Message:
  • Modified: Optimalized microthread execution from main thread.
  • Fixed: Passthrough of WaitFor on already signaled event.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/UMicroThreading.pas

    r162 r164  
    2424
    2525const
    26   DefaultStackSize = $4000;
     26  DefaultStackSize = $8000;
    2727
    2828resourcestring
     
    210210    FMainThreadStarter: TTimer;
    211211    FEvents: TObjectList;
     212    FMainThreadOutsideStart: TDateTime;
     213    FMainThreadOutsideDuration: TDateTime;
    212214    function GetMicroThreadCount: Integer;
    213215    function GetThreadPoolCount: Integer;
     
    241243    property Active: Boolean read FActive write SetActive;
    242244    property UseMainThread: Boolean read FUseMainThread write SetUseMainThread;
     245    property MainThreadOutsideDuration: TDateTime read FMainThreadOutsideDuration;
    243246  end;
    244247
     
    339342  MT := GetCurrentMicroThread;
    340343  if Assigned(MT) then Result := MT.WaitForEvent(Event, Duration)
    341     else raise Exception.Create(SNotInThread);
     344    else begin
     345      while not Event.Signaled do begin
     346        Sleep(1);
     347        Application.ProcessMessages;
     348      end;
     349      //raise Exception.Create(SNotInThread);
    342350//    else Result := Event.WaitFor(Trunc(Duration / OneMillisecond));
     351    end;
    343352end;
    344353
     
    487496function TMicroThreadManager.Execute(Count: Integer): Integer;
    488497begin
    489   FLoopStart := NowPrecise;
     498  //FLoopStart := NowPrecise;
    490499  FStack := StackBottom;
    491500  FStackSize := StackBottom + StackLength;
     
    494503  Yield;
    495504  Result := FExecutedCount;
    496   FLoopDuration := NowPrecise - FLoopStart;
     505  //FLoopDuration := NowPrecise - FLoopStart;
    497506end;
    498507
     
    615624begin
    616625  if Assigned(FThread) then
    617     FThread.Synchronize(FThread, AMethod);
     626    FThread.Synchronize(FThread, AMethod)
     627    else AMethod;
    618628end;
    619629
     
    744754procedure TMicroThread.WaitFor;
    745755begin
    746   if GetMicroThreadId <> -1 then
    747   while not ((FState = tsBlocked) and (FBlockState = tbsTerminated)) do begin
    748     MTSleep(1);
     756  if GetMicroThreadId <> -1 then begin
     757    // Called from another microthread
     758    while not ((FState = tsBlocked) and (FBlockState = tbsTerminated)) do begin
     759      MTSleep(1);
     760    end;
     761  end else begin
     762    // Called directly from main thread
     763    while not ((FState = tsBlocked) and (FBlockState = tbsTerminated)) do begin
     764      Sleep(1);
     765      Application.ProcessMessages;
     766    end;
    749767  end;
    750768end;
     
    762780  try
    763781    Event.FMicroThreadsLock.Acquire;
     782    if Event.Signaled then begin
     783      Result := wrSignaled;
     784      Exit;
     785    end;
    764786    Event.FMicroThreads.Add(Self);
    765787    FBlockTime := NowPrecise + Duration;
     
    890912  FMainThreadManager.FScheduler := Self;
    891913  UseMainThread := False;
    892   BurstCount := 100;
     914  BurstCount := 50;
    893915end;
    894916
     
    9841006begin
    9851007//  try
    986     Duration := 100 * OneMillisecond;
    987     StartTime := NowPrecise;
     1008    FMainThreadOutsideDuration := NowPrecise - FMainThreadOutsideStart;
     1009    BurstCount := 1;
     1010    Duration := 50 * OneMillisecond;
     1011    StartTime := Now;
    9881012    Executed := -1;
    989     while (Executed <> 0) and ((NowPrecise - StartTime) < Duration) do begin
     1013    while (Executed <> 0) and ((Now - StartTime) < Duration) do begin
    9901014      Executed := FMainThreadManager.Execute(BurstCount);
    9911015    end;
     
    9991023//    raise;
    10001024//  end;
     1025  FMainThreadOutsideStart := NowPrecise;
    10011026end;
    10021027
Note: See TracChangeset for help on using the changeset viewer.