systhrd.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Peter Vreman,
  5. member of the Free Pascal development team.
  6. Linux (pthreads) threading support implementation
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {*****************************************************************************
  14. Local Api imports
  15. *****************************************************************************}
  16. const
  17. pag_Read = 1;
  18. pag_Write = 2;
  19. pag_Execute = 4;
  20. pag_Guard = 8;
  21. pag_Commit = $10;
  22. obj_Tile = $40;
  23. sem_Indefinite_Wait = -1;
  24. dtSuspended = 1;
  25. dtStack_Commited = 2;
  26. { import the necessary stuff from the OS }
  27. function DosAllocThreadLocalMemory (Count: cardinal; var P: pointer): cardinal;
  28. cdecl; external 'DOSCALLS' index 454;
  29. function DosFreeThreadLocalMemory (P: pointer): cardinal; cdecl;
  30. external 'DOSCALLS' index 455;
  31. function DosCreateThread (var TID: cardinal; Address: pointer;
  32. (* TThreadFunc *)
  33. aParam: pointer; Flags: cardinal; StackSize: cardinal): cardinal; cdecl;
  34. external 'DOSCALLS' index 311;
  35. procedure DosExit (Action, Result: cardinal); cdecl;
  36. external 'DOSCALLS' index 234;
  37. function DosCreateMutExSem (Name: PChar; var Handle: longint; Attr: cardinal;
  38. State: boolean): cardinal; cdecl; external 'DOSCALLS' index 331;
  39. function DosCloseMutExSem (Handle: longint): cardinal; cdecl;
  40. external 'DOSCALLS' index 333;
  41. function DosQueryMutExSem (Handle: longint; var PID, TID, Count: cardinal):
  42. cardinal; cdecl; external 'DOSCALLS' index 336;
  43. function DosRequestMutExSem (Handle:longint; Timeout: cardinal): cardinal; cdecl;
  44. external 'DOSCALLS' index 334;
  45. function DosReleaseMutExSem (Handle: longint): cardinal; cdecl;
  46. external 'DOSCALLS' index 335;
  47. function DosAllocMem (var P: pointer; Size, Flag: cardinal): cardinal; cdecl;
  48. external 'DOSCALLS' index 299;
  49. function DosFreeMem (P: pointer): cardinal; cdecl;
  50. external 'DOSCALLS' index 304;
  51. function DosEnterCritSec:cardinal; cdecl; external 'DOSCALLS' index 232;
  52. function DosExitCritSec:cardinal; cdecl; external 'DOSCALLS' index 233;
  53. {*****************************************************************************
  54. Threadvar support
  55. *****************************************************************************}
  56. {$ifdef HASTHREADVAR}
  57. const
  58. ThreadVarBlockSize: dword = 0;
  59. var
  60. (* Pointer to an allocated dword space within the local thread *)
  61. (* memory area. Pointer to the real memory block allocated for *)
  62. (* thread vars in this block is then stored in this dword. *)
  63. DataIndex: PPointer;
  64. procedure SysInitThreadvar (var Offset: dword; Size: dword);
  65. begin
  66. Offset := ThreadVarBlockSize;
  67. Inc (ThreadVarBlockSize, Size);
  68. end;
  69. function SysRelocateThreadVar (Offset: dword): pointer;
  70. begin
  71. SysRelocateThreadVar := DataIndex^ + Offset;
  72. end;
  73. procedure SysAllocateThreadVars;
  74. begin
  75. { we've to allocate the memory from the OS }
  76. { because the FPC heap management uses }
  77. { exceptions which use threadvars but }
  78. { these aren't allocated yet ... }
  79. { allocate room on the heap for the thread vars }
  80. if DosAllocMem (DataIndex^, ThreadVarBlockSize, pag_Read or pag_Write
  81. or pag_Commit) <> 0 then HandleError (8);
  82. end;
  83. procedure SysReleaseThreadVars;
  84. begin
  85. { release thread vars }
  86. DosFreeMem (DataIndex^);
  87. end;
  88. procedure InitThreadVars;
  89. begin
  90. { allocate one ThreadVar entry from the OS, we use this entry }
  91. { for a pointer to our threadvars }
  92. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then HandleError (8);
  93. { initialize threadvars }
  94. init_all_unit_threadvars;
  95. { allocate mem for main thread threadvars }
  96. SysAllocateThreadVars;
  97. { copy main thread threadvars }
  98. copy_all_unit_threadvars;
  99. { install threadvar handler }
  100. fpc_threadvar_relocate_proc := @SysRelocateThreadvar;
  101. end;
  102. {$endif HASTHREADVAR}
  103. {*****************************************************************************
  104. Delphi/Win32 compatibility
  105. *****************************************************************************}
  106. { we implement these procedures for win32 by importing them }
  107. { directly from windows }
  108. procedure SysInitCriticalSection(var cs : TRTLCriticalSection);
  109. external 'kernel32' name 'InitializeCriticalSection';
  110. procedure SysDoneCriticalSection(var cs : TRTLCriticalSection);
  111. external 'kernel32' name 'DeleteCriticalSection';
  112. procedure SysEnterCriticalSection(var cs : TRTLCriticalSection);
  113. external 'kernel32' name 'EnterCriticalSection';
  114. procedure SysLeaveCriticalSection(var cs : TRTLCriticalSection);
  115. external 'kernel32' name 'LeaveCriticalSection';
  116. {*****************************************************************************
  117. Heap Mutex Protection
  118. *****************************************************************************}
  119. var
  120. HeapMutex : TRTLCriticalSection;
  121. procedure OS2HeapMutexInit;
  122. begin
  123. SysInitCriticalSection(heapmutex);
  124. end;
  125. procedure OS2HeapMutexDone;
  126. begin
  127. SysDoneCriticalSection(heapmutex);
  128. end;
  129. procedure OS2HeapMutexLock;
  130. begin
  131. SysEnterCriticalSection(heapmutex);
  132. end;
  133. procedure OS2HeapMutexUnlock;
  134. begin
  135. SysLeaveCriticalSection(heapmutex);
  136. end;
  137. const
  138. OS2MemoryMutexManager : TMemoryMutexManager = (
  139. MutexInit : @OS2HeapMutexInit;
  140. MutexDone : @OS2HeapMutexDone;
  141. MutexLock : @OS2HeapMutexLock;
  142. MutexUnlock : @OS2HeapMutexUnlock;
  143. );
  144. procedure InitSystemThreads;
  145. begin
  146. SetNoThreadManager;
  147. SetMemoryMutexManager(OS2MemoryMutexManager);
  148. end;
  149. {
  150. $Log$
  151. Revision 1.1 2005-02-06 16:57:18 peter
  152. * threads for go32v2,os,emx,netware
  153. Revision 1.1 2005/02/06 13:06:20 peter
  154. * moved file and dir functions to sysfile/sysdir
  155. * win32 thread in systemunit
  156. }