123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- {
- $Id$
- This file is part of the Free Pascal run time library.
- Copyright (c) 2002 by Peter Vreman,
- member of the Free Pascal development team.
- Win32 threading support implementation
- 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.
- **********************************************************************}
- unit systhrds;
- interface
- {$S-}
- type
- { the fields of this record are os dependent }
- { and they shouldn't be used in a program }
- { only the type TCriticalSection is important }
- PRTLCriticalSection = ^TRTLCriticalSection;
- TRTLCriticalSection = packed record
- DebugInfo : pointer;
- LockCount : longint;
- RecursionCount : longint;
- OwningThread : DWord;
- LockSemaphore : DWord;
- Reserved : DWord;
- end;
- { Include generic thread interface }
- {$i threadh.inc}
- implementation
- {*****************************************************************************
- Generic overloaded
- *****************************************************************************}
- { Include generic overloaded routines }
- {$i thread.inc}
- {*****************************************************************************
- Local WINApi imports
- *****************************************************************************}
- const
- { GlobalAlloc, GlobalFlags }
- GMEM_FIXED = 0;
- GMEM_ZEROINIT = 64;
- function TlsAlloc : DWord;
- external 'kernel32' name 'TlsAlloc';
- function TlsGetValue(dwTlsIndex : DWord) : pointer;
- external 'kernel32' name 'TlsGetValue';
- function TlsSetValue(dwTlsIndex : DWord;lpTlsValue : pointer) : LongBool;
- external 'kernel32' name 'TlsSetValue';
- function TlsFree(dwTlsIndex : DWord) : LongBool;
- external 'kernel32' name 'TlsFree';
- function CreateThread(lpThreadAttributes : pointer;
- dwStackSize : DWord; lpStartAddress : pointer;lpParameter : pointer;
- dwCreationFlags : DWord;var lpThreadId : DWord) : Dword;
- external 'kernel32' name 'CreateThread';
- procedure ExitThread(dwExitCode : DWord);
- external 'kernel32' name 'ExitThread';
- function GlobalAlloc(uFlags:DWord; dwBytes:DWORD):Pointer;
- external 'kernel32' name 'GlobalAlloc';
- function GlobalFree(hMem : Pointer):Pointer; external 'kernel32' name 'GlobalFree';
- {*****************************************************************************
- Threadvar support
- *****************************************************************************}
- {$ifdef HASTHREADVAR}
- const
- threadvarblocksize : dword = 0;
- var
- TLSKey : Dword;
- procedure SysInitThreadvar(var offset : dword;size : dword);
- begin
- offset:=threadvarblocksize;
- inc(threadvarblocksize,size);
- end;
- function SysRelocateThreadvar(offset : dword) : pointer;
- begin
- SysRelocateThreadvar:=TlsGetValue(tlskey)+Offset;
- end;
- procedure SysAllocateThreadVars;
- var
- dataindex : pointer;
- begin
- { we've to allocate the memory from system }
- { because the FPC heap management uses }
- { exceptions which use threadvars but }
- { these aren't allocated yet ... }
- { allocate room on the heap for the thread vars }
- dataindex:=pointer(GlobalAlloc(GMEM_FIXED or GMEM_ZEROINIT,threadvarblocksize));
- TlsSetValue(tlskey,dataindex);
- end;
- procedure SysReleaseThreadVars;
- begin
- GlobalFree(TlsGetValue(tlskey));
- end;
- { Include OS independent Threadvar initialization }
- {$i threadvr.inc}
- {$endif HASTHREADVAR}
- {*****************************************************************************
- Thread starting
- *****************************************************************************}
- type
- pthreadinfo = ^tthreadinfo;
- tthreadinfo = record
- f : tthreadfunc;
- p : pointer;
- stklen : cardinal;
- end;
- procedure DoneThread;
- begin
- { Release Threadvars }
- {$ifdef HASTHREADVAR}
- SysReleaseThreadVars;
- {$endif HASTHREADVAR}
- end;
- function ThreadMain(param : pointer) : pointer;cdecl;
- var
- ti : tthreadinfo;
- begin
- {$ifdef HASTHREADVAR}
- { Allocate local thread vars, this must be the first thing,
- because the exception management and io depends on threadvars }
- SysAllocateThreadVars;
- {$endif HASTHREADVAR}
- { Copy parameter to local data }
- {$ifdef DEBUG_MT}
- writeln('New thread started, initialising ...');
- {$endif DEBUG_MT}
- ti:=pthreadinfo(param)^;
- dispose(pthreadinfo(param));
- { Initialize thread }
- InitThread(ti.stklen);
- { Start thread function }
- {$ifdef DEBUG_MT}
- writeln('Jumping to thread function');
- {$endif DEBUG_MT}
- ThreadMain:=pointer(ti.f(ti.p));
- end;
- function BeginThread(sa : Pointer;stacksize : dword;
- ThreadFunction : tthreadfunc;p : pointer;
- creationFlags : dword; var ThreadId : DWord) : DWord;
- var
- ti : pthreadinfo;
- begin
- {$ifdef DEBUG_MT}
- writeln('Creating new thread');
- {$endif DEBUG_MT}
- { Initialize multithreading if not done }
- if not IsMultiThread then
- begin
- {$ifdef HASTHREADVAR}
- { We're still running in single thread mode, setup the TLS }
- TLSKey:=TlsAlloc;
- InitThreadVars(@SysRelocateThreadvar);
- {$endif HASTHREADVAR}
- IsMultiThread:=true;
- end;
- { the only way to pass data to the newly created thread
- in a MT safe way, is to use the heap }
- new(ti);
- ti^.f:=ThreadFunction;
- ti^.p:=p;
- ti^.stklen:=stacksize;
- { call pthread_create }
- {$ifdef DEBUG_MT}
- writeln('Starting new thread');
- {$endif DEBUG_MT}
- BeginThread:=CreateThread(sa,stacksize,@ThreadMain,ti,creationflags,threadid);
- BeginThread:=threadid;
- end;
- procedure EndThread(ExitCode : DWord);
- begin
- DoneThread;
- ExitThread(ExitCode);
- end;
- {*****************************************************************************
- Delphi/Win32 compatibility
- *****************************************************************************}
- { we implement these procedures for win32 by importing them }
- { directly from windows }
- procedure InitCriticalSection(var cs : TRTLCriticalSection);
- external 'kernel32' name 'InitializeCriticalSection';
- procedure DoneCriticalSection(var cs : TRTLCriticalSection);
- external 'kernel32' name 'DeleteCriticalSection';
- procedure EnterCriticalSection(var cs : TRTLCriticalSection);
- external 'kernel32' name 'EnterCriticalSection';
- procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
- external 'kernel32' name 'LeaveCriticalSection';
- {*****************************************************************************
- Heap Mutex Protection
- *****************************************************************************}
- var
- HeapMutex : TRTLCriticalSection;
- procedure Win32HeapMutexInit;
- begin
- InitCriticalSection(heapmutex);
- end;
- procedure Win32HeapMutexDone;
- begin
- DoneCriticalSection(heapmutex);
- end;
- procedure Win32HeapMutexLock;
- begin
- EnterCriticalSection(heapmutex);
- end;
- procedure Win32HeapMutexUnlock;
- begin
- LeaveCriticalSection(heapmutex);
- end;
- const
- Win32MemoryMutexManager : TMemoryMutexManager = (
- MutexInit : @Win32HeapMutexInit;
- MutexDone : @Win32HeapMutexDone;
- MutexLock : @Win32HeapMutexLock;
- MutexUnlock : @Win32HeapMutexUnlock;
- );
- procedure InitHeapMutexes;
- begin
- SetMemoryMutexManager(Win32MemoryMutexManager);
- end;
- initialization
- InitHeapMutexes;
- end.
- {
- $Log$
- Revision 1.2 2002-10-31 13:45:44 carl
- * threadvar.inc -> threadvr.inc
- Revision 1.1 2002/10/16 06:27:30 michael
- + Renamed thread unit to systhrds
- Revision 1.1 2002/10/14 19:39:18 peter
- * threads unit added for thread support
- }
-
|