Changeset 145


Ignore:
Timestamp:
Jan 25, 2011, 8:51:57 PM (14 years ago)
Author:
george
Message:
  • Modified: Yield method of micro thread manager now does'n accept micro thread as parameter and instead get current running micro thread from own field CurrentMicroThread.
  • Fixed: Calculating high precision time on linux.
Location:
MicroThreading
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/Demo.lpi

    r144 r145  
    5050        <TopLine Value="1"/>
    5151        <CursorPos X="1" Y="15"/>
    52         <UsageCount Value="46"/>
     52        <UsageCount Value="47"/>
    5353      </Unit0>
    5454      <Unit1>
     
    5858        <ResourceBaseClass Value="Form"/>
    5959        <UnitName Value="UMainForm"/>
     60        <IsVisibleTab Value="True"/>
    6061        <EditorIndex Value="0"/>
    6162        <WindowIndex Value="0"/>
    62         <TopLine Value="182"/>
    63         <CursorPos X="28" Y="184"/>
    64         <UsageCount Value="46"/>
     63        <TopLine Value="84"/>
     64        <CursorPos X="3" Y="88"/>
     65        <UsageCount Value="47"/>
    6566        <Loaded Value="True"/>
    6667        <LoadedDesigner Value="True"/>
     
    6970        <Filename Value="../UMicroThreading.pas"/>
    7071        <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"/>
    7777        <Loaded Value="True"/>
    7878      </Unit2>
     
    159159      <Unit13>
    160160        <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"/>
    165167      </Unit13>
    166168      <Unit14>
    167169        <Filename Value="../../../../lazarus/lcl/include/buttoncontrol.inc"/>
     170        <EditorIndex Value="3"/>
    168171        <WindowIndex Value="0"/>
    169172        <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"/>
    172176      </Unit14>
    173177      <Unit15>
    174178        <Filename Value="../../../../lazarus/lcl/include/control.inc"/>
     179        <EditorIndex Value="2"/>
    175180        <WindowIndex Value="0"/>
    176181        <TopLine Value="2271"/>
    177         <CursorPos X="4" Y="2274"/>
     182        <CursorPos X="19" Y="2274"/>
    178183        <UsageCount Value="11"/>
     184        <Loaded Value="True"/>
    179185      </Unit15>
    180186      <Unit16>
     
    289295      <Unit31>
    290296        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/sysosh.inc"/>
    291         <EditorIndex Value="3"/>
    292297        <WindowIndex Value="0"/>
    293298        <TopLine Value="12"/>
    294299        <CursorPos X="3" Y="25"/>
    295300        <UsageCount Value="13"/>
    296         <Loaded Value="True"/>
    297301      </Unit31>
    298302      <Unit32>
     
    354358      <Unit40>
    355359        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/systemh.inc"/>
    356         <EditorIndex Value="1"/>
    357360        <WindowIndex Value="0"/>
    358361        <TopLine Value="492"/>
    359362        <CursorPos X="3" Y="504"/>
    360363        <UsageCount Value="10"/>
    361         <Loaded Value="True"/>
    362364      </Unit40>
    363365      <Unit41>
    364366        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/win/syswin.inc"/>
    365         <EditorIndex Value="5"/>
    366367        <WindowIndex Value="0"/>
    367368        <TopLine Value="197"/>
    368369        <CursorPos X="30" Y="208"/>
    369370        <UsageCount Value="10"/>
    370         <Loaded Value="True"/>
    371371      </Unit41>
    372372      <Unit42>
    373373        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/system.inc"/>
    374         <EditorIndex Value="4"/>
    375374        <WindowIndex Value="0"/>
    376375        <TopLine Value="737"/>
    377376        <CursorPos X="21" Y="747"/>
    378377        <UsageCount Value="10"/>
    379         <Loaded Value="True"/>
    380378      </Unit42>
    381379      <Unit43>
    382380        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/inc/innr.inc"/>
    383         <EditorIndex Value="2"/>
    384381        <WindowIndex Value="0"/>
    385382        <TopLine Value="96"/>
    386383        <CursorPos X="1" Y="1"/>
    387384        <UsageCount Value="10"/>
    388         <Loaded Value="True"/>
    389385      </Unit43>
    390386    </Units>
     
    392388      <Position1>
    393389        <Filename Value="../UMicroThreading.pas"/>
    394         <Caret Line="322" Column="1" TopLine="315"/>
     390        <Caret Line="233" Column="1" TopLine="209"/>
    395391      </Position1>
    396392      <Position2>
    397393        <Filename Value="../UMicroThreading.pas"/>
    398         <Caret Line="328" Column="1" TopLine="315"/>
     394        <Caret Line="234" Column="1" TopLine="210"/>
    399395      </Position2>
    400396      <Position3>
    401397        <Filename Value="../UMicroThreading.pas"/>
    402         <Caret Line="363" Column="3" TopLine="350"/>
     398        <Caret Line="235" Column="1" TopLine="211"/>
    403399      </Position3>
    404400      <Position4>
    405401        <Filename Value="../UMicroThreading.pas"/>
    406         <Caret Line="322" Column="1" TopLine="309"/>
     402        <Caret Line="236" Column="1" TopLine="212"/>
    407403      </Position4>
    408404      <Position5>
    409405        <Filename Value="../UMicroThreading.pas"/>
    410         <Caret Line="326" Column="20" TopLine="309"/>
     406        <Caret Line="237" Column="1" TopLine="213"/>
    411407      </Position5>
    412408      <Position6>
    413409        <Filename Value="../UMicroThreading.pas"/>
    414         <Caret Line="169" Column="1" TopLine="156"/>
     410        <Caret Line="239" Column="1" TopLine="215"/>
    415411      </Position6>
    416412      <Position7>
    417413        <Filename Value="../UMicroThreading.pas"/>
    418         <Caret Line="363" Column="1" TopLine="350"/>
     414        <Caret Line="240" Column="1" TopLine="216"/>
    419415      </Position7>
    420416      <Position8>
    421417        <Filename Value="../UMicroThreading.pas"/>
    422         <Caret Line="322" Column="1" TopLine="309"/>
     418        <Caret Line="241" Column="1" TopLine="217"/>
    423419      </Position8>
    424420      <Position9>
    425421        <Filename Value="../UMicroThreading.pas"/>
    426         <Caret Line="330" Column="1" TopLine="309"/>
     422        <Caret Line="242" Column="1" TopLine="218"/>
    427423      </Position9>
    428424      <Position10>
    429425        <Filename Value="../UMicroThreading.pas"/>
    430         <Caret Line="316" Column="1" TopLine="309"/>
     426        <Caret Line="243" Column="1" TopLine="219"/>
    431427      </Position10>
    432428      <Position11>
    433429        <Filename Value="../UMicroThreading.pas"/>
    434         <Caret Line="169" Column="1" TopLine="156"/>
     430        <Caret Line="246" Column="1" TopLine="222"/>
    435431      </Position11>
    436432      <Position12>
    437433        <Filename Value="../UMicroThreading.pas"/>
    438         <Caret Line="316" Column="1" TopLine="303"/>
     434        <Caret Line="247" Column="1" TopLine="223"/>
    439435      </Position12>
    440436      <Position13>
    441437        <Filename Value="../UMicroThreading.pas"/>
    442         <Caret Line="330" Column="16" TopLine="317"/>
     438        <Caret Line="248" Column="1" TopLine="224"/>
    443439      </Position13>
    444440      <Position14>
    445441        <Filename Value="../UMicroThreading.pas"/>
    446         <Caret Line="363" Column="1" TopLine="350"/>
     442        <Caret Line="249" Column="1" TopLine="225"/>
    447443      </Position14>
    448444      <Position15>
    449         <Filename Value="UMainForm.pas"/>
    450         <Caret Line="228" Column="1" TopLine="208"/>
     445        <Filename Value="../UMicroThreading.pas"/>
     446        <Caret Line="250" Column="1" TopLine="226"/>
    451447      </Position15>
    452448      <Position16>
    453         <Filename Value="UMainForm.pas"/>
    454         <Caret Line="235" Column="1" TopLine="215"/>
     449        <Filename Value="../UMicroThreading.pas"/>
     450        <Caret Line="252" Column="1" TopLine="228"/>
    455451      </Position16>
    456452      <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"/>
    459455      </Position17>
    460456      <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"/>
    463459      </Position18>
    464460      <Position19>
    465461        <Filename Value="../UMicroThreading.pas"/>
    466         <Caret Line="527" Column="78" TopLine="504"/>
     462        <Caret Line="188" Column="1" TopLine="173"/>
    467463      </Position19>
    468464      <Position20>
    469465        <Filename Value="../UMicroThreading.pas"/>
    470         <Caret Line="531" Column="51" TopLine="513"/>
     466        <Caret Line="217" Column="1" TopLine="192"/>
    471467      </Position20>
    472468      <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"/>
    475471      </Position21>
    476472      <Position22>
    477473        <Filename Value="../UMicroThreading.pas"/>
    478         <Caret Line="531" Column="46" TopLine="522"/>
     474        <Caret Line="268" Column="7" TopLine="242"/>
    479475      </Position22>
    480476      <Position23>
    481477        <Filename Value="../UMicroThreading.pas"/>
    482         <Caret Line="161" Column="27" TopLine="158"/>
     478        <Caret Line="217" Column="1" TopLine="202"/>
    483479      </Position23>
    484480      <Position24>
    485481        <Filename Value="../UMicroThreading.pas"/>
    486         <Caret Line="195" Column="43" TopLine="180"/>
     482        <Caret Line="188" Column="1" TopLine="173"/>
    487483      </Position24>
    488484      <Position25>
    489485        <Filename Value="../UMicroThreading.pas"/>
    490         <Caret Line="211" Column="43" TopLine="198"/>
     486        <Caret Line="480" Column="1" TopLine="465"/>
    491487      </Position25>
    492488      <Position26>
    493489        <Filename Value="../UMicroThreading.pas"/>
    494         <Caret Line="241" Column="45" TopLine="226"/>
     490        <Caret Line="188" Column="1" TopLine="173"/>
    495491      </Position26>
    496492      <Position27>
    497493        <Filename Value="../UMicroThreading.pas"/>
    498         <Caret Line="542" Column="1" TopLine="524"/>
     494        <Caret Line="292" Column="1" TopLine="254"/>
    499495      </Position27>
    500496      <Position28>
    501497        <Filename Value="../UMicroThreading.pas"/>
    502         <Caret Line="538" Column="39" TopLine="525"/>
     498        <Caret Line="389" Column="3" TopLine="387"/>
    503499      </Position28>
    504500      <Position29>
    505501        <Filename Value="../UMicroThreading.pas"/>
    506         <Caret Line="181" Column="1" TopLine="155"/>
     502        <Caret Line="384" Column="9" TopLine="383"/>
    507503      </Position29>
    508504      <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"/>
    511507      </Position30>
    512508    </JumpHistory>
     
    533529  </CompilerOptions>
    534530  <Debugging>
    535     <BreakPoints Count="5">
     531    <BreakPoints Count="7">
    536532      <Item1>
    537533        <Source Value="../Coroutine.pas"/>
     
    554550        <Line Value="145"/>
    555551      </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>
    556560    </BreakPoints>
    557561    <Exceptions Count="3">
  • MicroThreading/Demo/Demo.lpr

    r133 r145  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UMainForm, microthreading, ExceptionLogger
     10  Forms, UMainForm, MicroThreading, ExceptionLogger
    1111  { you can add units after this };
    1212
  • MicroThreading/Demo/UMainForm.lfm

    r144 r145  
    5858    Height = 14
    5959    Top = 8
    60     Width = 74
     60    Width = 85
    6161    Caption = 'Microthread list'
    6262    ParentColor = False
     
    9393    Height = 14
    9494    Top = 80
    95     Width = 32
     95    Width = 38
    9696    Caption = 'Label2'
    9797    ParentColor = False
  • MicroThreading/Demo/UMainForm.pas

    r144 r145  
    8888  if Button1.Caption = 'Start scheduler' then begin
    8989    Button1.Caption := 'Stop scheduler';
    90     Scheduler.MicroThreads.Clear;
    9190    Memo1.Clear;
    9291    Scheduler.Start;
     
    159158  I: Integer;
    160159begin
     160  Scheduler.MicroThreads.Clear;
    161161  for I := 0 to SpinEdit1.Value do
    162162    Scheduler.AddMethod(Worker);
  • MicroThreading/UMicroThreading.pas

    r144 r145  
    9696    FStackPointer: Pointer;
    9797    FBasePointer: Pointer;
    98     FSelected: TMicroThread;
    9998    FExecuteCount: Integer;
    10099    FExecutedCount: Integer;
     
    105104    Scheduler: TMicroThreadScheduler;
    106105    CurrentMicroThread: TMicroThread;
    107     procedure Yield(MicroThread: TMicroThread);
     106    procedure Yield;
    108107    constructor Create;
    109108    destructor Destroy; override;
     
    154153  StaticMicroThread: TMicroThread;
    155154
    156   function GetMicroThreadId: Integer;
    157   var
    158     I: Integer;
    159     CurrentStack: Pointer;
    160   begin
    161     asm
    162       mov CurrentStack, sp
    163     end;
    164     with MainScheduler do begin
    165       try
    166         Lock.Acquire;
    167         I := 0;
    168         while (I < MicroThreads.Count) and
    169           not ((CurrentStack >= TMicroThread(MicroThreads[I]).FStack) and
    170           (CurrentStack <= (TMicroThread(MicroThreads[I]).FStack +
    171           TMicroThread(MicroThreads[I]).FStackSize))) do Inc(I);
    172         if I < MicroThreads.Count then begin
    173           Result := TMicroThread(MicroThreads[I]).Id;
    174         end else Result := -1;
    175       finally
    176         Lock.Release;
    177       end;
    178     end;
    179   end;
     155function GetMicroThreadId: Integer;
     156var
     157  I: Integer;
     158  CurrentStack: Pointer;
     159begin
     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;
     178end;
    180179
    181180{ TMicroThreadManager }
     
    187186  FExecuteCount := Count;
    188187  FExecutedCount := 0;
    189   Yield(nil);
     188  Yield;
    190189  Result := FExecutedCount;
    191190end;
    192191
    193 procedure TMicroThreadManager.Yield(MicroThread: TMicroThread);
     192procedure TMicroThreadManager.Yield;
    194193var
    195194  I: Integer;
     
    197196begin
    198197  Time := Scheduler.GetNow;
    199   if Assigned(MicroThread) then begin
    200     MicroThread.Manager := nil;
    201     MicroThread.FExecutionEndTime := Time;
    202     MicroThread.FExecutionTime := MicroThread.FExecutionTime +
    203       (MicroThread.FExecutionEndTime - MicroThread.FExecutionStartTime);
    204     if MicroThread.State = tsRunning then
    205       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;
    206205    asm
    207206      // Store microthread stack
    208       mov eax, MicroThread
     207      mov eax, StaticMicroThread
    209208      mov edx, esp
    210209      mov [eax].TMicroThread.FStackPointer, edx
     
    212211      mov [eax].TMicroThread.FBasePointer, edx
    213212    end;
    214     StaticManager := MicroThread.Manager;
     213    StaticManager := CurrentMicroThread.Manager;
    215214    asm
    216215      // Restore scheduler stack
     
    221220      mov ebp, edx
    222221    end;
     222    CurrentMicroThread.Manager := nil;
    223223    CurrentMicroThread := nil;
    224224  end;
    225225
    226   FSelected := Scheduler.GetNextMicroThread;
    227 
    228   if Assigned(FSelected) and (FExecutedCount < FExecuteCount) then begin
    229     FSelected.Manager := Self;
     226  CurrentMicroThread := Scheduler.GetNextMicroThread;
     227
     228  if Assigned(CurrentMicroThread) and (FExecutedCount < FExecuteCount) then begin
     229    CurrentMicroThread.Manager := Self;
    230230    Inc(FExecutedCount);
    231     CurrentMicroThread := FSelected;
    232231    asm
    233232      // Store scheduler stack
     
    238237      mov [eax].TMicroThreadManager.FBasePointer, edx
    239238    end;
    240     if not FSelected.FExecuted then begin
    241       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;
    245244      asm
    246245        // Restore microthread stack
    247         mov eax, Self
    248         mov edx, [eax].TMicroThreadManager.FTempPointer
     246        mov eax, StaticMicroThread
     247        mov edx, [eax].TMicroThread.FStackPointer
    249248        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
    256250        mov ebp, edx
    257251      end;
     
    267261        mov ebp, edx
    268262      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;
    277272    end else
    278     if FSelected.State = tsWaiting then begin
     273    if CurrentMicroThread.State = tsWaiting then begin
    279274      // 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;
    283278      asm
    284279        // Restore microthread stack
     
    287282        mov esp, edx
    288283      end;
    289       FTempPointer := FSelected.FBasePointer;
     284      FTempPointer := CurrentMicroThread.FBasePointer;
    290285      asm
    291286        mov eax, Self
     
    294289      end;
    295290    end;
     291  end else begin
     292    CurrentMicroThread := nil;
    296293  end;
    297294end;
     
    299296constructor TMicroThreadManager.Create;
    300297begin
    301 
     298  CurrentMicroThread := nil;
    302299end;
    303300
     
    343340procedure TMicroThread.Yield;
    344341begin
    345   Manager.Yield(Self);
     342  Manager.Yield;
    346343end;
    347344
     
    430427  fpgettimeofday(@t, nil);
    431428   // 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;
    433430  {$ENDIF}
    434431
Note: See TracChangeset for help on using the changeset viewer.