Ignore:
Timestamp:
Feb 7, 2011, 1:04:27 PM (14 years ago)
Author:
george
Message:
  • Add: MicroThread critical section.
  • Add: Thread list to microthread list window.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/UMainForm.pas

    r161 r162  
    2020    MainForm: TMainForm;
    2121    procedure DoWriteToMemo;
     22    constructor Create(CreateSuspended: Boolean;
     23      const StackSize: SizeUInt = DefaultStackSize);
     24    destructor Destroy; override;
    2225  end;
    2326
     
    3841    CheckBox2: TCheckBox;
    3942    CheckBox3: TCheckBox;
     43    CheckBox4: TCheckBox;
    4044    CheckBoxUseMainThread: TCheckBox;
    4145    GroupBox1: TGroupBox;
     
    4549    Label11: TLabel;
    4650    Label12: TLabel;
    47     Label13: TLabel;
    4851    Label14: TLabel;
    4952    Label15: TLabel;
    5053    Label16: TLabel;
    5154    Label17: TLabel;
     55    Label18: TLabel;
    5256    Label2: TLabel;
    5357    Label3: TLabel;
     
    5862    Label8: TLabel;
    5963    Label9: TLabel;
    60     ListView2: TListView;
    6164    Memo1: TMemo;
    6265    PageControl1: TPageControl;
     
    6669    SpinEdit4: TSpinEdit;
    6770    SpinEdit5: TSpinEdit;
     71    SpinEdit6: TSpinEdit;
    6872    TabSheet1: TTabSheet;
    6973    TabSheet2: TTabSheet;
     
    8387    procedure CheckBox2Change(Sender: TObject);
    8488    procedure CheckBox3Change(Sender: TObject);
     89    procedure CheckBox4Change(Sender: TObject);
    8590    procedure CheckBoxUseMainThreadChange(Sender: TObject);
    8691    procedure FormCreate(Sender: TObject);
     
    9196    procedure SpinEdit3Change(Sender: TObject);
    9297    procedure SpinEdit5Change(Sender: TObject);
     98    procedure SpinEdit6Change(Sender: TObject);
    9399    procedure TimerRedrawTimer(Sender: TObject);
    94100  private
    95101    MicroThreadList: TMicroThreadList;
     102    Lock: TMicroThreadCriticalSection;
    96103    LastException: Exception;
    97104    LastExceptionSender: TObject;
     
    102109    DoWriteToMemo: Boolean;
    103110    DoSleep: Boolean;
     111    DoCriticalSection: Boolean;
    104112    RaiseException: Boolean;
    105113    SleepDuration: Integer;
     114    CriticalSectionSleepDuration: Integer;
    106115    DoWaitForEvent: Boolean;
    107116    Event: TMicroThreadEvent;
     
    129138  MicroThreadList := TMicroThreadList.Create(Self);
    130139  UMicroThreading.ExceptionHandler := ShowException;
     140  Lock := TMicroThreadCriticalSection.Create;
    131141end;
    132142
     
    280290end;
    281291
     292procedure TMainForm.CheckBox4Change(Sender: TObject);
     293begin
     294  CriticalSectionSleepDuration := SpinEdit4.Value;
     295  DoCriticalSection := CheckBox4.Checked;
     296end;
     297
    282298procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject);
    283299begin
     
    290306  MainScheduler.Active := False;
    291307  Event.Free;
     308  Lock.Free;
    292309end;
    293310
     
    303320procedure TMainForm.ListView2Data(Sender: TObject; Item: TListItem);
    304321begin
    305   if Item.Index < MainScheduler.ThreadPoolCount then
    306   try
    307     MainScheduler.ThreadPoolLock.Acquire;
    308     with TMicroThreadThread(MainScheduler.ThreadPool[Item.Index]) do begin
    309       Item.Caption := IntToStr(ThreadID);
    310       Item.SubItems.Add(MicroThreadThreadStateText[State]);
    311     end;
    312   finally
    313     MainScheduler.ThreadPoolLock.Release;
    314   end;
    315322end;
    316323
     
    330337end;
    331338
     339procedure TMainForm.SpinEdit6Change(Sender: TObject);
     340begin
     341end;
     342
    332343procedure TMainForm.TimerRedrawTimer(Sender: TObject);
    333344begin
    334   if ListView2.Items.Count <> MainScheduler.ThreadPoolCount then
    335     ListView2.Items.Count := MainScheduler.ThreadPoolCount;
    336   ListView2.Items[-1];
    337   ListView2.Refresh;
    338 
    339345  Label2.Caption := DateTimeToStr(NowPrecise) + ' ' +
    340346    FloatToStr(Frac(NowPrecise / OneSecond));
     
    369375begin
    370376  for I := 0 to MainForm.Iterations - 1 do begin
    371     try
    372377    Q := 0;
    373     while Q < 100 do Inc(Q);
     378    while Q < 100000 do Inc(Q);
    374379    if MainForm.DoWriteToMemo then Synchronize(DoWriteToMemo);
    375380    if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond);
     
    379384      raise Exception.Create('Exception from microthread');
    380385    end;
     386    if MainForm.DoCriticalSection then begin
     387      try
     388        MainForm.Lock.Acquire;
     389        MTSleep(MainForm.CriticalSectionSleepDuration * OneMillisecond);
     390      finally
     391        MainForm.Lock.Release;
     392      end;
     393    end;
    381394    //WorkerSubRoutine;
    382395    Completion := I / MainForm.Iterations;
    383396    Yield;
    384 
    385     except
    386       Q := 0;
    387       raise  Exception.Create('Exception from microthread');
    388     end;
    389397  end;
    390398end;
     
    395403end;
    396404
     405constructor TWorker.Create(CreateSuspended: Boolean; const StackSize: SizeUInt);
     406begin
     407  inherited;
     408end;
     409
     410destructor TWorker.Destroy;
     411begin
     412  inherited Destroy;
     413end;
     414
    397415end.
    398416
Note: See TracChangeset for help on using the changeset viewer.