Changeset 146


Ignore:
Timestamp:
Jan 26, 2011, 7:28:29 AM (14 years ago)
Author:
george
Message:
  • Fixed: Freeing micro threads on finish.
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Common/UCommon.pas

    r129 r146  
    4242function AddLeadingZeroes(const aNumber, Length : integer) : string;
    4343function LastPos(const SubStr: String; const S: String): Integer;
     44function GenerateNewName(OldName: string): string;
    4445
    4546implementation
     47
     48function GenerateNewName(OldName: string): string;
     49var
     50  I: Integer;
     51  Number: Integer;
     52begin
     53  Number := 1;
     54  // Find number on end
     55  if Length(OldName) > 0 then begin
     56    I := Length(OldName);
     57    while (I > 1) and ((OldName[I] >= '0') and (OldName[I] <= '9')) do Dec(I);
     58    TryStrToInt(Copy(OldName, I + 1, Length(OldName) - I), Number);
     59    Inc(Number)
     60  end;
     61  Result := Copy(OldName, 1, I) + IntToStr(Number);
     62end;
    4663
    4764(*function DelTree(DirName : string): Boolean;
  • MicroThreading/Demo/Demo.lpi

    r145 r146  
    5050        <TopLine Value="1"/>
    5151        <CursorPos X="1" Y="15"/>
    52         <UsageCount Value="47"/>
     52        <UsageCount Value="48"/>
    5353      </Unit0>
    5454      <Unit1>
     
    5858        <ResourceBaseClass Value="Form"/>
    5959        <UnitName Value="UMainForm"/>
    60         <IsVisibleTab Value="True"/>
    6160        <EditorIndex Value="0"/>
    6261        <WindowIndex Value="0"/>
    63         <TopLine Value="84"/>
    64         <CursorPos X="3" Y="88"/>
    65         <UsageCount Value="47"/>
     62        <TopLine Value="218"/>
     63        <CursorPos X="17" Y="230"/>
     64        <UsageCount Value="48"/>
    6665        <Loaded Value="True"/>
    6766        <LoadedDesigner Value="True"/>
     
    7069        <Filename Value="../UMicroThreading.pas"/>
    7170        <UnitName Value="UMicroThreading"/>
     71        <IsVisibleTab Value="True"/>
    7272        <EditorIndex Value="1"/>
    7373        <WindowIndex Value="0"/>
    74         <TopLine Value="383"/>
    75         <CursorPos X="9" Y="384"/>
    76         <UsageCount Value="26"/>
     74        <TopLine Value="259"/>
     75        <CursorPos X="11" Y="278"/>
     76        <UsageCount Value="27"/>
    7777        <Loaded Value="True"/>
    7878      </Unit2>
     
    159159      <Unit13>
    160160        <Filename Value="../../../../lazarus/lcl/include/buttons.inc"/>
    161         <EditorIndex Value="4"/>
    162161        <WindowIndex Value="0"/>
    163162        <TopLine Value="317"/>
    164163        <CursorPos X="1" Y="341"/>
    165164        <UsageCount Value="10"/>
    166         <Loaded Value="True"/>
    167165      </Unit13>
    168166      <Unit14>
    169167        <Filename Value="../../../../lazarus/lcl/include/buttoncontrol.inc"/>
    170         <EditorIndex Value="3"/>
    171168        <WindowIndex Value="0"/>
    172169        <TopLine Value="45"/>
    173170        <CursorPos X="1" Y="62"/>
    174171        <UsageCount Value="10"/>
    175         <Loaded Value="True"/>
    176172      </Unit14>
    177173      <Unit15>
    178174        <Filename Value="../../../../lazarus/lcl/include/control.inc"/>
    179         <EditorIndex Value="2"/>
    180175        <WindowIndex Value="0"/>
    181176        <TopLine Value="2271"/>
    182177        <CursorPos X="19" Y="2274"/>
    183178        <UsageCount Value="11"/>
    184         <Loaded Value="True"/>
    185179      </Unit15>
    186180      <Unit16>
     
    385379      </Unit43>
    386380    </Units>
    387     <JumpHistory Count="30" HistoryIndex="29">
     381    <JumpHistory Count="30" HistoryIndex="19">
    388382      <Position1>
    389383        <Filename Value="../UMicroThreading.pas"/>
    390         <Caret Line="233" Column="1" TopLine="209"/>
     384        <Caret Line="272" Column="1" TopLine="250"/>
    391385      </Position1>
    392386      <Position2>
    393387        <Filename Value="../UMicroThreading.pas"/>
    394         <Caret Line="234" Column="1" TopLine="210"/>
     388        <Caret Line="273" Column="1" TopLine="251"/>
    395389      </Position2>
    396390      <Position3>
    397391        <Filename Value="../UMicroThreading.pas"/>
    398         <Caret Line="235" Column="1" TopLine="211"/>
     392        <Caret Line="274" Column="1" TopLine="252"/>
    399393      </Position3>
    400394      <Position4>
    401395        <Filename Value="../UMicroThreading.pas"/>
    402         <Caret Line="236" Column="1" TopLine="212"/>
     396        <Caret Line="397" Column="1" TopLine="384"/>
    403397      </Position4>
    404398      <Position5>
    405399        <Filename Value="../UMicroThreading.pas"/>
    406         <Caret Line="237" Column="1" TopLine="213"/>
     400        <Caret Line="398" Column="1" TopLine="384"/>
    407401      </Position5>
    408402      <Position6>
    409         <Filename Value="../UMicroThreading.pas"/>
    410         <Caret Line="239" Column="1" TopLine="215"/>
     403        <Filename Value="UMainForm.pas"/>
     404        <Caret Line="230" Column="17" TopLine="218"/>
    411405      </Position6>
    412406      <Position7>
    413407        <Filename Value="../UMicroThreading.pas"/>
    414         <Caret Line="240" Column="1" TopLine="216"/>
     408        <Caret Line="256" Column="1" TopLine="243"/>
    415409      </Position7>
    416410      <Position8>
    417411        <Filename Value="../UMicroThreading.pas"/>
    418         <Caret Line="241" Column="1" TopLine="217"/>
     412        <Caret Line="259" Column="1" TopLine="243"/>
    419413      </Position8>
    420414      <Position9>
    421415        <Filename Value="../UMicroThreading.pas"/>
    422         <Caret Line="242" Column="1" TopLine="218"/>
     416        <Caret Line="262" Column="1" TopLine="243"/>
    423417      </Position9>
    424418      <Position10>
    425419        <Filename Value="../UMicroThreading.pas"/>
    426         <Caret Line="243" Column="1" TopLine="219"/>
     420        <Caret Line="263" Column="1" TopLine="243"/>
    427421      </Position10>
    428422      <Position11>
    429423        <Filename Value="../UMicroThreading.pas"/>
    430         <Caret Line="246" Column="1" TopLine="222"/>
     424        <Caret Line="264" Column="1" TopLine="249"/>
    431425      </Position11>
    432426      <Position12>
    433427        <Filename Value="../UMicroThreading.pas"/>
    434         <Caret Line="247" Column="1" TopLine="223"/>
     428        <Caret Line="265" Column="1" TopLine="249"/>
    435429      </Position12>
    436430      <Position13>
    437431        <Filename Value="../UMicroThreading.pas"/>
    438         <Caret Line="248" Column="1" TopLine="224"/>
     432        <Caret Line="266" Column="1" TopLine="249"/>
    439433      </Position13>
    440434      <Position14>
    441435        <Filename Value="../UMicroThreading.pas"/>
    442         <Caret Line="249" Column="1" TopLine="225"/>
     436        <Caret Line="268" Column="1" TopLine="249"/>
    443437      </Position14>
    444438      <Position15>
    445439        <Filename Value="../UMicroThreading.pas"/>
    446         <Caret Line="250" Column="1" TopLine="226"/>
     440        <Caret Line="269" Column="1" TopLine="249"/>
    447441      </Position15>
    448442      <Position16>
    449443        <Filename Value="../UMicroThreading.pas"/>
    450         <Caret Line="252" Column="1" TopLine="228"/>
     444        <Caret Line="270" Column="1" TopLine="249"/>
    451445      </Position16>
    452446      <Position17>
    453447        <Filename Value="../UMicroThreading.pas"/>
    454         <Caret Line="204" Column="1" TopLine="189"/>
     448        <Caret Line="271" Column="1" TopLine="249"/>
    455449      </Position17>
    456450      <Position18>
    457451        <Filename Value="../UMicroThreading.pas"/>
    458         <Caret Line="217" Column="1" TopLine="193"/>
     452        <Caret Line="272" Column="1" TopLine="250"/>
    459453      </Position18>
    460454      <Position19>
    461455        <Filename Value="../UMicroThreading.pas"/>
    462         <Caret Line="188" Column="1" TopLine="173"/>
     456        <Caret Line="273" Column="1" TopLine="251"/>
    463457      </Position19>
    464458      <Position20>
    465459        <Filename Value="../UMicroThreading.pas"/>
    466         <Caret Line="217" Column="1" TopLine="192"/>
     460        <Caret Line="274" Column="1" TopLine="252"/>
    467461      </Position20>
    468462      <Position21>
    469463        <Filename Value="../UMicroThreading.pas"/>
    470         <Caret Line="188" Column="1" TopLine="173"/>
     464        <Caret Line="397" Column="1" TopLine="384"/>
    471465      </Position21>
    472466      <Position22>
    473467        <Filename Value="../UMicroThreading.pas"/>
    474         <Caret Line="268" Column="7" TopLine="242"/>
     468        <Caret Line="400" Column="44" TopLine="384"/>
    475469      </Position22>
    476470      <Position23>
    477471        <Filename Value="../UMicroThreading.pas"/>
    478         <Caret Line="217" Column="1" TopLine="202"/>
     472        <Caret Line="397" Column="1" TopLine="384"/>
    479473      </Position23>
    480474      <Position24>
    481475        <Filename Value="../UMicroThreading.pas"/>
    482         <Caret Line="188" Column="1" TopLine="173"/>
     476        <Caret Line="398" Column="1" TopLine="384"/>
    483477      </Position24>
    484478      <Position25>
    485479        <Filename Value="../UMicroThreading.pas"/>
    486         <Caret Line="480" Column="1" TopLine="465"/>
     480        <Caret Line="399" Column="1" TopLine="384"/>
    487481      </Position25>
    488482      <Position26>
    489483        <Filename Value="../UMicroThreading.pas"/>
    490         <Caret Line="188" Column="1" TopLine="173"/>
     484        <Caret Line="400" Column="18" TopLine="384"/>
    491485      </Position26>
    492486      <Position27>
    493487        <Filename Value="../UMicroThreading.pas"/>
    494         <Caret Line="292" Column="1" TopLine="254"/>
     488        <Caret Line="398" Column="1" TopLine="384"/>
    495489      </Position27>
    496490      <Position28>
    497491        <Filename Value="../UMicroThreading.pas"/>
    498         <Caret Line="389" Column="3" TopLine="387"/>
     492        <Caret Line="399" Column="1" TopLine="384"/>
    499493      </Position28>
    500494      <Position29>
    501495        <Filename Value="../UMicroThreading.pas"/>
    502         <Caret Line="384" Column="9" TopLine="383"/>
     496        <Caret Line="400" Column="1" TopLine="384"/>
    503497      </Position29>
    504498      <Position30>
    505         <Filename Value="UMainForm.pas"/>
    506         <Caret Line="235" Column="1" TopLine="142"/>
     499        <Filename Value="../UMicroThreading.pas"/>
     500        <Caret Line="274" Column="15" TopLine="261"/>
    507501      </Position30>
    508502    </JumpHistory>
     
    529523  </CompilerOptions>
    530524  <Debugging>
    531     <BreakPoints Count="7">
     525    <BreakPoints Count="5">
    532526      <Item1>
    533527        <Source Value="../Coroutine.pas"/>
     
    550544        <Line Value="145"/>
    551545      </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>
    560546    </BreakPoints>
    561547    <Exceptions Count="3">
  • MicroThreading/Demo/UMainForm.lfm

    r145 r146  
    11object Form1: TForm1
    2   Left = 300
     2  Left = 231
    33  Height = 510
    4   Top = 137
    5   Width = 703
     4  Top = 48
     5  Width = 798
    66  Caption = 'MicroThreading demo'
    77  ClientHeight = 510
    8   ClientWidth = 703
     8  ClientWidth = 798
    99  OnClose = FormClose
    1010  OnCreate = FormCreate
     
    2424    Height = 483
    2525    Top = 24
    26     Width = 386
     26    Width = 481
    2727    Anchors = [akTop, akLeft, akRight, akBottom]
    2828    Columns = <   
    2929      item
    3030        Caption = 'Id'
     31        Width = 40
    3132      end   
    3233      item
     
    4546      item
    4647        Caption = 'Execution time'
    47         Width = 105
     48        Width = 70
     49      end   
     50      item
     51        Caption = 'Completion'
    4852      end>
    4953    OwnerData = True
     
    5862    Height = 14
    5963    Top = 8
    60     Width = 85
     64    Width = 74
    6165    Caption = 'Microthread list'
    6266    ParentColor = False
     
    9397    Height = 14
    9498    Top = 80
    95     Width = 38
     99    Width = 32
    96100    Caption = 'Label2'
    97101    ParentColor = False
  • MicroThreading/Demo/UMainForm.pas

    r145 r146  
    208208      Item.SubItems.Add(MicroThreadStateText[State]);
    209209      Item.SubItems.Add(FloatToStr(ExecutionTime));
     210      Item.SubItems.Add(IntToStr(Trunc(Completion * 100)) + '%');
    210211    end;
    211212  finally
     
    226227var
    227228  I: Integer;
     229const
     230  TotalSteps = 100;
    228231begin
    229232  with MicroThread do begin
    230233    Memo1.Lines.Add('Worker ' + IntToStr(Id));
    231     for I := 0 to 1000 do begin
     234    for I := 0 to TotalSteps - 1 do begin
    232235      Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' +
    233236        FloatToStr(ExecutionTime));
     237      Completion := I / TotalSteps;
    234238      //Sleep(1 * Id * OneMillisecond);
    235239      Yield;
  • MicroThreading/UMicroThreading.pas

    r145 r146  
    4545    Manager: TMicroThreadManager;
    4646    Scheduler: TMicroThreadScheduler;
     47    Completion: Single; // Can be used for progress information in range <0, 1>
    4748    procedure Execute; virtual;
    4849
     
    247248        mov edx, [eax].TMicroThread.FStackPointer
    248249        mov esp, edx
     250        push ebp
    249251        mov edx, [eax].TMicroThread.FBasePointer
    250252        mov ebp, edx
    251253      end;
    252254      StaticMicroThread.Execute;
     255      asm
     256        pop ebp
     257      end;
    253258      //FSelected.Method(FSelected);
    254       StaticManager := StaticMicroThread.Manager;
     259      StaticManager := CurrentMicroThread.Manager;
    255260      asm
    256261        // Restore scheduler stack
     
    267272      CurrentMicroThread.FFinished := True;
    268273      if CurrentMicroThread.FFreeOnTerminate then begin
    269         CurrentMicroThread.Free;
     274        // Microthread is finished, remove it from queue
     275        with Scheduler do
     276        try
     277          Lock.Acquire;
     278          MicroThreads.Delete(MicroThreads.IndexOf(CurrentMicroThread));
     279        finally
     280          Lock.Release;
     281        end;
    270282      end;
    271283      CurrentMicroThread := nil;
     
    387399destructor TMicroThread.Destroy;
    388400begin
    389   Terminate;
    390   WaitFor;
    391   // Microthread is finished, remove it from queue
    392   try
    393     Manager.Scheduler.Lock.Acquire;
    394     Manager.Scheduler.MicroThreads.Delete(Manager.Scheduler.MicroThreads.IndexOf(Self));
    395   finally
    396     Manager.Scheduler.Lock.Release;
    397   end;
     401  //Terminate;
     402  //WaitFor;
    398403  FreeMem(FStack);
    399404  inherited Destroy;
Note: See TracChangeset for help on using the changeset viewer.