From 387f91f5a1a9018841e3829a8aeb068e1b24bea4 Mon Sep 17 00:00:00 2001 From: mgaertner Date: Thu, 20 Aug 2009 15:36:29 +0000 Subject: [PATCH] multithreadprocs: added utility functions for blocks git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@938 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/multithreadprocs/mtprocs.pas | 37 ++++++++++++++++++++++-- components/multithreadprocs/mtputils.pas | 3 +- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/components/multithreadprocs/mtprocs.pas b/components/multithreadprocs/mtprocs.pas index de2541319..986fd2d7d 100644 --- a/components/multithreadprocs/mtprocs.pas +++ b/components/multithreadprocs/mtprocs.pas @@ -58,6 +58,8 @@ type destructor Destroy; override; function WaitForIndexRange(StartIndex, EndIndex: PtrInt): boolean; function WaitForIndex(Index: PtrInt): boolean; inline; + procedure CalcBlock(Index, BlockSize, LoopLength: PtrInt; + out BlockStart, BlockEnd: PtrInt); inline; property Index: PtrInt read FIndex; property Group: TProcThreadGroup read FGroup; property WaitingForIndexStart: PtrInt read FWaitingForIndexStart; @@ -171,10 +173,12 @@ type StartIndex, EndIndex: PtrInt; Data: Pointer = nil; MaxThreads: PtrInt = 0); public - constructor Create; - destructor Destroy; override; + // for debugging only: the critical section is public: procedure EnterPoolCriticalSection; inline; procedure LeavePoolCriticalSection; inline; + public + constructor Create; + destructor Destroy; override; procedure DoParallel(const AMethod: TMTMethod; StartIndex, EndIndex: PtrInt; @@ -187,6 +191,10 @@ type procedure DoParallelLocalProc(const LocalProc: Pointer; StartIndex, EndIndex: PtrInt; Data: Pointer = nil; MaxThreads: PtrInt = 0); // do not make this inline! + + // utility functions for loops: + procedure CalcBlockSize(LoopLength: PtrInt; + out BlockCount, BlockSize: PtrInt; MinBlockSize: PtrInt = 0); inline; public property MaxThreadCount: PtrInt read FMaxThreadCount write SetMaxThreadCount; property ThreadCount: PtrInt read FThreadCount; @@ -195,6 +203,7 @@ type var ProcThreadPool: TProcThreadPool = nil; + implementation { TMultiThreadProcItem } @@ -251,6 +260,15 @@ begin Result:=WaitForIndexRange(Index,Index); end; +procedure TMultiThreadProcItem.CalcBlock(Index, BlockSize, LoopLength: PtrInt; + out BlockStart, BlockEnd: PtrInt); +begin + BlockStart:=BlockSize*Index; + BlockEnd:=BlockStart+BlockSize; + if LoopLength