Ignore:
Timestamp:
Jan 28, 2011, 9:10:16 PM (14 years ago)
Author:
george
Message:
  • Fixed: WaitForEvent functionality. Now TEvent.WaitFor will chane microthread to blocked state with defined timeout. TEvent.SetSignal method will unblock all waiting microthreads.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/UMicroThreading.pas

    r157 r158  
    178178    FUseMainThread: Boolean;
    179179    FMainThreadStarter: TTimer;
     180    FEvents: TObjectList;
    180181    function GetMicroThreadCount: Integer;
    181182    function GetThreadPoolCount: Integer;
     
    322323  I: Integer;
    323324begin
    324   for I := 0 to FMicroThreads.Count - 1 do
    325     TMicroThread(FMicroThreads[I]).FStatePending := tsWaiting;
    326   if not FAutoReset then FSignaled := True;
     325  try
     326    MainScheduler.FMicroThreadsLock.Acquire;
     327    for I := 0 to FMicroThreads.Count - 1 do
     328    with TMicroThread(FMicroThreads[I]) do begin
     329      if (FState = tsBlocked) and (FBlockState = tbsWaitFor) then
     330        FState := tsWaiting;
     331    end;
     332    if not FAutoReset then FSignaled := True;
     333  finally
     334    MainScheduler.FMicroThreadsLock.Release;
     335  end;
    327336end;
    328337
     
    342351constructor TMicroThreadEvent.Create;
    343352begin
     353  FAutoReset := True;
    344354  FMicroThreads := TObjectList.Create;
    345355  FMicroThreads.OwnsObjects := False;
    346356  FMicroThreadsLock := TCriticalSection.Create;
     357  MainScheduler.FEvents.Add(Self);
    347358end;
    348359
    349360destructor TMicroThreadEvent.Destroy;
    350361begin
     362  MainScheduler.FEvents.Delete(MainScheduler.FEvents.IndexOf(Self));
    351363  FMicroThreadsLock.Free;
    352364  FMicroThreads.Free;
     
    399411    end;
    400412    FCurrentMicroThread.CheckStack;
    401     if FCurrentMicroThread = nil then
    402       raise Exception.Create('x');
    403413    FScheduler.ReleaseMicroThread(FCurrentMicroThread);
    404414  end;
     
    588598  if not Assigned(FManager) then
    589599    raise Exception.Create('Manager reference lost');
    590   FStatePending := tsWaiting;
     600  if FStatePending = tsNone then
     601    FStatePending := tsWaiting;
    591602  FManager.Yield;
    592603end;
     
    613624    Event.FMicroThreadsLock.Acquire;
    614625    Event.FMicroThreads.Add(Self);
     626    FBlockTime := NowPrecise + Duration;
     627    FBlockState := tbsWaitFor;
     628    FStatePending := tsBlocked;
    615629  finally
    616630    Event.FMicroThreadsLock.Release;
    617631  end;
    618   FBlockTime := NowPrecise + Duration;
    619   FBlockState := tbsWaitFor;
    620   FStatePending := tsBlocked;
    621632  Yield;
    622633  try
     
    642653  end;
    643654  FFreeOnTerminate := True;
     655  MainScheduler.Add(Self);
    644656end;
    645657
     
    718730constructor TMicroThreadScheduler.Create;
    719731begin
     732  FEvents := TObjectList.Create;
    720733  FMainThreadStarter := TTimer.Create(nil);
    721734  FMainThreadStarter.Enabled := False;
     
    742755  FMicroThreads.Free;
    743756  FMicroThreadsLock.Free;
     757  FEvents.Free;
    744758  inherited Destroy;
    745759end;
     
    878892begin
    879893  if not Assigned(MicroThread) then
    880     raise Exception.Create('Can''t realease nil thread.');
     894    raise Exception.Create('Can''t release nil thread.');
    881895  try
    882896    FMicroThreadsLock.Acquire;
Note: See TracChangeset for help on using the changeset viewer.