Is there a preferred way of reading back whether something (including the current thread) has already entered a TCriticalSection?

I'm trying to put assertions in code that, partly under non-GUI thread control via Synchronize, adds and deletes pages to a TPageControl. If I do this

property BloodyGreatLock: boolean read GetBGL write SetBGL;
..
        fBGL: TCriticalSection;
..
function GetBGL: boolean;

var     gotLock: boolean;

begin
  try
    gotLock := fBGL.TryEnter
  finally
    if gotLock then
      fBGL.Leave
  end;
  result := not gotLock
end { GetBGL } ;

then this initialisation code and test sequence

initialization
  fBGL := TCriticalSection.Create;
  Assert(BloodyGreatLock = false); (* Check lock read works properly  *)
  Assert(BloodyGreatLock = false);
  BloodyGreatLock := true;
  Assert(BloodyGreatLock = true); <===== FAILURE HERE
  Assert(BloodyGreatLock = true);

fails where indicated since the lock is owned by the current thread so TryEnter has succeeded.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to