thread.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Multithreading implementation for Win32
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$ifdef MT}
  13. const
  14. threadvarblocksize : dword = 0;
  15. type
  16. tthreadinfo = record
  17. f : tthreadfunc;
  18. p : pointer;
  19. end;
  20. pthreadinfo = ^tthreadinfo;
  21. var
  22. dataindex : dword;
  23. { import the necessary stuff from windows }
  24. function TlsAlloc : DWord;
  25. external 'kernel32' name 'TlsAlloc';
  26. function TlsGetValue(dwTlsIndex : DWord) : pointer;
  27. external 'kernel32' name 'TlsGetValue';
  28. function TlsSetValue(dwTlsIndex : DWord;lpTlsValue : pointer) : LongBool;
  29. external 'kernel32' name 'TlsSetValue';
  30. function TlsFree(dwTlsIndex : DWord) : LongBool;
  31. external 'kernel32' name 'TlsFree';
  32. function CreateThread(lpThreadAttributes : pointer;
  33. dwStackSize : DWord; lpStartAddress : pointer;lpParameter : pointer;
  34. dwCreationFlags : DWord;var lpThreadId : DWord) : THandle;
  35. external 'kernel32' name 'CreateThread';
  36. procedure ExitThread(dwExitCode : DWord);
  37. external 'kernel32' name 'ExitThread';
  38. function GlobalAlloc(uFlags:UINT; dwBytes:DWORD):Pointer;
  39. external 'kernel32' name 'GlobalAlloc';
  40. function GlobalFree(hMem : Pointer):Pointer; external 'kernel32' name 'GlobalFree';
  41. const
  42. { GlobalAlloc, GlobalFlags }
  43. GMEM_FIXED = 0;
  44. GMEM_ZEROINIT = 64;
  45. procedure init_threadvar(var offset : dword;size : dword);[public,alias: 'FPC_INIT_THREADVAR'];
  46. begin
  47. offset:=threadvarblocksize;
  48. inc(threadvarblocksize,size);
  49. end;
  50. type ltvInitEntry =
  51. record
  52. varaddr : pdword;
  53. size : longint;
  54. end;
  55. pltvInitEntry = ^ltvInitEntry;
  56. procedure init_unit_threadvars (tableEntry : pltvInitEntry);
  57. begin
  58. while tableEntry^.varaddr <> nil do
  59. begin
  60. init_threadvar (tableEntry^.varaddr^, tableEntry^.size);
  61. inc (pchar (tableEntry), sizeof (tableEntry^));
  62. end;
  63. end;
  64. type TltvInitTablesTable =
  65. record
  66. count : dword;
  67. tables: array [1..32767] of pltvInitEntry;
  68. end;
  69. var
  70. ThreadvarTablesTable : TltvInitTablesTable; external name 'FPC_LOCALTHREADVARTABLES';
  71. procedure init_all_unit_threadvars; [public,alias: 'FPC_INITIALIZELOCALTHREADVARS'];
  72. var i : integer;
  73. begin
  74. {$ifdef DEBUG_MT}
  75. WriteLn ('init_all_unit_threadvars (%d) units',ThreadvarTablesTable.count);
  76. {$endif}
  77. for i := 1 to ThreadvarTablesTable.count do
  78. init_unit_threadvars (ThreadvarTablesTable.tables[i]);
  79. end;
  80. function relocate_threadvar(offset : dword) : pointer;[public,alias: 'FPC_RELOCATE_THREADVAR'];
  81. begin
  82. relocate_threadvar:=TlsGetValue(dataindex)+offset;
  83. end;
  84. procedure AllocateThreadVars;
  85. var
  86. threadvars : pointer;
  87. begin
  88. { we've to allocate the memory from windows }
  89. { because the FPC heap management uses }
  90. { exceptions which use threadvars but }
  91. { these aren't allocated yet ... }
  92. { allocate room on the heap for the thread vars }
  93. threadvars:=pointer(GlobalAlloc(GMEM_FIXED or GMEM_ZEROINIT,
  94. threadvarblocksize));
  95. TlsSetValue(dataindex,threadvars);
  96. end;
  97. procedure ReleaseThreadVars;
  98. var
  99. threadvars : pointer;
  100. begin
  101. { release thread vars }
  102. threadvars:=TlsGetValue(dataindex);
  103. GlobalFree(threadvars);
  104. end;
  105. procedure InitThread;
  106. begin
  107. InitFPU;
  108. { we don't need to set the data to 0 because we did this with }
  109. { the fillchar above, but it looks nicer }
  110. { ExceptAddrStack and ExceptObjectStack are threadvars }
  111. { so every thread has its on exception handling capabilities }
  112. InitExceptions;
  113. InOutRes:=0;
  114. // ErrNo:=0;
  115. end;
  116. procedure DoneThread;
  117. begin
  118. { release thread vars }
  119. ReleaseThreadVars;
  120. end;
  121. function ThreadMain(param : pointer) : dword;stdcall;
  122. var
  123. ti : tthreadinfo;
  124. begin
  125. {$ifdef DEBUG_MT}
  126. writeln('New thread started, initialising ...');
  127. {$endif DEBUG_MT}
  128. AllocateThreadVars;
  129. InitThread;
  130. ti:=pthreadinfo(param)^;
  131. dispose(pthreadinfo(param));
  132. {$ifdef DEBUG_MT}
  133. writeln('Jumping to thread function');
  134. {$endif DEBUG_MT}
  135. ThreadMain:=ti.f(ti.p);
  136. DoneThread;
  137. end;
  138. function BeginThread(sa : Pointer;stacksize : dword;
  139. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  140. var ThreadId : DWord) : DWord;
  141. var
  142. ti : pthreadinfo;
  143. begin
  144. {$ifdef DEBUG_MT}
  145. writeln('Creating new thread');
  146. {$endif DEBUG_MT}
  147. IsMultithread:=true;
  148. { the only way to pass data to the newly created thread }
  149. { in a MT safe way, is to use the heap }
  150. new(ti);
  151. ti^.f:=ThreadFunction;
  152. ti^.p:=p;
  153. {$ifdef DEBUG_MT}
  154. writeln('Starting new thread');
  155. {$endif DEBUG_MT}
  156. BeginThread:=CreateThread(sa,stacksize,@ThreadMain,ti,
  157. creationflags,threadid);
  158. end;
  159. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  160. var
  161. dummy : dword;
  162. begin
  163. BeginThread:=BeginThread(nil,0,ThreadFunction,nil,0,dummy);
  164. end;
  165. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  166. var
  167. dummy : dword;
  168. begin
  169. BeginThread:=BeginThread(nil,0,ThreadFunction,p,0,dummy);
  170. end;
  171. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
  172. var ThreadId : DWord) : DWord;
  173. begin
  174. BeginThread:=BeginThread(nil,0,ThreadFunction,p,0,ThreadId);
  175. end;
  176. procedure EndThread(ExitCode : DWord);
  177. begin
  178. DoneThread;
  179. ExitThread(ExitCode);
  180. end;
  181. procedure EndThread;
  182. begin
  183. EndThread(0);
  184. end;
  185. { we implement these procedures for win32 by importing them }
  186. { directly from windows }
  187. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  188. external 'kernel32' name 'InitializeCriticalSection';
  189. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  190. external 'kernel32' name 'DeleteCriticalSection';
  191. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  192. external 'kernel32' name 'EnterCriticalSection';
  193. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  194. external 'kernel32' name 'LeaveCriticalSection';
  195. {$endif MT}
  196. {
  197. $Log$
  198. Revision 1.8 2002-03-31 10:03:13 armin
  199. + call to DoneThread was missing
  200. Revision 1.7 2002/03/28 16:31:35 armin
  201. + initialize threadvars defined local in units
  202. Revision 1.6 2001/10/23 21:51:03 peter
  203. * criticalsection renamed to rtlcriticalsection for kylix compatibility
  204. Revision 1.5 2001/10/09 02:38:39 carl
  205. * bugfix #1639 (IsMultiThread varialbe setting)
  206. Revision 1.4 2001/01/26 21:02:21 florian
  207. *** empty log message ***
  208. Revision 1.3 2001/01/26 16:38:03 florian
  209. *** empty log message ***
  210. Revision 1.2 2001/01/24 21:47:38 florian
  211. + more MT stuff added
  212. Revision 1.1 2001/01/01 19:06:36 florian
  213. + initial release
  214. }