thread.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. function relocate_threadvar(offset : dword) : pointer;[public,alias: 'FPC_RELOCATE_THREADVAR'];
  51. begin
  52. relocate_threadvar:=TlsGetValue(dataindex)+offset;
  53. end;
  54. procedure AllocateThreadVars;
  55. var
  56. threadvars : pointer;
  57. begin
  58. { we've to allocate the memory from windows }
  59. { because the FPC heap management uses }
  60. { exceptions which use threadvars but }
  61. { these aren't allocated yet ... }
  62. { allocate room on the heap for the thread vars }
  63. threadvars:=pointer(GlobalAlloc(GMEM_FIXED or GMEM_ZEROINIT,
  64. threadvarblocksize));
  65. TlsSetValue(dataindex,threadvars);
  66. end;
  67. procedure ReleaseThreadVars;
  68. var
  69. threadvars : pointer;
  70. begin
  71. { release thread vars }
  72. threadvars:=TlsGetValue(dataindex);
  73. GlobalFree(threadvars);
  74. end;
  75. procedure InitThread;
  76. begin
  77. InitFPU;
  78. { we don't need to set the data to 0 because we did this with }
  79. { the fillchar above, but it looks nicer }
  80. { ExceptAddrStack and ExceptObjectStack are threadvars }
  81. { so every thread has its on exception handling capabilities }
  82. InitExceptions;
  83. InOutRes:=0;
  84. // ErrNo:=0;
  85. end;
  86. procedure DoneThread;
  87. begin
  88. { release thread vars }
  89. ReleaseThreadVars;
  90. end;
  91. function ThreadMain(param : pointer) : dword;stdcall;
  92. var
  93. ti : tthreadinfo;
  94. begin
  95. {$ifdef DEBUG_MT}
  96. writeln('New thread started, initialising ...');
  97. {$endif DEBUG_MT}
  98. AllocateThreadVars;
  99. InitThread;
  100. ti:=pthreadinfo(param)^;
  101. dispose(pthreadinfo(param));
  102. {$ifdef DEBUG_MT}
  103. writeln('Jumping to thread function');
  104. {$endif DEBUG_MT}
  105. ThreadMain:=ti.f(ti.p);
  106. end;
  107. function BeginThread(sa : Pointer;stacksize : dword;
  108. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  109. var ThreadId : DWord) : DWord;
  110. var
  111. ti : pthreadinfo;
  112. begin
  113. {$ifdef DEBUG_MT}
  114. writeln('Creating new thread');
  115. {$endif DEBUG_MT}
  116. IsMultithread:=true;
  117. { the only way to pass data to the newly created thread }
  118. { in a MT safe way, is to use the heap }
  119. new(ti);
  120. ti^.f:=ThreadFunction;
  121. ti^.p:=p;
  122. {$ifdef DEBUG_MT}
  123. writeln('Starting new thread');
  124. {$endif DEBUG_MT}
  125. BeginThread:=CreateThread(sa,stacksize,@ThreadMain,ti,
  126. creationflags,threadid);
  127. end;
  128. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  129. var
  130. dummy : dword;
  131. begin
  132. BeginThread:=BeginThread(nil,0,ThreadFunction,nil,0,dummy);
  133. end;
  134. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  135. var
  136. dummy : dword;
  137. begin
  138. BeginThread:=BeginThread(nil,0,ThreadFunction,p,0,dummy);
  139. end;
  140. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
  141. var ThreadId : DWord) : DWord;
  142. begin
  143. BeginThread:=BeginThread(nil,0,ThreadFunction,p,0,ThreadId);
  144. end;
  145. procedure EndThread(ExitCode : DWord);
  146. begin
  147. DoneThread;
  148. ExitThread(ExitCode);
  149. end;
  150. procedure EndThread;
  151. begin
  152. EndThread(0);
  153. end;
  154. { we implement these procedures for win32 by importing them }
  155. { directly from windows }
  156. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  157. external 'kernel32' name 'InitializeCriticalSection';
  158. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  159. external 'kernel32' name 'DeleteCriticalSection';
  160. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  161. external 'kernel32' name 'EnterCriticalSection';
  162. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  163. external 'kernel32' name 'LeaveCriticalSection';
  164. {$endif MT}
  165. {
  166. $Log$
  167. Revision 1.6 2001-10-23 21:51:03 peter
  168. * criticalsection renamed to rtlcriticalsection for kylix compatibility
  169. Revision 1.5 2001/10/09 02:38:39 carl
  170. * bugfix #1639 (IsMultiThread varialbe setting)
  171. Revision 1.4 2001/01/26 21:02:21 florian
  172. *** empty log message ***
  173. Revision 1.3 2001/01/26 16:38:03 florian
  174. *** empty log message ***
  175. Revision 1.2 2001/01/24 21:47:38 florian
  176. + more MT stuff added
  177. Revision 1.1 2001/01/01 19:06:36 florian
  178. + initial release
  179. }