Ignore:
Timestamp:
Jan 27, 2011, 8:05:33 AM (14 years ago)
Author:
george
Message:
  • Modified: Elimination of global variables used by TMicroThreadManager Yield method.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/UMicroThreading.pas

    r150 r151  
    5252    FManager: TMicroThreadManager;
    5353    FId: Integer;
     54    procedure CallExecute;
    5455    function GetStackUsed: Integer;
     56    procedure SetScheduler(const AValue: TMicroThreadScheduler);
    5557  public
    5658    Name: string;
     
    7779      write FFreeOnTerminate;
    7880    property Terminated: Boolean read FTerminated;
    79     property Scheduler: TMicroThreadScheduler read FScheduler;
     81    property Scheduler: TMicroThreadScheduler read FScheduler
     82      write SetScheduler;
    8083    property Manager: TMicroThreadManager read FManager;
    8184    property StackUsed: Integer read GetStackUsed;
     
    112115    FExecutedCount: Integer;
    113116    FTerminated: Boolean;
    114     FTempPointer: Pointer;
    115117    FCurrentMicroThread: TMicroThread;
    116118    FScheduler: TMicroThreadScheduler;
     
    177179implementation
    178180
    179 var
    180   StaticManagers: TObjectList; // TList<TMicroThreadManager>;
    181   StaticManager: TMicroThreadManager;
    182   StaticMicroThread: TMicroThread;
     181//var
     182//  StaticManagers: TObjectList; // TList<TMicroThreadManager>;
     183//  StaticManager: TMicroThreadManager;
     184//  StaticMicroThread: TMicroThread;
    183185
    184186function GetMicroThreadId: Integer;
     
    231233    if FCurrentMicroThread.FState = tsRunning then
    232234      FCurrentMicroThread.FState := tsWaiting;
    233     StaticMicroThread := FCurrentMicroThread;
    234235    asm
    235236      // Store microthread stack
    236       mov eax, StaticMicroThread
     237      mov ecx, Self
     238      mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
    237239      mov edx, esp
    238240      mov [eax].TMicroThread.FStackPointer, edx
    239241      mov edx, ebp
    240242      mov [eax].TMicroThread.FBasePointer, edx
    241     end;
    242     StaticManager := FCurrentMicroThread.FManager;
    243     asm
     243
    244244      // Restore FScheduler stack
    245       mov eax, StaticManager  // Self is invalid before BP restore
    246       mov edx, [eax].TMicroThreadManager.FStackPointer
     245      mov edx, [ecx].TMicroThreadManager.FStackPointer
    247246      mov esp, edx
    248       mov edx, [eax].TMicroThreadManager.FBasePointer
     247      mov edx, [ecx].TMicroThreadManager.FBasePointer
    249248      mov ebp, edx
    250249    end;
     
    270269      FCurrentMicroThread.FState := tsRunning;
    271270      FCurrentMicroThread.FExecutionStartTime := CurrentTime;
    272       StaticMicroThread := FCurrentMicroThread;
    273271      asm
    274272        // Restore microthread stack
    275         mov eax, StaticMicroThread
     273        mov ecx, Self
     274        mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
    276275        mov edx, [eax].TMicroThread.FStackPointer
    277276        mov ecx, esp
     
    281280        mov edx, [eax].TMicroThread.FBasePointer
    282281        mov ebp, edx
    283       end;
    284       StaticMicroThread.Execute;
    285       asm
     282        // We want to call virtual method Execute
     283        // but virtual methods can be called only statically
     284        // Then static method CallExecute is calling virtual method Execute
     285        call TMicroThread.CallExecute
     286//      end;
     287//      StaticMicroThread.Execute;
     288//      asm
    286289        pop edx
    287290        pop ebp
     
    320323      FCurrentMicroThread.FState := tsRunning;
    321324      FCurrentMicroThread.FExecutionStartTime := CurrentTime;
    322       FTempPointer := FCurrentMicroThread.FStackPointer;
    323325      asm
    324326        // Restore microthread stack
    325         mov eax, Self
    326         mov edx, [eax].TMicroThreadManager.FTempPointer
     327        mov ecx, Self
     328        mov eax, [ecx].TMicroThreadManager.FCurrentMicroThread
     329        mov edx, [eax].TMicroThread.FStackPointer
    327330        mov esp, edx
    328       end;
    329       FTempPointer := FCurrentMicroThread.FBasePointer;
    330       asm
    331         mov eax, Self
    332         mov edx, [eax].TMicroThreadManager.FTempPointer
     331        mov edx, [eax].TMicroThread.FBasePointer
    333332        mov ebp, edx
    334333      end;
     
    391390{ TMicroThread }
    392391
     392procedure TMicroThread.CallExecute;
     393begin
     394  Execute;
     395end;
     396
    393397function TMicroThread.GetStackUsed: Integer;
    394398begin
    395399  Result := FStack + FStackSize - FStackPointer;
     400end;
     401
     402procedure TMicroThread.SetScheduler(const AValue: TMicroThreadScheduler);
     403begin
     404  FScheduler := AValue;
    396405end;
    397406
     
    435444  FStack := GetMem(FStackSize);
    436445  FBasePointer := FStack + FStackSize;
    437   FStackPointer := FBasePointer - 20;
     446  FStackPointer := FBasePointer - SizeOf(Pointer);
    438447  FExecutionTime := 0;
    439448  FTerminated := False;
     
    676685initialization
    677686
    678 StaticManagers := TObjectList.Create;
     687//StaticManagers := TObjectList.Create;
    679688MainScheduler := TMicroThreadScheduler.Create;
    680689
     
    682691
    683692MainScheduler.Free;
    684 StaticManagers.Free;
     693//StaticManagers.Free;
    685694
    686695end.
Note: See TracChangeset for help on using the changeset viewer.