Changeset 169


Ignore:
Timestamp:
Feb 11, 2011, 2:16:17 PM (14 years ago)
Author:
george
Message:
  • Modified: Reducing some spare methods.
Location:
MicroThreading
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/Demo.lpi

    r168 r169  
    5959        <ResourceBaseClass Value="Form"/>
    6060        <UnitName Value="UMainForm"/>
    61         <EditorIndex Value="13"/>
    62         <WindowIndex Value="0"/>
    63         <TopLine Value="393"/>
    64         <CursorPos X="29" Y="406"/>
     61        <EditorIndex Value="12"/>
     62        <WindowIndex Value="0"/>
     63        <TopLine Value="407"/>
     64        <CursorPos X="54" Y="428"/>
    6565        <UsageCount Value="270"/>
    6666        <Loaded Value="True"/>
     
    7373        <EditorIndex Value="0"/>
    7474        <WindowIndex Value="0"/>
    75         <TopLine Value="885"/>
    76         <CursorPos X="1" Y="897"/>
     75        <TopLine Value="449"/>
     76        <CursorPos X="1" Y="462"/>
    7777        <UsageCount Value="136"/>
    7878        <Loaded Value="True"/>
     
    241241        <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/FPC/packages/fcl-base/src/syncobjs.pp"/>
    242242        <UnitName Value="syncobjs"/>
    243         <EditorIndex Value="12"/>
     243        <EditorIndex Value="11"/>
    244244        <WindowIndex Value="0"/>
    245245        <TopLine Value="35"/>
     
    310310        <Filename Value="../UStackTrace.pas"/>
    311311        <UnitName Value="UStackTrace"/>
    312         <EditorIndex Value="9"/>
    313312        <WindowIndex Value="0"/>
    314313        <TopLine Value="17"/>
    315314        <CursorPos X="26" Y="122"/>
    316315        <UsageCount Value="11"/>
    317         <Loaded Value="True"/>
    318316      </Unit31>
    319317      <Unit32>
    320318        <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/FPC/rtl/inc/systemh.inc"/>
    321         <EditorIndex Value="10"/>
     319        <EditorIndex Value="9"/>
    322320        <WindowIndex Value="0"/>
    323321        <TopLine Value="974"/>
     
    328326      <Unit33>
    329327        <Filename Value="../../../../Projekty2/FreePascalManager/trunk/Instance/1/FPC/rtl/i386/i386.inc"/>
    330         <EditorIndex Value="11"/>
     328        <EditorIndex Value="10"/>
    331329        <WindowIndex Value="0"/>
    332330        <TopLine Value="1135"/>
     
    336334      </Unit33>
    337335    </Units>
    338     <JumpHistory Count="30" HistoryIndex="29">
     336    <JumpHistory Count="14" HistoryIndex="13">
    339337      <Position1>
    340         <Filename Value="../UStackTrace.pas"/>
    341         <Caret Line="76" Column="60" TopLine="63"/>
     338        <Filename Value="../UMicroThreading.pas"/>
     339        <Caret Line="854" Column="3" TopLine="847"/>
    342340      </Position1>
    343341      <Position2>
    344         <Filename Value="../UStackTrace.pas"/>
    345         <Caret Line="74" Column="17" TopLine="63"/>
     342        <Filename Value="../UMicroThreading.pas"/>
     343        <Caret Line="853" Column="41" TopLine="847"/>
    346344      </Position2>
    347345      <Position3>
    348         <Filename Value="../UStackTrace.pas"/>
    349         <Caret Line="63" Column="27" TopLine="49"/>
     346        <Filename Value="../UMicroThreading.pas"/>
     347        <Caret Line="854" Column="1" TopLine="847"/>
    350348      </Position3>
    351349      <Position4>
    352         <Filename Value="../UStackTrace.pas"/>
    353         <Caret Line="79" Column="32" TopLine="64"/>
     350        <Filename Value="../UMicroThreading.pas"/>
     351        <Caret Line="855" Column="1" TopLine="847"/>
    354352      </Position4>
    355353      <Position5>
    356         <Filename Value="../UStackTrace.pas"/>
    357         <Caret Line="76" Column="66" TopLine="61"/>
     354        <Filename Value="../UMicroThreadCallStack.pas"/>
     355        <Caret Line="67" Column="38" TopLine="46"/>
    358356      </Position5>
    359357      <Position6>
    360         <Filename Value="../UStackTrace.pas"/>
    361         <Caret Line="74" Column="1" TopLine="61"/>
     358        <Filename Value="../UMicroThreadCallStack.pas"/>
     359        <Caret Line="66" Column="19" TopLine="47"/>
    362360      </Position6>
    363361      <Position7>
    364         <Filename Value="../UStackTrace.pas"/>
    365         <Caret Line="75" Column="1" TopLine="61"/>
     362        <Filename Value="../UMicroThreadCallStack.pas"/>
     363        <Caret Line="67" Column="14" TopLine="47"/>
    366364      </Position7>
    367365      <Position8>
    368         <Filename Value="../UStackTrace.pas"/>
    369         <Caret Line="76" Column="1" TopLine="61"/>
     366        <Filename Value="../UMicroThreadCallStack.pas"/>
     367        <Caret Line="66" Column="22" TopLine="44"/>
    370368      </Position8>
    371369      <Position9>
    372         <Filename Value="../UStackTrace.pas"/>
    373         <Caret Line="74" Column="1" TopLine="61"/>
     370        <Filename Value="../UMicroThreadList.pas"/>
     371        <Caret Line="138" Column="10" TopLine="121"/>
    374372      </Position9>
    375373      <Position10>
    376         <Filename Value="../UStackTrace.pas"/>
    377         <Caret Line="75" Column="1" TopLine="61"/>
     374        <Filename Value="UMainForm.pas"/>
     375        <Caret Line="428" Column="43" TopLine="411"/>
    378376      </Position10>
    379377      <Position11>
    380         <Filename Value="../UMicroThreading.pas"/>
    381         <Caret Line="854" Column="3" TopLine="847"/>
     378        <Filename Value="UMainForm.pas"/>
     379        <Caret Line="116" Column="33" TopLine="103"/>
    382380      </Position11>
    383381      <Position12>
    384         <Filename Value="../UMicroThreading.pas"/>
    385         <Caret Line="853" Column="41" TopLine="847"/>
     382        <Filename Value="UMainForm.pas"/>
     383        <Caret Line="305" Column="44" TopLine="284"/>
    386384      </Position12>
    387385      <Position13>
    388         <Filename Value="../UMicroThreading.pas"/>
    389         <Caret Line="854" Column="1" TopLine="847"/>
     386        <Filename Value="UMainForm.pas"/>
     387        <Caret Line="399" Column="54" TopLine="378"/>
    390388      </Position13>
    391389      <Position14>
    392         <Filename Value="../UMicroThreading.pas"/>
    393         <Caret Line="855" Column="1" TopLine="847"/>
     390        <Filename Value="UMainForm.pas"/>
     391        <Caret Line="428" Column="54" TopLine="407"/>
    394392      </Position14>
    395       <Position15>
    396         <Filename Value="../UStackTrace.pas"/>
    397         <Caret Line="74" Column="1" TopLine="61"/>
    398       </Position15>
    399       <Position16>
    400         <Filename Value="../UStackTrace.pas"/>
    401         <Caret Line="75" Column="1" TopLine="61"/>
    402       </Position16>
    403       <Position17>
    404         <Filename Value="../UStackTrace.pas"/>
    405         <Caret Line="76" Column="1" TopLine="61"/>
    406       </Position17>
    407       <Position18>
    408         <Filename Value="../UStackTrace.pas"/>
    409         <Caret Line="74" Column="1" TopLine="61"/>
    410       </Position18>
    411       <Position19>
    412         <Filename Value="../UStackTrace.pas"/>
    413         <Caret Line="75" Column="50" TopLine="61"/>
    414       </Position19>
    415       <Position20>
    416         <Filename Value="../UStackTrace.pas"/>
    417         <Caret Line="74" Column="45" TopLine="61"/>
    418       </Position20>
    419       <Position21>
    420         <Filename Value="../UStackTrace.pas"/>
    421         <Caret Line="73" Column="29" TopLine="61"/>
    422       </Position21>
    423       <Position22>
    424         <Filename Value="../UStackTrace.pas"/>
    425         <Caret Line="76" Column="1" TopLine="61"/>
    426       </Position22>
    427       <Position23>
    428         <Filename Value="../UStackTrace.pas"/>
    429         <Caret Line="91" Column="1" TopLine="78"/>
    430       </Position23>
    431       <Position24>
    432         <Filename Value="../UMicroThreadCallStack.pas"/>
    433         <Caret Line="67" Column="38" TopLine="46"/>
    434       </Position24>
    435       <Position25>
    436         <Filename Value="../UStackTrace.pas"/>
    437         <Caret Line="76" Column="1" TopLine="63"/>
    438       </Position25>
    439       <Position26>
    440         <Filename Value="../UStackTrace.pas"/>
    441         <Caret Line="78" Column="1" TopLine="63"/>
    442       </Position26>
    443       <Position27>
    444         <Filename Value="../UMicroThreadCallStack.pas"/>
    445         <Caret Line="66" Column="19" TopLine="47"/>
    446       </Position27>
    447       <Position28>
    448         <Filename Value="../UMicroThreadCallStack.pas"/>
    449         <Caret Line="67" Column="14" TopLine="47"/>
    450       </Position28>
    451       <Position29>
    452         <Filename Value="../UMicroThreadCallStack.pas"/>
    453         <Caret Line="66" Column="22" TopLine="44"/>
    454       </Position29>
    455       <Position30>
    456         <Filename Value="../UMicroThreadList.pas"/>
    457         <Caret Line="138" Column="10" TopLine="121"/>
    458       </Position30>
    459393    </JumpHistory>
    460394  </ProjectOptions>
  • MicroThreading/Demo/UMainForm.pas

    r166 r169  
    303303procedure TMainForm.CheckBox4Change(Sender: TObject);
    304304begin
    305   CriticalSectionSleepDuration := SpinEdit4.Value;
     305  CriticalSectionSleepDuration := SpinEdit6.Value;
    306306  DoCriticalSection := CheckBox4.Checked;
    307307end;
  • MicroThreading/UMicroThreading.pas

    r168 r169  
    3535  SReleaseNotAcquiredLock = 'Release on not acquired lock';
    3636  SMethodNotAssigned = 'Method for microthread not assigned';
     37  SCriticalSectionDecrement = 'Critical section counter decremented to negative number';
    3738
    3839
     
    117118    procedure Yield;
    118119    procedure MTSleep(Duration: TDateTime); // No conflicting name to global Sleep procedure
    119     procedure WaitForCriticalSection(CriticalSection: TMicroThreadCriticalSection);
    120     function WaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult;
    121120    procedure WaitFor;
    122121    procedure Terminate;
     
    236235    BurstCount: Integer;
    237236    function Add(MicroThread: TMicroThread): Integer;
    238     function AddMethod(Method: TProcedureOfObject; WaitForFinish: Boolean = False): Integer;
     237    function AddMethod(Method: TProcedureOfObject; WaitForFinish: Boolean = True): Integer;
    239238    procedure Remove(MicroThread: TMicroThread; Free: Boolean = True);
    240239    constructor Create;
     
    278277procedure MTSleep(Duration: TDateTime);
    279278procedure MTSynchronize(Method: TThreadMethod);
    280 function MTWaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult;
    281279procedure Log(Text: string);
    282280procedure Register;
     
    345343end;
    346344
    347 function MTWaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult;
    348 var
    349   MT: TMicroThread;
    350 begin
    351   MT := GetCurrentMicroThread;
    352   if Assigned(MT) then Result := MT.WaitForEvent(Event, Duration)
    353     else raise EMicroThreadError.Create(SNotInMicroThread);
    354 end;
    355 
    356345var
    357346  LogLock: TCriticalSection;
     
    381370begin
    382371  MT := GetCurrentMicroThread;
    383   if Assigned(MT) then MT.WaitForCriticalSection(Self)
    384     else raise EMicroThreadError.Create(SNotInMicroThread);
     372  if Assigned(MT) then
     373  try
     374    MainScheduler.FMicroThreadsLock.Acquire;
     375    Lock.Acquire;
     376    Inc(FCounter);
     377    if FCounter > 1 then begin
     378      FMicroThreads.Add(MT);
     379      MT.FBlockState := tbsCriticalSection;
     380      MT.FStatePending := tsBlocked;
     381      try
     382        Lock.Release;
     383        MainScheduler.FMicroThreadsLock.Release;
     384        MT.Yield;
     385      finally
     386        MainScheduler.FMicroThreadsLock.Acquire;
     387        Lock.Acquire;
     388      end;
     389    end;
     390  finally
     391    Lock.Release;
     392    MainScheduler.FMicroThreadsLock.Release;
     393  end else
     394    raise EMicroThreadError.Create(SNotInMicroThread);
    385395end;
    386396
     
    394404      // Release one waiting micro thread
    395405      TMicroThread(FMicroThreads[0]).FState := tsWaiting;
     406      TMicroThread(FMicroThreads[0]).FStatePending := tsNone;
    396407      FMicroThreads.Delete(0);
    397408    end;
     409    if FCounter < 0 then
     410      raise EMicroThreadError.Create(SCriticalSectionDecrement);
    398411  finally
    399412    Lock.Release;
     
    468481begin
    469482  MT := GetCurrentMicroThread;
    470   if Assigned(MT) then Result := MT.WaitForEvent(Self, Duration)
    471     else raise EMicroThreadError.Create(SNotInMicroThread);
     483  if Assigned(MT) then begin
     484    try
     485      FMicroThreadsLock.Acquire;
     486      if Signaled then begin
     487        Result := wrSignaled;
     488        Exit;
     489      end;
     490      FMicroThreads.Add(Self);
     491      MT.FBlockTime := NowPrecise + Duration;
     492      MT.FBlockState := tbsWaitFor;
     493      MT.FStatePending := tsBlocked;
     494    finally
     495      FMicroThreadsLock.Release;
     496    end;
     497    MT.Yield;
     498    if (MT.FBlockTime <> 0) and (MT.FBlockTime < NowPrecise) then
     499      Result := wrTimeout else Result := wrSignaled;
     500
     501    try
     502      FMicroThreadsLock.Acquire;
     503      FMicroThreads.Remove(Self);
     504    finally
     505      FMicroThreadsLock.Release;
     506    end
     507  end else
     508    raise EMicroThreadError.Create(SNotInMicroThread);
    472509end;
    473510
     
    773810    // Called from another microthread
    774811    while not ((FState = tsBlocked) and (FBlockState = tbsTerminated)) do begin
    775       MTSleep(1);
     812      MTSleep(1 * OneMillisecond);
    776813    end;
    777814  end else begin
     
    792829end;
    793830
    794 procedure TMicroThread.WaitForCriticalSection(
    795   CriticalSection: TMicroThreadCriticalSection);
    796 begin
    797   try
    798     FScheduler.FMicroThreadsLock.Acquire;
    799     CriticalSection.Lock.Acquire;
    800     Inc(CriticalSection.FCounter);
    801     if CriticalSection.FCounter > 1 then begin
    802       CriticalSection.FMicroThreads.Add(Self);
    803       FBlockState := tbsCriticalSection;
    804       FStatePending := tsBlocked;
    805       try
    806         CriticalSection.Lock.Release;
    807         FScheduler.FMicroThreadsLock.Release;
    808         Yield;
    809       finally
    810         FScheduler.FMicroThreadsLock.Acquire;
    811         CriticalSection.Lock.Acquire;
    812       end;
    813     end;
    814   finally
    815     CriticalSection.Lock.Release;
    816     FScheduler.FMicroThreadsLock.Release;
    817   end;
    818 end;
    819 
    820 function TMicroThread.WaitForEvent(Event: TMicroThreadEvent; Duration: TDateTime): TWaitResult;
    821 begin
    822   try
    823     Event.FMicroThreadsLock.Acquire;
    824     if Event.Signaled then begin
    825       Result := wrSignaled;
    826       Exit;
    827     end;
    828     Event.FMicroThreads.Add(Self);
    829     FBlockTime := NowPrecise + Duration;
    830     FBlockState := tbsWaitFor;
    831     FStatePending := tsBlocked;
    832   finally
    833     Event.FMicroThreadsLock.Release;
    834   end;
    835   Yield;
    836   if (FBlockTime <> 0) and (FBlockTime < NowPrecise) then Result := wrTimeout
    837     else Result := wrSignaled;
    838 
    839   try
    840     Event.FMicroThreadsLock.Acquire;
    841     Event.FMicroThreads.Remove(Self);
    842   finally
    843     Event.FMicroThreadsLock.Release;
    844   end;
    845 end;
    846 
    847831constructor TMicroThread.Create(CreateSuspended: Boolean;
    848832  const StackSize: SizeUInt = DefaultStackSize);
     
    851835  FStackSize := StackSize;
    852836  FStack := GetMem(FStackSize);
    853   FBasePointer := FStack + FStackSize - SizeOf(Pointer);
    854   FStackPointer := FBasePointer - SizeOf(Pointer);
     837  FBasePointer := 0; // FStack + FStackSize - SizeOf(Pointer);
     838  FStackPointer := FStack + FStackSize - 2 * SizeOf(Pointer);
    855839  FillChar(FStackPointer^, 2 * SizeOf(Pointer), 0);
    856840
Note: See TracChangeset for help on using the changeset viewer.