mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-16 08:19:34 +02:00

Especially for the introduction of the Queue method the internal Synchronize handling was modified. Instead of handling only one event there is now a queue of events which is walked completely when CheckSynchronize is called. Each entry in the queue can carry a PRTLEvent which will be signaled when the contained method has been executed and thus Synchronize methods can still be blocking. Exceptions inside the queued methods are either handed back to the calling method for Synchronize events or raised directly (after leaving the queue in a valid state) to the caller of CheckSynchronize. The way platform specific adjustments can be made to TThread was changed. Instead of implementing the Constructor and Destructor directly one now implements the methods SysCreate and SysDestroy which are called from the Constructor and Destructor respectively. All RTLs were adjusted for this and should be controlled by the platform maintainers for correct compilation (Unix works). The new method NameThreadForDebugging has two overloaded variants: one with the thread name as AnsiString and one with the thread name as UnicodeString. By default the AnsiString variant calls the UnicodeString variant and the latter needs to be implemented. This can be changed by defining THREADNAME_IS_ANSISTRING for a platform. Then the UnicodeString variant calls the AnsiString one and the AnsiString one needs to be implemented. Also added was a global property CPUCount for the System unit. This property returns the number of virtual cores of the system. New methods and functions that should be implemented per platform are: System.GetCPUCount (default returns 1) Classes.TThread.GetSystemTimes (default zeros the struct) Classes.TThread.NameThreadForDebugging (default does nothing) More detailed information about the added methods will be available in the feature announcement mail. git-svn-id: trunk@23227 -
171 lines
8.3 KiB
PHP
171 lines
8.3 KiB
PHP
{
|
|
This file is part of the Free Pascal Run time library.
|
|
Copyright (c) 2000 by the Free Pascal development team
|
|
|
|
This file contains the OS indenpendend declartions for multi
|
|
threading support in FPC
|
|
|
|
See the File COPYING.FPC, included in this distribution,
|
|
for details about the copyright.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
**********************************************************************}
|
|
|
|
const
|
|
{ includes 16384 bytes margin for stackchecking }
|
|
DefaultStackSize = 4*1024*1024;
|
|
|
|
{ every platform can have it's own implementation of GetCPUCount; use the define
|
|
HAS_GETCPUCOUNT to disable the default implementation which simply returns 1 }
|
|
function GetCPUCount: LongWord;
|
|
|
|
property CPUCount: LongWord read GetCPUCount;
|
|
|
|
type
|
|
PEventState = pointer;
|
|
PRTLEvent = type pointer; // Windows=thandle, other=pointer to record.
|
|
TThreadFunc = function(parameter : pointer) : ptrint;
|
|
trtlmethod = procedure of object;
|
|
|
|
// Function prototypes for TThreadManager Record.
|
|
TBeginThreadHandler = Function (sa : Pointer;stacksize : PtrUInt; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
|
|
TEndThreadHandler = Procedure (ExitCode : DWord);
|
|
// Used for Suspend/Resume/Kill
|
|
TThreadHandler = Function (threadHandle : TThreadID) : dword;
|
|
TThreadSwitchHandler = Procedure;
|
|
TWaitForThreadTerminateHandler = Function (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
|
|
TThreadSetPriorityHandler = Function (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
|
|
TThreadGetPriorityHandler = Function (threadHandle : TThreadID): longint;
|
|
TGetCurrentThreadIdHandler = Function : TThreadID;
|
|
TCriticalSectionHandler = Procedure (var cs);
|
|
TCriticalSectionHandlerTryEnter = function (var cs):longint;
|
|
TInitThreadVarHandler = Procedure(var offset : dword;size : dword);
|
|
TRelocateThreadVarHandler = Function(offset : dword) : pointer;
|
|
TAllocateThreadVarsHandler = Procedure;
|
|
TReleaseThreadVarsHandler = Procedure;
|
|
TBasicEventHandler = procedure(state:peventstate);
|
|
TBasicEventWaitForHandler = function (timeout:cardinal;state:peventstate):longint;
|
|
TBasicEventCreateHandler = function (EventAttributes :Pointer; AManualReset,InitialState : Boolean;const Name:ansistring):pEventState;
|
|
TRTLEventHandler = procedure(AEvent:PRTLEvent);
|
|
TRTLEventHandlerTimeout = procedure(AEvent:PRTLEvent;timeout : longint);
|
|
TRTLCreateEventHandler = function:PRTLEvent;
|
|
// semaphores stuff
|
|
TSempahoreInitHandler = function: Pointer;
|
|
TSemaphoreDestroyHandler = procedure (const sem: Pointer);
|
|
TSemaphorePostHandler = procedure (const sem: Pointer);
|
|
TSemaphoreWaitHandler = procedure (const sem: Pointer);
|
|
|
|
// TThreadManager interface.
|
|
TThreadManager = Record
|
|
InitManager : Function : Boolean;
|
|
DoneManager : Function : Boolean;
|
|
BeginThread : TBeginThreadHandler;
|
|
EndThread : TEndThreadHandler;
|
|
SuspendThread : TThreadHandler;
|
|
ResumeThread : TThreadHandler;
|
|
KillThread : TThreadHandler;
|
|
CloseThread : TThreadHandler;
|
|
ThreadSwitch : TThreadSwitchHandler;
|
|
WaitForThreadTerminate : TWaitForThreadTerminateHandler;
|
|
ThreadSetPriority : TThreadSetPriorityHandler;
|
|
ThreadGetPriority : TThreadGetPriorityHandler;
|
|
GetCurrentThreadId : TGetCurrentThreadIdHandler;
|
|
InitCriticalSection : TCriticalSectionHandler;
|
|
DoneCriticalSection : TCriticalSectionHandler;
|
|
EnterCriticalSection : TCriticalSectionHandler;
|
|
TryEnterCriticalSection: TCriticalSectionHandlerTryEnter;
|
|
LeaveCriticalSection : TCriticalSectionHandler;
|
|
InitThreadVar : TInitThreadVarHandler;
|
|
RelocateThreadVar : TRelocateThreadVarHandler;
|
|
AllocateThreadVars : TAllocateThreadVarsHandler;
|
|
ReleaseThreadVars : TReleaseThreadVarsHandler;
|
|
BasicEventCreate : TBasicEventCreateHandler; // left in for a while.
|
|
BasicEventDestroy : TBasicEventHandler; // we might need BasicEvent
|
|
BasicEventResetEvent : TBasicEventHandler; // for a real TEvent
|
|
BasicEventSetEvent : TBasicEventHandler;
|
|
BasiceventWaitFOr : TBasicEventWaitForHandler;
|
|
RTLEventCreate : TRTLCreateEventHandler;
|
|
RTLEventDestroy : TRTLEventHandler;
|
|
RTLEventSetEvent : TRTLEventHandler;
|
|
RTLEventResetEvent : TRTLEventHandler;
|
|
RTLEventWaitFor : TRTLEventHandler;
|
|
RTLEventWaitForTimeout : TRTLEventHandlerTimeout;
|
|
// semaphores stuff
|
|
SemaphoreInit : TSempahoreInitHandler;
|
|
SemaphoreDestroy : TSemaphoreDestroyHandler;
|
|
SemaphorePost : TSemaphorePostHandler;
|
|
SemaphoreWait : TSemaphoreWaitHandler;
|
|
end;
|
|
|
|
{*****************************************************************************
|
|
Thread Handler routines
|
|
*****************************************************************************}
|
|
|
|
|
|
Function GetThreadManager(Var TM : TThreadManager) : Boolean;
|
|
Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
|
|
Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
|
|
{$ifndef DISABLE_NO_THREAD_MANAGER}
|
|
{$endif DISABLE_NO_THREAD_MANAGER}
|
|
// Needs to be exported, so the manager can call it.
|
|
procedure InitThreadVars(RelocProc : TRelocateThreadVarHandler);
|
|
procedure InitThread(stklen:SizeUInt);
|
|
procedure DoneThread;
|
|
|
|
{*****************************************************************************
|
|
Multithread Handling
|
|
*****************************************************************************}
|
|
|
|
function BeginThread(sa : Pointer;stacksize : SizeUInt;
|
|
ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
|
|
var ThreadId : TThreadID) : TThreadID;
|
|
|
|
{ add some simplfied forms which make lifer easier and porting }
|
|
{ to other OSes too ... }
|
|
function BeginThread(ThreadFunction : tthreadfunc) : TThreadID;
|
|
function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : TThreadID;
|
|
function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : TThreadID) : TThreadID;
|
|
function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
|
|
var ThreadId : TThreadID; const stacksize: SizeUInt) : TThreadID;
|
|
|
|
procedure EndThread(ExitCode : DWord);
|
|
procedure EndThread;
|
|
|
|
{some thread support functions}
|
|
procedure FlushThread;
|
|
function SuspendThread (threadHandle : TThreadID) : dword;
|
|
function ResumeThread (threadHandle : TThreadID) : dword;
|
|
function CloseThread (threadHandle : TThreadID) : dword;
|
|
procedure ThreadSwitch; {give time to other threads}
|
|
function KillThread (threadHandle : TThreadID) : dword;
|
|
function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
|
|
function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
|
|
function ThreadGetPriority (threadHandle : TThreadID): longint;
|
|
function GetCurrentThreadId : TThreadID;
|
|
|
|
|
|
{ this allows to do a lot of things in MT safe way }
|
|
{ it is also used to make the heap management }
|
|
{ thread safe }
|
|
procedure InitCriticalSection(var cs : TRTLCriticalSection);
|
|
procedure DoneCriticalsection(var cs : TRTLCriticalSection);
|
|
procedure EnterCriticalsection(var cs : TRTLCriticalSection);
|
|
procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
|
|
function TryEnterCriticalsection(var cs : TRTLCriticalSection):longint;
|
|
function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
|
|
procedure basiceventdestroy(state:peventstate);
|
|
procedure basiceventResetEvent(state:peventstate);
|
|
procedure basiceventSetEvent(state:peventstate);
|
|
function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
|
|
|
|
function RTLEventCreate :PRTLEvent;
|
|
procedure RTLeventdestroy(state:pRTLEvent);
|
|
procedure RTLeventSetEvent(state:pRTLEvent);
|
|
procedure RTLeventResetEvent(state:pRTLEvent);
|
|
procedure RTLeventWaitFor(state:pRTLEvent);
|
|
procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
|
|
|