lazcollections: TLazThreadedQueue, allow subclasses to Lock/Unlock and access queue.

git-svn-id: trunk@63827 -
This commit is contained in:
martin 2020-08-25 17:31:49 +00:00
parent 3c6130a277
commit 005fe7208d

View File

@ -38,6 +38,8 @@ type
{ TThreadedQueue }
{ TLazThreadedQueue }
generic TLazThreadedQueue<T> = class
private
FMonitor: TLazMonitor;
@ -50,8 +52,13 @@ type
FHasRoomEvent: PRTLEvent;
FHasItemEvent: PRTLEvent;
FShutDown: boolean;
function TryPushItem(const AItem: T): boolean;
function TryPopItem(out AItem: T): boolean;
function TryPushItem(const AItem: T): boolean; inline;
function TryPopItem(out AItem: T): boolean; inline;
protected
function TryPushItemUnprotected(const AItem: T): boolean;
function TryPopItemUnprotected(out AItem: T): boolean;
procedure Lock;
procedure Unlock;
public
constructor create(AQueueDepth: Integer = 10; PushTimeout: cardinal = INFINITE; PopTimeout: cardinal = INFINITE);
destructor Destroy; override;
@ -149,6 +156,24 @@ function TLazThreadedQueue.TryPushItem(const AItem: T): boolean;
begin
FMonitor.Enter;
try
result := TryPushItemUnprotected(AItem);
finally
FMonitor.Leave;
end;
end;
function TLazThreadedQueue.TryPopItem(out AItem: T): boolean;
begin
FMonitor.Enter;
try
result := TryPopItemUnprotected(AItem);
finally
FMonitor.Leave;
end;
end;
function TLazThreadedQueue.TryPushItemUnprotected(const AItem: T): boolean;
begin
result := FTotalItemsPushed-FTotalItemsPopped<FQueueSize;
if result then
begin
@ -159,15 +184,10 @@ begin
RTLeventResetEvent(FHasRoomEvent);
if FTotalItemsPushed-FTotalItemsPopped<FQueueSize then
RTLeventSetEvent(FHasRoomEvent);
finally
FMonitor.Leave;
end;
end;
function TLazThreadedQueue.TryPopItem(out AItem: T): boolean;
function TLazThreadedQueue.TryPopItemUnprotected(out AItem: T): boolean;
begin
FMonitor.Enter;
try
result := FTotalItemsPushed>FTotalItemsPopped;
if result then
begin
@ -178,9 +198,16 @@ begin
RTLeventResetEvent(FHasItemEvent);
if FTotalItemsPushed > FTotalItemsPopped then
RTLeventSetEvent(FHasItemEvent);
finally
end;
procedure TLazThreadedQueue.Lock;
begin
FMonitor.Enter;
end;
procedure TLazThreadedQueue.Unlock;
begin
FMonitor.Leave;
end;
end;
constructor TLazThreadedQueue.create(AQueueDepth: Integer; PushTimeout: cardinal; PopTimeout: cardinal);