Just like the heap is implemented using a heap manager, and widestring management is left to a widestring manager, the threads have been implemented using a thread manager. This means that there is a record which has fields of procedural type for all possible functions used in the thread routines. The thread routines use these fields to do the actual work.
The thread routines install a system thread manager specific for each system. On Windows, the normal Windows routines are used to implement the functions in the thread manager. On Linux and other unices, the system thread manager does nothing: it will generate an error when thread routines are used. The rationale is that the routines for thread management are located in the C library. Implementing the system thread manager would make the RTL dependent on the C library, which is not desirable. To avoid dependency on the C library, the Thread Manager is implemented in a separate unit (cthreads). The initialization code of this unit sets the thread manager to a thread manager record which uses the C (pthreads) routines.
The thread manager record can be retrieved and set just as the record for the heap manager. The record looks (currently) as follows:
TThreadManager = Record InitManager : Function : Boolean; DoneManager : Function : Boolean; BeginThread : TBeginThreadHandler; EndThread : TEndThreadHandler; SuspendThread : TThreadHandler; ResumeThread : TThreadHandler; KillThread : TThreadHandler; ThreadSwitch : TThreadSwitchHandler; WaitForThreadTerminate : TWaitForThreadTerminateHandler; ThreadSetPriority : TThreadSetPriorityHandler; ThreadGetPriority : TThreadGetPriorityHandler; GetCurrentThreadId : TGetCurrentThreadIdHandler; InitCriticalSection : TCriticalSectionHandler; DoneCriticalSection : TCriticalSectionHandler; EnterCriticalSection : TCriticalSectionHandler; LeaveCriticalSection : TCriticalSectionHandler; InitThreadVar : TInitThreadVarHandler; RelocateThreadVar : TRelocateThreadVarHandler; AllocateThreadVars : TAllocateThreadVarsHandler; ReleaseThreadVars : TReleaseThreadVarsHandler; end;
The meaning of most of these functions should be obvious from the descriptions in previous sections.
The InitManager and DoneManager are called when the threadmanager is set (InitManager), or when it is unset (DoneManager). They can be used to initialize the thread manager or to clean up when it is done. If either of them returns False, the operation fails.
There are some special entries in the record, linked to thread variable management:
is called when a thread variable must be initialized. It is of type
TInitThreadVarHandler = Procedure(var offset: dword; size: dword);
The offset parameter indicates the offset in the thread variable block: All thread variables are located in a single block, one after the other. The size parameter indicates the size of the thread variable. This function will be called once for all thread variables in the program.
is called each time when a thread is started, and once for the main thread. It is of type:
TRelocateThreadVarHandler = Function(offset : dword) : pointer;
It should return the new location for the thread-local variable.
is called when room must be allocated for all threadvars for a new thread. It’s a simple procedure, without parameters. The total size of the threadvars is stored by the compiler in the threadvarblocksize global variable. The heap manager may not be used in this procedure: the heap manager itself uses threadvars, which have not yet been allocated.
This procedure (without parameters) is called when a thread terminates, and all memory allocated must be released again.