Changeset 151 for MicroThreading


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.
Location:
MicroThreading
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/Demo.lpi

    r150 r151  
    4747        <TopLine Value="1"/>
    4848        <CursorPos X="5" Y="9"/>
    49         <UsageCount Value="59"/>
     49        <UsageCount Value="62"/>
    5050      </Unit0>
    5151      <Unit1>
     
    5555        <ResourceBaseClass Value="Form"/>
    5656        <UnitName Value="UMainForm"/>
    57         <IsVisibleTab Value="True"/>
    5857        <EditorIndex Value="0"/>
    5958        <WindowIndex Value="0"/>
    60         <TopLine Value="249"/>
    61         <CursorPos X="22" Y="267"/>
    62         <UsageCount Value="59"/>
     59        <TopLine Value="252"/>
     60        <CursorPos X="1" Y="274"/>
     61        <UsageCount Value="62"/>
    6362        <Loaded Value="True"/>
    6463        <LoadedDesigner Value="True"/>
     
    6766        <Filename Value="../UMicroThreading.pas"/>
    6867        <UnitName Value="UMicroThreading"/>
     68        <IsVisibleTab Value="True"/>
    6969        <EditorIndex Value="1"/>
    7070        <WindowIndex Value="0"/>
    71         <TopLine Value="271"/>
    72         <CursorPos X="1" Y="286"/>
    73         <UsageCount Value="33"/>
     71        <TopLine Value="258"/>
     72        <CursorPos X="1" Y="271"/>
     73        <UsageCount Value="34"/>
    7474        <Loaded Value="True"/>
    7575      </Unit2>
     
    406406        <Filename Value="../UPlatform.pas"/>
    407407        <UnitName Value="UPlatform"/>
    408         <EditorIndex Value="3"/>
     408        <EditorIndex Value="2"/>
    409409        <WindowIndex Value="0"/>
    410410        <TopLine Value="1"/>
    411411        <CursorPos X="24" Y="9"/>
    412         <UsageCount Value="13"/>
     412        <UsageCount Value="14"/>
    413413        <Loaded Value="True"/>
    414414      </Unit48>
     
    430430      <Unit51>
    431431        <Filename Value="../../../../lazarus/lcl/interfaces/gtk2/gtk2widgetset.inc"/>
    432         <EditorIndex Value="2"/>
    433432        <WindowIndex Value="0"/>
    434433        <TopLine Value="2380"/>
    435434        <CursorPos X="30" Y="2390"/>
    436435        <UsageCount Value="10"/>
    437         <Loaded Value="True"/>
    438436      </Unit51>
    439437      <Unit52>
     
    477475      <Position1>
    478476        <Filename Value="../UMicroThreading.pas"/>
    479         <Caret Line="308" Column="1" TopLine="284"/>
     477        <Caret Line="257" Column="1" TopLine="241"/>
    480478      </Position1>
    481479      <Position2>
    482480        <Filename Value="../UMicroThreading.pas"/>
    483         <Caret Line="309" Column="1" TopLine="285"/>
     481        <Caret Line="258" Column="1" TopLine="241"/>
    484482      </Position2>
    485483      <Position3>
    486484        <Filename Value="../UMicroThreading.pas"/>
    487         <Caret Line="310" Column="1" TopLine="286"/>
     485        <Caret Line="261" Column="1" TopLine="241"/>
    488486      </Position3>
    489487      <Position4>
    490488        <Filename Value="../UMicroThreading.pas"/>
    491         <Caret Line="312" Column="1" TopLine="288"/>
     489        <Caret Line="321" Column="13" TopLine="311"/>
    492490      </Position4>
    493491      <Position5>
    494492        <Filename Value="../UMicroThreading.pas"/>
    495         <Caret Line="315" Column="1" TopLine="291"/>
     493        <Caret Line="116" Column="1" TopLine="103"/>
    496494      </Position5>
    497495      <Position6>
    498496        <Filename Value="../UMicroThreading.pas"/>
    499         <Caret Line="338" Column="1" TopLine="323"/>
     497        <Caret Line="326" Column="1" TopLine="307"/>
    500498      </Position6>
    501499      <Position7>
    502500        <Filename Value="../UMicroThreading.pas"/>
    503         <Caret Line="218" Column="1" TopLine="203"/>
     501        <Caret Line="183" Column="3" TopLine="159"/>
    504502      </Position7>
    505503      <Position8>
    506504        <Filename Value="../UMicroThreading.pas"/>
    507         <Caret Line="219" Column="1" TopLine="203"/>
     505        <Caret Line="281" Column="40" TopLine="266"/>
    508506      </Position8>
    509507      <Position9>
    510508        <Filename Value="../UMicroThreading.pas"/>
    511         <Caret Line="360" Column="1" TopLine="345"/>
     509        <Caret Line="183" Column="1" TopLine="172"/>
    512510      </Position9>
    513511      <Position10>
    514512        <Filename Value="../UMicroThreading.pas"/>
    515         <Caret Line="290" Column="25" TopLine="275"/>
     513        <Caret Line="683" Column="3" TopLine="660"/>
    516514      </Position10>
    517515      <Position11>
    518516        <Filename Value="../UMicroThreading.pas"/>
    519         <Caret Line="291" Column="28" TopLine="276"/>
     517        <Caret Line="273" Column="21" TopLine="260"/>
    520518      </Position11>
    521519      <Position12>
    522520        <Filename Value="../UMicroThreading.pas"/>
    523         <Caret Line="286" Column="1" TopLine="276"/>
     521        <Caret Line="281" Column="1" TopLine="269"/>
    524522      </Position12>
    525523      <Position13>
    526524        <Filename Value="../UMicroThreading.pas"/>
    527         <Caret Line="301" Column="1" TopLine="277"/>
     525        <Caret Line="282" Column="40" TopLine="269"/>
    528526      </Position13>
    529527      <Position14>
    530528        <Filename Value="../UMicroThreading.pas"/>
    531         <Caret Line="286" Column="1" TopLine="277"/>
     529        <Caret Line="281" Column="44" TopLine="269"/>
    532530      </Position14>
    533531      <Position15>
    534532        <Filename Value="../UMicroThreading.pas"/>
    535         <Caret Line="287" Column="1" TopLine="277"/>
     533        <Caret Line="282" Column="1" TopLine="269"/>
    536534      </Position15>
    537535      <Position16>
    538536        <Filename Value="../UMicroThreading.pas"/>
    539         <Caret Line="288" Column="1" TopLine="277"/>
     537        <Caret Line="281" Column="3" TopLine="269"/>
    540538      </Position16>
    541539      <Position17>
    542540        <Filename Value="../UMicroThreading.pas"/>
    543         <Caret Line="301" Column="1" TopLine="277"/>
     541        <Caret Line="282" Column="17" TopLine="263"/>
    544542      </Position17>
    545543      <Position18>
    546544        <Filename Value="../UMicroThreading.pas"/>
    547         <Caret Line="302" Column="1" TopLine="278"/>
     545        <Caret Line="402" Column="1" TopLine="389"/>
    548546      </Position18>
    549547      <Position19>
    550548        <Filename Value="../UMicroThreading.pas"/>
    551         <Caret Line="303" Column="1" TopLine="279"/>
     549        <Caret Line="270" Column="48" TopLine="262"/>
    552550      </Position19>
    553551      <Position20>
    554552        <Filename Value="../UMicroThreading.pas"/>
    555         <Caret Line="304" Column="1" TopLine="280"/>
     553        <Caret Line="285" Column="1" TopLine="263"/>
    556554      </Position20>
    557555      <Position21>
    558556        <Filename Value="../UMicroThreading.pas"/>
    559         <Caret Line="305" Column="1" TopLine="281"/>
     557        <Caret Line="282" Column="19" TopLine="269"/>
    560558      </Position21>
    561559      <Position22>
    562560        <Filename Value="../UMicroThreading.pas"/>
    563         <Caret Line="306" Column="1" TopLine="282"/>
     561        <Caret Line="54" Column="26" TopLine="45"/>
    564562      </Position22>
    565563      <Position23>
    566564        <Filename Value="../UMicroThreading.pas"/>
    567         <Caret Line="308" Column="1" TopLine="284"/>
     565        <Caret Line="283" Column="1" TopLine="270"/>
    568566      </Position23>
    569567      <Position24>
    570568        <Filename Value="../UMicroThreading.pas"/>
    571         <Caret Line="309" Column="1" TopLine="285"/>
     569        <Caret Line="394" Column="1" TopLine="381"/>
    572570      </Position24>
    573571      <Position25>
    574572        <Filename Value="../UMicroThreading.pas"/>
    575         <Caret Line="310" Column="1" TopLine="286"/>
     573        <Caret Line="385" Column="1" TopLine="380"/>
    576574      </Position25>
    577575      <Position26>
    578576        <Filename Value="../UMicroThreading.pas"/>
    579         <Caret Line="311" Column="1" TopLine="287"/>
     577        <Caret Line="386" Column="1" TopLine="380"/>
    580578      </Position26>
    581579      <Position27>
    582         <Filename Value="../UMicroThreading.pas"/>
    583         <Caret Line="313" Column="1" TopLine="289"/>
     580        <Filename Value="UMainForm.pas"/>
     581        <Caret Line="274" Column="1" TopLine="252"/>
    584582      </Position27>
    585583      <Position28>
    586584        <Filename Value="../UMicroThreading.pas"/>
    587         <Caret Line="316" Column="1" TopLine="292"/>
     585        <Caret Line="287" Column="43" TopLine="269"/>
    588586      </Position28>
    589587      <Position29>
    590588        <Filename Value="../UMicroThreading.pas"/>
    591         <Caret Line="339" Column="1" TopLine="324"/>
     589        <Caret Line="286" Column="1" TopLine="269"/>
    592590      </Position29>
    593591      <Position30>
    594592        <Filename Value="../UMicroThreading.pas"/>
    595         <Caret Line="286" Column="1" TopLine="271"/>
     593        <Caret Line="181" Column="3" TopLine="160"/>
    596594      </Position30>
    597595    </JumpHistory>
     
    631629  </CompilerOptions>
    632630  <Debugging>
    633     <BreakPoints Count="1">
    634       <Item1>
    635         <Source Value="../UMicroThreading.pas"/>
    636         <Line Value="285"/>
    637       </Item1>
    638     </BreakPoints>
    639631    <Exceptions Count="3">
    640632      <Item1>
  • MicroThreading/Demo/UMainForm.lfm

    r150 r151  
    11object MainForm: TMainForm
    2   Left = 232
    3   Height = 557
    4   Top = 116
    5   Width = 863
     2  Left = 224
     3  Height = 533
     4  Top = 98
     5  Width = 775
    66  Caption = 'MicroThreading demo'
    7   ClientHeight = 557
    8   ClientWidth = 863
     7  ClientHeight = 533
     8  ClientWidth = 775
    99  OnClose = FormClose
    1010  OnCreate = FormCreate
     
    1414  object PageControl1: TPageControl
    1515    Left = 8
    16     Height = 543
     16    Height = 519
    1717    Top = 8
    18     Width = 849
    19     ActivePage = TabSheet2
     18    Width = 761
     19    ActivePage = TabSheet1
    2020    Anchors = [akTop, akLeft, akRight, akBottom]
    21     TabIndex = 1
     21    TabIndex = 0
    2222    TabOrder = 0
    2323    object TabSheet1: TTabSheet
    2424      Caption = 'Job control'
    25       ClientHeight = 516
    26       ClientWidth = 845
     25      ClientHeight = 493
     26      ClientWidth = 753
    2727      object ButtonAddWorkers: TButton
    2828        Left = 10
     
    3737        Left = 10
    3838        Height = 25
    39         Top = 37
    40         Width = 140
     39        Top = 74
     40        Width = 104
    4141        Caption = 'Clear jobs'
    4242        OnClick = ButtonClearMicroThreadsClick
     
    4444      end
    4545      object Label4: TLabel
    46         Left = 120
     46        Left = 124
    4747        Height = 14
    4848        Top = 14
    49         Width = 104
    50         Caption = 'Microthread count:'
     49        Width = 34
     50        Caption = 'Count:'
    5151        ParentColor = False
    5252      end
    5353      object SpinEdit1: TSpinEdit
    54         Left = 224
     54        Left = 204
    5555        Height = 21
    5656        Top = 11
     
    6262      end
    6363      object Memo1: TMemo
    64         Left = 382
    65         Height = 267
     64        Left = 316
     65        Height = 244
    6666        Top = 14
    6767        Width = 294
     
    7171      end
    7272      object Button1: TButton
    73         Left = 14
    74         Height = 25
    75         Top = 94
     73        Left = 12
     74        Height = 25
     75        Top = 138
    7676        Width = 75
    7777        Caption = 'Clear memo'
     
    7979        TabOrder = 4
    8080      end
     81      object Label12: TLabel
     82        Left = 123
     83        Height = 14
     84        Top = 38
     85        Width = 52
     86        Caption = 'Iterations:'
     87        ParentColor = False
     88      end
     89      object SpinEdit3: TSpinEdit
     90        Left = 204
     91        Height = 21
     92        Top = 36
     93        Width = 74
     94        MaxValue = 1000000000
     95        OnChange = SpinEdit3Change
     96        TabOrder = 5
     97        Value = 100000
     98      end
    8199    end
    82100    object TabSheet2: TTabSheet
    83101      Caption = 'Scheduler state'
    84       ClientHeight = 516
    85       ClientWidth = 845
     102      ClientHeight = 493
     103      ClientWidth = 753
    86104      object ListView1: TListView
    87105        Left = 222
    88         Height = 483
     106        Height = 460
    89107        Top = 24
    90         Width = 569
     108        Width = 477
    91109        Anchors = [akTop, akLeft, akRight, akBottom]
    92110        Columns = <       
     
    130148        Height = 14
    131149        Top = 7
    132         Width = 85
     150        Width = 74
    133151        Caption = 'Microthread list'
    134152        ParentColor = False
     
    140158        Width = 205
    141159        Caption = 'State'
    142         ClientHeight = 66
     160        ClientHeight = 63
    143161        ClientWidth = 201
    144162        TabOrder = 1
     
    147165          Height = 14
    148166          Top = 4
    149           Width = 135
     167          Width = 117
    150168          Caption = 'Logical processor count:'
    151169          ParentColor = False
     
    163181          Height = 14
    164182          Top = 20
    165           Width = 78
     183          Width = 69
    166184          Caption = 'Thread count:'
    167185          ParentColor = False
     
    171189          Height = 14
    172190          Top = 36
    173           Width = 107
     191          Width = 95
    174192          Caption = 'Micro thread count:'
    175193          ParentColor = False
     
    198216        Width = 206
    199217        Caption = 'Control'
    200         ClientHeight = 111
     218        ClientHeight = 108
    201219        ClientWidth = 202
    202220        TabOrder = 2
     
    214232          Height = 14
    215233          Top = 46
    216           Width = 78
     234          Width = 69
    217235          Caption = 'Thread count:'
    218236          ParentColor = False
     
    230248    object TabSheet3: TTabSheet
    231249      Caption = 'Testing'
    232       ClientHeight = 516
    233       ClientWidth = 845
     250      ClientHeight = 493
     251      ClientWidth = 753
    234252      object ButtonShowThreadId: TButton
    235253        Left = 14
     
    271289        Height = 14
    272290        Top = 135
    273         Width = 81
     291        Width = 70
    274292        Caption = 'Precision time:'
    275293        ParentColor = False
  • MicroThreading/Demo/UMainForm.pas

    r150 r151  
    2626    Label10: TLabel;
    2727    Label11: TLabel;
     28    Label12: TLabel;
    2829    Label2: TLabel;
    2930    Label3: TLabel;
     
    3940    SpinEdit1: TSpinEdit;
    4041    SpinEdit2: TSpinEdit;
     42    SpinEdit3: TSpinEdit;
    4143    TabSheet1: TTabSheet;
    4244    TabSheet2: TTabSheet;
     
    5759    procedure ListView1Data(Sender: TObject; Item: TListItem);
    5860    procedure SpinEdit2Change(Sender: TObject);
     61    procedure SpinEdit3Change(Sender: TObject);
    5962    procedure TimerRedrawTimer(Sender: TObject);
    6063    procedure TimerSchedulerStartTimer(Sender: TObject);
     
    6265    procedure Worker(MicroThread: TMicroThread);
    6366  public
     67    Iterations: Integer;
    6468    Scheduler: TMicroThreadScheduler;
    6569  end;
     
    209213procedure TMainForm.FormShow(Sender: TObject);
    210214begin
     215  Iterations := SpinEdit3.Value;
    211216end;
    212217
     
    236241end;
    237242
     243procedure TMainForm.SpinEdit3Change(Sender: TObject);
     244begin
     245  Iterations := SpinEdit3.Value;
     246end;
     247
    238248procedure TMainForm.TimerRedrawTimer(Sender: TObject);
    239249begin
    240   ListView1.Items.Count := Scheduler.MicroThreadCount;
     250  if ListView1.Items.Count <> Scheduler.MicroThreadCount then
     251    ListView1.Items.Count := Scheduler.MicroThreadCount;
    241252  ListView1.Items[-1];
    242253  ListView1.Refresh;
     
    258269  I: Integer;
    259270  Q: Integer;
    260 const
    261   TotalSteps = 100;
    262271begin
    263272  with MicroThread do begin
    264273    //Memo1.Lines.Add('Worker ' + IntToStr(Id));
    265     for I := 0 to TotalSteps - 1 do begin
     274    for I := 0 to Iterations - 1 do begin
    266275      Q := 0;
    267       while Q < 10000 do Inc(Q);
     276      while Q < 100 do Inc(Q);
    268277      //Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' +
    269278      //  FloatToStr(ExecutionTime));
    270       Completion := I / TotalSteps;
     279      Completion := I / Iterations;
    271280      //Sleep(1 * Id * OneMillisecond);
    272281      Yield;
  • 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.