Changeset 147 for MicroThreading


Ignore:
Timestamp:
Jan 26, 2011, 8:06:25 AM (14 years ago)
Author:
george
Message:
  • Added: Now multiple thread managers are running within single scheduler.
Location:
MicroThreading
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/Demo.lpi

    r146 r147  
    4242      </Item3>
    4343    </RequiredPackages>
    44     <Units Count="44">
     44    <Units Count="46">
    4545      <Unit0>
    4646        <Filename Value="Demo.lpr"/>
     
    5050        <TopLine Value="1"/>
    5151        <CursorPos X="1" Y="15"/>
    52         <UsageCount Value="48"/>
     52        <UsageCount Value="49"/>
    5353      </Unit0>
    5454      <Unit1>
     
    6060        <EditorIndex Value="0"/>
    6161        <WindowIndex Value="0"/>
    62         <TopLine Value="218"/>
    63         <CursorPos X="17" Y="230"/>
    64         <UsageCount Value="48"/>
     62        <TopLine Value="82"/>
     63        <CursorPos X="1" Y="95"/>
     64        <UsageCount Value="49"/>
    6565        <Loaded Value="True"/>
    6666        <LoadedDesigner Value="True"/>
     
    6969        <Filename Value="../UMicroThreading.pas"/>
    7070        <UnitName Value="UMicroThreading"/>
    71         <IsVisibleTab Value="True"/>
    72         <EditorIndex Value="1"/>
    73         <WindowIndex Value="0"/>
    74         <TopLine Value="259"/>
    75         <CursorPos X="11" Y="278"/>
     71        <EditorIndex Value="2"/>
     72        <WindowIndex Value="0"/>
     73        <TopLine Value="500"/>
     74        <CursorPos X="1" Y="517"/>
    7675        <UsageCount Value="27"/>
    7776        <Loaded Value="True"/>
     
    238237      <Unit24>
    239238        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/sysutils/datih.inc"/>
    240         <WindowIndex Value="0"/>
    241         <TopLine Value="161"/>
    242         <CursorPos X="11" Y="184"/>
     239        <EditorIndex Value="4"/>
     240        <WindowIndex Value="0"/>
     241        <TopLine Value="106"/>
     242        <CursorPos X="10" Y="119"/>
    243243        <UsageCount Value="16"/>
     244        <Loaded Value="True"/>
    244245      </Unit24>
    245246      <Unit25>
     
    296297      <Unit32>
    297298        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classesh.inc"/>
    298         <WindowIndex Value="0"/>
    299         <TopLine Value="1507"/>
    300         <CursorPos X="3" Y="1469"/>
    301         <UsageCount Value="13"/>
     299        <EditorIndex Value="5"/>
     300        <WindowIndex Value="0"/>
     301        <TopLine Value="1504"/>
     302        <CursorPos X="1" Y="1510"/>
     303        <UsageCount Value="13"/>
     304        <Loaded Value="True"/>
    302305      </Unit32>
    303306      <Unit33>
     
    324327      <Unit36>
    325328        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/fpc/2.4.3/source/rtl/objpas/classes/classes.inc"/>
     329        <EditorIndex Value="6"/>
    326330        <WindowIndex Value="0"/>
    327331        <TopLine Value="109"/>
    328         <CursorPos X="6" Y="115"/>
    329         <UsageCount Value="13"/>
     332        <CursorPos X="3" Y="111"/>
     333        <UsageCount Value="13"/>
     334        <Loaded Value="True"/>
    330335      </Unit36>
    331336      <Unit37>
     
    378383        <UsageCount Value="10"/>
    379384      </Unit43>
     385      <Unit44>
     386        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/include/spinedit.inc"/>
     387        <EditorIndex Value="1"/>
     388        <WindowIndex Value="0"/>
     389        <TopLine Value="221"/>
     390        <CursorPos X="1" Y="235"/>
     391        <UsageCount Value="10"/>
     392        <Loaded Value="True"/>
     393      </Unit44>
     394      <Unit45>
     395        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/>
     396        <IsVisibleTab Value="True"/>
     397        <EditorIndex Value="3"/>
     398        <WindowIndex Value="0"/>
     399        <TopLine Value="387"/>
     400        <CursorPos X="1" Y="400"/>
     401        <UsageCount Value="10"/>
     402        <Loaded Value="True"/>
     403      </Unit45>
    380404    </Units>
    381     <JumpHistory Count="30" HistoryIndex="19">
     405    <JumpHistory Count="30" HistoryIndex="29">
    382406      <Position1>
    383407        <Filename Value="../UMicroThreading.pas"/>
    384         <Caret Line="272" Column="1" TopLine="250"/>
     408        <Caret Line="579" Column="1" TopLine="566"/>
    385409      </Position1>
    386410      <Position2>
    387411        <Filename Value="../UMicroThreading.pas"/>
    388         <Caret Line="273" Column="1" TopLine="251"/>
     412        <Caret Line="580" Column="1" TopLine="566"/>
    389413      </Position2>
    390414      <Position3>
    391415        <Filename Value="../UMicroThreading.pas"/>
    392         <Caret Line="274" Column="1" TopLine="252"/>
     416        <Caret Line="581" Column="1" TopLine="566"/>
    393417      </Position3>
    394418      <Position4>
    395419        <Filename Value="../UMicroThreading.pas"/>
    396         <Caret Line="397" Column="1" TopLine="384"/>
     420        <Caret Line="582" Column="1" TopLine="566"/>
    397421      </Position4>
    398422      <Position5>
    399423        <Filename Value="../UMicroThreading.pas"/>
    400         <Caret Line="398" Column="1" TopLine="384"/>
     424        <Caret Line="583" Column="1" TopLine="566"/>
    401425      </Position5>
    402426      <Position6>
    403         <Filename Value="UMainForm.pas"/>
    404         <Caret Line="230" Column="17" TopLine="218"/>
     427        <Filename Value="../UMicroThreading.pas"/>
     428        <Caret Line="584" Column="1" TopLine="566"/>
    405429      </Position6>
    406430      <Position7>
    407431        <Filename Value="../UMicroThreading.pas"/>
    408         <Caret Line="256" Column="1" TopLine="243"/>
     432        <Caret Line="142" Column="26" TopLine="125"/>
    409433      </Position7>
    410434      <Position8>
    411         <Filename Value="../UMicroThreading.pas"/>
    412         <Caret Line="259" Column="1" TopLine="243"/>
     435        <Filename Value="UMainForm.pas"/>
     436        <Caret Line="95" Column="17" TopLine="87"/>
    413437      </Position8>
    414438      <Position9>
    415439        <Filename Value="../UMicroThreading.pas"/>
    416         <Caret Line="262" Column="1" TopLine="243"/>
     440        <Caret Line="512" Column="3" TopLine="507"/>
    417441      </Position9>
    418442      <Position10>
    419         <Filename Value="../UMicroThreading.pas"/>
    420         <Caret Line="263" Column="1" TopLine="243"/>
     443        <Filename Value="UMainForm.pas"/>
     444        <Caret Line="95" Column="17" TopLine="87"/>
    421445      </Position10>
    422446      <Position11>
    423447        <Filename Value="../UMicroThreading.pas"/>
    424         <Caret Line="264" Column="1" TopLine="249"/>
     448        <Caret Line="513" Column="1" TopLine="507"/>
    425449      </Position11>
    426450      <Position12>
    427451        <Filename Value="../UMicroThreading.pas"/>
    428         <Caret Line="265" Column="1" TopLine="249"/>
     452        <Caret Line="514" Column="1" TopLine="507"/>
    429453      </Position12>
    430454      <Position13>
    431455        <Filename Value="../UMicroThreading.pas"/>
    432         <Caret Line="266" Column="1" TopLine="249"/>
     456        <Caret Line="327" Column="3" TopLine="323"/>
    433457      </Position13>
    434458      <Position14>
    435459        <Filename Value="../UMicroThreading.pas"/>
    436         <Caret Line="268" Column="1" TopLine="249"/>
     460        <Caret Line="513" Column="1" TopLine="500"/>
    437461      </Position14>
    438462      <Position15>
    439463        <Filename Value="../UMicroThreading.pas"/>
    440         <Caret Line="269" Column="1" TopLine="249"/>
     464        <Caret Line="514" Column="1" TopLine="500"/>
    441465      </Position15>
    442466      <Position16>
    443467        <Filename Value="../UMicroThreading.pas"/>
    444         <Caret Line="270" Column="1" TopLine="249"/>
     468        <Caret Line="516" Column="1" TopLine="500"/>
    445469      </Position16>
    446470      <Position17>
    447471        <Filename Value="../UMicroThreading.pas"/>
    448         <Caret Line="271" Column="1" TopLine="249"/>
     472        <Caret Line="517" Column="1" TopLine="500"/>
    449473      </Position17>
    450474      <Position18>
    451475        <Filename Value="../UMicroThreading.pas"/>
    452         <Caret Line="272" Column="1" TopLine="250"/>
     476        <Caret Line="328" Column="1" TopLine="315"/>
    453477      </Position18>
    454478      <Position19>
    455479        <Filename Value="../UMicroThreading.pas"/>
    456         <Caret Line="273" Column="1" TopLine="251"/>
     480        <Caret Line="330" Column="1" TopLine="315"/>
    457481      </Position19>
    458482      <Position20>
    459483        <Filename Value="../UMicroThreading.pas"/>
    460         <Caret Line="274" Column="1" TopLine="252"/>
     484        <Caret Line="331" Column="1" TopLine="315"/>
    461485      </Position20>
    462486      <Position21>
    463487        <Filename Value="../UMicroThreading.pas"/>
    464         <Caret Line="397" Column="1" TopLine="384"/>
     488        <Caret Line="332" Column="1" TopLine="315"/>
    465489      </Position21>
    466490      <Position22>
    467         <Filename Value="../UMicroThreading.pas"/>
    468         <Caret Line="400" Column="44" TopLine="384"/>
     491        <Filename Value="UMainForm.pas"/>
     492        <Caret Line="95" Column="17" TopLine="87"/>
    469493      </Position22>
    470494      <Position23>
    471         <Filename Value="../UMicroThreading.pas"/>
    472         <Caret Line="397" Column="1" TopLine="384"/>
     495        <Filename Value="UMainForm.pas"/>
     496        <Caret Line="235" Column="19" TopLine="224"/>
    473497      </Position23>
    474498      <Position24>
    475499        <Filename Value="../UMicroThreading.pas"/>
    476         <Caret Line="398" Column="1" TopLine="384"/>
     500        <Caret Line="513" Column="1" TopLine="500"/>
    477501      </Position24>
    478502      <Position25>
    479         <Filename Value="../UMicroThreading.pas"/>
    480         <Caret Line="399" Column="1" TopLine="384"/>
     503        <Filename Value="UMainForm.pas"/>
     504        <Caret Line="244" Column="9" TopLine="224"/>
    481505      </Position25>
    482506      <Position26>
    483         <Filename Value="../UMicroThreading.pas"/>
    484         <Caret Line="400" Column="18" TopLine="384"/>
     507        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/>
     508        <Caret Line="400" Column="1" TopLine="387"/>
    485509      </Position26>
    486510      <Position27>
    487511        <Filename Value="../UMicroThreading.pas"/>
    488         <Caret Line="398" Column="1" TopLine="384"/>
     512        <Caret Line="517" Column="1" TopLine="500"/>
    489513      </Position27>
    490514      <Position28>
    491         <Filename Value="../UMicroThreading.pas"/>
    492         <Caret Line="399" Column="1" TopLine="384"/>
     515        <Filename Value="UMainForm.pas"/>
     516        <Caret Line="95" Column="1" TopLine="82"/>
    493517      </Position28>
    494518      <Position29>
    495519        <Filename Value="../UMicroThreading.pas"/>
    496         <Caret Line="400" Column="1" TopLine="384"/>
     520        <Caret Line="517" Column="1" TopLine="500"/>
    497521      </Position29>
    498522      <Position30>
    499         <Filename Value="../UMicroThreading.pas"/>
    500         <Caret Line="274" Column="15" TopLine="261"/>
     523        <Filename Value="../../../../Programy/Lazarus/0.9.31_2.4.3/lcl/interfaces/win32/win32object.inc"/>
     524        <Caret Line="394" Column="30" TopLine="387"/>
    501525      </Position30>
    502526    </JumpHistory>
     
    534558      <Item3>
    535559        <Source Value="umainform.pas"/>
    536         <Line Value="62"/>
     560        <Line Value="65"/>
    537561      </Item3>
    538562      <Item4>
  • MicroThreading/Demo/UMainForm.lfm

    r146 r147  
    11object Form1: TForm1
    2   Left = 231
     2  Left = 235
    33  Height = 510
    4   Top = 48
     4  Top = 44
    55  Width = 798
    66  Caption = 'MicroThreading demo'
     
    6868  object Memo1: TMemo
    6969    Left = 10
    70     Height = 379
    71     Top = 128
     70    Height = 267
     71    Top = 240
    7272    Width = 294
    7373    Anchors = [akTop, akLeft, akBottom]
     
    7878    Left = 224
    7979    Height = 25
    80     Top = 96
     80    Top = 208
    8181    Width = 75
    8282    Caption = 'Button2'
     
    8787    Left = 10
    8888    Height = 25
    89     Top = 96
     89    Top = 208
    9090    Width = 75
    9191    Caption = 'Coroutine'
     
    9696    Left = 10
    9797    Height = 14
    98     Top = 80
     98    Top = 192
    9999    Width = 32
    100100    Caption = 'Label2'
     
    102102  end
    103103  object Button4: TButton
    104     Left = 120
     104    Left = 10
    105105    Height = 25
    106     Top = 12
     106    Top = 48
    107107    Width = 88
    108108    Caption = 'Start workers'
     
    111111  end
    112112  object SpinEdit1: TSpinEdit
    113     Left = 217
     113    Left = 216
    114114    Height = 21
    115     Top = 13
     115    Top = 48
    116116    Width = 82
    117117    MaxValue = 100000
    118118    MinValue = 1
    119119    TabOrder = 6
    120     Value = 20
     120    Value = 200
    121121  end
    122122  object Button5: TButton
    123123    Left = 168
    124124    Height = 25
    125     Top = 48
     125    Top = 160
    126126    Width = 131
    127127    Caption = 'Get max TThread count'
     
    132132    Left = 88
    133133    Height = 25
    134     Top = 48
     134    Top = 160
    135135    Width = 75
    136136    Caption = 'Button6'
    137137    OnClick = Button6Click
    138138    TabOrder = 8
     139  end
     140  object SpinEdit2: TSpinEdit
     141    Left = 216
     142    Height = 21
     143    Top = 13
     144    Width = 82
     145    TabOrder = 9
     146    Value = 1
     147  end
     148  object Label3: TLabel
     149    Left = 120
     150    Height = 14
     151    Top = 20
     152    Width = 69
     153    Caption = 'Thread count:'
     154    ParentColor = False
     155  end
     156  object Label4: TLabel
     157    Left = 104
     158    Height = 14
     159    Top = 48
     160    Width = 92
     161    Caption = 'Microthread count:'
     162    ParentColor = False
    139163  end
    140164  object Timer1: TTimer
  • MicroThreading/Demo/UMainForm.pas

    r146 r147  
    2828    Label1: TLabel;
    2929    Label2: TLabel;
     30    Label3: TLabel;
     31    Label4: TLabel;
    3032    ListView1: TListView;
    3133    Memo1: TMemo;
    3234    SpinEdit1: TSpinEdit;
     35    SpinEdit2: TSpinEdit;
    3336    Timer1: TTimer;
    3437    procedure Button1Click(Sender: TObject);
     
    8992    Button1.Caption := 'Stop scheduler';
    9093    Memo1.Clear;
     94    Scheduler.ThreadPoolSize := SpinEdit2.Value;
    9195    Scheduler.Start;
    9296  end else begin
    9397    Button1.Caption := 'Start scheduler';
    9498    Scheduler.Stop;
     99    Scheduler.ThreadPoolSize := 0;
    95100  end;
    96101end;
     
    227232var
    228233  I: Integer;
     234  Q: Integer;
    229235const
    230236  TotalSteps = 100;
     
    233239    Memo1.Lines.Add('Worker ' + IntToStr(Id));
    234240    for I := 0 to TotalSteps - 1 do begin
    235       Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' +
    236         FloatToStr(ExecutionTime));
     241      Q := 0;
     242      while Q < 1000000 do Inc(Q);
     243      //Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' +
     244      //  FloatToStr(ExecutionTime));
    237245      Completion := I / TotalSteps;
    238246      //Sleep(1 * Id * OneMillisecond);
  • MicroThreading/UMicroThreading.pas

    r146 r147  
    3434    FStackSize: Integer;
    3535    FBasePointer: Pointer;
    36     FWakeupTime: TDateTime;
     36    FWakeUpTime: TDateTime;
    3737    FTerminated: Boolean;
    3838    FExecuted: Boolean; // At first go through Execute method, then switch context
    3939    FFinished: Boolean;
     40    FSuspended: Boolean;
     41    FState: TMicroThreadState;
     42    FScheduler: TMicroThreadScheduler;
     43    FManager: TMicroThreadManager;
     44    FId: Integer;
    4045  public
    41     Id: Integer;
    4246    Name: string;
    4347    Priority: Integer;
    44     State: TMicroThreadState;
    45     Manager: TMicroThreadManager;
    46     Scheduler: TMicroThreadScheduler;
    4748    Completion: Single; // Can be used for progress information in range <0, 1>
    4849    procedure Execute; virtual;
    4950
    50     // Internal execution
    5151    procedure Yield;
    5252    procedure Sleep(Duration: TDateTime);
    5353    function WaitForSignal(Signal: TEvent): TWaitResult;
    54 
    55     // External execution
    5654    procedure WaitFor;
    5755    procedure Terminate;
    5856    procedure Start;
    59     procedure Stop;
     57    procedure Resume;
     58    procedure Suspend;
    6059
    6160    constructor Create(CreateSuspended: Boolean;
    6261      const StackSize: SizeUInt = DefaultStackSize);
    6362    destructor Destroy; override;
     63    property Id: Integer read FId;
     64    property State: TMicroThreadState read FState;
    6465    property ExecutionTime: TDateTime read FExecutionTime;
    6566    property FreeOnTerminate: Boolean read FFreeOnTerminate
    6667      write FFreeOnTerminate;
    6768    property Terminated: Boolean read FTerminated;
     69    property Scheduler: TMicroThreadScheduler read FScheduler;
     70    property Manager: TMicroThreadManager read FManager;
    6871  end;
    6972
     
    8083
    8184  TMicroThreadSchedulerPoolThread = class(TThread)
    82     Scheduler: TMicroThreadScheduler;
    8385    Manager: TMicroThreadManager;
    8486    procedure Execute; override;
     87    constructor Create(CreateSuspended: Boolean;
     88      const StackSize: SizeUInt = DefaultStackSize);
     89    destructor Destroy; override;
    8590  end;
    8691
    8792  TThreadPool = class(TObjectList)
    88 
    8993  end;
    9094
     
    116120    ThreadPool: TThreadPool;
    117121    RoundRobinIndex: Integer;
    118     LastId: Integer;
     122    FLastId: Integer;
    119123    FFrequency: Int64;
    120     FThreadPoolSize: Integer;
    121124    FTerminated: Boolean;
    122125    function GetMicroThreadCount: Integer;
     
    171174        TMicroThread(MicroThreads[I]).FStackSize))) do Inc(I);
    172175      if I < MicroThreads.Count then begin
    173         Result := TMicroThread(MicroThreads[I]).Id;
     176        Result := TMicroThread(MicroThreads[I]).FId;
    174177      end else Result := -1;
    175178    finally
     
    194197var
    195198  I: Integer;
    196   Time: TDateTime;
    197 begin
    198   Time := Scheduler.GetNow;
     199  CurrentTime: TDateTime;
     200begin
     201  CurrentTime := Scheduler.GetNow;
    199202  if Assigned(CurrentMicroThread) then begin
    200     CurrentMicroThread.FExecutionEndTime := Time;
     203    CurrentMicroThread.FExecutionEndTime := CurrentTime;
    201204    CurrentMicroThread.FExecutionTime := CurrentMicroThread.FExecutionTime +
    202205      (CurrentMicroThread.FExecutionEndTime - CurrentMicroThread.FExecutionStartTime);
    203     if CurrentMicroThread.State = tsRunning then
    204       CurrentMicroThread.State := tsWaiting;
     206    if CurrentMicroThread.FState = tsRunning then
     207      CurrentMicroThread.FState := tsWaiting;
    205208    StaticMicroThread := CurrentMicroThread;
    206209    asm
     
    212215      mov [eax].TMicroThread.FBasePointer, edx
    213216    end;
    214     StaticManager := CurrentMicroThread.Manager;
     217    StaticManager := CurrentMicroThread.FManager;
    215218    asm
    216219      // Restore scheduler stack
     
    221224      mov ebp, edx
    222225    end;
    223     CurrentMicroThread.Manager := nil;
     226    CurrentMicroThread.FManager := nil;
    224227    CurrentMicroThread := nil;
    225228  end;
     
    228231
    229232  if Assigned(CurrentMicroThread) and (FExecutedCount < FExecuteCount) then begin
    230     CurrentMicroThread.Manager := Self;
     233    CurrentMicroThread.FManager := Self;
    231234    Inc(FExecutedCount);
    232235    asm
     
    240243    if not CurrentMicroThread.FExecuted then begin
    241244      CurrentMicroThread.FExecuted := True;
    242       CurrentMicroThread.State := tsRunning;
    243       CurrentMicroThread.FExecutionStartTime := Time;
     245      CurrentMicroThread.FState := tsRunning;
     246      CurrentMicroThread.FExecutionStartTime := CurrentTime;
    244247      StaticMicroThread := CurrentMicroThread;
    245248      asm
     
    257260      end;
    258261      //FSelected.Method(FSelected);
    259       StaticManager := CurrentMicroThread.Manager;
     262      StaticManager := CurrentMicroThread.FManager;
    260263      asm
    261264        // Restore scheduler stack
     
    266269        mov ebp, edx
    267270      end;
    268       CurrentMicroThread.Manager := nil;
    269       CurrentMicroThread.FExecutionEndTime := Time;
     271      CurrentMicroThread.FManager := nil;
     272      CurrentMicroThread.FExecutionEndTime := CurrentTime;
    270273      CurrentMicroThread.FExecutionTime := CurrentMicroThread.FExecutionTime +
    271274       (CurrentMicroThread.FExecutionEndTime - CurrentMicroThread.FExecutionStartTime);
     
    285288    if CurrentMicroThread.State = tsWaiting then begin
    286289      // Execute selected thread
    287       CurrentMicroThread.State := tsRunning;
    288       CurrentMicroThread.FExecutionStartTime := Time;
     290      CurrentMicroThread.FState := tsRunning;
     291      CurrentMicroThread.FExecutionStartTime := CurrentTime;
    289292      FTempPointer := CurrentMicroThread.FStackPointer;
    290293      asm
     
    334337end;
    335338
     339constructor TMicroThreadSchedulerPoolThread.Create(CreateSuspended: Boolean;
     340  const StackSize: SizeUInt);
     341begin
     342  inherited;
     343  Manager := TMicroThreadManager.Create;
     344end;
     345
     346destructor TMicroThreadSchedulerPoolThread.Destroy;
     347begin
     348  Manager.Free;
     349  inherited Destroy;
     350end;
     351
    336352{ TMicroThreadMethod }
    337353
     
    352368procedure TMicroThread.Yield;
    353369begin
    354   Manager.Yield;
     370  FManager.Yield;
    355371end;
    356372
     
    365381procedure TMicroThread.Sleep(Duration: TDateTime);
    366382begin
    367   FWakeUpTime := Scheduler.GetNow + Duration;
    368   State := tsSleeping;
     383  FWakeUpTime := FScheduler.GetNow + Duration;
     384  FState := tsSleeping;
    369385  Yield;
    370386end;
     
    387403  FExecutionTime := 0;
    388404  FTerminated := False;
    389   if CreateSuspended then
    390     State := tsSuspended;
     405  if CreateSuspended then begin
     406    FState := tsSuspended;
     407    FSuspended := True;
     408  end else FSuspended := False;
    391409  FFreeOnTerminate := True;
    392410end;
     
    395413begin
    396414  FTerminated := True;
     415end;
     416
     417procedure TMicroThread.Start;
     418begin
     419  FState := tsWaiting;
    397420end;
    398421
     
    405428end;
    406429
    407 procedure TMicroThread.Start;
    408 begin
    409   State := tsWaiting;
    410 end;
    411 
    412 procedure TMicroThread.Stop;
    413 begin
    414   State := tsSuspended;
     430procedure TMicroThread.Resume;
     431begin
     432  FSuspended := False;
     433  if FState = tsSuspended then
     434    FState := tsWaiting;
     435end;
     436
     437procedure TMicroThread.Suspend;
     438begin
     439  FSuspended := True;
     440  //Yield;
    415441end;
    416442
     
    440466function TMicroThreadScheduler.Add(MicroThread: TMicroThread): Integer;
    441467begin
    442   Inc(LastId);
    443   MicroThread.Scheduler := Self;
    444   MicroThread.Id := LastId;
     468  Inc(FLastId);
     469  MicroThread.FScheduler := Self;
     470  MicroThread.FId := FLastId;
    445471  Result := MicroThreads.Add(MicroThread);
    446472end;
     
    452478  NewMicroThread := TMicroThreadMethod.Create(False);
    453479  NewMicroThread.Method := Method;
    454   NewMicroThread.Scheduler := Self;
     480  NewMicroThread.FScheduler := Self;
    455481  Result := Add(NewMicroThread);
    456482end;
     
    482508var
    483509  Executed: Integer;
     510  I: Integer;
    484511begin
    485512  FTerminated := False;
     513  for I := 0 to ThreadPool.Count - 1 do
     514    TMicroThreadSchedulerPoolThread(ThreadPool[I]).Start;
    486515  repeat
    487516    Executed := MainThreadManager.Execute(10);
     
    492521
    493522procedure TMicroThreadScheduler.Stop;
    494 begin
     523var
     524  I: Integer;
     525begin
     526  for I := 0 to ThreadPool.Count - 1 do
     527    TMicroThreadSchedulerPoolThread(ThreadPool[I]).Terminate;
    495528  FTerminated := True;
    496529end;
     
    499532var
    500533  I: Integer;
    501 begin
     534  CurrentTime: TDateTime;
     535begin
     536  CurrentTime := GetNow;
    502537  Result := nil;
    503538  try
     
    510545     (TMicroThread(MicroThreads[RoundRobinIndex]).State <> tsWaiting) do begin
    511546      // WakeUp sleeping threads
    512       if (TMicroThread(MicroThreads[RoundRobinIndex]).State = tsSleeping) and
    513         (TMicroThread(MicroThreads[RoundRobinIndex]).FWakeupTime < Time) then
    514           TMicroThread(MicroThreads[RoundRobinIndex]).State := tsWaiting else
     547      if (TMicroThread(MicroThreads[RoundRobinIndex]).FState = tsSleeping) and
     548        (TMicroThread(MicroThreads[RoundRobinIndex]).FWakeupTime < CurrentTime) then
     549          TMicroThread(MicroThreads[RoundRobinIndex]).FState := tsWaiting else
    515550      begin
    516551        // Go to next thread
     
    541576function TMicroThreadScheduler.GetThreadPoolSize: Integer;
    542577begin
    543   Result := FThreadPoolSize;
     578  Result := ThreadPool.Count;
    544579end;
    545580
    546581procedure TMicroThreadScheduler.SetThreadPoolSize(const AValue: Integer);
    547 begin
    548   FThreadPoolSize := AValue;
     582var
     583  I: Integer;
     584  NewThread: TMicroThreadSchedulerPoolThread;
     585begin
     586  if AValue > ThreadPool.Count then begin
     587    ThreadPool.Capacity := AValue;
     588    while ThreadPool.Count < AValue do begin
     589      NewThread := TMicroThreadSchedulerPoolThread.Create(True);
     590      NewThread.Manager.Scheduler := Self;
     591      ThreadPool.Add(NewThread);
     592    end;
     593  end else
     594  ThreadPool.Count := AValue;
    549595end;
    550596
Note: See TracChangeset for help on using the changeset viewer.